File: GooString.cc

package info (click to toggle)
poppler 20.09.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 16,092 kB
  • sloc: cpp: 151,577; ansic: 30,155; python: 367; makefile: 42; sh: 36
file content (648 lines) | stat: -rw-r--r-- 21,289 bytes parent folder | download | duplicates (2)
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
//========================================================================
//
// GooString.cc
//
// Simple variable-length string type.
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================

//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
// Copyright (C) 2006 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
// Copyright (C) 2007 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2008-2011, 2016-2018 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2011 Kenji Uno <ku@digitaldolphins.jp>
// Copyright (C) 2012, 2013 Fabio D'Urso <fabiodurso@hotmail.it>
// Copyright (C) 2012, 2017 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2012 Pino Toscano <pino@kde.org>
// Copyright (C) 2013 Jason Crain <jason@aquaticape.us>
// Copyright (C) 2015 William Bader <williambader@hotmail.com>
// Copyright (C) 2016 Jakub Alba <jakubalba@gmail.com>
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
// Copyright (C) 2018 Greg Knight <lyngvi@gmail.com>
// Copyright (C) 2019 Oliver Sander <oliver.sander@tu-dresden.de>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

#include <config.h>

#include <cassert>
#include <cctype>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <cstring>

#include "gmem.h"
#include "GooString.h"

//------------------------------------------------------------------------

namespace {

union GooStringFormatArg {
    int i;
    unsigned int ui;
    long l;
    unsigned long ul;
    long long ll;
    unsigned long long ull;
    double f;
    char c;
    char *s;
    GooString *gs;
};

enum GooStringFormatType
{
    fmtIntDecimal,
    fmtIntHex,
    fmtIntHexUpper,
    fmtIntOctal,
    fmtIntBinary,
    fmtUIntDecimal,
    fmtUIntHex,
    fmtUIntHexUpper,
    fmtUIntOctal,
    fmtUIntBinary,
    fmtLongDecimal,
    fmtLongHex,
    fmtLongHexUpper,
    fmtLongOctal,
    fmtLongBinary,
    fmtULongDecimal,
    fmtULongHex,
    fmtULongHexUpper,
    fmtULongOctal,
    fmtULongBinary,
    fmtLongLongDecimal,
    fmtLongLongHex,
    fmtLongLongHexUpper,
    fmtLongLongOctal,
    fmtLongLongBinary,
    fmtULongLongDecimal,
    fmtULongLongHex,
    fmtULongLongHexUpper,
    fmtULongLongOctal,
    fmtULongLongBinary,
    fmtDouble,
    fmtDoubleTrimSmallAware,
    fmtDoubleTrim,
    fmtChar,
    fmtString,
    fmtGooString,
    fmtSpace
};

const char *const formatStrings[] = { "d",   "x",   "X",   "o",   "b",   "ud",  "ux",   "uX",   "uo",   "ub",   "ld",   "lx", "lX", "lo", "lb", "uld", "ulx", "ulX", "ulo",
                                      "ulb", "lld", "llx", "llX", "llo", "llb", "ulld", "ullx", "ullX", "ullo", "ullb", "f",  "gs", "g",  "c",  "s",   "t",   "w",   nullptr };

void formatInt(long long x, char *buf, int bufSize, bool zeroFill, int width, int base, const char **p, int *len, bool upperCase = false);

void formatUInt(unsigned long long x, char *buf, int bufSize, bool zeroFill, int width, int base, const char **p, int *len, bool upperCase = false);

void formatDouble(double x, char *buf, int bufSize, int prec, bool trim, const char **p, int *len);

void formatDoubleSmallAware(double x, char *buf, int bufSize, int prec, bool trim, const char **p, int *len);

}

//------------------------------------------------------------------------

GooString *GooString::format(const char *fmt, ...)
{
    auto *s = new GooString();

    va_list argList;
    va_start(argList, fmt);
    s->appendfv(fmt, argList);
    va_end(argList);

    return s;
}

GooString *GooString::formatv(const char *fmt, va_list argList)
{
    auto *s = new GooString();

    s->appendfv(fmt, argList);

    return s;
}

GooString *GooString::appendf(const char *fmt, ...)
{
    va_list argList;
    va_start(argList, fmt);
    appendfv(fmt, argList);
    va_end(argList);

    return this;
}

