Debian Codegraph @

libs/xpdf/xpdf-src/goo/GString.cc

raw
1 //========================================================================
2 //
3 // GString.cc
4 //
5 // Simple variable-length string type.
6 //
7 // Copyright 1996-2003 Glyph & Cog, LLC
8 //
9 //========================================================================
10
11 #include <aconf.h>
12
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <math.h>
18 #include <limits.h>
19 #include "gmem.h"
20 #include "gmempp.h"
21 #include "GString.h"
22
23 //------------------------------------------------------------------------
24
25 union GStringFormatArg {
26 int i;
27 Guint ui;
28 long l;
29 Gulong ul;
30 #ifdef LLONG_MAX
31 long long ll;
32 #endif
33 #ifdef ULLONG_MAX
34 unsigned long long ull;
35 #endif
36 double f;
37 char c;
38 char *s;
39 GString *gs;
40 };
41
42 enum GStringFormatType {
43 fmtIntDecimal,
44 fmtIntHex,
45 fmtIntOctal,
46 fmtIntBinary,
47 fmtUIntDecimal,
48 fmtUIntHex,
49 fmtUIntOctal,
50 fmtUIntBinary,
51 fmtLongDecimal,
52 fmtLongHex,
53 fmtLongOctal,
54 fmtLongBinary,
55 fmtULongDecimal,
56 fmtULongHex,
57 fmtULongOctal,
58 fmtULongBinary,
59 #ifdef LLONG_MAX
60 fmtLongLongDecimal,
61 fmtLongLongHex,
62 fmtLongLongOctal,
63 fmtLongLongBinary,
64 #endif
65 #ifdef ULLONG_MAX
66 fmtULongLongDecimal,
67 fmtULongLongHex,
68 fmtULongLongOctal,
69 fmtULongLongBinary,
70 #endif
71 fmtDouble,
72 fmtDoubleTrim,
73 fmtChar,
74 fmtString,
75 fmtGString,
76 fmtSpace
77 };
78
79 static const char *formatStrings[] = {
80 "d", "x", "o", "b", "ud", "ux", "uo", "ub",
81 "ld", "lx", "lo", "lb", "uld", "ulx", "ulo", "ulb",
82 #ifdef LLONG_MAX
83 "lld", "llx", "llo", "llb",
84 #endif
85 #ifdef ULLONG_MAX
86 "ulld", "ullx", "ullo", "ullb",
87 #endif
88 "f", "g",
89 "c",
90 "s",
91 "t",
92 "w",
93 NULL
94 };
95
96 //------------------------------------------------------------------------
97
98 static inline int size(int len) {
99 int delta;
100 for (delta = 8; delta < len && delta < 0x100000; delta <<= 1) ;
101 if (len > INT_MAX - delta) {
102 gMemError("Integer overflow in GString::size()");
103 }
104 // this is ((len + 1) + (delta - 1)) & ~(delta - 1)
105 return (len + delta) & ~(delta - 1);
106 }
107
108 inline void GString::resize(int length1) {
109 char *s1;
110
111 if (length1 < 0) {
112 gMemError("GString::resize() with negative length");
113 }
114 if (!s) {
115 s = new char[size(length1)];
116 } else if (size(length1) != size(length)) {
117 s1 = new char[size(length1)];
118 if (length1 < length) {
119 memcpy(s1, s, length1);
120 s1[length1] = '\0';
121 } else {
122 memcpy(s1, s, length + 1);
123 }
124 delete[] s;
125 s = s1;
126 }
127 }
128
129 GString::GString() {
130 s = NULL;
131 resize(length = 0);
132 s[0] = '\0';
133 }
134
135 GString::GString(const char *sA) {
136 int n = (int)strlen(sA);
137
138 s = NULL;
139 resize(length = n);
140 memcpy(s, sA, n + 1);
141 }
142
143 GString::GString(const char *sA, int lengthA) {
144 s = NULL;
145 resize(length = lengthA);
146 memcpy(s, sA, length * sizeof(char));
147 s[length] = '\0';
148 }
149
150 GString::GString(GString *str, int idx, int lengthA) {
151 s = NULL;
152 resize(length = lengthA);
153 memcpy(s, str->getCString() + idx, length);
154 s[length] = '\0';
155 }
156
157 GString::GString(GString *str) {
158 s = NULL;
159 resize(length = str->getLength());
160 memcpy(s, str->getCString(), length + 1);
161 }
162
163 GString::GString(GString *str1, GString *str2) {
164 int n1 = str1->getLength();
165 int n2 = str2->getLength();
166
167 s = NULL;
168 if (n1 > INT_MAX - n2) {
169 gMemError("Integer overflow in GString::GString()");
170 }
171 resize(length = n1 + n2);
172 memcpy(s, str1->getCString(), n1);
173 memcpy(s + n1, str2->getCString(), n2 + 1);
174 }
175
176 GString *GString::fromInt(int x) {
177 char buf[24]; // enough space for 64-bit ints plus a little extra
178 const char *p;
179 int len;
180
181 formatInt(x, buf, sizeof(buf), gFalse, 0, 10, &p, &len);
182 return new GString(p, len);
183 }
184
185 GString *GString::format(const char *fmt, ...) {
186 va_list argList;
187 GString *s;
188
189 s = new GString();
190 va_start(argList, fmt);
191 s->appendfv(fmt, argList);
192 va_end(argList);
193 return s;
194 }
195
196 GString *GString::formatv(const char *fmt, va_list argList) {
197 GString *s;
198
199 s = new GString();
200 s->appendfv(fmt, argList);
201 return s;
202 }
203
204 GString::~GString() {
205 delete[] s;
206 }
207
208 GString *GString::clear() {
209 s[length = 0] = '\0';
210 resize(0);
211 return this;
212 }
213
214 GString *GString::append(char c) {
215 if (length > INT_MAX - 1) {
216 gMemError("Integer overflow in GString::append()");
217 }
218 resize(length + 1);
219 s[length++] = c;
220 s[length] = '\0';
221 return this;
222 }
223
224 GString *GString::append(GString *str) {
225 int n = str->getLength();
226
227 if (length > INT_MAX - n) {
228 gMemError("Integer overflow in GString::append()");
229 }
230 resize(length + n);
231 memcpy(s + length, str->getCString(), n + 1);
232 length += n;
233 return this;
234 }
235
236 GString *GString::append(const char *str) {
237 int n = (int)strlen(str);
238
239 if (length > INT_MAX - n) {
240 gMemError("Integer overflow in GString::append()");
241 }
242 resize(length + n);
243 memcpy(s + length, str, n + 1);
244 length += n;
245 return this;
246 }
247
248 GString *GString::append(const char *str, int lengthA) {
249 if (lengthA < 0 || length > INT_MAX - lengthA) {
250 gMemError("Integer overflow in GString::append()");
251 }
252 resize(length + lengthA);
253 memcpy(s + length, str, lengthA);
254 length += lengthA;
255 s[length] = '\0';
256 return this;
257 }
258
259 GString *GString::appendf(const char *fmt, ...) {
260 va_list argList;
261
262 va_start(argList, fmt);
263 appendfv(fmt, argList);
264 va_end(argList);
265 return this;
266 }
267
268 GString *GString::appendfv(const char *fmt, va_list argList) {
269 GStringFormatArg *args;
270 int argsLen, argsSize;
271 GStringFormatArg arg;
272 int idx, width, prec;
273 GBool reverseAlign, zeroFill;
274 GStringFormatType ft;
275 char buf[65];
276 int len, i;
277 const char *p0, *p1;
278 const char *str;
279
280 argsLen = 0;
281 argsSize = 8;
282 args = (GStringFormatArg *)gmallocn(argsSize, sizeof(GStringFormatArg));
283
284 p0 = fmt;
285 while (*p0) {
286 if (*p0 == '{') {
287 ++p0;
288 if (*p0 == '{') {
289 ++p0;
290 append('{');
291 } else {
292
293 // parse the format string
294 if (!(*p0 >= '0' && *p0 <= '9')) {
295 break;
296 }
297 idx = *p0 - '0';
298 for (++p0; *p0 >= '0' && *p0 <= '9'; ++p0) {
299 idx = 10 * idx + (*p0 - '0');
300 }
301 if (*p0 != ':') {
302 break;
303 }
304 ++p0;
305 if (*p0 == '-') {
306 reverseAlign = gTrue;
307 ++p0;
308 } else {
309 reverseAlign = gFalse;
310 }
311 width = 0;
312 zeroFill = *p0 == '0';
313 for (; *p0 >= '0' && *p0 <= '9'; ++p0) {
314 width = 10 * width + (*p0 - '0');
315 }
316 if (width < 0) {
317 width = 0;
318 }
319 if (*p0 == '.') {
320 ++p0;
321 prec = 0;
322 for (; *p0 >= '0' && *p0 <= '9'; ++p0) {
323 prec = 10 * prec + (*p0 - '0');
324 }
325 } else {
326 prec = 0;
327 }
328 for (ft = (GStringFormatType)0;
329 formatStrings[ft];
330 ft = (GStringFormatType)(ft + 1)) {
331 if (!strncmp(p0, formatStrings[ft], strlen(formatStrings[ft]))) {
332 break;
333 }
334 }
335 if (!formatStrings[ft]) {
336 break;
337 }
338 p0 += strlen(formatStrings[ft]);
339 if (*p0 != '}') {
340 break;
341 }
342 ++p0;
343
344 // fetch the argument
345 if (idx > argsLen) {
346 break;
347 }
348 if (idx == argsLen) {
349 if (argsLen == argsSize) {
350 argsSize *= 2;
351 args = (GStringFormatArg *)greallocn(args, argsSize,
352 sizeof(GStringFormatArg));
353 }
354 switch (ft) {
355 case fmtIntDecimal:
356 case fmtIntHex:
357 case fmtIntOctal:
358 case fmtIntBinary:
359 case fmtSpace:
360 args[argsLen].i = va_arg(argList, int);
361 break;
362 case fmtUIntDecimal:
363 case fmtUIntHex:
364 case fmtUIntOctal:
365 case fmtUIntBinary:
366 args[argsLen].ui = va_arg(argList, Guint);
367 break;
368 case fmtLongDecimal:
369 case fmtLongHex:
370 case fmtLongOctal:
371 case fmtLongBinary:
372 args[argsLen].l = va_arg(argList, long);
373 break;
374 case fmtULongDecimal:
375 case fmtULongHex:
376 case fmtULongOctal:
377 case fmtULongBinary:
378 args[argsLen].ul = va_arg(argList, Gulong);
379 break;
380 #ifdef LLONG_MAX
381 case fmtLongLongDecimal:
382 case fmtLongLongHex:
383 case fmtLongLongOctal:
384 case fmtLongLongBinary:
385 args[argsLen].ll = va_arg(argList, long long);
386 break;
387 #endif
388 #ifdef ULLONG_MAX
389 case fmtULongLongDecimal:
390 case fmtULongLongHex:
391 case fmtULongLongOctal:
392 case fmtULongLongBinary:
393 args[argsLen].ull = va_arg(argList, unsigned long long);
394 break;
395 #endif
396 case fmtDouble:
397 case fmtDoubleTrim:
398 args[argsLen].f = va_arg(argList, double);
399 break;
400 case fmtChar:
401 args[argsLen].c = (char)va_arg(argList, int);
402 break;
403 case fmtString:
404 args[argsLen].s = va_arg(argList, char *);
405 break;
406 case fmtGString:
407 args[argsLen].gs = va_arg(argList, GString *);
408 break;
409 }
410 ++argsLen;
411 }
412
413 // format the argument
414 arg = args[idx];
415 str = NULL;
416 len = 0;
417 switch (ft) {
418 case fmtIntDecimal:
419 formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
420 break;
421 case fmtIntHex:
422 formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
423 break;
424 case fmtIntOctal:
425 formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
426 break;
427 case fmtIntBinary:
428 formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
429 break;
430 case fmtUIntDecimal:
431 formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 10,
432 &str, &len);
433 break;
434 case fmtUIntHex:
435 formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 16,
436 &str, &len);
437 break;
438 case fmtUIntOctal:
439 formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
440 break;
441 case fmtUIntBinary:
442 formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
443 break;
444 case fmtLongDecimal:
445 formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
446 break;
447 case fmtLongHex:
448 formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
449 break;
450 case fmtLongOctal:
451 formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
452 break;
453 case fmtLongBinary:
454 formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
455 break;
456 case fmtULongDecimal:
457 formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 10,
458 &str, &len);
459 break;
460 case fmtULongHex:
461 formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 16,
462 &str, &len);
463 break;
464 case fmtULongOctal:
465 formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
466 break;
467 case fmtULongBinary:
468 formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
469 break;
470 #ifdef LLONG_MAX
471 case fmtLongLongDecimal:
472 formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
473 break;
474 case fmtLongLongHex:
475 formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
476 break;
477 case fmtLongLongOctal:
478 formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
479 break;
480 case fmtLongLongBinary:
481 formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
482 break;
483 #endif
484 #ifdef ULLONG_MAX
485 case fmtULongLongDecimal:
486 formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 10,
487 &str, &len);
488 break;
489 case fmtULongLongHex:
490 formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 16,
491 &str, &len);
492 break;
493 case fmtULongLongOctal:
494 formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 8,
495 &str, &len);
496 break;
497 case fmtULongLongBinary:
498 formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 2,
499 &str, &len);
500 break;
501 #endif
502 case fmtDouble:
503 formatDouble(arg.f, buf, sizeof(buf), prec, gFalse, &str, &len);
504 break;
505 case fmtDoubleTrim:
506 formatDouble(arg.f, buf, sizeof(buf), prec, gTrue, &str, &len);
507 break;
508 case fmtChar:
509 buf[0] = arg.c;
510 str = buf;
511 len = 1;
512 reverseAlign = !reverseAlign;
513 break;
514 case fmtString:
515 if (arg.s) {
516 str = arg.s;
517 len = (int)strlen(str);
518 } else {
519 str = "(null)";
520 len = 6;
521 }
522 reverseAlign = !reverseAlign;
523 break;
524 case fmtGString:
525 if (arg.gs) {
526 str = arg.gs->getCString();
527 len = arg.gs->getLength();
528 } else {
529 str = "(null)";
530 len = 6;
531 }
532 reverseAlign = !reverseAlign;
533 break;
534 case fmtSpace:
535 str = buf;
536 len = 0;
537 width = arg.i;
538 break;
539 }
540
541 // append the formatted arg, handling width and alignment
542 if (!reverseAlign && len < width) {
543 for (i = len; i < width; ++i) {
544 append(' ');
545 }
546 }
547 append(str, len);
548 if (reverseAlign && len < width) {
549 for (i = len; i < width; ++i) {
550 append(' ');
551 }
552 }
553 }
554
555 } else if (*p0 == '}') {
556 ++p0;
557 if (*p0 == '}') {
558 ++p0;
559 }
560 append('}');
561
562 } else {
563 for (p1 = p0 + 1; *p1 && *p1 != '{' && *p1 != '}'; ++p1) ;
564 append(p0, (int)(p1 - p0));
565 p0 = p1;
566 }
567 }
568
569 gfree(args);
570 return this;
571 }
572
573 #ifdef LLONG_MAX
574 void GString::formatInt(long long x, char *buf, int bufSize,
575 GBool zeroFill, int width, int base,
576 const char **p, int *len) {
577 #else
578 void GString::formatInt(long x, char *buf, int bufSize,
579 GBool zeroFill, int width, int base,
580 const char **p, int *len) {
581 #endif
582 static char vals[17] = "0123456789abcdef";
583 GBool neg;
584 int start, i, j;
585
586 i = bufSize;
587 if ((neg = x < 0)) {
588 x = -x;
589 }
590 start = neg ? 1 : 0;
591 if (x == 0) {
592 buf[--i] = '0';
593 } else {
594 while (i > start && x) {
595 buf[--i] = vals[x % base];
596 x /= base;
597 }
598 }
599 if (zeroFill) {
600 for (j = bufSize - i; i > start && j < width - start; ++j) {
601 buf[--i] = '0';
602 }
603 }
604 if (neg) {
605 buf[--i] = '-';
606 }
607 *p = buf + i;
608 *len = bufSize - i;
609 }
610
611 #ifdef ULLONG_MAX
612 void GString::formatUInt(unsigned long long x, char *buf, int bufSize,
613 GBool zeroFill, int width, int base,
614 const char **p, int *len) {
615 #else
616 void GString::formatUInt(Gulong x, char *buf, int bufSize,
617 GBool zeroFill, int width, int base,
618 const char **p, int *len) {
619 #endif
620 static char vals[17] = "0123456789abcdef";
621 int i, j;
622
623 i = bufSize;
624 if (x == 0) {
625 buf[--i] = '0';
626 } else {
627 while (i > 0 && x) {
628 buf[--i] = vals[x % base];
629 x /= base;
630 }
631 }
632 if (zeroFill) {
633 for (j = bufSize - i; i > 0 && j < width; ++j) {
634 buf[--i] = '0';
635 }
636 }
637 *p = buf + i;
638 *len = bufSize - i;
639 }
640
641 void GString::formatDouble(double x, char *buf, int bufSize, int prec,
642 GBool trim, const char **p, int *len) {
643 GBool neg, started;
644 double x2;
645 int d, i, j;
646
647 if ((neg = x < 0)) {
648 x = -x;
649 }
650 x = floor(x * pow(10.0, prec) + 0.5);
651 i = bufSize;
652 started = !trim;
653 for (j = 0; j < prec && i > 1; ++j) {
654 x2 = floor(0.1 * (x + 0.5));
655 d = (int)floor(x - 10 * x2 + 0.5);
656 if (started || d != 0) {
657 buf[--i] = (char)('0' + d);
658 started = gTrue;
659 }
660 x = x2;
661 }
662 if (i > 1 && started) {
663 buf[--i] = '.';
664 }
665 if (i > 1) {
666 do {
667 x2 = floor(0.1 * (x + 0.5));
668 d = (int)floor(x - 10 * x2 + 0.5);
669 buf[--i] = (char)('0' + d);
670 x = x2;
671 } while (i > 1 && x);
672 }
673 if (neg) {
674 buf[--i] = '-';
675 }
676 *p = buf + i;
677 *len = bufSize - i;
678 }
679
680 GString *GString::insert(int i, char c) {
681 int j;
682
683 if (length > INT_MAX - 1) {
684 gMemError("Integer overflow in GString::insert()");
685 }
686 resize(length + 1);
687 for (j = length + 1; j > i; --j)
688 s[j] = s[j-1];
689 s[i] = c;
690 ++length;
691 return this;
692 }
693
694 GString *GString::insert(int i, GString *str) {
695 int n = str->getLength();
696 int j;
697
698 if (length > INT_MAX - n) {
699 gMemError("Integer overflow in GString::insert()");
700 }
701 resize(length + n);
702 for (j = length; j >= i; --j)
703 s[j+n] = s[j];
704 memcpy(s+i, str->getCString(), n);
705 length += n;
706 return this;
707 }
708
709 GString *GString::insert(int i, const char *str) {
710 int n = (int)strlen(str);
711 int j;
712
713 if (length > INT_MAX - n) {
714 gMemError("Integer overflow in GString::insert()");
715 }
716 resize(length + n);
717 for (j = length; j >= i; --j)
718 s[j+n] = s[j];
719 memcpy(s+i, str, n);
720 length += n;
721 return this;
722 }
723
724 GString *GString::insert(int i, const char *str, int lengthA) {
725 int j;
726
727 if (lengthA < 0 || length > INT_MAX - lengthA) {
728 gMemError("Integer overflow in GString::insert()");
729 }
730 resize(length + lengthA);
731 for (j = length; j >= i; --j)
732 s[j+lengthA] = s[j];
733 memcpy(s+i, str, lengthA);
734 length += lengthA;
735 return this;
736 }
737
738 GString *GString::del(int i, int n) {
739 int j;
740
741 if (i >= 0 && n > 0 && i <= INT_MAX - n) {
742 if (i + n > length) {
743 n = length - i;
744 }
745 for (j = i; j <= length - n; ++j) {
746 s[j] = s[j + n];
747 }
748 resize(length -= n);
749 }
750 return this;
751 }
752
753 GString *GString::upperCase() {
754 int i;
755
756 for (i = 0; i < length; ++i) {
757 if (islower(s[i] & 0xff)) {
758 s[i] = (char)toupper(s[i] & 0xff);
759 }
760 }
761 return this;
762 }
763
764 GString *GString::lowerCase() {
765 int i;
766
767 for (i = 0; i < length; ++i) {
768 if (isupper(s[i] & 0xff)) {
769 s[i] = (char)tolower(s[i] & 0xff);
770 }
771 }
772 return this;
773 }
774
775 int GString::cmp(GString *str) {
776 int n1, n2, i, x;
777 char *p1, *p2;
778
779 n1 = length;
780 n2 = str->length;
781 for (i = 0, p1 = s, p2 = str->s; i < n1 && i < n2; ++i, ++p1, ++p2) {
782 x = (*p1 & 0xff) - (*p2 & 0xff);
783 if (x != 0) {
784 return x;
785 }
786 }
787 return n1 - n2;
788 }
789
790 int GString::cmpN(GString *str, int n) {
791 int n1, n2, i, x;
792 char *p1, *p2;
793
794 n1 = length;
795 n2 = str->length;
796 for (i = 0, p1 = s, p2 = str->s;
797 i < n1 && i < n2 && i < n;
798 ++i, ++p1, ++p2) {
799 x = (*p1 & 0xff) - (*p2 & 0xff);
800 if (x != 0) {
801 return x;
802 }
803 }
804 if (i == n) {
805 return 0;
806 }
807 return n1 - n2;
808 }
809
810 int GString::cmp(const char *sA) {
811 int n1, i, x;
812 const char *p1, *p2;
813
814 n1 = length;
815 for (i = 0, p1 = s, p2 = sA; i < n1 && *p2; ++i, ++p1, ++p2) {
816 x = (*p1 & 0xff) - (*p2 & 0xff);
817 if (x != 0) {
818 return x;
819 }
820 }
821 if (i < n1) {
822 return 1;
823 }
824 if (*p2) {
825 return -1;
826 }
827 return 0;
828 }
829
830 int GString::cmpN(const char *sA, int n) {
831 int n1, i, x;
832 const char *p1, *p2;
833
834 n1 = length;
835 for (i = 0, p1 = s, p2 = sA; i < n1 && *p2 && i < n; ++i, ++p1, ++p2) {
836 x = (*p1 & 0xff) - (*p2 & 0xff);
837 if (x != 0) {
838 return x;
839 }
840 }
841 if (i == n) {
842 return 0;
843 }
844 if (i < n1) {
845 return 1;
846 }
847 if (*p2) {
848 return -1;
849 }
850 return 0;
851 }