0
|
1 /*
|
|
2 * $Id: strings.c,v 1.5 2005/04/10 15:26:37 aonoto Exp $
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
|
|
7 * This file is part of FreeWnn.
|
|
8 *
|
|
9 * Copyright Kyoto University Research Institute for Mathematical Sciences
|
|
10 * 1987, 1988, 1989, 1990, 1991, 1992
|
|
11 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
|
|
12 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
|
|
13 * Copyright FreeWnn Project 1999, 2000
|
|
14 *
|
|
15 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp>
|
|
16 *
|
|
17 * This library is free software; you can redistribute it and/or
|
|
18 * modify it under the terms of the GNU Lesser General Public
|
|
19 * License as published by the Free Software Foundation; either
|
|
20 * version 2 of the License, or (at your option) any later version.
|
|
21 *
|
|
22 * This library is distributed in the hope that it will be useful,
|
|
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
25 * Lesser General Public License for more details.
|
|
26 *
|
|
27 * You should have received a copy of the GNU Lesser General Public
|
|
28 * License along with this library; if not, write to the
|
|
29 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
30 * Boston, MA 02111-1307, USA.
|
|
31 */
|
|
32
|
|
33 /*
|
|
34 long strings library. Copyed from GMW programs!!
|
|
35 */
|
|
36 #include <stdio.h>
|
|
37 #include "commonhd.h"
|
|
38 #include "wnn_string.h"
|
|
39
|
|
40 #define is_half_width(c) (! is_full_width(c))
|
|
41 #define is_full_width(c) (((c) & 0xff00))
|
|
42 #define is_gaiji(c) (is_full_width(c) && ! ((c) & 0x8080))
|
|
43 #define is_jis_kanji(c) (((c) & 0x8080) == 0x8080)
|
|
44
|
|
45 w_char *
|
|
46 wnn_Strcat (s1, s2)
|
|
47 register w_char *s1;
|
|
48 register w_char *s2;
|
|
49 {
|
|
50 register w_char *d;
|
|
51
|
|
52 for (d = s1; *d++ != 0;);
|
|
53 for (--d; (*d++ = *s2++) != 0;);
|
|
54 return s1;
|
|
55 }
|
|
56
|
|
57 w_char *
|
|
58 wnn_Strncat (s1, s2, n)
|
|
59 register w_char *s1;
|
|
60 register w_char *s2;
|
|
61 register int n;
|
|
62 {
|
|
63 register w_char *d;
|
|
64
|
|
65 for (d = s1; *d++ != 0;);
|
|
66 for (--d; n > 0 && (*d++ = *s2++) != 0; n--);
|
|
67 if (n <= 0)
|
|
68 {
|
|
69 *d = 0;
|
|
70 return d;
|
|
71 }
|
|
72 else
|
|
73 {
|
|
74 return --d;
|
|
75 }
|
|
76 }
|
|
77
|
|
78 int
|
|
79 wnn_Strcmp (s1, s2)
|
|
80 register w_char *s1;
|
|
81 register w_char *s2;
|
|
82 {
|
|
83 for (; *s1 != 0 && *s1 == *s2; s1++, s2++);
|
|
84 if (*s1 > *s2)
|
|
85 return 1;
|
|
86 if (*s1 == *s2)
|
|
87 return 0;
|
|
88 return (-1);
|
|
89 /* return (int)(*s1 - *s2); Since w_char, it is always positive */
|
|
90 }
|
|
91
|
|
92 /* s1 is substr of s2? then return 1*/
|
|
93 int
|
|
94 wnn_Substr (s1, s2)
|
|
95 register w_char *s1;
|
|
96 register w_char *s2;
|
|
97 {
|
|
98 for (; *s1 != 0 && *s1 == *s2; s1++, s2++);
|
|
99 return (int) (!*s1);
|
|
100 }
|
|
101
|
|
102
|
|
103
|
|
104 int
|
|
105 wnn_Strncmp (s1, s2, n)
|
|
106 register w_char *s1;
|
|
107 register w_char *s2;
|
|
108 register int n;
|
|
109 {
|
|
110 if (n == 0)
|
|
111 return (0);
|
|
112 for (; n > 0 && *s1++ == *s2++; n--);
|
|
113 return (int) (*--s1 - *--s2);
|
|
114 }
|
|
115
|
|
116 w_char *
|
|
117 wnn_Strcpy (s1, s2)
|
|
118 register w_char *s1;
|
|
119 register w_char *s2;
|
|
120 {
|
|
121 register w_char *d;
|
|
122
|
|
123 for (d = s1; (*d++ = *s2++) != 0;);
|
|
124 return s1;
|
|
125 }
|
|
126
|
|
127 w_char *
|
|
128 wnn_Strncpy (s1, s2, n)
|
|
129 register w_char *s1;
|
|
130 register w_char *s2;
|
|
131 register int n;
|
|
132 {
|
|
133 /*
|
|
134 register w_char *d;
|
|
135
|
|
136 for (d = s1;n > 0;n--) {
|
|
137 *d++ = *s2++;
|
|
138 }
|
|
139 return s1;
|
|
140 */
|
|
141 if (s2 > s1)
|
|
142 {
|
|
143 for (; n-- > 0;)
|
|
144 {
|
|
145 *s1++ = *s2++;
|
|
146 }
|
|
147 }
|
|
148 else if (s2 < s1)
|
|
149 {
|
|
150 s1 += n - 1;
|
|
151 s2 += n - 1;
|
|
152 for (; n-- > 0;)
|
|
153 {
|
|
154 *s1-- = *s2--;
|
|
155 }
|
|
156 }
|
|
157 return s1;
|
|
158 }
|
|
159
|
|
160 int
|
|
161 wnn_Strlen (s)
|
|
162 register w_char *s;
|
|
163 {
|
|
164 register int n;
|
|
165
|
|
166 for (n = 0; *s++ != 0; n++);
|
|
167 return n;
|
|
168 }
|
|
169
|
|
170 void
|
|
171 wnn_Sreverse (d, s)
|
|
172 w_char *d, *s;
|
|
173 {
|
|
174 w_char *s0;
|
|
175
|
|
176 s0 = s;
|
|
177 for (; *s++;);
|
|
178 s--;
|
|
179 for (; --s >= s0;)
|
|
180 {
|
|
181 *d++ = *s;
|
|
182 }
|
|
183 *d = 0;
|
|
184 }
|
|
185
|
|
186 #ifdef nodef
|
|
187 w_char *
|
|
188 wnn_Index (s, c)
|
|
189 register w_char *s;
|
|
190 register w_char c;
|
|
191 {
|
|
192 while (*s != c)
|
|
193 {
|
|
194 if (*s++ == 0)
|
|
195 return 0;
|
|
196 }
|
|
197 return s;
|
|
198 }
|
|
199
|
|
200 w_char *
|
|
201 wnn_Rindex (s, c)
|
|
202 register w_char *s;
|
|
203 register w_char c;
|
|
204 {
|
|
205 register w_char *p = 0;
|
|
206
|
|
207 while (*s != 0)
|
|
208 {
|
|
209 if (*s++ == c)
|
|
210 p = s - 1;
|
|
211 }
|
|
212 return p;
|
|
213 }
|
|
214
|
|
215 w_char *
|
|
216 wnn_Strpbrk (s1, s2)
|
|
217 register w_char *s1;
|
|
218 register w_char *s2;
|
|
219 {
|
|
220 register w_char *p;
|
|
221
|
|
222 while (*s1 != 0)
|
|
223 {
|
|
224 for (p = s2; *p != 0; p++)
|
|
225 {
|
|
226 if (*s1++ == *p)
|
|
227 return s1 - 1;
|
|
228 }
|
|
229 }
|
|
230 return 0;
|
|
231 }
|
|
232
|
|
233 int
|
|
234 wnn_Strspn (s1, s2)
|
|
235 register w_char *s1;
|
|
236 register w_char *s2;
|
|
237 {
|
|
238 register w_char *p;
|
|
239 register int n;
|
|
240
|
|
241 while (*s1 != 0)
|
|
242 {
|
|
243 for (p = s2; *p != 0; p++)
|
|
244 {
|
|
245 if (*s1++ == *p)
|
|
246 {
|
|
247 n = 1;
|
|
248 while (*s1 != 0)
|
|
249 {
|
|
250 for (p = s2; *p != 0; p++)
|
|
251 {
|
|
252 if (*s1++ == *p)
|
|
253 {
|
|
254 n++;
|
|
255 break;
|
|
256 }
|
|
257 }
|
|
258 if (*p == 0)
|
|
259 {
|
|
260 return n;
|
|
261 }
|
|
262 }
|
|
263 return n;
|
|
264 }
|
|
265 }
|
|
266 }
|
|
267 return 0;
|
|
268 }
|
|
269
|
|
270 int
|
|
271 wnn_Strcspn (s1, s2)
|
|
272 register w_char *s1;
|
|
273 register w_char *s2;
|
|
274 {
|
|
275 register w_char *p;
|
|
276 register int n;
|
|
277
|
|
278 while (*s1 != 0)
|
|
279 {
|
|
280 for (p = s2; *p != 0; p++)
|
|
281 {
|
|
282 if (*s1++ != *p)
|
|
283 {
|
|
284 n = 1;
|
|
285 while (*s1 != 0)
|
|
286 {
|
|
287 for (p = s2; *p != 0; p++)
|
|
288 {
|
|
289 if (*s1++ != *p)
|
|
290 {
|
|
291 n++;
|
|
292 break;
|
|
293 }
|
|
294 }
|
|
295 if (*p == 0)
|
|
296 {
|
|
297 return n;
|
|
298 }
|
|
299 }
|
|
300 return n;
|
|
301 }
|
|
302 }
|
|
303 }
|
|
304 return 0;
|
|
305 }
|
|
306
|
|
307 w_char *
|
|
308 wnn_Strtok (s1, s2)
|
|
309 register w_char *s1;
|
|
310 register w_char *s2;
|
|
311 {
|
|
312 static w_char *p = 0, *s;
|
|
313 static w_char c;
|
|
314 register int i, j, n;
|
|
315
|
|
316 if (s1 == 0)
|
|
317 {
|
|
318 if (p == 0)
|
|
319 return 0;
|
|
320 *p = c;
|
|
321 }
|
|
322 else
|
|
323 {
|
|
324 p = s1;
|
|
325 }
|
|
326 n = wnn_Strlen (p) - (j = wnn_Strlen (s2));
|
|
327 for (i = 0; i <= n; i++)
|
|
328 {
|
|
329 if (wnn_Strncmp (p++, s2, j) == 0)
|
|
330 {
|
|
331 s = p + j - 1;
|
|
332 break;
|
|
333 }
|
|
334 }
|
|
335 if (i > n)
|
|
336 return 0;
|
|
337 for (; i <= n; i++)
|
|
338 {
|
|
339 if (wnn_Strncmp (p++, s2, j) == 0)
|
|
340 {
|
|
341 p--;
|
|
342 c = *p;
|
|
343 *p = 0;
|
|
344 break;
|
|
345 }
|
|
346 }
|
|
347 return s;
|
|
348 }
|
|
349
|
|
350 int
|
|
351 wnn_Strwidth (buf)
|
|
352 w_char *buf;
|
|
353 {
|
|
354 register int width;
|
|
355
|
|
356 for (width = 0; *buf != 0; buf++)
|
|
357 {
|
|
358 if (is_full_width (*buf))
|
|
359 {
|
|
360 width += 2;
|
|
361 }
|
|
362 else
|
|
363 {
|
|
364 width++;
|
|
365 }
|
|
366 }
|
|
367 return width;
|
|
368 }
|
|
369
|
|
370 int
|
|
371 wnn_Strnwidth (buf, n)
|
|
372 w_char *buf;
|
|
373 int n;
|
|
374 {
|
|
375 register int width, i;
|
|
376
|
|
377 for (width = 0, i = 0; i < n; buf++, i++)
|
|
378 {
|
|
379 if (is_full_width (*buf))
|
|
380 {
|
|
381 width += 2;
|
|
382 }
|
|
383 else
|
|
384 {
|
|
385 width++;
|
|
386 }
|
|
387 }
|
|
388 return width;
|
|
389 }
|
|
390
|
|
391 void
|
|
392 wnn_delete_ss2 (s, n)
|
|
393 register unsigned int *s;
|
|
394 register int n;
|
|
395 {
|
|
396 register unsigned int x;
|
|
397
|
|
398 for (; n != 0 && (x = *s); n--, s++)
|
|
399 {
|
|
400 if ((x & 0xff00) == 0x8e00)
|
|
401 *s &= ~0xff00;
|
|
402 if (x == 0xffffffff)
|
|
403 break;
|
|
404 }
|
|
405 }
|
|
406
|
|
407 void
|
|
408 wnn_delete_w_ss2 (s, n)
|
|
409 register w_char *s;
|
|
410 register int n;
|
|
411 {
|
|
412 register w_char x;
|
|
413
|
|
414 for (; n != 0 && (x = *s); n--, s++)
|
|
415 {
|
|
416 if ((x & 0xff00) == 0x8e00)
|
|
417 *s &= ~0xff00;
|
|
418 }
|
|
419 }
|
|
420
|
|
421 int
|
|
422 wnn_byte_count (in)
|
|
423 register int in;
|
|
424 {
|
|
425 return (((in <= 0xa0 && in != 0x00 && in != 0x8e) || in == 0xff) ? 1 : 2);
|
|
426 }
|
|
427 #endif /* nodef */
|