GooString *GooString::appendfv(const char *fmt, va_list argList)
{
    GooStringFormatArg *args;
    int argsLen, argsSize;
    GooStringFormatArg arg;
    int idx, width, prec;
    bool reverseAlign, zeroFill;
    GooStringFormatType ft;
    char buf[65];
    int len, i;
    const char *p0, *p1;
    const char *str;
    GooStringFormatArg argsBuf[8];

    argsLen = 0;
    argsSize = sizeof(argsBuf) / sizeof(argsBuf[0]);
    args = argsBuf;

    p0 = fmt;
    while (*p0) {
        if (*p0 == '{') {
            ++p0;
            if (*p0 == '{') {
                ++p0;
                append('{');
            } else {

                // parse the format string
                if (!(*p0 >= '0' && *p0 <= '9')) {
                    break;
                }
                idx = *p0 - '0';
                for (++p0; *p0 >= '0' && *p0 <= '9'; ++p0) {
                    idx = 10 * idx + (*p0 - '0');
                }
                if (*p0 != ':') {
                    break;
                }
                ++p0;
                if (*p0 == '-') {
                    reverseAlign = true;
                    ++p0;
                } else {
                    reverseAlign = false;
                }
                width = 0;
                zeroFill = *p0 == '0';
                for (; *p0 >= '0' && *p0 <= '9'; ++p0) {
                    width = 10 * width + (*p0 - '0');
                }
                if (width < 0) {
                    width = 0;
                }
                if (*p0 == '.') {
                    ++p0;
                    prec = 0;
                    for (; *p0 >= '0' && *p0 <= '9'; ++p0) {
                        prec = 10 * prec + (*p0 - '0');
                    }
                } else {
                    prec = 0;
                }
                for (ft = (GooStringFormatType)0; formatStrings[ft]; ft = (GooStringFormatType)(ft + 1)) {
                    if (!strncmp(p0, formatStrings[ft], strlen(formatStrings[ft]))) {
                        break;
                    }
                }
                if (!formatStrings[ft]) {
                    break;
                }
                p0 += strlen(formatStrings[ft]);
                if (*p0 != '}') {
                    break;
                }
                ++p0;

                // fetch the argument
                if (idx > argsLen) {
                    break;
                }
                if (idx == argsLen) {
                    if (argsLen == argsSize) {
                        argsSize *= 2;
                        if (args == argsBuf) {
                            args = (GooStringFormatArg *)gmallocn(argsSize, sizeof(GooStringFormatArg));
                            memcpy(args, argsBuf, argsLen * sizeof(GooStringFormatArg));
                        } else {
                            args = (GooStringFormatArg *)greallocn(args, argsSize, sizeof(GooStringFormatArg));
                        }
                    }
                    switch (ft) {
                    case fmtIntDecimal:
                    case fmtIntHex:
                    case fmtIntHexUpper:
                    case fmtIntOctal:
                    case fmtIntBinary:
                    case fmtSpace:
                        args[argsLen].i = va_arg(argList, int);
                        break;
                    case fmtUIntDecimal:
                    case fmtUIntHex:
                    case fmtUIntHexUpper:
                    case fmtUIntOctal:
                    case fmtUIntBinary:
                        args[argsLen].ui = va_arg(argList, unsigned int);
                        break;
                    case fmtLongDecimal:
                    case fmtLongHex:
                    case fmtLongHexUpper:
                    case fmtLongOctal:
                    case fmtLongBinary:
                        args[argsLen].l = va_arg(argList, long);
                        break;
                    case fmtULongDecimal:
                    case fmtULongHex:
                    case fmtULongHexUpper:
                    case fmtULongOctal:
                    case fmtULongBinary:
                        args[argsLen].ul = va_arg(argList, unsigned long);
                        break;
                    case fmtLongLongDecimal:
                    case fmtLongLongHex:
                    case fmtLongLongHexUpper:
                    case fmtLongLongOctal:
                    case fmtLongLongBinary:
                        args[argsLen].ll = va_arg(argList, long long);
                        break;
                    case fmtULongLongDecimal:
                    case fmtULongLongHex:
                    case fmtULongLongHexUpper:
                    case fmtULongLongOctal:
                    case fmtULongLongBinary:
                        args[argsLen].ull = va_arg(argList, unsigned long long);
                        break;
                    case fmtDouble:
                    case fmtDoubleTrim:
                    case fmtDoubleTrimSmallAware:
                        args[argsLen].f = va_arg(argList, double);
                        break;
                    case fmtChar:
                        args[argsLen].c = (char)va_arg(argList, int);
                        break;
                    case fmtString:
                        args[argsLen].s = va_arg(argList, char *);
                        break;
                    case fmtGooString:
                        args[argsLen].gs = va_arg(argList, GooString *);
                        break;
                    }
                    ++argsLen;
                }

                // format the argument
                arg = args[idx];
                switch (ft) {
                case fmtIntDecimal:
                    formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
                    break;
                case fmtIntHex:
                    formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
                    break;
                case fmtIntHexUpper:
                    formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 16, &str, &len, true);
                    break;
                case fmtIntOctal:
                    formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
                    break;
                case fmtIntBinary:
                    formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
                    break;
                case fmtUIntDecimal:
                    formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
                    break;
                case fmtUIntHex:
                    formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
                    break;
                case fmtUIntHexUpper:
                    formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 16, &str, &len, true);
                    break;
                case fmtUIntOctal:
                    formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
                    break;
                case fmtUIntBinary:
                    formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
                    break;
                case fmtLongDecimal:
                    formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
                    break;
                case fmtLongHex:
                    formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
                    break;
                case fmtLongHexUpper:
                    formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 16, &str, &len, true);
                    break;
                case fmtLongOctal:
                    formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
                    break;
                case fmtLongBinary:
                    formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
                    break;
                case fmtULongDecimal:
                    formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
                    break;
                case fmtULongHex:
                    formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
                    break;
                case fmtULongHexUpper:
                    formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 16, &str, &len, true);
                    break;
                case fmtULongOctal:
                    formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
                    break;
                case fmtULongBinary:
                    formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
                    break;
                case fmtLongLongDecimal:
                    formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
                    break;
                case fmtLongLongHex:
                    formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
                    break;
                case fmtLongLongHexUpper:
                    formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 16, &str, &len, true);
                    break;
                case fmtLongLongOctal:
                    formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
                    break;
                case fmtLongLongBinary:
                    formatInt(arg.ll, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
                    break;
                case fmtULongLongDecimal:
                    formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 10, &str, &len);
                    break;
                case fmtULongLongHex:
                    formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 16, &str, &len);
                    break;
                case fmtULongLongHexUpper:
                    formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 16, &str, &len, true);
                    break;
                case fmtULongLongOctal:
                    formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 8, &str, &len);
                    break;
                case fmtULongLongBinary:
                    formatUInt(arg.ull, buf, sizeof(buf), zeroFill, width, 2, &str, &len);
                    break;
                case fmtDouble:
                    formatDouble(arg.f, buf, sizeof(buf), prec, false, &str, &len);
                    break;
                case fmtDoubleTrim:
                    formatDouble(arg.f, buf, sizeof(buf), prec, true, &str, &len);
                    break;
                case fmtDoubleTrimSmallAware:
                    formatDoubleSmallAware(arg.f, buf, sizeof(buf), prec, true, &str, &len);
                    break;
                case fmtChar:
                    buf[0] = arg.c;
                    str = buf;
                    len = 1;
                    reverseAlign = !reverseAlign;
                    break;
                case fmtString:
                    str = arg.s;
                    len = strlen(str);
                    reverseAlign = !reverseAlign;
                    break;
                case fmtGooString:
                    str = arg.gs->c_str();
                    len = arg.gs->getLength();
                    reverseAlign = !reverseAlign;
                    break;
                case fmtSpace:
                    str = buf;
                    len = 0;
                    width = arg.i;
                    break;
                }

                // append the formatted arg, handling width and alignment
                if (!reverseAlign && len < width) {
                    for (i = len; i < width; ++i) {
                        append(' ');
                    }
                }
                append(str, len);
                if (reverseAlign && len < width) {
                    for (i = len; i < width; ++i) {
                        append(' ');
                    }
                }
            }

        } else if (*p0 == '}') {
            ++p0;
            if (*p0 == '}') {
                ++p0;
            }
            append('}');

        } else {
            for (p1 = p0 + 1; *p1 && *p1 != '{' && *p1 != '}'; ++p1)
                ;
            append(p0, p1 - p0);
            p0 = p1;
        }
    }

    if (args != argsBuf) {
        gfree(args);
    }

    return this;
}

