1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
|
/* -----------------------------------------------------------------------------
* This file is part of SWIG, which is licensed as a whole under version 3
* (or any later version) of the GNU General Public License. Some additional
* terms also apply to certain portions of SWIG. The full details of the SWIG
* license and copyrights can be found in the LICENSE and COPYRIGHT files
* included with the SWIG source code as distributed by the SWIG developers
* and at http://www.swig.org/legal.html.
*
* string.c
*
* Implements a string object that supports both sequence operations and
* file semantics.
* ----------------------------------------------------------------------------- */
#include "dohint.h"
extern DohObjInfo DohStringType;
typedef struct String {
DOH *file;
int line;
int maxsize; /* Max size allocated */
int len; /* Current length */
int hashkey; /* Hash key value */
int sp; /* Current position */
char *str; /* String data */
} String;
/* -----------------------------------------------------------------------------
* String_data() - Return as a 'void *'
* ----------------------------------------------------------------------------- */
static void *String_data(DOH *so) {
String *s = (String *) ObjData(so);
s->str[s->len] = 0;
return (void *) s->str;
}
/* static char *String_char(DOH *so) {
return (char *) String_data(so);
}
*/
/* -----------------------------------------------------------------------------
* String_dump() - Serialize a string onto out
* ----------------------------------------------------------------------------- */
static int String_dump(DOH *so, DOH *out) {
int nsent;
int ret;
String *s = (String *) ObjData(so);
nsent = 0;
while (nsent < s->len) {
ret = Write(out, s->str + nsent, (s->len - nsent));
if (ret < 0)
return ret;
nsent += ret;
}
return nsent;
}
/* -----------------------------------------------------------------------------
* CopyString() - Copy a string
* ----------------------------------------------------------------------------- */
static DOH *CopyString(DOH *so) {
String *str;
String *s = (String *) ObjData(so);
str = (String *) DohMalloc(sizeof(String));
str->hashkey = s->hashkey;
str->sp = s->sp;
str->line = s->line;
str->file = s->file;
if (str->file)
Incref(str->file);
str->str = (char *) DohMalloc(s->len + 1);
memcpy(str->str, s->str, s->len);
str->maxsize = s->len;
str->len = s->len;
str->str[str->len] = 0;
return DohObjMalloc(&DohStringType, str);
}
/* -----------------------------------------------------------------------------
* DelString() - Delete a string
* ----------------------------------------------------------------------------- */
static void DelString(DOH *so) {
String *s = (String *) ObjData(so);
DohFree(s->str);
DohFree(s);
}
/* -----------------------------------------------------------------------------
* DohString_len() - Length of a string
* ----------------------------------------------------------------------------- */
static int String_len(DOH *so) {
String *s = (String *) ObjData(so);
return s->len;
}
/* -----------------------------------------------------------------------------
* String_cmp() - Compare two strings
* ----------------------------------------------------------------------------- */
static int String_cmp(DOH *so1, DOH *so2) {
String *s1, *s2;
char *c1, *c2;
int maxlen, i;
s1 = (String *) ObjData(so1);
s2 = (String *) ObjData(so2);
maxlen = s1->len;
if (s2->len < maxlen)
maxlen = s2->len;
c1 = s1->str;
c2 = s2->str;
for (i = maxlen; i; --i, c1++, c2++) {
if (*c1 != *c2)
break;
}
if (i != 0) {
if (*c1 < *c2)
return -1;
else
return 1;
}
if (s1->len == s2->len)
return 0;
if (s1->len > s2->len)
return 1;
return -1;
}
/* -----------------------------------------------------------------------------
* String_equal() - Say if two string are equal
* ----------------------------------------------------------------------------- */
static int String_equal(DOH *so1, DOH *so2) {
String *s1 = (String *) ObjData(so1);
String *s2 = (String *) ObjData(so2);
register int len = s1->len;
if (len != s2->len) {
return 0;
} else {
register char *c1 = s1->str;
register char *c2 = s2->str;
#if 0
register int mlen = len >> 2;
register int i = mlen;
for (; i; --i) {
if (*(c1++) != *(c2++))
return 0;
if (*(c1++) != *(c2++))
return 0;
if (*(c1++) != *(c2++))
return 0;
if (*(c1++) != *(c2++))
return 0;
}
for (i = len - (mlen << 2); i; --i) {
if (*(c1++) != *(c2++))
return 0;
}
return 1;
#else
return memcmp(c1, c2, len) == 0;
#endif
}
}
/* -----------------------------------------------------------------------------
* String_hash() - Compute string hash value
* ----------------------------------------------------------------------------- */
static int String_hash(DOH *so) {
String *s = (String *) ObjData(so);
if (s->hashkey >= 0) {
return s->hashkey;
} else {
register char *c = s->str;
register unsigned int len = s->len > 50 ? 50 : s->len;
register unsigned int h = 0;
register unsigned int mlen = len >> 2;
register unsigned int i = mlen;
for (; i; --i) {
h = (h << 5) + *(c++);
h = (h << 5) + *(c++);
h = (h << 5) + *(c++);
h = (h << 5) + *(c++);
}
for (i = len - (mlen << 2); i; --i) {
h = (h << 5) + *(c++);
}
h &= 0x7fffffff;
s->hashkey = (int)h;
return h;
}
}
/* -----------------------------------------------------------------------------
* DohString_append() - Append to s
* ----------------------------------------------------------------------------- */
static void DohString_append(DOH *so, const DOHString_or_char *str) {
int oldlen, newlen, newmaxsize, l, sp;
char *tc;
String *s = (String *) ObjData(so);
char *newstr = 0;
if (DohCheck(str)) {
String *ss = (String *) ObjData(str);
newstr = (char *) String_data((DOH *) str);
l = ss->len;
} else {
newstr = (char *) (str);
l = (int) strlen(newstr);
}
if (!newstr)
return;
s->hashkey = -1;
oldlen = s->len;
newlen = oldlen + l + 1;
if (newlen >= s->maxsize - 1) {
newmaxsize = 2 * s->maxsize;
if (newlen >= newmaxsize - 1)
newmaxsize = newlen + 1;
s->str = (char *) DohRealloc(s->str, newmaxsize);
assert(s->str);
s->maxsize = newmaxsize;
}
tc = s->str;
memcpy(tc + oldlen, newstr, l + 1);
sp = s->sp;
if (sp >= oldlen) {
int i = oldlen + l - sp;
tc += sp;
for (; i; --i) {
if (*(tc++) == '\n')
s->line++;
}
s->sp = oldlen + l;
}
s->len += l;
}
/* -----------------------------------------------------------------------------
* String_clear() - Clear a string
* ----------------------------------------------------------------------------- */
static void String_clear(DOH *so) {
String *s = (String *) ObjData(so);
s->hashkey = -1;
s->len = 0;
*(s->str) = 0;
s->sp = 0;
s->line = 1;
}
/* -----------------------------------------------------------------------------
* String_insert() - Insert a string
* ----------------------------------------------------------------------------- */
static int String_insert(DOH *so, int pos, DOH *str) {
String *s;
int len;
char *data;
if (pos == DOH_END) {
DohString_append(so, str);
return 0;
}
s = (String *) ObjData(so);
s->hashkey = -1;
if (DohCheck(str)) {
String *ss = (String *) ObjData(str);
data = (char *) String_data(str);
len = ss->len;
} else {
data = (char *) (str);
len = (int) strlen(data);
}
if (pos < 0)
pos = 0;
else if (pos > s->len)
pos = s->len;
/* See if there is room to insert the new data */
while (s->maxsize <= s->len + len) {
int newsize = 2 * s->maxsize;
s->str = (char *) DohRealloc(s->str, newsize);
assert(s->str);
s->maxsize = newsize;
}
memmove(s->str + pos + len, s->str + pos, (s->len - pos));
memcpy(s->str + pos, data, len);
if (s->sp >= pos) {
int i;
for (i = 0; i < len; i++) {
if (data[i] == '\n')
s->line++;
}
s->sp += len;
}
s->len += len;
s->str[s->len] = 0;
return 0;
}
/* -----------------------------------------------------------------------------
* String_delitem() - Delete a character
* ----------------------------------------------------------------------------- */
static int String_delitem(DOH *so, int pos) {
String *s = (String *) ObjData(so);
s->hashkey = -1;
if (pos == DOH_END)
pos = s->len - 1;
if (pos == DOH_BEGIN)
pos = 0;
if (s->len == 0)
return 0;
if (s->sp > pos) {
s->sp--;
assert(s->sp >= 0);
if (s->str[pos] == '\n')
s->line--;
}
memmove(s->str + pos, s->str + pos + 1, ((s->len - 1) - pos));
s->len--;
s->str[s->len] = 0;
return 0;
}
/* -----------------------------------------------------------------------------
* String_delslice() - Delete a range
* ----------------------------------------------------------------------------- */
static int String_delslice(DOH *so, int sindex, int eindex) {
String *s = (String *) ObjData(so);
int size;
if (s->len == 0)
return 0;
s->hashkey = -1;
if (eindex == DOH_END)
eindex = s->len;
if (sindex == DOH_BEGIN)
sindex = 0;
size = eindex - sindex;
if (s->sp > sindex) {
/* Adjust the file pointer and line count */
int i, end;
if (s->sp > eindex) {
end = eindex;
s->sp -= size;
} else {
end = s->sp;
s->sp = sindex;
}
for (i = sindex; i < end; i++) {
if (s->str[i] == '\n')
s->line--;
}
assert(s->sp >= 0);
}
memmove(s->str + sindex, s->str + eindex, s->len - eindex);
s->len -= size;
s->str[s->len] = 0;
return 0;
}
/* -----------------------------------------------------------------------------
* String_str() - Returns a string (used by printing commands)
* ----------------------------------------------------------------------------- */
static DOH *String_str(DOH *so) {
String *s = (String *) ObjData(so);
s->str[s->len] = 0;
return NewString(s->str);
}
/* -----------------------------------------------------------------------------
* String_read() - Read data from a string
* ----------------------------------------------------------------------------- */
static int String_read(DOH *so, void *buffer, int len) {
int reallen, retlen;
char *cb;
String *s = (String *) ObjData(so);
if ((s->sp + len) > s->len)
reallen = (s->len - s->sp);
else
reallen = len;
cb = (char *) buffer;
retlen = reallen;
if (reallen > 0) {
memmove(cb, s->str + s->sp, reallen);
s->sp += reallen;
}
return retlen;
}
/* -----------------------------------------------------------------------------
* String_write() - Write data to a string
* ----------------------------------------------------------------------------- */
static int String_write(DOH *so, const void *buffer, int len) {
int newlen;
String *s = (String *) ObjData(so);
s->hashkey = -1;
if (s->sp > s->len)
s->sp = s->len;
newlen = s->sp + len + 1;
if (newlen > s->maxsize) {
s->str = (char *) DohRealloc(s->str, newlen);
assert(s->str);
s->maxsize = newlen;
s->len = s->sp + len;
}
if ((s->sp + len) > s->len)
s->len = s->sp + len;
memmove(s->str + s->sp, buffer, len);
s->sp += len;
s->str[s->len] = 0;
return len;
}
/* -----------------------------------------------------------------------------
* String_seek() - Seek to a new position
* ----------------------------------------------------------------------------- */
static int String_seek(DOH *so, long offset, int whence) {
int pos, nsp, inc;
String *s = (String *) ObjData(so);
if (whence == SEEK_SET)
pos = 0;
else if (whence == SEEK_CUR)
pos = s->sp;
else if (whence == SEEK_END) {
pos = s->len;
offset = -offset;
} else
pos = s->sp;
nsp = pos + offset;
if (nsp < 0)
nsp = 0;
if (s->len > 0 && nsp > s->len)
nsp = s->len;
inc = (nsp > s->sp) ? 1 : -1;
{
#if 0
register int sp = s->sp;
register char *tc = s->str;
register int len = s->len;
while (sp != nsp) {
int prev = sp + inc;
if (prev >= 0 && prev <= len && tc[prev] == '\n')
s->line += inc;
sp += inc;
}
#else
register int sp = s->sp;
register char *tc = s->str;
if (inc > 0) {
while (sp != nsp) {
if (tc[++sp] == '\n')
++s->line;
}
} else {
while (sp != nsp) {
if (tc[--sp] == '\n')
--s->line;
}
}
#endif
s->sp = sp;
}
assert(s->sp >= 0);
return 0;
}
/* -----------------------------------------------------------------------------
* String_tell() - Return current position
* ----------------------------------------------------------------------------- */
static long String_tell(DOH *so) {
String *s = (String *) ObjData(so);
return (long) (s->sp);
}
/* -----------------------------------------------------------------------------
* String_putc()
* ----------------------------------------------------------------------------- */
static int String_putc(DOH *so, int ch) {
String *s = (String *) ObjData(so);
register int len = s->len;
register int sp = s->sp;
s->hashkey = -1;
if (sp >= len) {
register int maxsize = s->maxsize;
register char *tc = s->str;
if (len > (maxsize - 2)) {
maxsize *= 2;
tc = (char *) DohRealloc(tc, maxsize);
assert(tc);
s->maxsize = (int) maxsize;
s->str = tc;
}
tc += sp;
*tc = (char) ch;
*(++tc) = 0;
s->len = s->sp = sp + 1;
} else {
s->str[s->sp++] = (char) ch;
}
if (ch == '\n')
s->line++;
return ch;
}
/* -----------------------------------------------------------------------------
* String_getc()
* ----------------------------------------------------------------------------- */
static int String_getc(DOH *so) {
int c;
String *s = (String *) ObjData(so);
if (s->sp >= s->len)
c = EOF;
else
c = (int)(unsigned char) s->str[s->sp++];
if (c == '\n')
s->line++;
return c;
}
/* -----------------------------------------------------------------------------
* String_ungetc()
* ----------------------------------------------------------------------------- */
static int String_ungetc(DOH *so, int ch) {
String *s = (String *) ObjData(so);
if (ch == EOF)
return ch;
if (s->sp <= 0)
return EOF;
s->sp--;
if (ch == '\n')
s->line--;
return ch;
}
static char *end_quote(char *s) {
char *qs;
char qc;
char *q;
char *nl;
qc = *s;
qs = s;
while (1) {
q = strpbrk(s + 1, "\"\'");
nl = strchr(s + 1, '\n');
if (nl && (nl < q)) {
/* A new line appears before the end of the string */
if (*(nl - 1) == '\\') {
s = nl + 1;
continue;
}
/* String was terminated by a newline. Wing it */
return qs;
}
if (!q && nl) {
return qs;
}
if (!q)
return 0;
if ((*q == qc) && (*(q - 1) != '\\'))
return q;
s = q;
}
}
static char *match_simple(char *base, char *s, char *token, int tokenlen) {
(void) base;
(void) tokenlen;
return strstr(s, token);
}
static char *match_identifier(char *base, char *s, char *token, int tokenlen) {
while (s) {
s = strstr(s, token);
if (!s)
return 0;
if ((s > base) && (isalnum((int) *(s - 1)) || (*(s - 1) == '_'))) {
s += tokenlen;
continue;
}
if (isalnum((int) *(s + tokenlen)) || (*(s + tokenlen) == '_')) {
s += tokenlen;
continue;
}
return s;
}
return 0;
}
static char *match_identifier_begin(char *base, char *s, char *token, int tokenlen) {
while (s) {
s = strstr(s, token);
if (!s)
return 0;
if ((s > base) && (isalnum((int) *(s - 1)) || (*(s - 1) == '_'))) {
s += tokenlen;
continue;
}
return s;
}
return 0;
}
static char *match_identifier_end(char *base, char *s, char *token, int tokenlen) {
(void) base;
while (s) {
s = strstr(s, token);
if (!s)
return 0;
if (isalnum((int) *(s + tokenlen)) || (*(s + tokenlen) == '_')) {
s += tokenlen;
continue;
}
return s;
}
return 0;
}
static char *match_number_end(char *base, char *s, char *token, int tokenlen) {
(void) base;
while (s) {
s = strstr(s, token);
if (!s)
return 0;
if (isdigit((int) *(s + tokenlen))) {
s += tokenlen;
continue;
}
return s;
}
return 0;
}
/* -----------------------------------------------------------------------------
* replace_simple()
*
* Replaces count non-overlapping occurrences of token with rep in a string.
* ----------------------------------------------------------------------------- */
static int replace_simple(String *str, char *token, char *rep, int flags, int count, char *(*match) (char *, char *, char *, int)) {
int tokenlen; /* Length of the token */
int replen; /* Length of the replacement */
int delta, expand = 0;
int ic;
int rcount = 0;
int noquote = 0;
char *c, *s, *t, *first;
char *q, *q2;
register char *base;
int i;
/* Figure out if anything gets replaced */
if (!strlen(token))
return 0;
base = str->str;
tokenlen = (int)strlen(token);
s = (*match) (base, base, token, tokenlen);
if (!s)
return 0; /* No matches. Who cares */
str->hashkey = -1;
if (flags & DOH_REPLACE_NOQUOTE)
noquote = 1;
/* If we are not replacing inside quotes, we need to do a little extra work */
if (noquote) {
q = strpbrk(base, "\"\'");
if (!q) {
noquote = 0; /* Well, no quotes to worry about. Oh well */
} else {
while (q && (q < s)) {
/* First match was found inside a quote. Try to find another match */
q2 = end_quote(q);
if (!q2) {
return 0;
}
if (q2 > s) {
/* Find next match */
s = (*match) (base, q2 + 1, token, tokenlen);
}
if (!s)
return 0; /* Oh well, no matches */
q = strpbrk(q2 + 1, "\"\'");
if (!q)
noquote = 0; /* No more quotes */
}
}
}
first = s;
replen = (int)strlen(rep);
delta = (replen - tokenlen);
if (delta <= 0) {
/* String is either shrinking or staying the same size */
/* In this case, we do the replacement in place without memory reallocation */
ic = count;
t = s; /* Target of memory copies */
while (ic && s) {
if (replen) {
memcpy(t, rep, replen);
t += replen;
}
rcount++;
expand += delta;
/* Find the next location */
s += tokenlen;
if (ic == 1)
break;
c = (*match) (base, s, token, tokenlen);
if (noquote) {
q = strpbrk(s, "\"\'");
if (!q) {
noquote = 0;
} else {
while (q && (q < c)) {
/* First match was found inside a quote. Try to find another match */
q2 = end_quote(q);
if (!q2) {
c = 0;
break;
}
if (q2 > c)
c = (*match) (base, q2 + 1, token, tokenlen);
if (!c)
break;
q = strpbrk(q2 + 1, "\"\'");
if (!q)
noquote = 0; /* No more quotes */
}
}
}
if (delta) {
if (c) {
memmove(t, s, c - s);
t += (c - s);
} else {
memmove(t, s, (str->str + str->len) - s + 1);
}
} else {
t += (c - s);
}
s = c;
ic--;
}
if (s && delta) {
memmove(t, s, (str->str + str->len) - s + 1);
}
str->len += expand;
str->str[str->len] = 0;
if (str->sp >= str->len)
str->sp += expand; /* Fix the end of file pointer */
return rcount;
}
/* The string is expanding as a result of the replacement */
/* Figure out how much expansion is going to occur and allocate a new string */
{
char *ns;
int newsize;
rcount++;
ic = count - 1;
s += tokenlen;
while (ic && (c = (*match) (base, s, token, tokenlen))) {
if (noquote) {
q = strpbrk(s, "\"\'");
if (!q) {
break;
} else {
while (q && (q < c)) {
/* First match was found inside a quote. Try to find another match */
q2 = end_quote(q);
if (!q2) {
c = 0;
break;
}
if (q2 > c) {
c = (*match) (base, q2 + 1, token, tokenlen);
if (!c)
break;
}
q = strpbrk(q2 + 1, "\"\'");
if (!q)
noquote = 0;
}
}
}
if (c) {
rcount++;
ic--;
s = c + tokenlen;
} else {
break;
}
}
expand = delta * rcount; /* Total amount of expansion for the replacement */
newsize = str->maxsize;
while ((str->len + expand) >= newsize)
newsize *= 2;
ns = (char *) DohMalloc(newsize);
assert(ns);
t = ns;
s = first;
/* Copy the first part of the string */
if (first > str->str) {
memcpy(t, str->str, (first - str->str));
t += (first - str->str);
}
for (i = 0; i < rcount; i++) {
memcpy(t, rep, replen);
t += replen;
s += tokenlen;
c = (*match) (base, s, token, tokenlen);
if (noquote) {
q = strpbrk(s, "\"\'");
if (!q) {
noquote = 0;
} else {
while (q && (q < c)) {
/* First match was found inside a quote. Try to find another match */
q2 = end_quote(q);
if (!q2) {
c = 0;
break;
}
if (q2 > c) {
c = (*match) (base, q2 + 1, token, tokenlen);
if (!c)
break;
}
q = strpbrk(q2 + 1, "\"\'");
if (!q)
noquote = 0; /* No more quotes */
}
}
}
if (i < (rcount - 1)) {
memcpy(t, s, c - s);
t += (c - s);
} else {
memcpy(t, s, (str->str + str->len) - s + 1);
}
s = c;
}
c = str->str;
str->str = ns;
if (str->sp >= str->len)
str->sp += expand;
str->len += expand;
str->str[str->len] = 0;
str->maxsize = newsize;
DohFree(c);
return rcount;
}
}
/* -----------------------------------------------------------------------------
* String_replace()
* ----------------------------------------------------------------------------- */
static int String_replace(DOH *stro, const DOHString_or_char *token, const DOHString_or_char *rep, int flags) {
int count = -1;
String *str = (String *) ObjData(stro);
if (flags & DOH_REPLACE_FIRST)
count = 1;
if (flags & DOH_REPLACE_ID_END) {
return replace_simple(str, Char(token), Char(rep), flags, count, match_identifier_end);
} else if (flags & DOH_REPLACE_ID_BEGIN) {
return replace_simple(str, Char(token), Char(rep), flags, count, match_identifier_begin);
} else if (flags & DOH_REPLACE_ID) {
return replace_simple(str, Char(token), Char(rep), flags, count, match_identifier);
} else if (flags & DOH_REPLACE_NUMBER_END) {
return replace_simple(str, Char(token), Char(rep), flags, count, match_number_end);
} else {
return replace_simple(str, Char(token), Char(rep), flags, count, match_simple);
}
}
/* -----------------------------------------------------------------------------
* String_chop()
* ----------------------------------------------------------------------------- */
static void String_chop(DOH *so) {
char *c;
String *str = (String *) ObjData(so);
/* Replace trailing whitespace */
c = str->str + str->len - 1;
while ((str->len > 0) && (isspace((int) *c))) {
if (str->sp >= str->len) {
str->sp--;
if (*c == '\n')
str->line--;
}
str->len--;
c--;
}
str->str[str->len] = 0;
assert(str->sp >= 0);
str->hashkey = -1;
}
static void String_setfile(DOH *so, DOH *file) {
DOH *fo;
String *str = (String *) ObjData(so);
if (!DohCheck(file)) {
fo = NewString(file);
Decref(fo);
} else
fo = file;
Incref(fo);
Delete(str->file);
str->file = fo;
}
static DOH *String_getfile(DOH *so) {
String *str = (String *) ObjData(so);
return str->file;
}
static void String_setline(DOH *so, int line) {
String *str = (String *) ObjData(so);
str->line = line;
}
static int String_getline(DOH *so) {
String *str = (String *) ObjData(so);
return str->line;
}
static DohListMethods StringListMethods = {
0, /* doh_getitem */
0, /* doh_setitem */
String_delitem, /* doh_delitem */
String_insert, /* doh_insitem */
String_delslice, /* doh_delslice */
};
static DohFileMethods StringFileMethods = {
String_read,
String_write,
String_putc,
String_getc,
String_ungetc,
String_seek,
String_tell,
0, /* close */
};
static DohStringMethods StringStringMethods = {
String_replace,
String_chop,
};
DohObjInfo DohStringType = {
"String", /* objname */
DelString, /* doh_del */
CopyString, /* doh_copy */
String_clear, /* doh_clear */
String_str, /* doh_str */
String_data, /* doh_data */
String_dump, /* doh_dump */
String_len, /* doh_len */
String_hash, /* doh_hash */
String_cmp, /* doh_cmp */
String_equal, /* doh_equal */
0, /* doh_first */
0, /* doh_next */
String_setfile, /* doh_setfile */
String_getfile, /* doh_getfile */
String_setline, /* doh_setline */
String_getline, /* doh_getline */
0, /* doh_mapping */
&StringListMethods, /* doh_sequence */
&StringFileMethods, /* doh_file */
&StringStringMethods, /* doh_string */
0, /* doh_position */
0
};
#define INIT_MAXSIZE 16
/* -----------------------------------------------------------------------------
* NewString() - Create a new string
* ----------------------------------------------------------------------------- */
DOHString *DohNewString(const DOHString_or_char *so) {
int l = 0, max;
String *str;
char *s;
int hashkey = -1;
if (DohCheck(so)) {
str = (String *) ObjData(so);
s = (char *) String_data((String *) so);
l = s ? str->len : 0;
hashkey = str->hashkey;
} else {
s = (char *) so;
l = s ? (int) strlen(s) : 0;
}
str = (String *) DohMalloc(sizeof(String));
str->hashkey = hashkey;
str->sp = 0;
str->line = 1;
str->file = 0;
max = INIT_MAXSIZE;
if (s) {
if ((l + 1) > max)
max = l + 1;
}
str->str = (char *) DohMalloc(max);
str->maxsize = max;
if (s) {
strcpy(str->str, s);
str->len = l;
str->sp = l;
} else {
str->str[0] = 0;
str->len = 0;
}
return DohObjMalloc(&DohStringType, str);
}
/* -----------------------------------------------------------------------------
* NewStringEmpty() - Create a new string
* ----------------------------------------------------------------------------- */
DOHString *DohNewStringEmpty(void) {
int max = INIT_MAXSIZE;
String *str = (String *) DohMalloc(sizeof(String));
str->hashkey = 0;
str->sp = 0;
str->line = 1;
str->file = 0;
str->str = (char *) DohMalloc(max);
str->maxsize = max;
str->str[0] = 0;
str->len = 0;
return DohObjMalloc(&DohStringType, str);
}
/* -----------------------------------------------------------------------------
* NewStringWithSize() - Create a new string
* ----------------------------------------------------------------------------- */
DOHString *DohNewStringWithSize(const DOHString_or_char *so, int len) {
int l = 0, max;
String *str;
char *s;
if (DohCheck(so)) {
s = (char *) String_data((String *) so);
} else {
s = (char *) so;
}
str = (String *) DohMalloc(sizeof(String));
str->hashkey = -1;
str->sp = 0;
str->line = 1;
str->file = 0;
max = INIT_MAXSIZE;
if (s) {
l = (int) len;
if ((l + 1) > max)
max = l + 1;
}
str->str = (char *) DohMalloc(max);
str->maxsize = max;
if (s) {
strncpy(str->str, s, len);
str->str[l] = 0;
str->len = l;
str->sp = l;
} else {
str->str[0] = 0;
str->len = 0;
}
return DohObjMalloc(&DohStringType, str);
}
/* -----------------------------------------------------------------------------
* NewStringf()
*
* Create a new string from a list of objects.
* ----------------------------------------------------------------------------- */
DOHString *DohNewStringf(const DOHString_or_char *fmt, ...) {
va_list ap;
DOH *r;
va_start(ap, fmt);
r = NewStringEmpty();
DohvPrintf(r, Char(fmt), ap);
va_end(ap);
return (DOHString *) r;
}
/* -----------------------------------------------------------------------------
* Strcmp()
* Strncmp()
* Strstr()
* Strchr()
*
* Some utility functions.
* ----------------------------------------------------------------------------- */
int DohStrcmp(const DOHString_or_char *s1, const DOHString_or_char *s2) {
const char *c1 = Char(s1);
const char *c2 = Char(s2);
return strcmp(c1, c2);
}
int DohStrncmp(const DOHString_or_char *s1, const DOHString_or_char *s2, int n) {
return strncmp(Char(s1), Char(s2), n);
}
char *DohStrstr(const DOHString_or_char *s1, const DOHString_or_char *s2) {
char *p1 = Char(s1);
char *p2 = Char(s2);
return p1 == 0 || p2 == 0 || *p2 == '\0' ? p1 : strstr(p1, p2);
}
char *DohStrchr(const DOHString_or_char *s1, int ch) {
return strchr(Char(s1), ch);
}
|