Debian Codegraph @

Unicode/ustring.cpp

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "ustring.h"
31 #include "utype.h"
32
33 #include "ffglib_compat.h"
34
35 #include <assert.h>
36 #include <stddef.h>
37 #include <stdlib.h>
38
39 long uc_strcmp(const unichar_t *str1,const char *str2) {
40 long ch1, ch2;
41 for (;;) {
42 ch1 = *str1++; ch2 = *(unsigned char *) str2++ ;
43 if ( ch1!=ch2 || ch1=='\0' )
44 return(ch1-ch2);
45 }
46 }
47
48 long uc_strncmp(const unichar_t *str1,const char *str2,int n) {
49 long ch1, ch2;
50 while ( --n>=0 ) {
51 ch1 = *str1++; ch2 = *(unsigned char *) str2++ ;
52 if ( ch1!=ch2 || ch1=='\0' )
53 return(ch1-ch2);
54 }
55 return( 0 );
56 }
57
58 long uc_strmatch(const unichar_t *str1, const char *str2) {
59 long ch1, ch2;
60 for (;;) {
61 ch1 = *str1++; ch2 = *(unsigned char *) str2++ ;
62 ch1 = tolower(ch1);
63 ch2 = tolower(ch2);
64 if ( ch1!=ch2 || ch1=='\0' )
65 return(ch1-ch2);
66 }
67 }
68
69 long uc_strnmatch(const unichar_t *str1, const char *str2, int len) {
70 long ch1, ch2;
71 for (;--len>=0;) {
72 ch1 = *str1++; ch2 = *(unsigned char *) str2++ ;
73 ch1 = tolower(ch1);
74 ch2 = tolower(ch2);
75 if ( ch1!=ch2 || ch1=='\0' || len<=0 )
76 return(ch1-ch2);
77 }
78 return( 0 );
79 }
80
81 long u_strnmatch(const unichar_t *str1, const unichar_t *str2, int len) {
82 long ch1, ch2;
83 for (;--len>=0;) {
84 ch1 = *str1++; ch2 = *str2++ ;
85 ch1 = tolower(ch1);
86 ch2 = tolower(ch2);
87 if ( ch1!=ch2 || ch1=='\0' || len<=0 )
88 return(ch1-ch2);
89 }
90 return( 0 );
91 }
92
93 long u_strcmp(const unichar_t *str1,const unichar_t *str2) {
94 long ch1, ch2;
95 for (;;) {
96 ch1 = *str1++; ch2 = *str2++ ;
97 if ( ch1!=ch2 || ch1=='\0' )
98 return(ch1-ch2);
99 }
100 }
101
102 long u_strncmp(const unichar_t *str1,const unichar_t *str2,int n) {
103 long ch1, ch2;
104 while ( --n>=0 ) {
105 ch1 = *str1++; ch2 = *str2++ ;
106 if ( ch1!=ch2 || ch1=='\0' )
107 return(ch1-ch2);
108 }
109 return( 0 );
110 }
111
112 long u_strmatch(const unichar_t *str1, const unichar_t *str2) {
113 long ch1, ch2;
114 for (;;) {
115 ch1 = *str1++; ch2 = *str2++ ;
116 ch1 = tolower(ch1);
117 ch2 = tolower(ch2);
118 if ( ch1!=ch2 || ch1=='\0' )
119 return(ch1-ch2);
120 }
121 }
122
123 void cu_strcpy(char *to, const unichar_t *from) {
124 unichar_t ch;
125 while ( (ch = *from++) != '\0' )
126 *(to++) = ch;
127 *to = 0;
128 }
129
130 void uc_strcpy(unichar_t *to, const char *from) {
131 unichar_t ch;
132 while ( (ch = *(unsigned char *) from++) != '\0' )
133 *(to++) = ch;
134 *to = 0;
135 }
136
137 void u_strcpy(unichar_t *to, const unichar_t *from) {
138 unichar_t ch;
139 while ( (ch = *from++) != '\0' )
140 *(to++) = ch;
141 *to = 0;
142 }
143
144 char *cc_strncpy(char *to, const char *from, int len) {
145 if( !from ) {
146 to[0] = '\0';
147 return to;
148 }
149 strncpy( to, from, len );
150 return to;
151 }
152
153
154 void u_strncpy(unichar_t *to, const unichar_t *from, int len) {
155 unichar_t ch;
156 while ( (ch = *from++) != '\0' && --len>=0 )
157 *(to++) = ch;
158 *to = 0;
159 }
160
161 void cu_strncpy(char *to, const unichar_t *from, int len) {
162 unichar_t ch;
163 while ( (ch = *from++) != '\0' && --len>=0 )
164 *(to++) = ch;
165 *to = 0;
166 }
167
168 void uc_strncpy(unichar_t *to, const char *from, int len) {
169 unichar_t ch;
170 while ( (ch = *(unsigned char *) from++) != '\0' && --len>=0 )
171 *(to++) = ch;
172 *to = 0;
173 }
174
175 void uc_strcat(unichar_t *to, const char *from) {
176 uc_strcpy(to+u_strlen(to),from);
177 }
178
179 void uc_strncat(unichar_t *to, const char *from,int len) {
180 uc_strncpy(to+u_strlen(to),from,len);
181 }
182
183 void cu_strcat(char *to, const unichar_t *from) {
184 cu_strcpy(to+strlen(to),from);
185 }
186
187 void cu_strncat(char *to, const unichar_t *from, int len) {
188 cu_strncpy(to+strlen(to),from,len);
189 }
190
191 void u_strcat(unichar_t *to, const unichar_t *from) {
192 u_strcpy(to+u_strlen(to),from);
193 }
194
195 void u_strncat(unichar_t *to, const unichar_t *from, int len) {
196 u_strncpy(to+u_strlen(to),from,len);
197 }
198
199
200 int u_strlen(const unichar_t *str) {
201 int len = 0;
202
203 while ( *str++!='\0' )
204 ++len;
205 return( len );
206 }
207
208 int c_strlen( const char * p )
209 {
210 if(!p)
211 return 0;
212 return strlen(p);
213 }
214
215
216 unichar_t *u_strchr(const unichar_t *str ,unichar_t ch) {
217 unichar_t test;
218
219 while ( (test=*(str++))!='\0' )
220 if ( test==ch )
221 return( (unichar_t *) str-1 );
222
223 return( NULL );
224 }
225
226 unichar_t *u_strrchr(const unichar_t *str ,unichar_t ch) {
227 unichar_t test;
228 unichar_t *last = NULL;
229
230 while ( (test=*(str++))!='\0' )
231 if ( test==ch )
232 last = (unichar_t *) str-1;
233
234 return( last );
235 }
236
237 unichar_t *uc_strstr(const unichar_t *longer, const char *substr) {
238 long ch1, ch2;
239 const unichar_t *lpt, *str1; const char *str2;
240
241 for ( lpt=longer; *lpt!='\0'; ++lpt ) {
242 str1 = lpt; str2 = substr;
243 for (;;) {
244 ch1 = *str1++; ch2 = *(unsigned char *) str2++ ;
245 if ( ch2=='\0' )
246 return((unichar_t *) lpt);
247 if ( ch1!=ch2 )
248 break;
249 }
250 }
251 return( NULL );
252 }
253
254 unichar_t *u_strstr(const unichar_t *longer, const unichar_t *substr) {
255 long ch1, ch2;
256 const unichar_t *lpt, *str1, *str2;
257
258 for ( lpt=longer; *lpt!='\0'; ++lpt ) {
259 str1 = lpt; str2 = substr;
260 for (;;) {
261 ch1 = *str1++; ch2 = *str2++ ;
262 if ( ch2=='\0' )
263 return((unichar_t *) lpt);
264 if ( ch1!=ch2 )
265 break;
266 }
267 }
268 return( NULL );
269 }
270
271 unichar_t *uc_strstrmatch(const unichar_t *longer, const char *substr) {
272 long ch1, ch2;
273 const unichar_t *lpt, *str1; const unsigned char *str2;
274
275 for ( lpt=longer; *lpt!='\0'; ++lpt ) {
276 str1 = lpt; str2 = (unsigned char *) substr;
277 for (;;) {
278 ch1 = *str1++; ch2 = *str2++ ;
279 ch1 = tolower(ch1);
280 ch2 = tolower(ch2);
281 if ( ch2=='\0' )
282 return((unichar_t *) lpt);
283 if ( ch1!=ch2 )
284 break;
285 }
286 }
287 return( NULL );
288 }
289
290 unichar_t *u_strstrmatch(const unichar_t *longer, const unichar_t *substr) {
291 long ch1, ch2;
292 const unichar_t *lpt, *str1, *str2;
293
294 for ( lpt=longer; *lpt!='\0'; ++lpt ) {
295 str1 = lpt; str2 = substr;
296 for (;;) {
297 ch1 = *str1++; ch2 = *str2++ ;
298 ch1 = tolower(ch1);
299 ch2 = tolower(ch2);
300 if ( ch2=='\0' )
301 return((unichar_t *) lpt);
302 if ( ch1!=ch2 )
303 break;
304 }
305 }
306 return( NULL );
307 }
308
309 unichar_t *u_copyn(const unichar_t *pt, long n) {
310 unichar_t *res;
311 #ifdef MEMORY_MASK
312 if ( n*sizeof(unichar_t)>=MEMORY_MASK )
313 n = MEMORY_MASK/sizeof(unichar_t)-1;
314 #endif
315 res = (unichar_t *) malloc((n+1)*sizeof(unichar_t));
316 memcpy(res,pt,n*sizeof(unichar_t));
317 res[n]='\0';
318 return(res);
319 }
320
321 unichar_t *u_copynallocm(const unichar_t *pt, long n, long m) {
322 unichar_t *res;
323 #ifdef MEMORY_MASK
324 if ( n*sizeof(unichar_t)>=MEMORY_MASK )
325 n = MEMORY_MASK/sizeof(unichar_t)-1;
326 #endif
327 res = (unichar_t *) malloc((m+1)*sizeof(unichar_t));
328 memcpy(res,pt,n*sizeof(unichar_t));
329 res[n]='\0';
330 return(res);
331 }
332
333 unichar_t *u_copy(const unichar_t *pt) {
334 if(pt)
335 return u_copyn(pt,u_strlen(pt));
336
337 return((unichar_t *)0);
338 }
339
340 unichar_t *u_concat(const unichar_t *s1, const unichar_t *s2) {
341 long len1, len2;
342 unichar_t *pt;
343
344 if ( s1==NULL )
345 return( u_copy( s2 ));
346 else if ( s2==NULL )
347 return( u_copy( s1 ));
348 len1 = u_strlen(s1); len2 = u_strlen(s2);
349 pt = (unichar_t *) malloc((len1+len2+1)*sizeof(unichar_t));
350 u_strcpy(pt,s1);
351 u_strcpy(pt+len1,s2);
352 return( pt );
353 }
354
355 unichar_t *uc_copyn(const char *pt,int len) {
356 unichar_t *res, *rpt;
357
358 if(!pt)
359 return((unichar_t *)0);
360
361 #ifdef MEMORY_MASK
362 if ( (len+1)*sizeof(unichar_t)>=MEMORY_MASK )
363 len = MEMORY_MASK/sizeof(unichar_t)-1;
364 #endif
365 res = (unichar_t *) malloc((len+1)*sizeof(unichar_t));
366 for ( rpt=res; --len>=0 ; *rpt++ = *(unsigned char *) pt++ );
367 *rpt = '\0';
368 return(res);
369 }
370
371 unichar_t *uc_copy(const char *pt) {
372 unichar_t *res, *rpt;
373 int n;
374
375 if(!pt)
376 return((unichar_t *)0);
377
378 n = strlen(pt);
379 #ifdef MEMORY_MASK
380 if ( (n+1)*sizeof(unichar_t)>=MEMORY_MASK )
381 n = MEMORY_MASK/sizeof(unichar_t)-1;
382 #endif
383 res = (unichar_t *) malloc((n+1)*sizeof(unichar_t));
384 for ( rpt=res; --n>=0 ; *rpt++ = *(unsigned char *) pt++ );
385 *rpt = '\0';
386 return(res);
387 }
388
389 char *cu_copyn(const unichar_t *pt,int len) {
390 char *res, *rpt;
391
392 if(!pt)
393 return(NULL);
394
395 #ifdef MEMORY_MASK
396 if ( (len+1)>=MEMORY_MASK )
397 len = MEMORY_MASK-1;
398 #endif
399 res = (char *) malloc(len+1);
400 for ( rpt=res; --len>=0 ; *rpt++ = *pt++ );
401 *rpt = '\0';
402 return(res);
403 }
404
405 char *cu_copy(const unichar_t *pt) {
406 char *res, *rpt;
407 int n;
408
409 if(!pt)
410 return((char *)0);
411
412 n = u_strlen(pt);
413 #ifdef MEMORY_MASK
414 if ( (n+1)>=MEMORY_MASK )
415 n = MEMORY_MASK/sizeof(unichar_t)-1;
416 #endif
417 res = (char *) malloc(n+1);
418 for ( rpt=res; --n>=0 ; *rpt++ = *pt++ );
419 *rpt = '\0';
420 return(res);
421 }
422
423 double u_strtod(const unichar_t *str, unichar_t **ptr) {
424 char buf[60], *pt, *ret;
425 const unichar_t *upt;
426 double val;
427
428 for ( upt=str, pt=buf; *upt<128 && *upt!='\0' && pt-buf<(ptrdiff_t)(sizeof(buf)-1); )
429 *pt++ = *upt++;
430 *pt = '\0';
431 val = strtod(buf,&ret);
432 if ( ptr!=NULL ) {
433 if ( pt==ret )
434 *ptr = (unichar_t *) upt;
435 else
436 *ptr = (unichar_t *) (str + (ret-buf));
437 }
438 return( val );
439 }
440
441 long u_strtol(const unichar_t *str, unichar_t **ptr, int base) {
442 char buf[60], *pt, *ret;
443 const unichar_t *upt;
444 long val;
445
446 for ( upt=str, pt=buf; *upt<128 && *upt!='\0' && pt<buf+sizeof(buf)-1; )
447 *pt++ = *upt++;
448 *pt = '\0';
449 val = strtol(buf,&ret,base);
450 if ( ptr!=NULL ) {
451 if ( pt==ret )
452 *ptr = (unichar_t *) upt;
453 else
454 *ptr = (unichar_t *) (str + (ret-buf));
455 }
456 return( val );
457 }
458
459 unsigned long u_strtoul(const unichar_t *str, unichar_t **ptr, int base) {
460 char buf[60], *pt, *ret;
461 const unichar_t *upt;
462 unsigned long val;
463
464 for ( upt=str, pt=buf; *upt<128 && *upt!='\0' && pt<buf+sizeof(buf)-1; )
465 *pt++ = *upt++;
466 *pt = '\0';
467 val = strtoul(buf,&ret,base);
468 if ( ptr!=NULL ) {
469 if ( pt==ret )
470 *ptr = (unichar_t *) upt;
471 else
472 *ptr = (unichar_t *) (str + (ret-buf));
473 }
474 return( val );
475 }
476
477 unichar_t *cu_strstartmatch(const char *key,const unichar_t *str) {
478 if ( key && str ) {
479 while( *key ) {
480 if(tolower(*key) != tolower(*str))
481 return 0;
482 key++;
483 str++;
484 }
485 }
486 return (unichar_t *)str;
487 }
488
489 unichar_t *u_strstartmatch(const unichar_t *initial, const unichar_t *full) {
490 int ch1, ch2;
491 for (;;) {
492 ch1 = *initial++; ch2 = *full++ ;
493 if ( ch1=='\0' )
494 return( (unichar_t *) full );
495 ch1 = tolower(ch1);
496 ch2 = tolower(ch2);
497 if ( ch1!=ch2 || ch1=='\0' )
498 return(NULL);
499 }
500 }
501
502 char *u_to_c(const unichar_t *ubuf) {
503 if( !ubuf )
504 return 0;
505 static char buf[400];
506 cu_strncpy(buf,ubuf,sizeof(buf));
507 return( buf );
508 }
509
510 unichar_t *c_to_u(const char *buf) {
511 static unichar_t ubuf[400];
512 uc_strncpy(ubuf,buf,sizeof(ubuf));
513 return( ubuf );
514 }
515
516 unichar_t *utf82u_strncpy(unichar_t *ubuf,const char *utf8buf,int len) {
517 unichar_t *upt = ubuf;
518 int32_t ch;
519
520 if (!ubuf || !utf8buf || len <= 0) {
521 return ubuf;
522 }
523
524 while (len > 1 && (ch = utf8_ildb(&utf8buf)) != 0) {
525 if (ch > 0) {
526 *upt++ = ch;
527 --len;
528 } else {
529 TRACE("Invalid UTF-8 sequence detected %s\n", utf8buf);
530 do {
531 ++utf8buf;
532 } while ((*utf8buf & 0xc0) == 0x80);
533 }
534 }
535 *upt = '\0';
536
537 return ubuf;
538 }
539
540 unichar_t *utf82u_strcpy(unichar_t *ubuf,const char *utf8buf) {
541 return( utf82u_strncpy(ubuf,utf8buf,c_strlen(utf8buf)+1));
542 }
543
544 unichar_t *utf82u_copyn(const char *utf8buf,int len) {
545 unichar_t *ubuf = (unichar_t *) malloc((len+1)*sizeof(unichar_t));
546 return( utf82u_strncpy(ubuf,utf8buf,len+1));
547 }
548
549 unichar_t *utf82u_copy(const char *utf8buf) {
550 int len;
551 unichar_t *ubuf;
552
553 if ( utf8buf==NULL )
554 return( NULL );
555
556 len = strlen(utf8buf);
557 ubuf = (unichar_t *) malloc((len+1)*sizeof(unichar_t));
558 return( utf82u_strncpy(ubuf,utf8buf,len+1));
559 }
560
561 void utf82u_strcat(unichar_t *to,const char *from) {
562 utf82u_strcpy(to+u_strlen(to),from);
563 }
564
565 char *u2utf8_strncpy(char *utf8buf,const unichar_t *ubuf,int len) {
566 /* Copy unichar string 'ubuf' into utf8 buffer string 'utf8buf' */
567 char *pt = utf8buf;
568
569 assert(utf8buf != NULL);
570
571 if ( ubuf!=NULL ) {
572 while ( *ubuf && (--len != 0) ) {
573 pt=utf8_idpb(pt,*ubuf++,0);
574 }
575 *pt = '\0';
576 return( utf8buf );
577 }
578
579 return( NULL );
580 }
581
582 char *u2utf8_strcpy(char *utf8buf,const unichar_t *ubuf) {
583 return u2utf8_strncpy(utf8buf, ubuf, -1);
584 }
585
586 char *utf8_strchr(const char *str, int search) {
587 int ch;
588 const char *old = str;
589
590 while ( (ch = utf8_ildb(&str))!=0 ) {
591 if ( ch==search )
592 return( (char *) old );
593 old = str;
594 }
595 return( NULL );
596 }
597
598 char *latin1_2_utf8_strcpy(char *utf8buf,const char *lbuf) {
599 char *pt = utf8buf;
600 const unsigned char *lpt = (const unsigned char *) lbuf;
601
602 while ( *lpt ) {
603 if ( *lpt<0x80 )
604 *pt++ = *lpt;
605 else {
606 *pt++ = 0xc0 | (*lpt>>6);
607 *pt++ = 0x80 | (*lpt&0x3f);
608 }
609 ++lpt;
610 }
611 *pt = '\0';
612 return( utf8buf );
613 }
614
615 char *latin1_2_utf8_copy(const char *lbuf) {
616 int len;
617 char *utf8buf;
618
619 if ( lbuf==NULL )
620 return( NULL );
621
622 len = strlen(lbuf);
623 utf8buf = (char *) malloc(2*len+1);
624 return( latin1_2_utf8_strcpy(utf8buf,lbuf));
625 }
626
627 char *utf8_2_latin1_copy(const char *utf8buf) {
628 int len;
629 int ch;
630 char *lbuf, *pt; const char *upt;
631
632 if ( utf8buf==NULL )
633 return( NULL );
634
635 len = strlen(utf8buf);
636 pt = lbuf = (char *) malloc(len+1);
637 for ( upt=utf8buf; (ch=utf8_ildb(&upt))!='\0'; )
638 if ( ch>=0xff )
639 *pt++ = '?';
640 else
641 *pt++ = ch;
642 *pt = '\0';
643 return( lbuf );
644 }
645
646 char *u2utf8_copy(const unichar_t *ubuf) {
647 /* Make a utf8 string copy of unichar string ubuf */
648
649 if ( ubuf==NULL )
650 return( NULL );
651
652 return( u2utf8_copyn(ubuf,u_strlen(ubuf)+1) );
653 }
654
655 char *u2utf8_copyn(const unichar_t *ubuf,int len) {
656 /* Make a utf8 string copy of unichar string ubuf[0..len] */
657 char *utf8buf, *pt, *pt2;
658
659 if ( ubuf==NULL || len<=0 || (utf8buf=pt=(char *)malloc(len*6+1))==NULL )
660 return( NULL );
661
662 while ( (pt2=utf8_idpb(pt,*ubuf++,0)) && --len )
663 pt = pt2;
664
665 if ( pt2 )
666 pt = pt2;
667 else
668 TRACE("u2utf8_copyn: truncated on invalid char 0x%x\n", ubuf[-1]);
669
670 *pt = '\0';
671 return( utf8buf );
672 }
673
674 int32_t utf8_ildb(const char **_text) {
675 int32_t val= -1;
676 int ch;
677 const uint8_t *text = (const uint8_t *) *_text;
678 /* Increment and load character */
679
680 if ( text==NULL )
681 return( val );
682 else if ( (ch = *text++)<0x80 ) {
683 val = ch;
684 } else if ( ch<=0xbf ) {
685 /* error */
686 } else if ( ch<=0xdf ) {
687 if ( *text>=0x80 && *text<0xc0 )
688 val = ((ch&0x1f)<<6) | (*text++&0x3f);
689 } else if ( ch<=0xef ) {
690 if ( *text>=0x80 && *text<0xc0 && text[1]>=0x80 && text[1]<0xc0 ) {
691 val = ((ch&0xf)<<12) | ((text[0]&0x3f)<<6) | (text[1]&0x3f);
692 text += 2;
693 }
694 } else {
695 int w = ( ((ch&0x7)<<2) | ((text[0]&0x30)>>4) )-1, w2;
696 w = (w<<6) | ((text[0]&0xf)<<2) | ((text[1]&0x30)>>4);
697 w2 = ((text[1]&0xf)<<6) | (text[2]&0x3f);
698 val = w*0x400 + w2 + 0x10000;
699 if ( *text<0x80 || text[1]<0x80 || text[2]<0x80 ||
700 *text>=0xc0 || text[1]>=0xc0 || text[2]>=0xc0 )
701 val = -1;
702 else
703 text += 3;
704 }
705 *_text = (const char *) text;
706 return( val );
707 }
708
709 char *utf8_idpb(char *utf8_text,uint32_t ch,int flags) {
710 /* Increment and deposit character, no '\0' appended */
711 /* NOTE: Unicode only needs range of 17x65535 values */
712 /* and strings must be long enough to hold +4 chars. */
713 /* ISO/IEC 10646 description of UTF8 allows encoding */
714 /* character values up to U+7FFFFFFF before RFC3629. */
715
716 if ( ch>0x7fffffff || \
717 (!(flags&UTF8IDPB_OLDLIMIT) && ((ch>=0xd800 && ch<=0xdfff) || ch>=17*65536)) )
718 return( 0 ); /* Error, ch is out of range */
719
720 if ( (flags&(UTF8IDPB_UCS2|UTF8IDPB_UTF16|UTF8IDPB_UTF32)) ) {
721 if ( (flags&UTF8IDPB_UCS2) && ch>0xffff )
722 return( 0 ); /* Error, ch is out of range */
723 if ( (flags&UTF8IDPB_UTF32) ) {
724 *utf8_text++ = ((ch>>24)&0xff);
725 *utf8_text++ = ((ch>>16)&0xff);
726 ch &= 0xffff;
727 }
728 if ( ch>0xffff ) {
729 /* ...here if a utf16 encoded value */
730 unsigned long us;
731 ch -= 0x10000;
732 us = (ch>>10)+0xd800;
733 *utf8_text++ = us>>8;
734 *utf8_text++ = us&0xff;
735 ch = (ch&0x3ff)+0xdc00;
736 }
737 *utf8_text++ = ch>>8;
738 ch &= 0xff;
739 } else if ( ch>127 || (ch==0 && (flags&UTF8IDPB_NOZERO)) ) {
740 if ( ch<=0x7ff )
741 /* ch>=0x80 && ch<=0x7ff */
742 *utf8_text++ = 0xc0 | (ch>>6);
743 else {
744 if ( ch<=0xffff )
745 /* ch>=0x800 && ch<=0xffff */
746 *utf8_text++ = 0xe0 | (ch>>12);
747 else {
748 if ( ch<=0x1fffff )
749 /* ch>=0x10000 && ch<=0x1fffff */
750 *utf8_text++ = 0xf0 | (ch>>18);
751 else {
752 if ( ch<=0x3ffffff )
753 /* ch>=0x200000 && ch<=0x3ffffff */
754 *utf8_text++ = 0xf8 | (ch>>24);
755 else {
756 /* ch>=0x4000000 && ch<=0x7fffffff */
757 *utf8_text++ = 0xfc | (ch>>30);
758 *utf8_text++ = 0x80 | ((ch>>24)&0x3f);
759 }
760 *utf8_text++ = 0x80 | ((ch>>18)&0x3f);
761 }
762 *utf8_text++ = 0x80 | ((ch>>12)&0x3f);
763 }
764 *utf8_text++ = 0x80 | ((ch>>6)&0x3f);
765 }
766 ch = 0x80 | (ch&0x3f);
767 }
768 *utf8_text++ = ch;
769 return( utf8_text );
770 }
771
772 char *utf8_ib(char *utf8_text) {
773 /* Increment to next utf8 character */
774 unsigned char ch;
775
776 if ( (ch = (unsigned char) *utf8_text)=='\0' )
777 return( utf8_text );
778 else if ( ch<=127 )
779 return( utf8_text+1 );
780 else if ( ch<0xe0 )
781 return( utf8_text+2 );
782 else if ( ch<0xf0 )
783 return( utf8_text+3 );
784 else if ( ch<0xf8 )
785 return( utf8_text+4 );
786 else if ( ch<0xfc )
787 return( utf8_text+5 );
788 else
789 return( utf8_text+6 );
790 }
791
792 int utf8_valid(const char *str) {
793 /* Is this a valid utf8 string? */
794 int ch;
795
796 while ( (ch=utf8_ildb(&str))!='\0' )
797 if ( ch==-1 )
798 return( false );
799
800 return( true );
801 }
802
803 void utf8_truncatevalid(char *str) {
804 /* There are certain cases where we have a fixed amount of space to display */
805 /* something, and if it doesn't fit in that, then we truncate it. But... */
806 /* that can leave us with a half completed utf8 byte sequence. So truncate*/
807 /* again, right before the start of the bad sequence */
808 int ch;
809 char *old;
810
811 old = str;
812 while ( (ch=utf8_ildb((const char **) &str))!='\0' ) {
813 if ( ch==-1 ) {
814 *old = '\0';
815 return;
816 }
817 old = str;
818 }
819 }
820
821 char *utf8_db(char *utf8_text) {
822 /* Decrement utf8 pointer to previous utf8 character.*/
823 /* NOTE: This should never happen but if the pointer */
824 /* was looking at an intermediate character, it will */
825 /* be properly positioned at the start of a new char */
826 /* and not the previous character. */
827 unsigned char *pt = (unsigned char *) utf8_text;
828
829 --pt;
830 if ( *pt>=0x80 && *pt<0xc0 ) {
831 --pt;
832 if ( *pt>=0x80 && *pt<0xc0 ) {
833 --pt;
834 if ( *pt>=0x80 && *pt<0xc0 ) {
835 --pt;
836 if ( *pt>=0x80 && *pt<0xc0 ) {
837 --pt;
838 if ( *pt>=0x80 && *pt<0xc0 )
839 --pt;
840 }
841 }
842 }
843 }
844 return( (char *) pt );
845 }
846
847 long utf8_strlen(const char *utf8_str) {
848 /* Count how many characters in the string NOT bytes */
849 long len = 0;
850
851 while ( utf8_ildb(&utf8_str)>0 && ++len>0 );
852 return( len );
853 }
854
855 long utf82u_strlen(const char *utf8_str) {
856 /* Count how many shorts needed to represent in UCS2 */
857 int32_t ch;
858 long len = 0;
859
860 while ( (ch = utf8_ildb(&utf8_str))>0 && ++len>0 )
861 if ( ch>=0x10000 )
862 ++len;
863 return( len );
864 }
865
866 void utf8_strncpy(char *to, const char *from, int len) {
867 /* copy n characters NOT bytes */
868 const char *old = from;
869 while ( len && *old ) {
870 utf8_ildb(&old);
871 len--;
872 }
873 strncpy(to, from, old-from);
874 to[old-from] = 0;
875 }
876
877 uint16_t *u2utf16_strcpy(uint16_t *utf16buf,const unichar_t *ubuf) {
878 /* Copy unichar string 'ubuf' into utf16 buffer string 'utf16buf' */
879 uint16_t *pt = utf16buf;
880 unichar_t ch;
881
882 if (utf16buf == NULL || ubuf == NULL)
883 return NULL;
884
885 while ( *ubuf ) {
886 ch = *ubuf++;
887 if (ch <= 0xFFFF)
888 *pt++ = ch;
889 else {
890 /* Encode with surrogate pair */
891 *pt++ = ((ch - 0x10000)>>10) + 0xD800; /* high surrogate */
892 *pt++ = ((ch - 0x10000)&0x3FF) + 0xDC00; /* low surrogate */
893 }
894 }
895 *pt = '\0';
896 return( utf16buf );
897 }
898
899 extern unichar_t *utf162u_strcpy(unichar_t*ubuf, const uint16_t *utf16buf) {
900 unichar_t uch = 0x0, uch2 = 0x0;
901 unichar_t *pt = ubuf;
902
903 if (utf16buf == NULL || ubuf == NULL)
904 return NULL;
905
906 while ( *utf16buf ) {
907 uch = *utf16buf++;
908 if ( uch>=0xd800 && uch<0xdc00 ) {
909 /* Is this a possible utf16 high surrogate value? */
910 uch2 = *utf16buf++;
911 if ( uch2>=0xdc00 && uch2<0xe000 ) /* low surrogate */
912 uch = ((uch-0xd800)<<10 | (uch2&0x3ff)) + 0x10000;
913 else {
914 *pt++ = uch;
915 if (uch2==0)
916 break;
917 uch = uch2;
918 }
919 }
920 *pt++ = uch;
921 }
922 *pt = '\0';
923 return( ubuf );
924 }
925
926 uint16_t *utf82utf16_copy(const char* utf8buf) {
927 unichar_t *utf32_str = utf82u_copy(utf8buf); /* Convert from utf8 to utf32 */
928 uint16_t *utf16buf = (uint16_t *) malloc(2*(u_strlen(utf32_str)+1)*sizeof(uint16_t));
929 u2utf16_strcpy(utf16buf, utf32_str);
930 free(utf32_str);
931
932 return utf16buf;
933 }
934
935 char *StripToASCII(const char *utf8_str) {
936 /* Remove any non-ascii characters: Special case, convert the copyright symbol to (c) */
937 char *newcr, *pt, *end;
938 int len, ch;
939 const unichar_t *alt;
940
941 len = strlen(utf8_str);
942 pt = newcr = (char *) malloc(len+1);
943 end = pt+len;
944 while ( (ch= utf8_ildb(&utf8_str))!='\0' ) {
945 if ( pt>=end ) {
946 int off = pt-newcr;
947 newcr = (char *) realloc(newcr,(off+10)+1);
948 pt = newcr+off;
949 end = pt+10;
950 }
951 if ( (ch>=' ' && ch<'\177' ) || ch=='\n' || ch=='\t' )
952 *pt++ = ch;
953 else if ( ch=='\r' && *utf8_str!='\n' )
954 *pt++ = '\n';
955 else if ( ch==0xa9 /* Copyright sign */ ) {
956 const char *str = "(c)";
957 if ( pt+strlen(str)>=end ) {
958 int off = pt-newcr;
959 newcr = (char *) realloc(newcr,(off+10+strlen(str))+1);
960 pt = newcr+off;
961 end = pt+10;
962 }
963 while ( *str )
964 *pt++ = *str++;
965 } else if ( (alt = unialt(ch))!=NULL ) {
966 while ( *alt!='\0' ) {
967 if ( pt>=end ) {
968 int off = pt-newcr;
969 newcr = (char *) realloc(newcr,(off+10)+1);
970 pt = newcr+off;
971 end = pt+10;
972 }
973 if ( *alt>=' ' && *alt<'\177' )
974 *pt++ = *alt;
975 else if ( *alt==0x300 )
976 *pt++ = '`';
977 else if ( *alt==0x301 )
978 *pt++ = '\'';
979 else if ( *alt==0x302 )
980 *pt++ = '^';
981 else if ( *alt==0x303 )
982 *pt++ = '~';
983 else if ( *alt==0x308 )
984 *pt++ = ':';
985 ++alt;
986 }
987 }
988 }
989 *pt = '\0';
990 return( newcr );
991 }
992
993 int AllAscii(const char *txt) {
994 /* Verify string only All ASCII printable characters */
995 unsigned char ch;
996
997 if ( txt==NULL )
998 return( false );
999
1000 for ( ; (ch=(unsigned char) *txt)!='\0'; ++txt )
1001 if ( (ch>=' ' && ch<'\177' ) || \
1002 ch=='\t' || ch=='\n' || ch=='\r' )
1003 /* All right */;
1004 else
1005 return( false );
1006
1007 return( true );
1008 }
1009
1010 int uAllAscii(const unichar_t *txt) {
1011 /* Verify string only All ASCII printable unichar_t. */
1012
1013 if ( txt==NULL )
1014 return( false );
1015
1016 for ( ; *txt!='\0'; ++txt )
1017 if ( (*txt>=' ' && *txt<'\177' ) || \
1018 *txt=='\t' || *txt=='\n' || *txt=='\r' )
1019 /* All right */;
1020 else
1021 return( false );
1022
1023 return( true );
1024 }
1025
1026 char *chomp( char *line ) {
1027 /* Chomp the last '\r' and '\n' in character string. */
1028 /* \r = CR used as a newline char in MAC OS before X */
1029 /* \n = LF used as a newline char in MAC OSX & Linux */
1030 /* \r\n = CR + LF used as a newline char in Windows. */
1031 int x;
1032
1033 if( line==NULL || (x=strlen(line)-1)<0 )
1034 return( line );
1035
1036 if ( line[x]=='\n' ) {
1037 line[x] = '\0';
1038 --x;
1039 }
1040 if ( x>=0 && line[x]=='\r' )
1041 line[x] = '\0';
1042 return( line );
1043 }
1044
1045 int endswith(const char *haystack,const char *needle) {
1046 int haylen = strlen( haystack );
1047 int nedlen = strlen( needle );
1048 if( haylen < nedlen )
1049 return 0;
1050 const char* p = strstr( haystack + haylen - nedlen, needle );
1051 return p == ( haystack + haylen - nedlen );
1052 }
1053
1054 int endswithi(const char *haystackZ,const char *needleZ) {
1055 char* haystack = ff_ascii_strdown(haystackZ,-1);
1056 char* needle = ff_ascii_strdown(needleZ,-1);
1057 int ret = endswith( haystack, needle );
1058 free( haystack );
1059 free( needle );
1060 return ret;
1061 }
1062
1063 int endswithi_partialExtension( const char *haystackZ,const char *needleZ) {
1064 int nedlen = strlen(needleZ);
1065 if( nedlen == 0 ) {
1066 return 0;
1067 }
1068 char* haystack = ff_ascii_strdown(haystackZ,-1);
1069 char* needle = ff_ascii_strdown(needleZ,-1);
1070 int ret = 0;
1071 int i = nedlen-1;
1072 ret |= endswith( haystack, needle );
1073 for( ; i>=0 && !ret ; --i ) {
1074 needle[i] = '\0';
1075 ret |= endswith( haystack, needle );
1076 }
1077 free( haystack );
1078 free( needle );
1079 return ret;
1080 }
1081
1082 int u_endswith(const unichar_t *haystack,const unichar_t *needle) {
1083 int haylen = u_strlen( haystack );
1084 int nedlen = u_strlen( needle );
1085 if( haylen < nedlen )
1086 return 0;
1087 unichar_t* p = u_strstr( haystack + haylen - nedlen, needle );
1088 return p == ( haystack + haylen - nedlen );
1089 }
1090
1091 int u_startswith(const unichar_t *haystack,const unichar_t *needle) {
1092
1093 if( !haystack || !needle )
1094 return 0;
1095
1096 unichar_t* p = u_strstr( haystack, needle );
1097 return p == ( haystack );
1098 }
1099
1100 int uc_startswith(const unichar_t *haystack,const char* needle)
1101 {
1102 return u_startswith( haystack, c_to_u(needle));
1103 }
1104
1105
1106 char* c_itostr( int v )
1107 {
1108 static char ret[100+1];
1109 snprintf(ret,100,"%d",v );
1110 return ret;
1111 }
1112
1113 char* str_replace_all( char* s, char* orig, char* replacement, int free_s )
1114 {
1115 char* p = strstr( s, orig );
1116 if( !p )
1117 {
1118 if( free_s )
1119 return s;
1120 return copy( s );
1121 }
1122
1123 int count = 0;
1124 p = s;
1125 while( p )
1126 {
1127 p = strstr( p, orig );
1128 if( !p )
1129 break;
1130 p++;
1131 count++;
1132 }
1133 count++;
1134
1135 // more than strictly needed, but always enough RAM.
1136 int retsz = strlen(s) + count*strlen(replacement) + 1;
1137 char* ret = (char *) malloc( retsz );
1138 memset( ret, '\0', retsz );
1139 char* output = ret;
1140 char* remains = s;
1141 p = remains;
1142 while( p )
1143 {
1144 p = strstr( remains, orig );
1145 if( !p )
1146 {
1147 strcpy( output, remains );
1148 break;
1149 }
1150 if( p > remains )
1151 strncpy( output, remains, p-remains );
1152 strcat( output, replacement );
1153 output += strlen(output);
1154 remains = p + strlen(orig);
1155 }
1156
1157 if( free_s )
1158 free(s);
1159 return ret;
1160 }
1161
1162 int toint( char* v )
1163 {
1164 if( !v )
1165 return 0;
1166 return atoi(v);
1167 }
1168 char* tostr( int v )
1169 {
1170 const int bufsz = 100;
1171 static char buf[101];
1172 snprintf(buf,bufsz,"%d",v);
1173 return buf;
1174 }
1175
1176 void realloc_tail(char** p_buf, size_t size_delta, char** p_tail,
1177 char** p_proc) {
1178 size_t new_size = size_delta + (*p_tail - *p_buf);
1179 char* new_buf = (char*) realloc(*p_buf, new_size);
1180 *p_tail = new_buf + new_size;
1181 if (p_proc) *p_proc = new_buf + (*p_proc - *p_buf);
1182 *p_buf = new_buf;
1183 }