Coverage Report

Created: 2026-07-30 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poppler/goo/GooString.h
Line
Count
Source
1
//========================================================================
2
//
3
// GooString.h
4
//
5
// Simple variable-length string type.
6
//
7
// Copyright 1996-2003 Glyph & Cog, LLC
8
//
9
//========================================================================
10
11
//========================================================================
12
//
13
// Modified under the Poppler project - http://poppler.freedesktop.org
14
//
15
// All changes made under the Poppler project to this file are licensed
16
// under GPL version 2 or later
17
//
18
// Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
19
// Copyright (C) 2006 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
20
// Copyright (C) 2008-2010, 2012, 2014, 2017-2022, 2024, 2025 Albert Astals Cid <aacid@kde.org>
21
// Copyright (C) 2012-2014 Fabio D'Urso <fabiodurso@hotmail.it>
22
// Copyright (C) 2013 Jason Crain <jason@aquaticape.us>
23
// Copyright (C) 2015, 2018 Adam Reichold <adam.reichold@t-online.de>
24
// Copyright (C) 2016 Jakub Alba <jakubalba@gmail.com>
25
// Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
26
// 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
27
// Copyright (C) 2019 Christophe Fergeau <cfergeau@redhat.com>
28
// Copyright (C) 2019 Tomoyuki Kubota <himajin100000@gmail.com>
29
// Copyright (C) 2019, 2020, 2022-2024 Oliver Sander <oliver.sander@tu-dresden.de>
30
// Copyright (C) 2019 Hans-Ulrich Jüttner <huj@froreich-bioscientia.de>
31
// Copyright (C) 2020 Thorsten Behrens <Thorsten.Behrens@CIB.de>
32
// Copyright (C) 2022 Even Rouault <even.rouault@spatialys.com>
33
// Copyright (C) 2025, 2026 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk>
34
// Copyright (C) 2025 Jonathan Hähne <jonathan.haehne@hotmail.com>
35
//
36
// To see a description of the changes please see the Changelog file that
37
// came with your tarball or type make ChangeLog if you are building from git
38
//
39
//========================================================================
40
41
#ifndef GooString_H
42
#define GooString_H
43
44
#include "poppler_private_export.h"
45
46
#include <cstdarg>
47
#include <memory>
48
#include <string>
49
50
#ifdef __clang__
51
#    define GOOSTRING_FORMAT __attribute__((__annotate__("gooformat")))
52
#else
53
#    define GOOSTRING_FORMAT
54
#endif
55
56
class GooString : private std::string
57
{
58
public:
59
    // Create an empty string.
60
11.3k
    GooString() = default;
61
62
    // Destructor.
63
    ~GooString() = default;
64
65
14.2k
    GooString(GooString &&other) = default;
66
3.78k
    GooString &operator=(GooString &&other) = default;
67
68
    GooString(const GooString &other) = delete;
69
    GooString &operator=(const GooString &other) = delete;
70
71
    // Create a string from a C string.
72
21.3k
    explicit GooString(const char *sA) : std::string(sA ? sA : "") { }
73
74
    // disallow explicit creation from nullptr
75
    // (c++23 also disallows it for std::string)
76
    // note, this only prevents GooString(nullptr),
77
    // not const char*s = nullptr; GooString(s).
78
    explicit GooString(std::nullptr_t) = delete;
79
80
    // Zero-cost conversion from and to std::string
81
7.01k
    explicit GooString(const std::string &str) : std::string(str) { }
82
0
    explicit GooString(std::string &&str) : std::string(std::move(str)) { }
83
84
10.9k
    constexpr const std::string &toStr() const { return *this; }
85
0
    std::string &toNonConstStr() { return *this; }
86
87
    // Create a string from <lengthA> chars at <sA>.  This string
88
    // can contain null characters.
89
0
    GooString(const char *sA, size_t lengthA) : std::string(sA ? sA : "", sA ? lengthA : 0) { }
90
0
    explicit GooString(std::string_view str) : std::string(str) { }
91
92
    // Create a string from <lengthA> chars at <idx> in <str>.
93
0
    GooString(const GooString *str, int idx, size_t lengthA) : std::string(*str, idx, lengthA) { }
94
0
    GooString(const std::string &str, int idx, size_t lengthA) : std::string(str, idx, lengthA) { }
95
96
    using std::string::assign;
97
98
    // Copy a string.
99
0
    std::unique_ptr<GooString> copy() const { return std::make_unique<GooString>(this->toStr()); }
100
101
    // Create a formatted string.  Similar to printf, but without the
102
    // string overflow issues.  Formatting elements consist of:
103
    //     {<arg>:[<width>][.<precision>]<type>}
104
    // where:
105
    // - <arg> is the argument number (arg 0 is the first argument
106
    //   following the format string) -- NB: args must be first used in
107
    //   order; they can be reused in any order
108
    // - <width> is the field width -- negative to reverse the alignment;
109
    //   starting with a leading zero to zero-fill (for integers)
110
    // - <precision> is the number of digits to the right of the decimal
111
    //   point (for floating point numbers)
112
    // - <type> is one of:
113
    //     d, x, X, o, b -- int in decimal, lowercase hex, uppercase hex, octal, binary
114
    //     ud, ux, uX, uo, ub -- unsigned int
115
    //     ld, lx, lX, lo, lb, uld, ulx, ulX, ulo, ulb -- long, unsigned long
116
    //     lld, llx, llX, llo, llb, ulld, ullx, ullX, ullo, ullb
117
    //         -- long long, unsigned long long
118
    //     f, g, gs -- floating point (float or double)
119
    //         f  -- always prints trailing zeros (eg 1.0 with .2f will print 1.00)
120
    //         g  -- omits trailing zeros and, if possible, the dot (eg 1.0 shows up as 1)
121
    //         gs -- is like g, but treats <precision> as number of significant
122
    //               digits to show (eg 0.0123 with .2gs will print 0.012)
123
    //     c -- character (char, short or int)
124
    //     r -- (const) std::string *
125
    //     s -- string (char *)
126
    //     t -- GooString *
127
    //     w -- blank space; arg determines width
128
    // To get literal curly braces, use {{ or }}.
129
    POPPLER_PRIVATE_EXPORT static std::string format(const char *fmt, ...) GOOSTRING_FORMAT;
130
    POPPLER_PRIVATE_EXPORT static std::string formatv(const char *fmt, va_list argList);
131
132
    using std::string::operator std::string_view;
133
134
    // Get length.
135
    using std::string::empty;
136
    using std::string::size;
137
138
    // Get C string.
139
    using std::string::c_str;
140
141
    // Get <i>th character.
142
0
    char getChar(size_t i) const { return (*this)[i]; }
143
144
    // Clear string to zero length.
145
    using std::string::clear;
146
147
    // Append a character or string.
148
    using std::string::append;
149
    using std::string::push_back;
150
151
    // Append a formatted string.
152
    POPPLER_PRIVATE_EXPORT static std::string &appendf(std::string &that, const char *fmt, ...) GOOSTRING_FORMAT;
153
    POPPLER_PRIVATE_EXPORT static std::string &appendfv(std::string &that, const char *fmt, va_list argList);
154
155
    using std::string::insert;
156
157
    // Delete a character or range of characters.
158
    using std::string::erase;
159
160
    // Convert string to all-lower case.
161
    POPPLER_PRIVATE_EXPORT static void lowerCase(std::string &s);
162
163
    // Returns a new string converted to all-lower case.
164
    POPPLER_PRIVATE_EXPORT static std::string toLowerCase(std::string_view s);
165
166
    // Compare two strings:  -1:<  0:=  +1:>
167
    using std::string::compare;
168
169
    // Return true if strings starts with prefix
170
    using std::string::starts_with;
171
172
    // Return true if string ends with suffix
173
    using std::string::ends_with;
174
};
175
176
#endif