namespace {

const char lowerCaseDigits[17] = "0123456789abcdef";
const char upperCaseDigits[17] = "0123456789ABCDEF";

void formatInt(long long x, char *buf, int bufSize, bool zeroFill, int width, int base, const char **p, int *len, bool upperCase)
{
    const char *vals = upperCase ? upperCaseDigits : lowerCaseDigits;
    bool neg;
    int start, i, j;
    unsigned long long abs_x;

    i = bufSize;
    if ((neg = x < 0)) {
        abs_x = -x;
    } else {
        abs_x = x;
    }
    start = neg ? 1 : 0;
    if (abs_x == 0) {
        buf[--i] = '0';
    } else {
        while (i > start && abs_x) {
            buf[--i] = vals[abs_x % base];
            abs_x /= base;
        }
    }
    if (zeroFill) {
        for (j = bufSize - i; i > start && j < width - start; ++j) {
            buf[--i] = '0';
        }
    }
    if (neg) {
        buf[--i] = '-';
    }
    *p = buf + i;
    *len = bufSize - i;
}

void formatUInt(unsigned long long x, char *buf, int bufSize, bool zeroFill, int width, int base, const char **p, int *len, bool upperCase)
{
    const char *vals = upperCase ? upperCaseDigits : lowerCaseDigits;
    int i, j;

    i = bufSize;
    if (x == 0) {
        buf[--i] = '0';
    } else {
        while (i > 0 && x) {
            buf[--i] = vals[x % base];
            x /= base;
        }
    }
    if (zeroFill) {
        for (j = bufSize - i; i > 0 && j < width; ++j) {
            buf[--i] = '0';
        }
    }
    *p = buf + i;
    *len = bufSize - i;
}

void formatDouble(double x, char *buf, int bufSize, int prec, bool trim, const char **p, int *len)
{
    bool neg, started;
    double x2;
    int d, i, j;

    if ((neg = x < 0)) {
        x = -x;
    }
    x = floor(x * pow(10.0, prec) + 0.5);
    i = bufSize;
    started = !trim;
    for (j = 0; j < prec && i > 1; ++j) {
        x2 = floor(0.1 * (x + 0.5));
        d = (int)floor(x - 10 * x2 + 0.5);
        if (started || d != 0) {
            buf[--i] = '0' + d;
            started = true;
        }
        x = x2;
    }
    if (i > 1 && started) {
        buf[--i] = '.';
    }
    if (i > 1) {
        do {
            x2 = floor(0.1 * (x + 0.5));
            d = (int)floor(x - 10 * x2 + 0.5);
            buf[--i] = '0' + d;
            x = x2;
        } while (i > 1 && x);
    }
    if (neg) {
        buf[--i] = '-';
    }
    *p = buf + i;
    *len = bufSize - i;
}

void formatDoubleSmallAware(double x, char *buf, int bufSize, int prec, bool trim, const char **p, int *len)
{
    double absX = fabs(x);
    if (absX >= 0.1) {
        formatDouble(x, buf, bufSize, prec, trim, p, len);
    } else {
        while (absX < 0.1 && prec < 16) {
            absX = absX * 10;
            prec++;
        }
        formatDouble(x, buf, bufSize, prec, trim, p, len);
    }
}

}

