Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-11 10:26:10

0001 //========================================================================
0002 //
0003 // GooString.h
0004 //
0005 // Simple variable-length string type.
0006 //
0007 // Copyright 1996-2003 Glyph & Cog, LLC
0008 //
0009 //========================================================================
0010 
0011 //========================================================================
0012 //
0013 // Modified under the Poppler project - http://poppler.freedesktop.org
0014 //
0015 // All changes made under the Poppler project to this file are licensed
0016 // under GPL version 2 or later
0017 //
0018 // Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
0019 // Copyright (C) 2006 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
0020 // Copyright (C) 2008-2010, 2012, 2014, 2017-2022 Albert Astals Cid <aacid@kde.org>
0021 // Copyright (C) 2012-2014 Fabio D'Urso <fabiodurso@hotmail.it>
0022 // Copyright (C) 2013 Jason Crain <jason@aquaticape.us>
0023 // Copyright (C) 2015, 2018 Adam Reichold <adam.reichold@t-online.de>
0024 // Copyright (C) 2016 Jakub Alba <jakubalba@gmail.com>
0025 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
0026 // 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
0027 // Copyright (C) 2019 Christophe Fergeau <cfergeau@redhat.com>
0028 // Copyright (C) 2019 Tomoyuki Kubota <himajin100000@gmail.com>
0029 // Copyright (C) 2019, 2020, 2022 Oliver Sander <oliver.sander@tu-dresden.de>
0030 // Copyright (C) 2019 Hans-Ulrich Jüttner <huj@froreich-bioscientia.de>
0031 // Copyright (C) 2020 Thorsten Behrens <Thorsten.Behrens@CIB.de>
0032 // Copyright (C) 2022 Even Rouault <even.rouault@spatialys.com>
0033 //
0034 // To see a description of the changes please see the Changelog file that
0035 // came with your tarball or type make ChangeLog if you are building from git
0036 //
0037 //========================================================================
0038 
0039 #ifndef GooString_H
0040 #define GooString_H
0041 
0042 #include "poppler_private_export.h"
0043 
0044 #include <cstdarg>
0045 #include <memory>
0046 #include <string>
0047 
0048 #ifdef __clang__
0049 #    define GOOSTRING_FORMAT __attribute__((__annotate__("gooformat")))
0050 #else
0051 #    define GOOSTRING_FORMAT
0052 #endif
0053 
0054 class GooString : private std::string
0055 {
0056 public:
0057     // Create an empty string.
0058     GooString() = default;
0059 
0060     // Destructor.
0061     ~GooString() = default;
0062 
0063     GooString(GooString &&other) = default;
0064     GooString &operator=(GooString &&other) = default;
0065 
0066     GooString(const GooString &other) = delete;
0067     GooString &operator=(const GooString &other) = delete;
0068 
0069     // Create a string from a C string.
0070     explicit GooString(const char *sA) : std::string(sA ? sA : "") { }
0071 
0072     // Zero-cost conversion from and to std::string
0073     explicit GooString(const std::string &str) : std::string(str) { }
0074     explicit GooString(std::string &&str) : std::string(std::move(str)) { }
0075 
0076     const std::string &toStr() const { return *this; }
0077     std::string &toNonConstStr() { return *this; }
0078 
0079     // Create a string from <lengthA> chars at <sA>.  This string
0080     // can contain null characters.
0081     GooString(const char *sA, size_t lengthA) : std::string(sA ? sA : "", sA ? lengthA : 0) { }
0082 
0083     // Create a string from <lengthA> chars at <idx> in <str>.
0084     GooString(const GooString *str, int idx, size_t lengthA) : std::string(*str, idx, lengthA) { }
0085     GooString(const std::string &str, int idx, size_t lengthA) : std::string(str, idx, lengthA) { }
0086 
0087     // Set content of a string to <newStr>.
0088     GooString *Set(const GooString *newStr)
0089     {
0090         assign(newStr ? static_cast<const std::string &>(*newStr) : std::string {});
0091         return this;
0092     }
0093     GooString *Set(const char *newStr)
0094     {
0095         assign(newStr ? newStr : "");
0096         return this;
0097     }
0098     GooString *Set(const char *newStr, int newLen)
0099     {
0100         assign(newStr ? newStr : "", newStr ? newLen : 0);
0101         return this;
0102     }
0103 
0104     // Copy a string.
0105     explicit GooString(const GooString *str) : std::string(str ? static_cast<const std::string &>(*str) : std::string {}) { }
0106     GooString *copy() const { return new GooString(this); }
0107 
0108     // Concatenate two strings.
0109     GooString(const GooString *str1, const GooString *str2)
0110     {
0111         reserve(str1->size() + str2->size());
0112         static_cast<std::string &>(*this).append(*str1);
0113         static_cast<std::string &>(*this).append(*str2);
0114     }
0115 
0116     // Create a formatted string.  Similar to printf, but without the
0117     // string overflow issues.  Formatting elements consist of:
0118     //     {<arg>:[<width>][.<precision>]<type>}
0119     // where:
0120     // - <arg> is the argument number (arg 0 is the first argument
0121     //   following the format string) -- NB: args must be first used in
0122     //   order; they can be reused in any order
0123     // - <width> is the field width -- negative to reverse the alignment;
0124     //   starting with a leading zero to zero-fill (for integers)
0125     // - <precision> is the number of digits to the right of the decimal
0126     //   point (for floating point numbers)
0127     // - <type> is one of:
0128     //     d, x, X, o, b -- int in decimal, lowercase hex, uppercase hex, octal, binary
0129     //     ud, ux, uX, uo, ub -- unsigned int
0130     //     ld, lx, lX, lo, lb, uld, ulx, ulX, ulo, ulb -- long, unsigned long
0131     //     lld, llx, llX, llo, llb, ulld, ullx, ullX, ullo, ullb
0132     //         -- long long, unsigned long long
0133     //     f, g, gs -- floating point (float or double)
0134     //         f  -- always prints trailing zeros (eg 1.0 with .2f will print 1.00)
0135     //         g  -- omits trailing zeros and, if possible, the dot (eg 1.0 shows up as 1)
0136     //         gs -- is like g, but treats <precision> as number of significant
0137     //               digits to show (eg 0.0123 with .2gs will print 0.012)
0138     //     c -- character (char, short or int)
0139     //     s -- string (char *)
0140     //     t -- GooString *
0141     //     w -- blank space; arg determines width
0142     // To get literal curly braces, use {{ or }}.
0143     POPPLER_PRIVATE_EXPORT static std::unique_ptr<GooString> format(const char *fmt, ...) GOOSTRING_FORMAT;
0144     POPPLER_PRIVATE_EXPORT static std::unique_ptr<GooString> formatv(const char *fmt, va_list argList);
0145 
0146     // Get length.
0147     int getLength() const { return size(); }
0148 
0149     // Get C string.
0150     using std::string::c_str;
0151 
0152     // Get <i>th character.
0153     char getChar(size_t i) const { return (*this)[i]; }
0154 
0155     // Change <i>th character.
0156     void setChar(int i, char c) { (*this)[i] = c; }
0157 
0158     // Clear string to zero length.
0159     GooString *clear()
0160     {
0161         static_cast<std::string &>(*this).clear();
0162         return this;
0163     }
0164 
0165     // Append a character or string.
0166     GooString *append(char c)
0167     {
0168         push_back(c);
0169         return this;
0170     }
0171     GooString *append(const GooString *str)
0172     {
0173         static_cast<std::string &>(*this).append(*str);
0174         return this;
0175     }
0176     GooString *append(const std::string &str)
0177     {
0178         static_cast<std::string &>(*this).append(str);
0179         return this;
0180     }
0181     GooString *append(const char *str)
0182     {
0183         static_cast<std::string &>(*this).append(str);
0184         return this;
0185     }
0186     GooString *append(const char *str, size_t lengthA)
0187     {
0188         static_cast<std::string &>(*this).append(str, lengthA);
0189         return this;
0190     }
0191 
0192     // Append a formatted string.
0193     POPPLER_PRIVATE_EXPORT GooString *appendf(const char *fmt, ...) GOOSTRING_FORMAT;
0194     POPPLER_PRIVATE_EXPORT GooString *appendfv(const char *fmt, va_list argList);
0195 
0196     // Insert a character or string.
0197     GooString *insert(int i, char c)
0198     {
0199         static_cast<std::string &>(*this).insert(i, 1, c);
0200         return this;
0201     }
0202     GooString *insert(int i, const GooString *str)
0203     {
0204         static_cast<std::string &>(*this).insert(i, *str);
0205         return this;
0206     }
0207     GooString *insert(int i, const std::string &str)
0208     {
0209         static_cast<std::string &>(*this).insert(i, str);
0210         return this;
0211     }
0212     GooString *insert(int i, const char *str)
0213     {
0214         static_cast<std::string &>(*this).insert(i, str);
0215         return this;
0216     }
0217     GooString *insert(int i, const char *str, int lengthA)
0218     {
0219         static_cast<std::string &>(*this).insert(i, str, lengthA);
0220         return this;
0221     }
0222 
0223     // Delete a character or range of characters.
0224     GooString *del(int i, int n = 1)
0225     {
0226         erase(i, n);
0227         return this;
0228     }
0229 
0230     // Convert string to all-lower case.
0231     POPPLER_PRIVATE_EXPORT GooString *lowerCase();
0232     POPPLER_PRIVATE_EXPORT static void lowerCase(std::string &s);
0233 
0234     // Returns a new string converted to all-lower case.
0235     POPPLER_PRIVATE_EXPORT static std::string toLowerCase(const std::string &s);
0236 
0237     // Compare two strings:  -1:<  0:=  +1:>
0238     int cmp(const GooString *str) const { return compare(*str); }
0239     int cmp(const std::string &str) const { return compare(str); }
0240     int cmpN(GooString *str, int n) const { return compare(0, n, *str); }
0241     int cmp(const char *sA) const { return compare(sA); }
0242     int cmpN(const char *sA, int n) const { return compare(0, n, sA); }
0243 
0244     // Return true if strings starts with prefix
0245     POPPLER_PRIVATE_EXPORT bool startsWith(const char *prefix) const;
0246     // Return true if string ends with suffix
0247     POPPLER_PRIVATE_EXPORT bool endsWith(const char *suffix) const;
0248 
0249     static bool startsWith(std::string_view str, std::string_view prefix) { return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix); }
0250     static bool endsWith(std::string_view str, std::string_view suffix) { return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); }
0251 
0252     bool hasUnicodeMarker() const { return hasUnicodeMarker(*this); }
0253     static bool hasUnicodeMarker(const std::string &s) { return s.size() >= 2 && s[0] == '\xfe' && s[1] == '\xff'; }
0254     bool hasUnicodeMarkerLE() const { return hasUnicodeMarkerLE(*this); }
0255     static bool hasUnicodeMarkerLE(const std::string &s) { return s.size() >= 2 && s[0] == '\xff' && s[1] == '\xfe'; }
0256     bool hasJustUnicodeMarker() const { return size() == 2 && hasUnicodeMarker(); }
0257 
0258     POPPLER_PRIVATE_EXPORT void prependUnicodeMarker();
0259 
0260     // Sanitizes the string so that it does
0261     // not contain any ( ) < > [ ] { } / %
0262     // The caller owns the return value
0263     POPPLER_PRIVATE_EXPORT GooString *sanitizedName() const;
0264 };
0265 
0266 #endif