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 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
|
/*
* gensi.cpp
* by pts@fazekas.hu at Tue Feb 26 13:28:12 CET 2002
*/
#ifdef __GNUC__
#pragma implementation
#endif
#include "gensi.hpp"
#include <string.h> /* strlen() */
// #include <stdarg.h> /* va_list */
void GenBuffer::iter_char_sub(char const*beg, slen_t len, void *data) {
while (len--!=0) (1[(block_char_t**)data][0])(*beg++, 0[(void**)data]);
}
struct copydata_t {
char *to;
slen_t cfrom;
slen_t clen;
slen_t sumlen;
};
#define CD static_cast<copydata_t*>(data)
static void iter_copy_sub(char const*beg, slen_t len, void *data) {
slen_t i;
CD->sumlen+=len;
if (CD->clen==0) return;
i=CD->cfrom;
if (i>=len) { CD->cfrom-=len; return; }
if (i>0) { CD->cfrom=0; beg+=i; len-=i; }
if (len>=CD->clen) { memcpy(CD->to, beg, CD->clen); CD->clen=0; return; }
memcpy(CD->to, beg, len);
CD->to+=len; CD->clen-=len;
}
// #include <stdio.h>
slen_t GenBuffer::copyRange(char *to, slen_t cfrom, slen_t clen) const {
if (clen==0) return getLength();
copydata_t cd= { to, cfrom, clen, 0 };
each_sub(iter_copy_sub, &cd);
#if 1
while (cd.clen--!=0) *cd.to++='\0'; /* padding */
#else
fprintf(stderr,"cd.clen=%d\n", cd.clen);
while (cd.clen--!=0) {
fprintf(stderr,"padded.\n");
*cd.to++='\0'; /* padding */
}
#endif
return cd.sumlen;
}
#if HAVE_LONG_LONG && NEED_LONG_LONG
# define LONGEXT PTS_CFG_LONGEST
#else
# define LONGEXT long
#endif
/* Imp: ensure reentrace. Maybe vi_write wants to output a number which would
* overwrite ours...
*/
static char numtmp[sizeof(LONGEXT)*3+2];
bool GenBuffer::toBool(bool &dst) {
/* on 1 true yes ja igen igaz be oui vrai: 1tyjibov */
/* off 0 false no nein nem hamis ki non -- : 0fnhk */
slen_t len=copyRange(numtmp, 0, 3);
numtmp[0]|=32; numtmp[1]|=32; numtmp[2]|=32; /* poor man's uppercase */
dst=true;
if (len==0) return true;
if ((numtmp[0]=='o' && numtmp[1]=='f' && numtmp[2]=='f')
|| numtmp[0]=='0' || numtmp[0]=='f' || numtmp[0]=='n' || numtmp[0]=='h'
|| numtmp[0]=='k') dst=false;
else if ((numtmp[0]<'0' || numtmp[0]>'9') && (numtmp[0]<'a' || numtmp[0]>'z')) return true;
return false;
}
// #include <stdio.h>
bool GenBuffer::toInteger(unsigned long &dst) {
/* Imp: several bases (2, 8, 10 and 16), ignore _too_long_ */
/* Imp: check for overflow! */
slen_t len=copyRange(numtmp, 0, sizeof(numtmp));
// fprintf(stderr,"len=%d\n", len);
if (len>=sizeof(numtmp)) return true; /* too long */
/* ASSERT(numtmp null-terminated) */
char *p=numtmp;
if (*p=='+') p++;
unsigned long i=0;
while (1) {
// fprintf(stderr,"toInteger'%c'\n", *p);
if (*p<'0' || *p>'9') break;
i=10*i+(*p-'0');
p++;
}
dst=i;
return *p!='\0'; /* a non-digit arrived */
}
bool GenBuffer::toInteger(signed long &dst) {
/* Imp: several bases (2, 8, 10 and 16), ignore _too_long_ */
slen_t len=copyRange(numtmp, 0, sizeof(numtmp));
if (len>=sizeof(numtmp)) return true; /* too long */
/* ASSERT(numtmp null-terminated) */
char *p=numtmp;
bool neg=false;
if (*p=='+') p++;
else if (*p=='-') { neg=true; p++; }
unsigned long i=0;
while (1) {
if (*p<'0' || *p>'9') break;
i=10*i+(*p-'0');
p++;
}
dst=neg?-(long)i:i;
return *p!='\0'; /* a non-digit arrived */
}
#if HAVE_LONG_LONG && NEED_LONG_LONG
bool GenBuffer::toInteger(unsigned PTS_CFG_LONGEST &dst) {
/* Imp: several bases (2, 8, 10 and 16), ignore _too_long_ */
slen_t len=copyRange(numtmp, 0, sizeof(numtmp));
if (len>=sizeof(numtmp)) return true; /* too long */
/* ASSERT(numtmp null-terminated) */
char *p=numtmp;
if (*p=='+') p++;
unsigned PTS_CFG_LONGEST i=0;
while (1) {
if (*p<'0' || *p>'9') break;
i=10*i+(*p-'0');
p++;
}
dst=i;
return *p!='\0'; /* a non-digit arrived */
}
bool GenBuffer::toInteger(signed PTS_CFG_LONGEST &dst) {
/* Imp: several bases (2, 8, 10 and 16), ignore _too_long_ */
slen_t len=copyRange(numtmp, 0, sizeof(numtmp));
if (len>=sizeof(numtmp)) return true; /* too long */
/* ASSERT(numtmp null-terminated) */
char *p=numtmp;
bool neg=false;
if (*p=='+') p++;
else if (*p=='-') { neg=true; p++; }
unsigned PTS_CFG_LONGEST i=0;
while (1) {
if (*p<'0' || *p>'9') break;
i=10*i+(*p-'0');
p++;
}
dst=neg?-i:i;
return *p!='\0'; /* a non-digit arrived */
}
#endif
bool GenBuffer::toCString(char *&dst) {
slen_t len=getLength();
dst=new char[len+1];
copyRange(dst, 0, len+1); /* copies the terminating '\0' automatically. */
return false;
}
int GenBuffer::cmp(GenBuffer const& s2) const {
Sub u1, u2;
slen_t m;
int i;
first_sub(u1);
s2.first_sub(u2);
while (1) {
if (u1.len==0 && u2.len==0) return 0; /* (*this) == s2 */
else if (u1.len==0) return -1; /* (*this) < s2 */
else if (u2.len==0) return 1; /* (*this) > s2 */
m=(u1.len<u2.len)?u1.len:u2.len;
if (0!=(i=memcmp(u1.beg,u2.beg,m))) return i;
if (0==(u1.len-=m)) next_sub(u1); else u1.beg+=m;
if (0==(u2.len-=m)) s2.next_sub(u2); else u2.beg+=m;
}
}
int GenBuffer::cmp(char const* u2beg, slen_t u2len) const {
Sub u1;
slen_t m;
int i;
first_sub(u1);
while (1) {
if (u1.len==0 && u2len==0) return 0; /* (*this) == s2 */
else if (u1.len==0) return -1; /* (*this) < s2 */
else if (u2len ==0) return 1; /* (*this) > s2 */
m=(u1.len<u2len)?u1.len:u2len;
if (0!=(i=memcmp(u1.beg,u2beg,m))) return i;
if (0==(u1.len-=m)) next_sub(u1); else u1.beg+=m;
u2len-=m; u2beg+=m;
}
}
int GenBuffer::cmp(char const* u2beg) const {
return cmp(u2beg, strlen(u2beg));
}
/* --- */
bool GenBuffer::Readable::equal_content(GenBuffer::Readable &other) {
int i1, i2;
while (((i1=vi_getcc())&255) == ((i2=other.vi_getcc())&255) && i1!=-1) ;
return i1==i2;
}
slen_t GenBuffer::Readable::vi_read(char *to_buf, slen_t max) {
register char *p=to_buf;
char *end=to_buf+max;
int c;
while (p!=end && 0<=(c=vi_getcc())) *p++=c;
return p-to_buf;
}
int GenBuffer::Readable::readFill(char *to_buf, slen_t max) {
slen_t got, sum=0;
while (max>0 && 0<(got=vi_read(to_buf, max))) { to_buf+=got; sum+=got; max-=got; }
return sum;
}
/* --- */
void GenBuffer::Writable::iter_write_sub(char const*beg, slen_t len, void *data) {
if (len!=0) static_cast<GenBuffer::Writable*>(data)->vi_write(beg, len);
}
GenBuffer::Writable& GenBuffer::Writable::operator <<(char const*cstr) {
assert(cstr!=0);
vi_write(cstr, strlen(cstr));
return*this;
}
GenBuffer::Writable& GenBuffer::Writable::operator <<(void const*ptr) {
if (ptr==0) vi_write("(null)", 6);
else {
vi_write("(0d", 3);
/* Imp: hexadecimal pointer output */
*this << (PTS_INTP_T)ptr;
vi_putcc(')');
}
return*this;
}
void GenBuffer::Writable::write_num(unsigned long n, unsigned zdigits) {
if (zdigits>=sizeof(numtmp)) {
memset(numtmp,'0',sizeof(numtmp));
while (zdigits>2*sizeof(numtmp)) {
vi_write(numtmp, sizeof(numtmp));
zdigits-=sizeof(numtmp);
}
vi_write(numtmp, zdigits-sizeof(numtmp));
zdigits=sizeof(numtmp);
}
char *j=numtmp+sizeof(numtmp), *jend=j-zdigits;
while (j!=jend) { *--j='0'+n%10; n/=10; }
vi_write(j, zdigits);
}
#if HAVE_LONG_LONG && NEED_LONG_LONG
void GenBuffer::Writable::write_num(unsigned PTS_CFG_LONGEST n, unsigned zdigits) {
if (zdigits>=sizeof(numtmp)) {
memset(numtmp,'0',sizeof(numtmp));
while (zdigits>2*sizeof(numtmp)) {
vi_write(numtmp, sizeof(numtmp));
zdigits-=sizeof(numtmp);
}
vi_write(numtmp, zdigits-sizeof(numtmp));
zdigits=sizeof(numtmp);
}
char *j=numtmp+sizeof(numtmp), *jend=j-zdigits;
while (j!=jend) { *--j='0'+n%10; n/=10; }
vi_write(j, zdigits);
}
#endif
void GenBuffer::Writable::write_num(unsigned long n) {
char *j=numtmp+sizeof(numtmp);
do *--j='0'+n%10; while ((n/=10)!=0);
vi_write(j, numtmp+sizeof(numtmp)-j);
}
void GenBuffer::Writable::write_num(signed long nn) {
register unsigned long n;
char *j=numtmp+sizeof(numtmp);
if (nn<0) {
n=-nn; do *--j='0'+n%10; while ((n/=10)!=0);
*--j='-';
} else {
n=nn; do *--j='0'+n%10; while ((n/=10)!=0);
}
vi_write(j, numtmp+sizeof(numtmp)-j);
}
#if HAVE_LONG_LONG && NEED_LONG_LONG
void GenBuffer::Writable::write_num(unsigned PTS_CFG_LONGEST n) {
char *j=numtmp+sizeof(numtmp);
do *--j='0'+n%10; while ((n/=10)!=0);
vi_write(j, numtmp+sizeof(numtmp)-j);
}
void GenBuffer::Writable::write_num(signed PTS_CFG_LONGEST nn) {
register unsigned PTS_CFG_LONGEST n;
char *j=numtmp+sizeof(numtmp);
if (nn<0) {
n=-nn; do *--j='0'+n%10; while ((n/=10)!=0);
*--j='-';
} else {
n=nn;
do *--j='0'+n%10; while ((n/=10)!=0);
}
vi_write(j, numtmp+sizeof(numtmp)-j);
}
#endif
GenBuffer::Writable& GenBuffer::Writable::vformat(slen_t n, char const *fmt, va_list ap) {
SimBuffer::B buf;
buf.vformat(n, fmt, ap);
vi_write(buf(), buf.getLength());
return*this;
}
GenBuffer::Writable& GenBuffer::Writable::vformat(char const *fmt, va_list ap) {
SimBuffer::B buf;
buf.vformat(fmt, ap);
vi_write(buf(), buf.getLength());
return*this;
}
GenBuffer::Writable& GenBuffer::Writable::format(slen_t n, char const *fmt, ...) {
va_list ap;
PTS_va_start(ap, fmt);
vformat(n, fmt, ap);
va_end(ap);
return *this;
}
GenBuffer::Writable& GenBuffer::Writable::format(char const *fmt, ...) {
va_list ap;
PTS_va_start(ap, fmt);
vformat(fmt, ap);
va_end(ap);
return *this;
}
/* --- */
slen_t SimBuffer::Flat::copyRange(char *to, slen_t cfrom, slen_t clen) const {
if (cfrom<len) { /* added padding BUGFIX at Fri Mar 7 20:30:07 CET 2003 */
slen_t dlen;
memcpy(to, beg+cfrom, dlen=cfrom+clen>len ? len-cfrom : clen);
to+=dlen;
}
while (clen--!=0) *to++='\0'; /* padding */
return len;
}
slen_t SimBuffer::Flat::findLast(char const c) const {
char const*p;
for (p=beg+len;p>=beg;p--) if (*p==c) return p-beg;
return len;
}
slen_t SimBuffer::Flat::findFirst(char const c) const {
char const*p, *end=beg+len;
for (p=beg;p!=end;p++) if (*p==c) return p-beg;
return len;
}
slen_t SimBuffer::Flat::findFirst(char const* s, slen_t slen) const {
if (slen==0) return 0; /* found */
if (slen>len) return len; /* not found */
char const c=*s;
char const*p=beg, *end=beg+len-slen+1;
if (slen==1) {
for (;p!=end;p++) if (*p==c) return p-beg;
} else {
for (;p!=end;p++) if (*p==c && 0==memcmp(p,s,slen)) return p-beg;
}
return len;
}
int SimBuffer::Flat::cmp(SimBuffer::Flat const& s2) const {
return memcmp(beg, s2.beg, len<s2.len?len:s2.len) || (len>s2.len)-(len<s2.len);
}
int SimBuffer::Flat::cmp(char const* s2beg, slen_t s2len) const {
return memcmp(beg, s2beg, len<s2len?len:s2len) || (len>s2len)-(len<s2len);
}
/* --- */
SimBuffer::Static::Static(char const*cstr) { beg=cstr; len=strlen(cstr); }
/* --- */
void SimBuffer::Appendable::vi_write(char const*str, slen_t slen) {
if (slen>0) {
char *p=vi_mkend(slen);
memcpy(p, str, slen);
}
}
void SimBuffer::Appendable::prepend(char const*str, slen_t slen) {
if (slen>0) {
char *p=vi_mkbeg(slen);
memcpy(p, str, slen);
}
}
/* inlined.
void SimBuffer::Appendable::vi_putcc(char c) {
vi_mkend(1)[0]=c;
}*/
/* --- */
SimBuffer::Linked::~Linked() {
Node *n;
while (first!=0) {
if (first->beg!=(char*)(first+1)) delete [] first->beg;
n=first->next; delete [] first; first=n;
}
}
slen_t SimBuffer::Linked::getLength() const {
slen_t len=0;
Node *n=first;
while (n!=0) { len+=n->len; n=n->next; }
return len;
}
SimBuffer::Linked::Linked(GenBuffer const& other) {
slen_t len=other.getLength();
Node *n=static_cast<Node*>(static_cast<void*>(new char[sizeof(Node)+len]));
n->beg=(char*)(n+1);
other.copyRange(n->beg, 0, len);
n->len=len;
n->next=0;
first=last=n;
}
SimBuffer::Linked::Linked(char const*str) {
slen_t len=strlen(str);
Node *n=static_cast<Node*>(static_cast<void*>(new char[sizeof(Node)+len]));
n->beg=(char*)(n+1);
memcpy(n->beg, str, len);
n->len=len;
n->next=0;
first=last=n;
}
SimBuffer::Linked& SimBuffer::Linked::operator=(GenBuffer const& other) {
slen_t len=other.getLength();
Node *n=static_cast<Node*>(static_cast<void*>(new char[sizeof(Node)+len]));
n->beg=(char*)(n+1);
other.copyRange(n->beg, 0, len);
n->len=len;
n->next=0;
first=last=n;
return *this;
}
SimBuffer::Linked& SimBuffer::Linked::operator=(SimBuffer::Linked const& other) {
/* Imp: avoid code repeat */
slen_t len=other.getLength();
Node *n=static_cast<Node*>(static_cast<void*>(new char[sizeof(Node)+len]));
n->beg=(char*)(n+1);
other.copyRange(n->beg, 0, len);
n->len=len;
n->next=0;
first=last=n;
return *this;
}
void SimBuffer::Linked::each_sub(GenBuffer::block_sub_t block, void *data) const {
Node *n=first;
while (n!=0) { block(n->beg, n->len, data); n=n->next; }
}
void SimBuffer::Linked::first_sub(Sub &sub) const {
if (first!=0) {
sub.data=first->next;
sub.beg=first->beg;
sub.len=first->len;
} else sub.len=0;
}
void SimBuffer::Linked::next_sub(Sub &sub) const {
if (sub.data!=0) {
sub.beg=static_cast<Node*>(sub.data)->beg;
sub.len=static_cast<Node*>(sub.data)->len;
sub.data=static_cast<Node*>(sub.data)->next;
} else sub.len=0;
}
char *SimBuffer::Linked::vi_mkend(slen_t len) {
Node *n=static_cast<Node*>(static_cast<void*>(new char[sizeof(Node)+len]));
n->beg=(char*)(n+1);
n->len=len;
n->next=0;
if (last==0) first=last=n;
else { last->next=n; last=n; }
return n->beg;
}
char *SimBuffer::Linked::vi_mkbeg(slen_t len) {
Node *n=static_cast<Node*>(static_cast<void*>(new char[sizeof(Node)+len]));
n->beg=(char*)(n+1);
n->len=len;
n->next=first;
first=n;
if (last==0) last=n;
return n->beg;
}
/* --- */
SimBuffer::Resizable& SimBuffer::Resizable::operator=(GenBuffer const& s2) {
vi_grow2(0, s2.getLength()-getLength(), 0, 0);
assert(getLength()==s2.getLength());
Sub u1, u2;
slen_t m;
first_sub(u1);
s2.first_sub(u2);
assert(!((u1.len==0) ^ (u2.len==0))); /* s1 and s2 end in the same time */
while (u1.len!=0) {
m=(u1.len<u2.len)?u1.len:u2.len;
memcpy(const_cast<char*>(u1.beg), u2.beg, m);
if (0==(u1.len-=m)) next_sub(u1); else u1.beg+=m;
if (0==(u2.len-=m)) s2.next_sub(u2); else u2.beg+=m;
assert(!((u1.len==0) ^ (u2.len==0))); /* s1 and s2 end in the same time */
}
return*this;
}
//void SimBuffer::Resizable::clear() { /* Inlined. */
// vi_grow2(0, -getLength(), 0, 0);
//}
void SimBuffer::Resizable::keepLeft(slen_t howmuch) {
slen_t len=getLength();
vi_grow2(0, len>howmuch?0-len+howmuch:-(slendiff_t)len, 0, 0);
/* ^^^ BUGFIX at Tue Jun 11 19:57:03 CEST 2002 */
}
void SimBuffer::Resizable::keepRight(slen_t howmuch) {
slen_t len=getLength();
vi_grow2(len>howmuch?0-len+howmuch:-(slendiff_t)len, 0, 0, 0);
/* ^^^ BUGFIX at Tue Jun 11 19:57:03 CEST 2002 */
}
void SimBuffer::Resizable::keepSubstr(slen_t from_offset, slen_t slen) {
slen_t len=getLength();
if (from_offset>=len) vi_grow2(0, -(slendiff_t)len, 0, 0);
else if (from_offset+slen>=len) vi_grow2(-(slendiff_t)from_offset, 0, 0, 0);
else vi_grow2(-(slendiff_t)from_offset, len-from_offset-slen, 0, 0);
}
/* --- */
void SimBuffer::B::vi_grow2(slendiff_t left, slendiff_t right, char **lbeg, char **rbeg) {
assert(alloced>=len);
char *origbeg=const_cast<char*>(beg);
if (left<0) { if (len<=(slen_t)-left) len=0; else beg-=left; left=0; } /* ! */
if (right<0) { if (len<=(slen_t)-right)len=0; else len+=right;right=0; }
/* ^^^ BUGFIX at Tue Jun 11 16:07:56 CEST 2002 */
assert(left>=0);
assert(right>=0);
slen_t newlen=left+right+len;
assert(newlen>=len);
char *newbeg;
assert(alloced>=sizeof(small)/1);
if (beg==small) {
assert(alloced==sizeof(small));
if (newlen>sizeof(small)) {
assert(newlen>len);
newbeg=new char[alloced=2*newlen];
memcpy(newbeg+left, beg, len);
beg=newbeg;
}
} else { /* beg!=small */
// assert(len>=alloced/2); /* -- may not always be true, especially not after appending a `long' */
if (newlen<alloced/2) { /* shrink */
// assert(newlen<=len); /* filled-expectations are true _after_ vi_grow2, not before */
if (newlen>sizeof(small)) {
newbeg=new char[alloced=newlen];
memcpy(newbeg+left, beg, len);
} else {
memcpy((newbeg=small)+left, beg, len);
delete [] origbeg;
alloced=sizeof(small);
}
beg=newbeg;
} else if (newlen>alloced) { /* grow */
assert(newlen>sizeof(small));
assert(newlen>len);
newbeg=new char[alloced=2*newlen];
memcpy(newbeg+left, beg, len);
delete [] origbeg;
beg=newbeg;
} else if (beg!=origbeg) { /* called with negative `left' @param */
assert(left==0);
memmove(origbeg, beg, len); /* Slow, may move the whole buffer. */
}
}
// fprintf(stderr, "newlen=%u\n", newlen);
len=newlen;
if (lbeg) *lbeg=const_cast<char*>(beg);
if (rbeg) *rbeg=const_cast<char*>(beg+newlen-right);
assert(alloced==sizeof(small) || (alloced>sizeof(small) && len>=alloced/2));
assert(alloced>=len);
}
SimBuffer::B::B(char const* cstr): alloced(sizeof(small)) {
beg=small;
slen_t len_=strlen(cstr);
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
memcpy(const_cast<char*>(beg), cstr, len);
}
SimBuffer::B::B(char const* str, slen_t len_): alloced(sizeof(small)) {
beg=small;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
memcpy(const_cast<char*>(beg), str, len);
}
SimBuffer::B::B(SimBuffer::B const& other): GenBuffer(), SimBuffer::Resizable(), SimBuffer::Flat(), alloced(sizeof(small)) {
beg=small;
if (other.len>sizeof(small)) { len=0; vi_grow2(0, other.len, 0, 0); }
else len=other.len;
assert(len==other.len);
memcpy(const_cast<char*>(beg), other.beg, len);
}
SimBuffer::B::B(SimBuffer::Flat const& other): alloced(sizeof(small)) {
beg=small;
if (other.len>sizeof(small)) { len=0; vi_grow2(0, other.len, 0, 0); }
else len=other.len;
assert(len==other.len);
memcpy(const_cast<char*>(beg), other.beg, len);
}
SimBuffer::B::B(SimBuffer::Flat const& other,int): alloced(sizeof(small)) {
beg=small;
if (other.len>=sizeof(small)) { len=0; vi_grow2(0, other.len+1, 0, 0); len--; }
else len=other.len;
assert(len==other.len);
memcpy(const_cast<char*>(beg), other.beg, len);
const_cast<char*>(beg)[len]='\0';
}
SimBuffer::B::B(SimBuffer::Flat const& other, slen_t from_offset, slen_t len_): alloced(sizeof(small)) {
/* substr */
beg=small;
if (from_offset<other.len) {
if (from_offset+len_>other.len) len_=other.len-from_offset;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
memcpy(const_cast<char*>(beg), other.beg+from_offset, len);
} else len=0;
}
SimBuffer::B::B(GenBuffer const& other): alloced(sizeof(small)) {
slen_t len_=other.getLength();
beg=small;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
other.copyRange(const_cast<char*>(beg), 0, len_);
}
/** Constructor: copy (consume) data from a readable stream. */
SimBuffer::B::B(GenBuffer::Readable &other): alloced(sizeof(small)) {
beg=small;
len=0;
#if 000
operator <<(other);
#else
B_append(other);
#endif
}
SimBuffer::B::B(char const* as,slen_t al, char const* bs,slen_t bl): alloced(sizeof(small)) {
slen_t len_=al+bl;
beg=small;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
memcpy(const_cast<char*>(beg), as, al);
memcpy(const_cast<char*>(beg)+al, bs, bl);
}
SimBuffer::B::B(char const* as,slen_t al, char const* bs,slen_t bl,int): alloced(sizeof(small)) {
slen_t len_=al+bl;
beg=small;
if (len_>=sizeof(small)) { len=0; vi_grow2(0, len_+1, 0, 0); len--; }
else len=len_;
assert(len==len_);
memcpy(const_cast<char*>(beg), as, al);
memcpy(const_cast<char*>(beg)+al, bs, bl);
const_cast<char*>(beg)[al+bl]='\0';
}
SimBuffer::B::B(GenBuffer const& a, GenBuffer const& b): alloced(sizeof(small)) {
slen_t al=a.getLength();
slen_t bl=b.getLength();
slen_t len_=al+bl;
beg=small;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
a.copyRange(const_cast<char*>(beg), 0, al);
b.copyRange(const_cast<char*>(beg)+al, 0, bl);
}
SimBuffer::B::B(GenBuffer const& a, GenBuffer const& b, GenBuffer const& c): alloced(sizeof(small)) {
slen_t al=a.getLength();
slen_t bl=b.getLength();
slen_t cl=c.getLength();
slen_t len_=al+bl;
beg=small;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
a.copyRange(const_cast<char*>(beg), 0, al);
b.copyRange(const_cast<char*>(beg)+al, 0, bl);
c.copyRange(const_cast<char*>(beg)+al+bl, 0, cl);
}
SimBuffer::B::B(char const* as,slen_t al, char const* bs,slen_t bl, char const* cs,slen_t cl): alloced(sizeof(small)) {
slen_t len_=al+bl+cl;
beg=small;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
memcpy(const_cast<char*>(beg), as, al);
memcpy(const_cast<char*>(beg)+al, bs, bl);
memcpy(const_cast<char*>(beg)+al+bl, cs, cl);
}
SimBuffer::B::B(Flat const&b, char const*cs): alloced(sizeof(small)) {
slen_t cl=strlen(cs);
slen_t len_=b.len+cl;
beg=small;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
memcpy(const_cast<char*>(beg), b.beg, b.len);
memcpy(const_cast<char*>(beg)+b.len, cs, cl);
}
SimBuffer::B::B(char const*as, Flat const&b, char const*cs): alloced(sizeof(small)) {
slen_t al=strlen(as);
slen_t cl=strlen(cs);
slen_t len_=al+b.len+cl;
beg=small;
if (len_>sizeof(small)) { len=0; vi_grow2(0, len_, 0, 0); }
else len=len_;
assert(len==len_);
memcpy(const_cast<char*>(beg), as, al);
memcpy(const_cast<char*>(beg)+al, b.beg, b.len);
memcpy(const_cast<char*>(beg)+al+b.len, cs, cl);
}
/* SimBuffer::B& operator<<(SimBuffer::B& self, GenBuffer::Readable &stream); */
SimBuffer::B& SimBuffer::B::B_append(GenBuffer::Readable &stream) {
assert(alloced>=len);
slen_t oldlen, ask;
ask=stream.vi_availh();
if (ask>0) { oldlen=len; vi_grow2(0, ask+1, 0, 0); len=oldlen; }
if (alloced!=len) len+=stream.readFill(const_cast<char*>(beg+len), alloced-len);
while (alloced==len) { /* more data to be read */
oldlen=len; vi_grow2(0, alloced, 0, 0); len=oldlen;
stream.readFill(const_cast<char*>(beg+len), alloced-len);
}
return *this;
}
SimBuffer::B& SimBuffer::B::term0() {
if (len==alloced) { vi_grow2(0,1,0,0); len--; }
const_cast<char*>(beg)[len]='\0';
return *this;
}
void SimBuffer::B::grow_set0_by(slendiff_t lendiff) {
char *rbeg;
vi_grow2(0, lendiff, 0, &rbeg);
if (lendiff>0) memset(rbeg, '\0', lendiff);
}
char SimBuffer::B::getAt(slen_t idx) {
if (idx<len) return beg[idx];
/* grow_set0_by(idx+1-len); */
char *rbeg;
vi_grow2(0, idx-len+1, 0, &rbeg);
memset(rbeg, '\0', idx-len+1);
return '\0';
}
SimBuffer::B& SimBuffer::B::operator=(SimBuffer::Flat const& other) {
if (&other!=this) {
len=0;
vi_grow2(0, other.len, 0, 0);
memcpy(const_cast<char*>(beg), other.beg, len);
}
return*this;
}
SimBuffer::B& SimBuffer::B::operator=(SimBuffer::B const& other) {
if (&other!=this) {
len=0;
vi_grow2(0, other.len, 0, 0);
memcpy(const_cast<char*>(beg), other.beg, len);
}
return*this;
}
SimBuffer::B& SimBuffer::B::operator=(char const* cstr) {
slen_t slen=strlen(cstr);
len=0;
vi_grow2(0, slen, 0, 0);
memcpy(const_cast<char*>(beg), cstr, slen);
return*this;
}
SimBuffer::B SimBuffer::B::substr(slen_t first, slen_t howmuch) const {
return SimBuffer::B(*this, first, howmuch);
}
SimBuffer::B SimBuffer::B::substr(slen_t first) const {
/* Actually, this will probably be shorter than end-begin */
return SimBuffer::B(*this, first, len);
}
SimBuffer::B SimBuffer::B::right(slen_t howmuch) const {
return SimBuffer::B(*this, len<=howmuch?0:len-howmuch, howmuch);
}
SimBuffer::B SimBuffer::B::left(slen_t howmuch) const {
return SimBuffer::B(*this, 0, howmuch);
}
#if 0
void SimBuffer::B::append(char const*s, const slen_t len_) {
char *rbeg;
vi_grow2(0, len_, 0, &rbeg);
memcpy(rbeg, s, len_);
}
#endif
SimBuffer::B operator+(const SimBuffer::Flat& s1, const SimBuffer::Flat& s2) {
return SimBuffer::B(s1.beg, s1.len, s2.beg, s2.len);
}
SimBuffer::B operator+(const char *s1, const SimBuffer::Flat& s2) {
return SimBuffer::B(s1, strlen(s1), s2.beg, s2.len);
}
SimBuffer::B operator+(const SimBuffer::Flat& s1, const char *s2) {
return SimBuffer::B(s1.beg, s1.len, s2, strlen(s2));
}
SimBuffer::B& SimBuffer::B::operator<<(SimBuffer::Flat const& other) {
char *d;
vi_grow2(0, other.len, 0, &d);
memcpy(d,other.beg,other.len);
return*this;
}
SimBuffer::B& SimBuffer::B::operator<<(char c) {
if (len==alloced) { vi_grow2(0, 1, 0, 0); const_cast<char*>(beg)[len-1]=c; }
else const_cast<char*>(beg)[len++]=c;
return*this;
}
SimBuffer::B& SimBuffer::B::operator<<(char const *s) {
char *d;
slen_t slen=strlen(s);
vi_grow2(0, slen, 0, &d);
memcpy(d,s,slen);
return*this;
}
void SimBuffer::B::vi_write(char const*str, slen_t slen) {
if (slen>0) {
char *p; vi_grow2(0, slen, 0, &p);
memcpy(p, str, slen);
}
}
#define USGE(a,b) ((unsigned char)(a))>=((unsigned char)(b))
static inline bool is_path(char const c) {
return c=='-' || c=='.' || c=='_' || c=='/'
|| USGE('z'-'a',c-'a')
|| USGE('Z'-'A',c-'A')
|| USGE('9'-'0',c-'0');
}
static inline bool is_safe_c(char const c) {
return c!='\\' && c!='\'' && c!='\"' && USGE('~'-' ',c-' ');
}
static inline bool is_safe_ps(char const c) {
return c!='\\' && c!='(' && c!=')' && USGE('~'-' ',c-' ');
}
SimBuffer::B& SimBuffer::B::appendDump(const char c, bool dq) {
char t[7]; register char *p=t;
if (dq) *p++='\'';
if (is_path(c)) {
*p++=c;
} else {
*p++='\\';
*p++=('0'+((c>>6)&3));
*p++=('0'+((c>>3)&7));
*p++=('0'+(c&7));
}
if (dq) *p++='\'';
vi_write(t, p-t);
return*this;
}
SimBuffer::B& SimBuffer::B::appendDump(const SimBuffer::Flat &other, bool dq) {
slen_t rlen=dq?2:0;
register char c; register char const*p;
char const *pend; char *dst;
for (p=other.beg,pend=p+other.len; p!=pend; p++) rlen+=is_path(*p)?1:4;
vi_grow2(0, rlen, 0, &dst);
if (dq) *dst++='"';
for (p=other.beg,pend=p+other.len; p!=pend; p++) {
if (is_path(c=*p)) {
*dst++=c;
} else {
*dst++='\\';
*dst++=('0'+((c>>6)&3));
*dst++=('0'+((c>>3)&7));
*dst++=('0'+(c&7));
}
}
if (dq) *dst++='"';
assert(dst==end_());
return*this;
}
SimBuffer::B& SimBuffer::B::appendNpmq(const SimBuffer::Flat &other, bool dq) {
slen_t rlen=dq?2:0;
register char c; register char const*p;
char const *pend; char *dst;
for (p=other.beg,pend=p+other.len; p!=pend; p++) rlen+=is_path(*p)?1:2;
vi_grow2(0, rlen, 0, &dst);
if (dq) *dst++='"';
for (p=other.beg,pend=p+other.len; p!=pend; p++) {
if (is_path(c=*p)) {
*dst++=c;
} else {
*dst++='\\';
*dst++=c;
}
}
if (dq) *dst++='"';
assert(dst==end_());
return*this;
}
SimBuffer::B& SimBuffer::B::appendDumpC (const SimBuffer::Flat &other, bool dq) {
slen_t rlen=dq?2:0;
register char c; register char const*p;
char const *pend; char *dst;
for (p=other.beg,pend=p+other.len; p!=pend; p++) rlen+=is_safe_c(*p)?1:4;
vi_grow2(0, rlen, 0, &dst);
if (dq) *dst++='"';
for (p=other.beg,pend=p+other.len; p!=pend; p++) {
if (is_safe_c(c=*p)) {
*dst++=c;
} else {
*dst++='\\';
*dst++=('0'+((c>>6)&3));
*dst++=('0'+((c>>3)&7));
*dst++=('0'+(c&7));
}
}
if (dq) *dst++='"';
assert(dst==end_());
return*this;
}
SimBuffer::B& SimBuffer::B::appendFnq(const SimBuffer::Flat &other, bool preminus) {
slen_t rlen=0;
register char c; register char const*p;
char const *pend; char *dst;
if (OS_COTY==COTY_WINNT || OS_COTY==COTY_WIN9X) {
for (p=other.beg,pend=p+other.len; p!=pend; p++) {
if ('\0'==(c=*p) || c=='"') break;
rlen++;
}
if (preminus && rlen!=0 && other.beg[0]=='-') rlen+=2; /* .\ */
vi_grow2(0, rlen+2, 0, &dst);
*dst++='"'; /* Dat: "ab"c" ""def" is perfectly legal and parses to: `abc def' */
p=other.beg;
if (preminus && other.beg[0]=='-') { *dst++='.'; *dst++='\\'; }
for (p=other.beg,pend=p+other.len; p!=pend; p++) {
if ('\0'==(c=*p) || c=='"') break;
*dst++=c;
}
*dst++='"';
} else { /* Everything else is treated as UNIX */
for (p=other.beg,pend=p+other.len; p!=pend; p++) {
if ('\0'==(c=*p)) break;
rlen+=is_path(c)?1: c=='\n'?3:2;
}
if (preminus && rlen!=0 && other.beg[0]=='-') rlen+=2; /* ./ */
vi_grow2(0, rlen, 0, &dst);
if (preminus && other.beg[0]=='-') { *dst++='.'; *dst++='/'; }
for (p=other.beg,pend=p+other.len; p!=pend; p++) {
if ('\0'==(c=*p)) break;
if (is_path(c)) *dst++=c;
else if (c=='\n') { *dst++='"'; *dst++='\n'; *dst++='"'; }
else { *dst++='\\'; *dst++=c; }
}
} /* IF OS_COTY... */
assert(dst==end_());
return*this;
}
SimBuffer::B& SimBuffer::B::appendDumpPS (const SimBuffer::Flat &other, bool dq) {
slen_t rlen=dq?2:0;
register char c; register char const*p;
char const *pend;
for (p=other.beg,pend=p+other.len; p!=pend; p++) rlen+=is_safe_ps(*p)?1:4;
char *dst; vi_grow2(0, rlen, 0, &dst);
if (dq) *dst++='(';
for (p=other.beg,pend=p+other.len; p!=pend; p++) {
if (is_safe_ps(c=*p)) {
*dst++=c;
} else {
*dst++='\\';
*dst++=('0'+((c>>6)&3));
*dst++=('0'+((c>>3)&7));
*dst++=('0'+(c&7));
}
}
if (dq) *dst++=')';
assert(dst==end_());
return*this;
}
SimBuffer::B& SimBuffer::B::appendHppq(const SimBuffer::Flat &other) {
vi_grow2(0, other.len, 0, 0);
char *pend=const_cast<char*>(beg)+len;
register char c, *p=pend-other.len;
memcpy(p, other.beg, other.len);
for (;p!=pend;pend++) {
c=*p;
*p++=(
(c>='a' && c<='z') ? (char)(c-'a'+'A') :
(c>='A' && c<='Z') ? c :
'_'
);
}
return *this;
}
SimBuffer::B& SimBuffer::B::appendUnslash(const SimBuffer::Flat &other, int iniq) {
slen_t rlen=0;
slen_t left=other.len;
register char c; register char const*p=other.beg;
if (iniq<=256) {
if (left<2 || *p!=iniq || p[left-1]!=iniq) return*this;
/* ^^^ return empty string */
p++; left-=2;
}
while (0!=left) { /* Calculate lengths */
c=*p++;
if (c!='\\' || left==1) { rlen++; left--; continue; }
c=*p++;
if (c>='0' && c<='7') {
rlen++;
if (left>=3 && p[0]>='0' && p[0]<='7') {
if (left>=4 && p[1]>='0' && p[1]<='7') { p+=2; left-=4; }
else { p+=1; left-=3; }
} else left-=2;
} else if ((c=='x' || c=='X') && left>=3 && 16!=(hexc2n(p[0]))) {
rlen++;
if (left>=4 && 16!=(hexc2n(p[1]))) { p+=2; left-=4; }
else { p+=1; left-=2; }
} else if ((c=='c' || c=='C') && left>=4 && (p[0]=='-' || p[0]=='[')) {
rlen++; left-=4; p+=2;
} else if (c=='l' && left>=3) {
rlen++; left-=3; p++;
} else if (c=='u' && left>=3) {
rlen++; left-=3; p++;
} else if (c=='\n') {
left-=2;
} else { /* backslash is used for escaping a single char */
rlen++; left-=2;
}
}
char *dst; vi_grow2(0, rlen, 0, &dst);
unsigned tmp1, tmp2;
left=other.len; p=other.beg;
if (iniq<=256) {
assert(!(left<2 || *p!=iniq || p[left-1]!=iniq));
p++; left-=2;
}
while (0!=left) {
c=*p++;
if (c!='\\' || left==1) { *dst++=(c); left--; continue; }
c=*p++;
if (c>='0' && c<='7') {
if (left>=3 && p[0]>='0' && p[0]<='7') {
if (left>=4 && p[1]>='0' && p[1]<='7') {
*dst++=((char)((c<<6)+(p[0]<<3)+p[1]-'0'*73));
p+=2; left-=4;
} else {
*dst++=((char)((c<<3)+p[0]-'0'*9));
p+=1; left-=3;
}
} else {
*dst++=((char)(c-'0'));
left-=2;
}
} else if ((c=='x' || c=='X') && left>=3 && 16!=(tmp1=hexc2n(p[0]))) {
if (left>=4 && 16!=(tmp2=hexc2n(p[1]))) {
*dst++=((char)((tmp1<<4)+tmp2));
p+=2; left-=4;
} else {
*dst++=((char)tmp1);
p+=1; left-=2;
}
} else if ((c=='c' || c=='C') && left>=4 && (p[0]=='-' || p[0]=='[')) {
*dst++=((char)(p[1]>='a' && p[1]<='z' ? (p[1]+'A'-'a')^64 : p[1]^64));
left-=4; p+=2;
} else if (c=='l' && left>=3) {
*dst++=((char)(p[0]>='A' && p[0]<='Z' ? p[0]+'a'-'A' : p[0]));
left-=3; p++;
} else if (c=='u' && left>=3) {
*dst++=((char)(p[0]>='a' && p[0]<='z' ? p[0]+'A'-'a' : p[0]));
left-=3; p++;
} else if (c=='\n') {
left-=2;
} else { /* backslash is used for escaping a single char */
if (c=='a') c=007; // \x07 (alarm bell)
else if (c=='b') c=010; // \x08 (backspace) (_not_ alarm bell)
else if (c=='e') c=033; // \x1B (escape)
else if (c=='f') c=014; // \x0C (form feed)
else if (c=='n') c=012; // \x0A (newline, line feed)
else if (c=='r') c=015; // \x0D (carriage return)
else if (c=='t') c=011; // \x09 (horizontal tab)
else if (c=='v') c=013; // \x0B (vertical tab)
*dst++=(c); left-=2;
// if (0!=left--) { *dst++=(*p++); left--; } /* already escaped 1 */
}
}
return*this;
}
void SimBuffer::B::space_pad_cpy(char *dst, char const*src, slen_t pad) {
while (pad!=0 && src[0]!='\0') { *dst++=*src++; pad--; }
while (pad--!=0) *dst++=' ';
}
// #include <stdio.h>
char *SimBuffer::B::substr_grow(slen_t first, slen_t oldmuch, slen_t newmuch) {
slen_t idx=first;
if (first<len) {
if (first+oldmuch>len) oldmuch=len-first;
if (newmuch<oldmuch) { first+=newmuch; oldmuch-=newmuch; newmuch=0; }
else if (newmuch>oldmuch) { first+=oldmuch; newmuch-=oldmuch; oldmuch=0; }
else return const_cast<char*>(beg)+first;
} else {
len=first=idx=oldmuch=0;
if (newmuch==0) { vi_grow2(0,0,0,0); return const_cast<char*>(beg); }
}
// fprintf(stderr, "newmuch=%u oldmuch=%u len=%u\n", newmuch, oldmuch, len);
if (newmuch>0) {
vi_grow2(0,newmuch,0,0);
char *p=const_cast<char*>(beg)+first; /* after vi_grow2() */
memmove(p+newmuch, p, len-first-newmuch);
} else if (oldmuch>0) {
char *p=const_cast<char*>(beg)+first; /* before vi_grow2() */
memmove(p, p+oldmuch, len-first-oldmuch);
vi_grow2(0,-(slendiff_t)oldmuch,0,0);
} else assert(0);
// fprintf(stderr, "len=%u oldmuch=%u\n", len, oldmuch);
return const_cast<char*>(beg)+idx;
}
/* --- Tue Jul 2 10:47:14 CEST 2002 */
void GenBuffer::tolower_memcpy(char *dst, char const*s, slen_t slen) {
while (slen--!=0) *dst++=USGE('Z'-'A',*s-'A') ? *s+++'a'-'A' : *s++;
}
void GenBuffer::toupper_memcpy(char *dst, char const*s, slen_t slen) {
while (slen--!=0) *dst++=USGE('z'-'a',*s-'a') ? *s+++'A'-'a' : *s++;
}
int GenBuffer::nocase_memcmp(char const*a, char const *s, slen_t slen) {
int i;
while (slen--!=0) {
i=(USGE('Z'-'A',*a-'A') ? *a+++'a'-'A' : *a++)
-(USGE('Z'-'A',*s-'A') ? *s+++'a'-'A' : *s++);
if (i>0) return 1;
if (i<0) return -1;
}
return 0;
}
int GenBuffer::nocase_strcmp(char const*a, char const*b) {
slen_t alen=strlen(a), blen=strlen(b), min=alen<blen?alen:blen;
int ret=nocase_memcmp(a, b, min);
return ret!=0 || alen==blen ? ret : alen<blen ? -1 : 1;
}
bool GenBuffer::parseBool(char const *s, slen_t slen) {
if (slen==0) return true;
char c=s[0];
if ((slen==2 && (c=='o' || c=='O') && (s[1]=='n' || s[1]=='N'))
|| (slen==3 && (c=='o' || c=='O') && (s[1]=='u' || s[1]=='U'))
) return true;
/* on true yes ja igen oui enable 1 true vrai? right sure allowed
* off false no nein nem non disable 0 false faux? wrong nope disallowed
*/
return c!='f' && c!='n' && c!='d' && c!='w' && c!='0'
&& c!='F' && c!='N' && c!='D' && c!='W';
}
bool GenBuffer::strbegins(char const*a, char const *with) {
while (*with!='\0') if (*a++!=*with++) return false;
return true;
}
bool GenBuffer::nocase_strbegins(char const*a, char const *with) {
while (*with!='\0')
if ((USGE('Z'-'A',*a-'A') ? *a+++'a'-'A' : *a++)
!=(USGE('Z'-'A',*with-'A') ? *with+++'a'-'A' : *with++)
) return false;
return true;
}
/* __END__ */
|