Changeset 171 for trunk/src/sh_string.c
- Timestamp:
- Jul 8, 2008, 11:16:14 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/sh_string.c
r169 r171 4 4 #include <string.h> 5 5 #include <stdio.h> 6 #include <sys/types.h> 6 7 7 8 #include "sh_string.h" … … 40 41 /* skip leading WS 41 42 */ 42 for (s=e; *s && isspace( *s); ++s) /* nothing */;43 for (s=e; *s && isspace((int)*s); ++s) /* nothing */; 43 44 44 45 if (*s) … … 65 66 if (a != line) 66 67 { 67 /* chop off trailing WS 68 */ 69 for (a--; isspace(*a) && a > s; a--) /* do nothing */; 70 71 /* terminate string 72 */ 73 ++a; *a = '\0'; 74 } 68 if (i < (maxfields -1)) 69 { 70 71 /* chop off trailing WS 72 */ 73 for (a--; isspace((int)*a) && a > s; a--) /* do nothing */; 74 75 /* terminate string 76 */ 77 ++a; *a = '\0'; 78 } 79 else 80 { 81 /* If nfields < actual fields, last string 82 * will be remainder, therefore skip to end. 83 */ 84 if ( *a ) 85 { 86 do { 87 a++; 88 } while ( *a ); 89 } 90 } 91 } 75 92 else 76 93 { … … 127 144 /* skip leading WS 128 145 */ 129 if ( *s && isspace( *s) )146 if ( *s && isspace((int)*s) ) 130 147 { 131 148 do { 132 149 ++s; 133 } while ( *s && isspace( *s) );150 } while ( *s && isspace((int)*s) ); 134 151 } 135 152 … … 142 159 do { 143 160 a++; 144 } while ( *a && (!isspace( *a)) );161 } while ( *a && (!isspace((int)*a)) ); 145 162 146 163 /* next token, *a is either ws or '\0' … … 152 169 if (i < (maxfields-1)) 153 170 { 154 *a = '\0'; 171 *a = '\0'; 155 172 } 156 173 else … … 275 292 } 276 293 memcpy(s->str, str, (len+1)); 294 s->len = len; 277 295 return s; 278 296 } … … 397 415 sh_string * r = NULL; 398 416 char * p; 417 long tlen; 399 418 size_t len; 400 419 int end = 0; … … 403 422 size_t newlen = 0; 404 423 long diff; 405 int i ;424 int i, curr, last; 406 425 407 426 … … 443 462 } 444 463 445 if (r && ovecnum > 0) 464 465 curr = -1; 466 last = -1; 467 468 for (i = 0; i < ovecnum; ++i) 469 { 470 if (ovector[2*i] >= 0) 471 { 472 curr = i; 473 break; 474 } 475 } 476 477 if (r && ovecnum > 0 && ovector[curr] >= 0) 446 478 { 447 479 r->len = 0; r->str[0] = '\0'; p = r->str; … … 449 481 /* First part, until start of first replacement 450 482 */ 451 memcpy(p, s->str, ovector[0]); p += ovector[0]; 452 memcpy(p, replacement, rlen); p += rlen; 453 *p = '\0'; r->len += (ovector[0] + rlen); 483 memcpy(p, s->str, (size_t)ovector[curr]); p += ovector[curr]; 484 memcpy(p, replacement, rlen); p += rlen; 485 *p = '\0'; r->len += (ovector[curr] + rlen); 486 487 last = curr + 1; 454 488 455 489 for (i = 1; i < ovecnum; ++i) 456 490 { 491 if (ovector[2*i] < 0) 492 continue; 493 494 curr = 2*i; 495 457 496 /* From end of last replacement to start of this */ 458 len = ovector[2*i] - ovector[2*i -1]; 459 memcpy(p, &(s->str[ovector[2*i -1]]), len); 460 p += len; 461 462 /* The replacement */ 463 memcpy(p, replacement, rlen); 464 p += rlen; 465 466 /* null terminate */ 467 *p = '\0'; r->len += (len + rlen); 468 } 497 tlen = (long)(ovector[curr] - ovector[last]); 498 if (tlen >= 0) 499 { 500 len = (size_t) tlen; 501 502 if (tlen > 0) 503 { 504 memcpy(p, &(s->str[ovector[last]]), (size_t)len); 505 p += len; 506 } 507 508 /* The replacement */ 509 memcpy(p, replacement, rlen); 510 p += rlen; 511 512 /* null terminate */ 513 *p = '\0'; r->len += (len + rlen); 514 515 last = curr + 1; 516 } 517 } 469 518 470 519 /* Last part, after last replacement; includes terminating null 471 520 */ 472 len = (s->len + 1) - ovector[2*i -1]; 473 memcpy(p, &(s->str[ovector[2*i -1]]), len); 474 p += len; *p = '\0'; r->len += (len - 1); 475 } 521 if (last > 0) 522 { 523 /* If not, nothing has been replaced, and r is still a copy of s 524 */ 525 tlen = (long)((s->len + 1) - ovector[last]); 526 if (tlen > 0) 527 { 528 len = (size_t)tlen; 529 memcpy(p, &(s->str[ovector[2*i -1]]), (size_t)len); 530 p += len; *p = '\0'; r->len += (len - 1); 531 } 532 } 533 534 } 535 476 536 return r; 477 537 } … … 785 845 t = sh_string_replace(s, ovector, ovecnum, 786 846 "___", 3); 847 CuAssertPtrNotNull(tc, t); 787 848 CuAssertStrEquals(tc, "___c ___ ", t->str); 788 849 CuAssertIntEquals(tc, 9, (int)t->len); … … 792 853 t = sh_string_replace(s, ovector, ovecnum, 793 854 "___", 3); 855 CuAssertPtrNotNull(tc, t); 794 856 CuAssertStrEquals(tc, "___c ___", t->str); 795 857 CuAssertIntEquals(tc, 8, (int)t->len); … … 806 868 "___", 3); 807 869 870 CuAssertPtrNotNull(tc, t); 808 871 CuAssertStrEquals(tc, "______f ghi ", t->str); 809 872 CuAssertIntEquals(tc, 12, (int)t->len); … … 813 876 t = sh_string_replace(s, ovector, ovecnum, 814 877 "___", 3); 878 CuAssertPtrNotNull(tc, t); 815 879 CuAssertStrEquals(tc, "abc ___ef ghi___", t->str); 816 880 CuAssertIntEquals(tc, 16, (int)t->len); … … 818 882 t = sh_string_replace(s, ovector, 0, 819 883 "___", 3); 884 CuAssertPtrNotNull(tc, t); 820 885 CuAssertStrEquals(tc, s->str, t->str); 821 886 CuAssertIntEquals(tc, (int)s->len, (int)t->len);
Note:
See TracChangeset
for help on using the changeset viewer.