src/string.c

changeset 319
0380e438a7ce
parent 318
348fd9cb7b14
child 361
8ee9e23adbd2
equal deleted inserted replaced
318:348fd9cb7b14 319:0380e438a7ce
63 string.length = length; 63 string.length = length;
64 return string; 64 return string;
65 } 65 }
66 66
67 67
68 size_t ucx_strnlen(size_t n, ...) { 68 size_t scstrnlen(size_t n, ...) {
69 va_list ap; 69 va_list ap;
70 va_start(ap, n); 70 va_start(ap, n);
71 71
72 size_t size = 0; 72 size_t size = 0;
73 73
148 free(strings); 148 free(strings);
149 149
150 return str; 150 return str;
151 } 151 }
152 152
153 sstr_t ucx_strcat(size_t count, scstr_t s1, ...) { 153 sstr_t scstrcat(size_t count, scstr_t s1, ...) {
154 va_list ap; 154 va_list ap;
155 va_start(ap, s1); 155 va_start(ap, s1);
156 sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, ap); 156 sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, ap);
157 va_end(ap); 157 va_end(ap);
158 return s; 158 return s;
159 } 159 }
160 160
161 sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...) { 161 sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...) {
162 va_list ap; 162 va_list ap;
163 va_start(ap, s1); 163 va_start(ap, s1);
164 sstr_t s = sstrvcat_a(a, count, s1, ap); 164 sstr_t s = sstrvcat_a(a, count, s1, ap);
165 va_end(ap); 165 va_end(ap);
166 return s; 166 return s;
346 346
347 *newlen = resultlen; 347 *newlen = resultlen;
348 return result; 348 return result;
349 } 349 }
350 350
351 sstr_t ucx_sstrstr(sstr_t string, scstr_t match) { 351 sstr_t scstrsstr(sstr_t string, scstr_t match) {
352 sstr_t result; 352 sstr_t result;
353 353
354 size_t reslen; 354 size_t reslen;
355 const char *resstr = ucx_strstr(string.ptr, string.length, match.ptr, match.length, &reslen); 355 const char *resstr = ucx_strstr(string.ptr, string.length, match.ptr, match.length, &reslen);
356 if(!resstr) { 356 if(!resstr) {
364 result.length = reslen; 364 result.length = reslen;
365 365
366 return result; 366 return result;
367 } 367 }
368 368
369 scstr_t ucx_scstrstr(scstr_t string, scstr_t match) { 369 scstr_t scstrscstr(scstr_t string, scstr_t match) {
370 scstr_t result; 370 scstr_t result;
371 371
372 size_t reslen; 372 size_t reslen;
373 const char *resstr = ucx_strstr(string.ptr, string.length, match.ptr, match.length, &reslen); 373 const char *resstr = ucx_strstr(string.ptr, string.length, match.ptr, match.length, &reslen);
374 if(!resstr) { 374 if(!resstr) {
385 } 385 }
386 386
387 #undef ptable_r 387 #undef ptable_r
388 #undef ptable_w 388 #undef ptable_w
389 389
390 sstr_t* ucx_strsplit(scstr_t s, scstr_t d, ssize_t *n) { 390 sstr_t* scstrsplit(scstr_t s, scstr_t d, ssize_t *n) {
391 return ucx_strsplit_a(ucx_default_allocator(), s, d, n); 391 return scstrsplit_a(ucx_default_allocator(), s, d, n);
392 } 392 }
393 393
394 sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t s, scstr_t d, ssize_t *n) { 394 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t s, scstr_t d, ssize_t *n) {
395 if (s.length == 0 || d.length == 0) { 395 if (s.length == 0 || d.length == 0) {
396 *n = -1; 396 *n = -1;
397 return NULL; 397 return NULL;
398 } 398 }
399 399
433 break; 433 break;
434 } 434 }
435 match.length--; 435 match.length--;
436 } 436 }
437 } else { 437 } else {
438 match = scstrstr(curpos, d); 438 match = scstrscstr(curpos, d);
439 } 439 }
440 if (match.length > 0) { 440 if (match.length > 0) {
441 /* is this our last try? */ 441 /* is this our last try? */
442 if (nmax == 0 || j < nmax) { 442 if (nmax == 0 || j < nmax) {
443 /* copy the current string to the array */ 443 /* copy the current string to the array */
485 } 485 }
486 486
487 return result; 487 return result;
488 } 488 }
489 489
490 int ucx_strcmp(scstr_t s1, scstr_t s2) { 490 int scstrcmp(scstr_t s1, scstr_t s2) {
491 if (s1.length == s2.length) { 491 if (s1.length == s2.length) {
492 return memcmp(s1.ptr, s2.ptr, s1.length); 492 return memcmp(s1.ptr, s2.ptr, s1.length);
493 } else if (s1.length > s2.length) { 493 } else if (s1.length > s2.length) {
494 return 1; 494 return 1;
495 } else { 495 } else {
496 return -1; 496 return -1;
497 } 497 }
498 } 498 }
499 499
500 int ucx_strcasecmp(scstr_t s1, scstr_t s2) { 500 int scstrcasecmp(scstr_t s1, scstr_t s2) {
501 if (s1.length == s2.length) { 501 if (s1.length == s2.length) {
502 #ifdef _WIN32 502 #ifdef _WIN32
503 return _strnicmp(s1.ptr, s2.ptr, s1.length); 503 return _strnicmp(s1.ptr, s2.ptr, s1.length);
504 #else 504 #else
505 return strncasecmp(s1.ptr, s2.ptr, s1.length); 505 return strncasecmp(s1.ptr, s2.ptr, s1.length);
509 } else { 509 } else {
510 return -1; 510 return -1;
511 } 511 }
512 } 512 }
513 513
514 sstr_t ucx_strdup(scstr_t s) { 514 sstr_t scstrdup(scstr_t s) {
515 return sstrdup_a(ucx_default_allocator(), s); 515 return sstrdup_a(ucx_default_allocator(), s);
516 } 516 }
517 517
518 sstr_t ucx_strdup_a(UcxAllocator *allocator, scstr_t s) { 518 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t s) {
519 sstr_t newstring; 519 sstr_t newstring;
520 newstring.ptr = (char*)almalloc(allocator, s.length + 1); 520 newstring.ptr = (char*)almalloc(allocator, s.length + 1);
521 if (newstring.ptr) { 521 if (newstring.ptr) {
522 newstring.length = s.length; 522 newstring.length = s.length;
523 newstring.ptr[newstring.length] = 0; 523 newstring.ptr[newstring.length] = 0;
559 newstr.ptr = string.ptr 559 newstr.ptr = string.ptr
560 + ucx_strtrim(string.ptr, string.length, &newstr.length); 560 + ucx_strtrim(string.ptr, string.length, &newstr.length);
561 return newstr; 561 return newstr;
562 } 562 }
563 563
564 int ucx_strprefix(scstr_t string, scstr_t prefix) { 564 int scstrprefix(scstr_t string, scstr_t prefix) {
565 if (string.length == 0) { 565 if (string.length == 0) {
566 return prefix.length == 0; 566 return prefix.length == 0;
567 } 567 }
568 if (prefix.length == 0) { 568 if (prefix.length == 0) {
569 return 1; 569 return 1;
574 } else { 574 } else {
575 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; 575 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
576 } 576 }
577 } 577 }
578 578
579 int ucx_strsuffix(scstr_t string, scstr_t suffix) { 579 int scstrsuffix(scstr_t string, scstr_t suffix) {
580 if (string.length == 0) { 580 if (string.length == 0) {
581 return suffix.length == 0; 581 return suffix.length == 0;
582 } 582 }
583 if (suffix.length == 0) { 583 if (suffix.length == 0) {
584 return 1; 584 return 1;
590 return memcmp(string.ptr+string.length-suffix.length, 590 return memcmp(string.ptr+string.length-suffix.length,
591 suffix.ptr, suffix.length) == 0; 591 suffix.ptr, suffix.length) == 0;
592 } 592 }
593 } 593 }
594 594
595 sstr_t ucx_strlower(scstr_t string) { 595 sstr_t scstrlower(scstr_t string) {
596 sstr_t ret = sstrdup(string); 596 sstr_t ret = sstrdup(string);
597 for (size_t i = 0; i < ret.length ; i++) { 597 for (size_t i = 0; i < ret.length ; i++) {
598 ret.ptr[i] = tolower(ret.ptr[i]); 598 ret.ptr[i] = tolower(ret.ptr[i]);
599 } 599 }
600 return ret; 600 return ret;
601 } 601 }
602 602
603 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string) { 603 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string) {
604 sstr_t ret = sstrdup_a(allocator, string); 604 sstr_t ret = sstrdup_a(allocator, string);
605 for (size_t i = 0; i < ret.length ; i++) { 605 for (size_t i = 0; i < ret.length ; i++) {
606 ret.ptr[i] = tolower(ret.ptr[i]); 606 ret.ptr[i] = tolower(ret.ptr[i]);
607 } 607 }
608 return ret; 608 return ret;
609 } 609 }
610 610
611 sstr_t ucx_strupper(scstr_t string) { 611 sstr_t scstrupper(scstr_t string) {
612 sstr_t ret = sstrdup(string); 612 sstr_t ret = sstrdup(string);
613 for (size_t i = 0; i < ret.length ; i++) { 613 for (size_t i = 0; i < ret.length ; i++) {
614 ret.ptr[i] = toupper(ret.ptr[i]); 614 ret.ptr[i] = toupper(ret.ptr[i]);
615 } 615 }
616 return ret; 616 return ret;
617 } 617 }
618 618
619 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string) { 619 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string) {
620 sstr_t ret = sstrdup_a(allocator, string); 620 sstr_t ret = sstrdup_a(allocator, string);
621 for (size_t i = 0; i < ret.length ; i++) { 621 for (size_t i = 0; i < ret.length ; i++) {
622 ret.ptr[i] = toupper(ret.ptr[i]); 622 ret.ptr[i] = toupper(ret.ptr[i]);
623 } 623 }
624 return ret; 624 return ret;

mercurial