GooString *GooString::lowerCase()
{
    for (auto &c : *this) {
        if (std::isupper(c)) {
            c = std::tolower(c);
        }
    }

    return this;
}

void GooString::prependUnicodeMarker()
{
    insert(0, "\xFE\xFF", 2);
}

bool GooString::startsWith(const char *prefix) const
{
    const auto len = size();
    const auto prefixLen = std::strlen(prefix);

    if (len < prefixLen)
        return false;

    return static_cast<const std::string &>(*this).compare(0, prefixLen, prefix) == 0;
}

bool GooString::endsWith(const char *suffix) const
{
    const auto len = size();
    const auto suffixLen = std::strlen(suffix);

    if (len < suffixLen)
        return false;

    return static_cast<const std::string &>(*this).compare(len - suffixLen, suffixLen, suffix) == 0;
}

GooString *GooString::sanitizedName(bool psmode) const
{
    auto *name = new GooString();

    if (psmode) {
        // ghostscript chokes on names that begin with out-of-limits
        // numbers, e.g., 1e4foo is handled correctly (as a name), but
        // 1e999foo generates a limitcheck error
        const auto c = getChar(0);
        if (c >= '0' && c <= '9') {
            name->append('f');
        }
    }

    for (const auto c : *this) {
        if (c <= (char)0x20 || c >= (char)0x7f || c == ' ' || c == '(' || c == ')' || c == '<' || c == '>' || c == '[' || c == ']' || c == '{' || c == '}' || c == '/' || c == '%' || c == '#') {
            char buf[8];
            sprintf(buf, "#%02x", c & 0xff);
            name->append(buf);
        } else {
            name->append(c);
        }
    }

    return name;
}