| 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 |
#include "ustring.h" |
| 28 |
#include "utype.h" |
| 29 |
#include <stddef.h> |
| 30 |
|
| 31 |
long uc_strcmp(const unichar_t *str1,const char *str2) { |
| 32 |
long ch1, ch2; |
| 33 |
for (;;) { |
| 34 |
ch1 = *str1++; ch2 = *(unsigned char *) str2++ ; |
| 35 |
if ( ch1!=ch2 || ch1=='\0' ) |
| 36 |
return(ch1-ch2); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
long uc_strncmp(const unichar_t *str1,const char *str2,int n) { |
| 41 |
long ch1, ch2; |
| 42 |
while ( --n>=0 ) { |
| 43 |
ch1 = *str1++; ch2 = *(unsigned char *) str2++ ; |
| 44 |
if ( ch1!=ch2 || ch1=='\0' ) |
| 45 |
return(ch1-ch2); |
| 46 |
} |
| 47 |
return( 0 ); |
| 48 |
} |
| 49 |
|
| 50 |
long uc_strmatch(const unichar_t *str1, const char *str2) { |
| 51 |
long ch1, ch2; |
| 52 |
for (;;) { |
| 53 |
ch1 = *str1++; ch2 = *(unsigned char *) str2++ ; |
| 54 |
ch1 = tolower(ch1); |
| 55 |
ch2 = tolower(ch2); |
| 56 |
if ( ch1!=ch2 || ch1=='\0' ) |
| 57 |
return(ch1-ch2); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
long uc_strnmatch(const unichar_t *str1, const char *str2, int len) { |
| 62 |
long ch1, ch2; |
| 63 |
for (;--len>=0;) { |
| 64 |
ch1 = *str1++; ch2 = *(unsigned char *) str2++ ; |
| 65 |
ch1 = tolower(ch1); |
| 66 |
ch2 = tolower(ch2); |
| 67 |
if ( ch1!=ch2 || ch1=='\0' || len<=0 ) |
| 68 |
return(ch1-ch2); |
| 69 |
} |
| 70 |
return( 0 ); |
| 71 |
} |
| 72 |
|
| 73 |
long u_strnmatch(const unichar_t *str1, const unichar_t *str2, int len) { |
| 74 |
long ch1, ch2; |
| 75 |
for (;--len>=0;) { |
| 76 |
ch1 = *str1++; ch2 = *str2++ ; |
| 77 |
ch1 = tolower(ch1); |
| 78 |
ch2 = tolower(ch2); |
| 79 |
if ( ch1!=ch2 || ch1=='\0' || len<=0 ) |
| 80 |
return(ch1-ch2); |
| 81 |
} |
| 82 |
return( 0 ); |
| 83 |
} |
| 84 |
|
| 85 |
long u_strcmp(const unichar_t *str1,const unichar_t *str2) { |
| 86 |
long ch1, ch2; |
| 87 |
for (;;) { |
| 88 |
ch1 = *str1++; ch2 = *str2++ ; |
| 89 |
if ( ch1!=ch2 || ch1=='\0' ) |
| 90 |
return(ch1-ch2); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
long u_strncmp(const unichar_t *str1,const unichar_t *str2,int n) { |
| 95 |
long ch1, ch2; |
| 96 |
while ( --n>=0 ) { |
| 97 |
ch1 = *str1++; ch2 = *str2++ ; |
| 98 |
if ( ch1!=ch2 || ch1=='\0' ) |
| 99 |
return(ch1-ch2); |
| 100 |
} |
| 101 |
return( 0 ); |
| 102 |
} |
| 103 |
|
| 104 |
long u_strmatch(const unichar_t *str1, const unichar_t *str2) { |
| 105 |
long ch1, ch2; |
| 106 |
for (;;) { |
| 107 |
ch1 = *str1++; ch2 = *str2++ ; |
| 108 |
ch1 = tolower(ch1); |
| 109 |
ch2 = tolower(ch2); |
| 110 |
if ( ch1!=ch2 || ch1=='\0' ) |
| 111 |
return(ch1-ch2); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void cu_strcpy(char *to, const unichar_t *from) { |
| 116 |
register unichar_t ch; |
| 117 |
while ( (ch = *from++) != '\0' ) |
| 118 |
*(to++) = ch; |
| 119 |
*to = 0; |
| 120 |
} |
| 121 |
|
| 122 |
void uc_strcpy(unichar_t *to, const char *from) { |
| 123 |
register unichar_t ch; |
| 124 |
while ( (ch = *(unsigned char *) from++) != '\0' ) |
| 125 |
*(to++) = ch; |
| 126 |
*to = 0; |
| 127 |
} |
| 128 |
|
| 129 |
void u_strcpy(unichar_t *to, const unichar_t *from) { |
| 130 |
register unichar_t ch; |
| 131 |
while ( (ch = *from++) != '\0' ) |
| 132 |
*(to++) = ch; |
| 133 |
*to = 0; |
| 134 |
} |
| 135 |
|
| 136 |
void u_strncpy(register unichar_t *to, const unichar_t *from, int len) { |
| 137 |
register unichar_t ch; |
| 138 |
while ( (ch = *from++) != '\0' && --len>=0 ) |
| 139 |
*(to++) = ch; |
| 140 |
*to = 0; |
| 141 |
} |
| 142 |
|
| 143 |
void cu_strncpy(register char *to, const unichar_t *from, int len) { |
| 144 |
register unichar_t ch; |
| 145 |
while ( (ch = *from++) != '\0' && --len>=0 ) |
| 146 |
*(to++) = ch; |
| 147 |
*to = 0; |
| 148 |
} |
| 149 |
|
| 150 |
void uc_strncpy(register unichar_t *to, const char *from, int len) { |
| 151 |
register unichar_t ch; |
| 152 |
while ( (ch = *(unsigned char *) from++) != '\0' && --len>=0 ) |
| 153 |
*(to++) = ch; |
| 154 |
*to = 0; |
| 155 |
} |
| 156 |
|
| 157 |
void uc_strcat(unichar_t *to, const char *from) { |
| 158 |
uc_strcpy(to+u_strlen(to),from); |
| 159 |
} |
| 160 |
|
| 161 |
void uc_strncat(unichar_t *to, const char *from,int len) { |
| 162 |
uc_strncpy(to+u_strlen(to),from,len); |
| 163 |
} |
| 164 |
|
| 165 |
void cu_strcat(char *to, const unichar_t *from) { |
| 166 |
cu_strcpy(to+strlen(to),from); |
| 167 |
} |
| 168 |
|
| 169 |
void cu_strncat(char *to, const unichar_t *from, int len) { |
| 170 |
cu_strncpy(to+strlen(to),from,len); |
| 171 |
} |
| 172 |
|
| 173 |
void u_strcat(unichar_t *to, const unichar_t *from) { |
| 174 |
u_strcpy(to+u_strlen(to),from); |
| 175 |
} |
| 176 |
|
| 177 |
void u_strncat(unichar_t *to, const unichar_t *from, int len) { |
| 178 |
u_strncpy(to+u_strlen(to),from,len); |
| 179 |
} |
| 180 |
|
| 181 |
int u_strlen(register const unichar_t *str) { |
| 182 |
register int len = 0; |
| 183 |
|
| 184 |
while ( *str++!='\0' ) |
| 185 |
++len; |
| 186 |
return( len ); |
| 187 |
} |
| 188 |
|
| 189 |
unichar_t *u_strchr(const unichar_t *str ,unichar_t ch) { |
| 190 |
register unichar_t test; |
| 191 |
|
| 192 |
while ( (test=*(str++))!='\0' ) |
| 193 |
if ( test==ch ) |
| 194 |
return( (unichar_t *) str-1 ); |
| 195 |
|
| 196 |
return( NULL ); |
| 197 |
} |
| 198 |
|
| 199 |
unichar_t *u_strrchr(const unichar_t *str ,unichar_t ch) { |
| 200 |
register unichar_t test, *last = NULL; |
| 201 |
|
| 202 |
while ( (test=*(str++))!='\0' ) |
| 203 |
if ( test==ch ) |
| 204 |
last = (unichar_t *) str-1; |
| 205 |
|
| 206 |
return( last ); |
| 207 |
} |
| 208 |
|
| 209 |
unichar_t *uc_strstr(const unichar_t *longer, const char *substr) { |
| 210 |
long ch1, ch2; |
| 211 |
const unichar_t *lpt, *str1; const char *str2; |
| 212 |
|
| 213 |
for ( lpt=longer; *lpt!='\0'; ++lpt ) { |
| 214 |
str1 = lpt; str2 = substr; |
| 215 |
for (;;) { |
| 216 |
ch1 = *str1++; ch2 = *(unsigned char *) str2++ ; |
| 217 |
if ( ch2=='\0' ) |
| 218 |
return((unichar_t *) lpt); |
| 219 |
if ( ch1!=ch2 ) |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
return( NULL ); |
| 224 |
} |
| 225 |
|
| 226 |
unichar_t *u_strstr(const unichar_t *longer, const unichar_t *substr) { |
| 227 |
long ch1, ch2; |
| 228 |
const unichar_t *lpt, *str1, *str2; |
| 229 |
|
| 230 |
for ( lpt=longer; *lpt!='\0'; ++lpt ) { |
| 231 |
str1 = lpt; str2 = substr; |
| 232 |
for (;;) { |
| 233 |
ch1 = *str1++; ch2 = *str2++ ; |
| 234 |
if ( ch2=='\0' ) |
| 235 |
return((unichar_t *) lpt); |
| 236 |
if ( ch1!=ch2 ) |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
return( NULL ); |
| 241 |
} |
| 242 |
|
| 243 |
unichar_t *uc_strstrmatch(const unichar_t *longer, const char *substr) { |
| 244 |
long ch1, ch2; |
| 245 |
const unichar_t *lpt, *str1; const unsigned char *str2; |
| 246 |
|
| 247 |
for ( lpt=longer; *lpt!='\0'; ++lpt ) { |
| 248 |
str1 = lpt; str2 = (unsigned char *) substr; |
| 249 |
for (;;) { |
| 250 |
ch1 = *str1++; ch2 = *str2++ ; |
| 251 |
ch1 = tolower(ch1); |
| 252 |
ch2 = tolower(ch2); |
| 253 |
if ( ch2=='\0' ) |
| 254 |
return((unichar_t *) lpt); |
| 255 |
if ( ch1!=ch2 ) |
| 256 |
break; |
| 257 |
} |
| 258 |
} |
| 259 |
return( NULL ); |
| 260 |
} |
| 261 |
|
| 262 |
unichar_t *u_strstrmatch(const unichar_t *longer, const unichar_t *substr) { |
| 263 |
long ch1, ch2; |
| 264 |
const unichar_t *lpt, *str1, *str2; |
| 265 |
|
| 266 |
for ( lpt=longer; *lpt!='\0'; ++lpt ) { |
| 267 |
str1 = lpt; str2 = substr; |
| 268 |
for (;;) { |
| 269 |
ch1 = *str1++; ch2 = *str2++ ; |
| 270 |
ch1 = tolower(ch1); |
| 271 |
ch2 = tolower(ch2); |
| 272 |
if ( ch2=='\0' ) |
| 273 |
return((unichar_t *) lpt); |
| 274 |
if ( ch1!=ch2 ) |
| 275 |
break; |
| 276 |
} |
| 277 |
} |
| 278 |
return( NULL ); |
| 279 |
} |
| 280 |
|
| 281 |
unichar_t *u_copyn(const unichar_t *pt, long n) { |
| 282 |
unichar_t *res; |
| 283 |
#ifdef MEMORY_MASK |
| 284 |
if ( n*sizeof(unichar_t)>=MEMORY_MASK ) |
| 285 |
n = MEMORY_MASK/sizeof(unichar_t)-1; |
| 286 |
#endif |
| 287 |
res = (unichar_t *) xmalloc((n+1)*sizeof(unichar_t)); |
| 288 |
memcpy(res,pt,n*sizeof(unichar_t)); |
| 289 |
res[n]='\0'; |
| 290 |
return(res); |
| 291 |
} |
| 292 |
|
| 293 |
unichar_t *u_copynallocm(const unichar_t *pt, long n, long m) { |
| 294 |
unichar_t *res; |
| 295 |
#ifdef MEMORY_MASK |
| 296 |
if ( n*sizeof(unichar_t)>=MEMORY_MASK ) |
| 297 |
n = MEMORY_MASK/sizeof(unichar_t)-1; |
| 298 |
#endif |
| 299 |
res = xmalloc((m+1)*sizeof(unichar_t)); |
| 300 |
memcpy(res,pt,n*sizeof(unichar_t)); |
| 301 |
res[n]='\0'; |
| 302 |
return(res); |
| 303 |
} |
| 304 |
|
| 305 |
unichar_t *u_copy(const unichar_t *pt) { |
| 306 |
if(pt) |
| 307 |
return u_copyn(pt,u_strlen(pt)); |
| 308 |
|
| 309 |
return((unichar_t *)0); |
| 310 |
} |
| 311 |
|
| 312 |
unichar_t *u_concat(const unichar_t *s1, const unichar_t *s2) { |
| 313 |
long len1, len2; |
| 314 |
unichar_t *pt; |
| 315 |
|
| 316 |
if ( s1==NULL ) |
| 317 |
return( u_copy( s2 )); |
| 318 |
else if ( s2==NULL ) |
| 319 |
return( u_copy( s1 )); |
| 320 |
len1 = u_strlen(s1); len2 = u_strlen(s2); |
| 321 |
pt = (unichar_t *) xmalloc((len1+len2+1)*sizeof(unichar_t)); |
| 322 |
u_strcpy(pt,s1); |
| 323 |
u_strcpy(pt+len1,s2); |
| 324 |
return( pt ); |
| 325 |
} |
| 326 |
|
| 327 |
unichar_t *uc_copyn(const char *pt,int len) { |
| 328 |
unichar_t *res, *rpt; |
| 329 |
|
| 330 |
if(!pt) |
| 331 |
return((unichar_t *)0); |
| 332 |
|
| 333 |
#ifdef MEMORY_MASK |
| 334 |
if ( (len+1)*sizeof(unichar_t)>=MEMORY_MASK ) |
| 335 |
len = MEMORY_MASK/sizeof(unichar_t)-1; |
| 336 |
#endif |
| 337 |
res = (unichar_t *) xmalloc((len+1)*sizeof(unichar_t)); |
| 338 |
for ( rpt=res; --len>=0 ; *rpt++ = *(unsigned char *) pt++ ); |
| 339 |
*rpt = '\0'; |
| 340 |
return(res); |
| 341 |
} |
| 342 |
|
| 343 |
unichar_t *uc_copy(const char *pt) { |
| 344 |
unichar_t *res, *rpt; |
| 345 |
int n; |
| 346 |
|
| 347 |
if(!pt) |
| 348 |
return((unichar_t *)0); |
| 349 |
|
| 350 |
n = strlen(pt); |
| 351 |
#ifdef MEMORY_MASK |
| 352 |
if ( (n+1)*sizeof(unichar_t)>=MEMORY_MASK ) |
| 353 |
n = MEMORY_MASK/sizeof(unichar_t)-1; |
| 354 |
#endif |
| 355 |
res = (unichar_t *) xmalloc((n+1)*sizeof(unichar_t)); |
| 356 |
for ( rpt=res; --n>=0 ; *rpt++ = *(unsigned char *) pt++ ); |
| 357 |
*rpt = '\0'; |
| 358 |
return(res); |
| 359 |
} |
| 360 |
|
| 361 |
char *cu_copyn(const unichar_t *pt,int len) { |
| 362 |
char *res, *rpt; |
| 363 |
|
| 364 |
if(!pt) |
| 365 |
return(NULL); |
| 366 |
|
| 367 |
#ifdef MEMORY_MASK |
| 368 |
if ( (len+1)>=MEMORY_MASK ) |
| 369 |
len = MEMORY_MASK-1; |
| 370 |
#endif |
| 371 |
res = (char *) xmalloc(len+1); |
| 372 |
for ( rpt=res; --len>=0 ; *rpt++ = *pt++ ); |
| 373 |
*rpt = '\0'; |
| 374 |
return(res); |
| 375 |
} |
| 376 |
|
| 377 |
char *cu_copy(const unichar_t *pt) { |
| 378 |
char *res, *rpt; |
| 379 |
int n; |
| 380 |
|
| 381 |
if(!pt) |
| 382 |
return((char *)0); |
| 383 |
|
| 384 |
n = u_strlen(pt); |
| 385 |
#ifdef MEMORY_MASK |
| 386 |
if ( (n+1)>=MEMORY_MASK ) |
| 387 |
n = MEMORY_MASK/sizeof(unichar_t)-1; |
| 388 |
#endif |
| 389 |
res = (char *) xmalloc(n+1); |
| 390 |
for ( rpt=res; --n>=0 ; *rpt++ = *pt++ ); |
| 391 |
*rpt = '\0'; |
| 392 |
return(res); |
| 393 |
} |
| 394 |
|
| 395 |
double u_strtod(const unichar_t *str, unichar_t **ptr) { |
| 396 |
char buf[60], *pt, *ret; |
| 397 |
const unichar_t *upt; |
| 398 |
double val; |
| 399 |
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ <202311L) /* before C23 */ |
| 400 |
extern double strtod(); /* Please don't delete this, not all of us have good ansi headers */ |
| 401 |
#endif |
| 402 |
|
| 403 |
for ( upt=str, pt=buf; *upt<128 && *upt!='\0' && pt-buf<sizeof(buf)-1; ) |
| 404 |
*pt++ = *upt++; |
| 405 |
*pt = '\0'; |
| 406 |
val = strtod(buf,&ret); |
| 407 |
if ( ptr!=NULL ) { |
| 408 |
if ( pt==ret ) |
| 409 |
*ptr = (unichar_t *) upt; |
| 410 |
else |
| 411 |
*ptr = (unichar_t *) (str + (ret-buf)); |
| 412 |
} |
| 413 |
return( val ); |
| 414 |
} |
| 415 |
|
| 416 |
long u_strtol(const unichar_t *str, unichar_t **ptr, int base) { |
| 417 |
char buf[60], *pt, *ret; |
| 418 |
const unichar_t *upt; |
| 419 |
long val; |
| 420 |
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ <202311L) /* before C23 */ |
| 421 |
extern long strtol(); /* Please don't delete this, not all of us have good ansi headers */ |
| 422 |
#endif |
| 423 |
|
| 424 |
for ( upt=str, pt=buf; *upt<128 && *upt!='\0' && pt<buf+sizeof(buf)-1; ) |
| 425 |
*pt++ = *upt++; |
| 426 |
*pt = '\0'; |
| 427 |
val = strtol(buf,&ret,base); |
| 428 |
if ( ptr!=NULL ) { |
| 429 |
if ( pt==ret ) |
| 430 |
*ptr = (unichar_t *) upt; |
| 431 |
else |
| 432 |
*ptr = (unichar_t *) (str + (ret-buf)); |
| 433 |
} |
| 434 |
return( val ); |
| 435 |
} |
| 436 |
|
| 437 |
unsigned long u_strtoul(const unichar_t *str, unichar_t **ptr, int base) { |
| 438 |
char buf[60], *pt, *ret; |
| 439 |
const unichar_t *upt; |
| 440 |
unsigned long val; |
| 441 |
|
| 442 |
for ( upt=str, pt=buf; *upt<128 && *upt!='\0' && pt<buf+sizeof(buf)-1; ) |
| 443 |
*pt++ = *upt++; |
| 444 |
*pt = '\0'; |
| 445 |
val = strtoul(buf,&ret,base); |
| 446 |
if ( ptr!=NULL ) { |
| 447 |
if ( pt==ret ) |
| 448 |
*ptr = (unichar_t *) upt; |
| 449 |
else |
| 450 |
*ptr = (unichar_t *) (str + (ret-buf)); |
| 451 |
} |
| 452 |
return( val ); |
| 453 |
} |
| 454 |
|
| 455 |
unichar_t *cu_strstartmatch(const char *key,const unichar_t *str) { |
| 456 |
if ( key && str ) { |
| 457 |
while( *key ) { |
| 458 |
if(tolower(*key) != tolower(*str)) |
| 459 |
return 0; |
| 460 |
key++; |
| 461 |
str++; |
| 462 |
} |
| 463 |
} |
| 464 |
return (unichar_t *)str; |
| 465 |
} |
| 466 |
|
| 467 |
unichar_t *u_strstartmatch(const unichar_t *initial, const unichar_t *full) { |
| 468 |
int ch1, ch2; |
| 469 |
for (;;) { |
| 470 |
ch1 = *initial++; ch2 = *full++ ; |
| 471 |
if ( ch1=='\0' ) |
| 472 |
return( (unichar_t *) full ); |
| 473 |
ch1 = tolower(ch1); |
| 474 |
ch2 = tolower(ch2); |
| 475 |
if ( ch1!=ch2 || ch1=='\0' ) |
| 476 |
return(NULL); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
char *u_to_c(const unichar_t *ubuf) { |
| 481 |
static char buf[400]; |
| 482 |
cu_strncpy(buf,ubuf,sizeof(buf)); |
| 483 |
return( buf ); |
| 484 |
} |
| 485 |
|
| 486 |
unichar_t *c_to_u(const char *buf) { |
| 487 |
static unichar_t ubuf[400]; |
| 488 |
uc_strncpy(ubuf,buf,sizeof(ubuf)); |
| 489 |
return( ubuf ); |
| 490 |
} |
| 491 |
|
| 492 |
unichar_t *utf82u_strncpy(unichar_t *ubuf,const char *utf8buf,int len) { |
| 493 |
unichar_t *upt=ubuf, *uend=ubuf+len-1; |
| 494 |
const uint8 *pt = (const uint8 *) utf8buf, *end = pt+strlen(utf8buf); |
| 495 |
int w, w2; |
| 496 |
|
| 497 |
while ( pt<end && *pt!='\0' && upt<uend ) { |
| 498 |
if ( *pt<=127 ) |
| 499 |
*upt = *pt++; |
| 500 |
else if ( *pt<=0xdf ) { |
| 501 |
*upt = ((*pt&0x1f)<<6) | (pt[1]&0x3f); |
| 502 |
pt += 2; |
| 503 |
} else if ( *pt<=0xef ) { |
| 504 |
*upt = ((*pt&0xf)<<12) | ((pt[1]&0x3f)<<6) | (pt[2]&0x3f); |
| 505 |
pt += 3; |
| 506 |
} else { |
| 507 |
w = ( ((*pt&0x7)<<2) | ((pt[1]&0x30)>>4) )-1; |
| 508 |
w = (w<<6) | ((pt[1]&0xf)<<2) | ((pt[2]&0x30)>>4); |
| 509 |
w2 = ((pt[2]&0xf)<<6) | (pt[3]&0x3f); |
| 510 |
*upt = w*0x400 + w2 + 0x10000; |
| 511 |
pt += 4; |
| 512 |
} |
| 513 |
++upt; |
| 514 |
} |
| 515 |
*upt = '\0'; |
| 516 |
return( ubuf ); |
| 517 |
} |
| 518 |
|
| 519 |
unichar_t *utf82u_strcpy(unichar_t *ubuf,const char *utf8buf) { |
| 520 |
return( utf82u_strncpy(ubuf,utf8buf,strlen(utf8buf)+1)); |
| 521 |
} |
| 522 |
|
| 523 |
unichar_t *utf82u_copyn(const char *utf8buf,int len) { |
| 524 |
unichar_t *ubuf = (unichar_t *) xmalloc((len+1)*sizeof(unichar_t)); |
| 525 |
return( utf82u_strncpy(ubuf,utf8buf,len+1)); |
| 526 |
} |
| 527 |
|
| 528 |
unichar_t *utf82u_copy(const char *utf8buf) { |
| 529 |
int len; |
| 530 |
unichar_t *ubuf; |
| 531 |
|
| 532 |
if ( utf8buf==NULL ) |
| 533 |
return( NULL ); |
| 534 |
|
| 535 |
len = strlen(utf8buf); |
| 536 |
ubuf = (unichar_t *) xmalloc((len+1)*sizeof(unichar_t)); |
| 537 |
return( utf82u_strncpy(ubuf,utf8buf,len+1)); |
| 538 |
} |
| 539 |
|
| 540 |
void utf82u_strcat(unichar_t *to,const char *from) { |
| 541 |
utf82u_strcpy(to+u_strlen(to),from); |
| 542 |
} |
| 543 |
|
| 544 |
char *u2utf8_strcpy(char *utf8buf,const unichar_t *ubuf) { |
| 545 |
char *pt = utf8buf; |
| 546 |
|
| 547 |
while ( *ubuf ) { |
| 548 |
if ( *ubuf<0x80 ) |
| 549 |
*pt++ = *ubuf; |
| 550 |
else if ( *ubuf<0x800 ) { |
| 551 |
*pt++ = 0xc0 | (*ubuf>>6); |
| 552 |
*pt++ = 0x80 | (*ubuf&0x3f); |
| 553 |
} else if ( *ubuf < 0x10000 ) { |
| 554 |
*pt++ = 0xe0 | (*ubuf>>12); |
| 555 |
*pt++ = 0x80 | ((*ubuf>>6)&0x3f); |
| 556 |
*pt++ = 0x80 | (*ubuf&0x3f); |
| 557 |
} else { |
| 558 |
uint32 val = *ubuf-0x10000; |
| 559 |
int u = ((val&0xf0000)>>16)+1, z=(val&0x0f000)>>12, y=(val&0x00fc0)>>6, x=val&0x0003f; |
| 560 |
*pt++ = 0xf0 | (u>>2); |
| 561 |
*pt++ = 0x80 | ((u&3)<<4) | z; |
| 562 |
*pt++ = 0x80 | y; |
| 563 |
*pt++ = 0x80 | x; |
| 564 |
} |
| 565 |
++ubuf; |
| 566 |
} |
| 567 |
*pt = '\0'; |
| 568 |
return( utf8buf ); |
| 569 |
} |
| 570 |
|
| 571 |
char *utf8_strchr(const char *str, int search) { |
| 572 |
int ch; |
| 573 |
const char *old = str; |
| 574 |
|
| 575 |
while ( (ch = utf8_ildb(&str))!=0 ) { |
| 576 |
if ( ch==search ) |
| 577 |
return( (char *) old ); |
| 578 |
old = str; |
| 579 |
} |
| 580 |
return( NULL ); |
| 581 |
} |
| 582 |
|
| 583 |
char *latin1_2_utf8_strcpy(char *utf8buf,const char *lbuf) { |
| 584 |
char *pt = utf8buf; |
| 585 |
const unsigned char *lpt = (const unsigned char *) lbuf; |
| 586 |
|
| 587 |
while ( *lpt ) { |
| 588 |
if ( *lpt<0x80 ) |
| 589 |
*pt++ = *lpt; |
| 590 |
else { |
| 591 |
*pt++ = 0xc0 | (*lpt>>6); |
| 592 |
*pt++ = 0x80 | (*lpt&0x3f); |
| 593 |
} |
| 594 |
++lpt; |
| 595 |
} |
| 596 |
*pt = '\0'; |
| 597 |
return( utf8buf ); |
| 598 |
} |
| 599 |
|
| 600 |
char *latin1_2_utf8_copy(const char *lbuf) { |
| 601 |
int len; |
| 602 |
char *utf8buf; |
| 603 |
|
| 604 |
if ( lbuf==NULL ) |
| 605 |
return( NULL ); |
| 606 |
|
| 607 |
len = strlen(lbuf); |
| 608 |
utf8buf = (char *) xmalloc(2*len+1); |
| 609 |
return( latin1_2_utf8_strcpy(utf8buf,lbuf)); |
| 610 |
} |
| 611 |
|
| 612 |
char *utf8_2_latin1_copy(const char *utf8buf) { |
| 613 |
int len; |
| 614 |
int ch; |
| 615 |
char *lbuf, *pt; const char *upt; |
| 616 |
|
| 617 |
if ( utf8buf==NULL ) |
| 618 |
return( NULL ); |
| 619 |
|
| 620 |
len = strlen(utf8buf); |
| 621 |
pt = lbuf = (char *) xmalloc(len+1); |
| 622 |
for ( upt=utf8buf; (ch=utf8_ildb(&upt))!='\0'; ) |
| 623 |
if ( ch>=0xff ) |
| 624 |
*pt++ = '?'; |
| 625 |
else |
| 626 |
*pt++ = ch; |
| 627 |
*pt = '\0'; |
| 628 |
return( lbuf ); |
| 629 |
} |
| 630 |
|
| 631 |
char *u2utf8_copy(const unichar_t *ubuf) { |
| 632 |
int len; |
| 633 |
char *utf8buf; |
| 634 |
|
| 635 |
if ( ubuf==NULL ) |
| 636 |
return( NULL ); |
| 637 |
|
| 638 |
len = u_strlen(ubuf); |
| 639 |
utf8buf = (char *) xmalloc((len+1)*4); |
| 640 |
return( u2utf8_strcpy(utf8buf,ubuf)); |
| 641 |
} |
| 642 |
|
| 643 |
char *u2utf8_copyn(const unichar_t *ubuf,int len) { |
| 644 |
int i; |
| 645 |
char *utf8buf, *pt; |
| 646 |
|
| 647 |
if ( ubuf==NULL ) |
| 648 |
return( NULL ); |
| 649 |
|
| 650 |
utf8buf = pt = (char *) xmalloc((len+1)*4); |
| 651 |
for ( i=0; i<len && *ubuf!='\0'; ++i ) |
| 652 |
pt = utf8_idpb(pt, *ubuf++); |
| 653 |
*pt = '\0'; |
| 654 |
return( utf8buf ); |
| 655 |
} |
| 656 |
|
| 657 |
int32 utf8_ildb(const char **_text) { |
| 658 |
int32 val= -1; |
| 659 |
int ch; |
| 660 |
const uint8 *text = (const uint8 *) *_text; |
| 661 |
/* Increment and load character */ |
| 662 |
|
| 663 |
if ( (ch = *text++)<0x80 ) { |
| 664 |
val = ch; |
| 665 |
} else if ( ch<=0xbf ) { |
| 666 |
/* error */ |
| 667 |
} else if ( ch<=0xdf ) { |
| 668 |
if ( *text>=0x80 && *text<0xc0 ) |
| 669 |
val = ((ch&0x1f)<<6) | (*text++&0x3f); |
| 670 |
} else if ( ch<=0xef ) { |
| 671 |
if ( *text>=0x80 && *text<0xc0 && text[1]>=0x80 && text[1]<0xc0 ) { |
| 672 |
val = ((ch&0xf)<<12) | ((text[0]&0x3f)<<6) | (text[1]&0x3f); |
| 673 |
text += 2; |
| 674 |
} |
| 675 |
} else { |
| 676 |
int w = ( ((ch&0x7)<<2) | ((text[0]&0x30)>>4) )-1, w2; |
| 677 |
w = (w<<6) | ((text[0]&0xf)<<2) | ((text[1]&0x30)>>4); |
| 678 |
w2 = ((text[1]&0xf)<<6) | (text[2]&0x3f); |
| 679 |
val = w*0x400 + w2 + 0x10000; |
| 680 |
if ( *text<0x80 || text[1]<0x80 || text[2]<0x80 || |
| 681 |
*text>=0xc0 || text[1]>=0xc0 || text[2]>=0xc0 ) |
| 682 |
val = -1; |
| 683 |
else |
| 684 |
text += 3; |
| 685 |
} |
| 686 |
*_text = (const char *) text; |
| 687 |
return( val ); |
| 688 |
} |
| 689 |
|
| 690 |
char *utf8_idpb(char *utf8_text,uint32 ch) { |
| 691 |
/* Increment and deposit character */ |
| 692 |
if ( ch>=17*65536 ) |
| 693 |
return( utf8_text ); |
| 694 |
|
| 695 |
if ( ch<=127 ) |
| 696 |
*utf8_text++ = ch; |
| 697 |
else if ( ch<=0x7ff ) { |
| 698 |
*utf8_text++ = 0xc0 | (ch>>6); |
| 699 |
*utf8_text++ = 0x80 | (ch&0x3f); |
| 700 |
} else if ( ch<=0xffff ) { |
| 701 |
*utf8_text++ = 0xe0 | (ch>>12); |
| 702 |
*utf8_text++ = 0x80 | ((ch>>6)&0x3f); |
| 703 |
*utf8_text++ = 0x80 | (ch&0x3f); |
| 704 |
} else { |
| 705 |
uint32 val = ch-0x10000; |
| 706 |
int u = ((val&0xf0000)>>16)+1, z=(val&0x0f000)>>12, y=(val&0x00fc0)>>6, x=val&0x0003f; |
| 707 |
*utf8_text++ = 0xf0 | (u>>2); |
| 708 |
*utf8_text++ = 0x80 | ((u&3)<<4) | z; |
| 709 |
*utf8_text++ = 0x80 | y; |
| 710 |
*utf8_text++ = 0x80 | x; |
| 711 |
} |
| 712 |
return( utf8_text ); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
char *utf8_ib(char *utf8_text) { |
| 717 |
int ch; |
| 718 |
|
| 719 |
/* Increment character */ |
| 720 |
if ( (ch = *utf8_text)=='\0' ) |
| 721 |
return( utf8_text ); |
| 722 |
else if ( ch<=127 ) |
| 723 |
return( utf8_text+1 ); |
| 724 |
else if ( ch<0xe0 ) |
| 725 |
return( utf8_text+2 ); |
| 726 |
else if ( ch<0xf0 ) |
| 727 |
return( utf8_text+3 ); |
| 728 |
else |
| 729 |
return( utf8_text+4 ); |
| 730 |
} |
| 731 |
|
| 732 |
int utf8_valid(const char *str) { |
| 733 |
/* Is this a valid utf8 string? */ |
| 734 |
int ch; |
| 735 |
|
| 736 |
while ( (ch=utf8_ildb(&str))!='\0' ) |
| 737 |
if ( ch==-1 ) |
| 738 |
return( false ); |
| 739 |
|
| 740 |
return( true ); |
| 741 |
} |
| 742 |
|
| 743 |
void utf8_truncatevalid(char *str) { |
| 744 |
/* There are certain cases where we have a fixed amount of space to display */ |
| 745 |
/* something, and if it doesn't fit in that, then we truncate it. But... */ |
| 746 |
/* that can leave us with a half completed utf8 byte sequence. So truncate*/ |
| 747 |
/* again, right before the start of the bad sequence */ |
| 748 |
int ch; |
| 749 |
char *old; |
| 750 |
|
| 751 |
old = str; |
| 752 |
while ( (ch=utf8_ildb((const char **) &str))!='\0' ) { |
| 753 |
if ( ch==-1 ) { |
| 754 |
*old = '\0'; |
| 755 |
return; |
| 756 |
} |
| 757 |
old = str; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
char *utf8_db(char *utf8_text) { |
| 762 |
/* Decrement utf8 pointer */ |
| 763 |
unsigned char *pt = (unsigned char *) utf8_text; |
| 764 |
|
| 765 |
--pt; |
| 766 |
if ( *pt>=0xc0 ) |
| 767 |
/* This should never happen. The pointer was looking at an intermediate */ |
| 768 |
/* character. However, if it does happen then we are now properly */ |
| 769 |
/* positioned at the start of a new char */; |
| 770 |
else if ( *pt>=0x80 ) { |
| 771 |
--pt; |
| 772 |
if ( *pt>=0xc0 ) |
| 773 |
/* Done */; |
| 774 |
else if ( *pt>=0x80 ) { |
| 775 |
--pt; |
| 776 |
if ( *pt>=0xc0 ) |
| 777 |
/* Done */; |
| 778 |
else if ( *pt>=0x80 ) |
| 779 |
--pt; |
| 780 |
} |
| 781 |
} |
| 782 |
return( (char *) pt ); |
| 783 |
} |
| 784 |
|
| 785 |
int utf8_strlen(const char *utf8_str) { |
| 786 |
/* how many characters in the string NOT bytes */ |
| 787 |
int len = 0; |
| 788 |
|
| 789 |
while ( utf8_ildb(&utf8_str)>0 ) |
| 790 |
++len; |
| 791 |
return( len ); |
| 792 |
} |
| 793 |
|
| 794 |
int utf82u_strlen(const char *utf8_str) { |
| 795 |
/* how many shorts needed to represent it in UCS2 */ |
| 796 |
int ch; |
| 797 |
int len = 0; |
| 798 |
|
| 799 |
while ( (ch = utf8_ildb(&utf8_str))>0 ) |
| 800 |
if ( ch>0x10000 ) |
| 801 |
len += 2; |
| 802 |
else |
| 803 |
++len; |
| 804 |
return( len ); |
| 805 |
} |
| 806 |
|
| 807 |
void utf8_strncpy(register char *to, const char *from, int len) { |
| 808 |
/* copy n characters NOT bytes */ |
| 809 |
const char *old = from; |
| 810 |
while ( len && *old ) { |
| 811 |
utf8_ildb(&old); |
| 812 |
len--; |
| 813 |
} |
| 814 |
strncpy(to, from, old-from); |
| 815 |
to[old-from] = 0; |
| 816 |
} |
| 817 |
|
| 818 |
#include <chardata.h> |
| 819 |
char *StripToASCII(const char *utf8_str) { |
| 820 |
/* Remove any non-ascii characters: Special case, convert the copyright symbol to (c) */ |
| 821 |
char *newcr, *pt, *end; |
| 822 |
int len, ch; |
| 823 |
const unichar_t *alt; |
| 824 |
|
| 825 |
len = strlen(utf8_str); |
| 826 |
pt = newcr = (char *) xmalloc(len+1); |
| 827 |
end = pt+len; |
| 828 |
while ( (ch= utf8_ildb(&utf8_str))!='\0' ) { |
| 829 |
if ( pt>=end ) { |
| 830 |
int off = pt-newcr; |
| 831 |
newcr = (char *) xrealloc(newcr,(off+10)+1); |
| 832 |
pt = newcr+off; |
| 833 |
end = pt+10; |
| 834 |
} |
| 835 |
if ( (ch>=' ' && ch<'\177' ) || ch=='\n' || ch=='\t' ) |
| 836 |
*pt++ = ch; |
| 837 |
else if ( ch=='\r' && *utf8_str!='\n' ) |
| 838 |
*pt++ = '\n'; |
| 839 |
else if ( ch==0xa9 /* Copyright sign */ ) { |
| 840 |
char *str = "(c)"; |
| 841 |
if ( pt+strlen(str)>=end ) { |
| 842 |
int off = pt-newcr; |
| 843 |
newcr = (char *) xrealloc(newcr,(off+10+strlen(str))+1); |
| 844 |
pt = newcr+off; |
| 845 |
end = pt+10; |
| 846 |
} |
| 847 |
while ( *str ) |
| 848 |
*pt++ = *str++; |
| 849 |
} else if ( unicode_alternates[ch>>8]!=NULL && |
| 850 |
(alt = unicode_alternates[ch>>8][ch&0xff])!=NULL ) { |
| 851 |
while ( *alt!='\0' ) { |
| 852 |
if ( pt>=end ) { |
| 853 |
int off = pt-newcr; |
| 854 |
newcr = (char *) xrealloc(newcr,(off+10)+1); |
| 855 |
pt = newcr+off; |
| 856 |
end = pt+10; |
| 857 |
} |
| 858 |
if ( *alt>=' ' && *alt<'\177' ) |
| 859 |
*pt++ = *alt; |
| 860 |
else if ( *alt==0x300 ) |
| 861 |
*pt++ = '`'; |
| 862 |
else if ( *alt==0x301 ) |
| 863 |
*pt++ = '\''; |
| 864 |
else if ( *alt==0x302 ) |
| 865 |
*pt++ = '^'; |
| 866 |
else if ( *alt==0x303 ) |
| 867 |
*pt++ = '~'; |
| 868 |
else if ( *alt==0x308 ) |
| 869 |
*pt++ = ':'; |
| 870 |
++alt; |
| 871 |
} |
| 872 |
} |
| 873 |
} |
| 874 |
*pt = '\0'; |
| 875 |
return( newcr ); |
| 876 |
} |
| 877 |
|
| 878 |
int AllAscii(const char *txt) { |
| 879 |
for ( ; *txt!='\0'; ++txt ) { |
| 880 |
if ( *txt=='\t' || *txt=='\n' || *txt=='\r' ) |
| 881 |
/* All right */; |
| 882 |
else if ( *txt<' ' || *txt>='\177' ) |
| 883 |
return( false ); |
| 884 |
} |
| 885 |
return( true ); |
| 886 |
} |
| 887 |
|
| 888 |
int uAllAscii(const unichar_t *txt) { |
| 889 |
for ( ; *txt!='\0'; ++txt ) { |
| 890 |
if ( *txt=='\t' || *txt=='\n' || *txt=='\r' ) |
| 891 |
/* All right */; |
| 892 |
else if ( *txt<' ' || *txt>='\177' ) |
| 893 |
return( false ); |
| 894 |
} |
| 895 |
return( true ); |
| 896 |
} |
| 897 |
|
| 898 |
char* chomp( char* line ) { |
| 899 |
if( !line ) |
| 900 |
return line; |
| 901 |
if ( line[strlen(line)-1]=='\n' ) |
| 902 |
line[strlen(line)-1] = '\0'; |
| 903 |
if ( line[strlen(line)-1]=='\r' ) |
| 904 |
line[strlen(line)-1] = '\0'; |
| 905 |
return line; |
| 906 |
} |
| 907 |
|
| 908 |
char *copytolower(const char *input) |
| 909 |
{ |
| 910 |
char* ret = xstrdup(input); |
| 911 |
char* p = ret; |
| 912 |
for( ; *p; ++p ) { |
| 913 |
*p = tolower(*p); |
| 914 |
} |
| 915 |
return ret; |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
int endswith(const char *haystack,const char *needle) { |
| 920 |
int haylen = strlen( haystack ); |
| 921 |
int nedlen = strlen( needle ); |
| 922 |
char* p; |
| 923 |
if( haylen < nedlen ) |
| 924 |
return 0; |
| 925 |
p = strstr( haystack + haylen - nedlen, needle ); |
| 926 |
return p == ( haystack + haylen - nedlen ); |
| 927 |
} |
| 928 |
|
| 929 |
int endswithi(const char *haystackZ,const char *needleZ) { |
| 930 |
char* haystack = copytolower(haystackZ); |
| 931 |
char* needle = copytolower(needleZ); |
| 932 |
int ret = endswith( haystack, needle ); |
| 933 |
free( haystack ); |
| 934 |
free( needle ); |
| 935 |
return ret; |
| 936 |
} |
| 937 |
|
| 938 |
int endswithi_partialExtension( const char *haystackZ,const char *needleZ) { |
| 939 |
int nedlen = strlen(needleZ); |
| 940 |
char* haystack; |
| 941 |
char* needle; |
| 942 |
int ret; |
| 943 |
int i; |
| 944 |
if( nedlen == 0 ) { |
| 945 |
return 0; |
| 946 |
} |
| 947 |
haystack = copytolower(haystackZ); |
| 948 |
needle = copytolower(needleZ); |
| 949 |
ret = 0; |
| 950 |
i = nedlen-1; |
| 951 |
ret |= endswith( haystack, needle ); |
| 952 |
for( ; i>=0 && !ret ; --i ) { |
| 953 |
needle[i] = '\0'; |
| 954 |
ret |= endswith( haystack, needle ); |
| 955 |
} |
| 956 |
free( haystack ); |
| 957 |
free( needle ); |
| 958 |
return ret; |
| 959 |
} |
| 960 |
|
| 961 |
int u_endswith(const unichar_t *haystack,const unichar_t *needle) { |
| 962 |
int haylen = u_strlen( haystack ); |
| 963 |
int nedlen = u_strlen( needle ); |
| 964 |
unichar_t* p; |
| 965 |
if( haylen < nedlen ) |
| 966 |
return 0; |
| 967 |
p = u_strstr( haystack + haylen - nedlen, needle ); |
| 968 |
return p == ( haystack + haylen - nedlen ); |
| 969 |
} |