1 //========================================================================
5 // Simple variable-length string type.
7 // Copyright 1996-2003 Glyph & Cog, LLC
9 //========================================================================
11 //========================================================================
13 // Modified under the Poppler project - http://poppler.freedesktop.org
15 // All changes made under the Poppler project to this file are licensed
16 // under GPL version 2 or later
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 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 Adam Reichold <adam.reichold@t-online.de>
25 // To see a description of the changes please see the Changelog file that
26 // came with your tarball or type make ChangeLog if you are building from git
28 //========================================================================
33 #ifdef USE_GCC_PRAGMAS
37 #include <limits.h> // for LLONG_MAX and ULLONG_MAX
39 /* <limits.h> and/or the compiler may or may not define these. */
40 /* Minimum and maximum values a `signed long long int' can hold. */
42 # define LLONG_MAX 9223372036854775807LL
45 # define LLONG_MIN (-LLONG_MAX - 1LL)
48 /* Maximum value an `unsigned long long int' can hold. (Minimum is 0.) */
50 # define ULLONG_MAX 18446744073709551615ULL
54 #include <stdlib.h> // for NULL
58 # define GOOSTRING_FORMAT __attribute__((__annotate__("gooformat")))
60 # define GOOSTRING_FORMAT
66 // a special value telling that the length of the string is not given
67 // so it must be calculated from the strings
68 static const int CALC_STRING_LEN
= -1;
70 // Create an empty string.
73 // Create a string from a C string.
74 explicit GooString(const char *sA
);
76 // Create a string from <lengthA> chars at <sA>. This string
77 // can contain null characters.
78 GooString(const char *sA
, int lengthA
);
80 // Create a string from <lengthA> chars at <idx> in <str>.
81 GooString(GooString
*str
, int idx
, int lengthA
);
83 // Set content of a string to concatination of <s1> and <s2>. They can both
84 // be NULL. if <s1Len> or <s2Len> is CALC_STRING_LEN, then length of the string
85 // will be calculated with strlen(). Otherwise we assume they are a valid
86 // length of string (or its substring)
87 GooString
* Set(const char *s1
, int s1Len
=CALC_STRING_LEN
, const char *s2
=NULL
, int s2Len
=CALC_STRING_LEN
);
90 explicit GooString(const GooString
*str
);
91 GooString
*copy() const { return new GooString(this); }
93 // Concatenate two strings.
94 GooString(GooString
*str1
, GooString
*str2
);
96 // Convert an integer to a string.
97 static GooString
*fromInt(int x
);
99 // Create a formatted string. Similar to printf, but without the
100 // string overflow issues. Formatting elements consist of:
101 // {<arg>:[<width>][.<precision>]<type>}
103 // - <arg> is the argument number (arg 0 is the first argument
104 // following the format string) -- NB: args must be first used in
105 // order; they can be reused in any order
106 // - <width> is the field width -- negative to reverse the alignment;
107 // starting with a leading zero to zero-fill (for integers)
108 // - <precision> is the number of digits to the right of the decimal
109 // point (for floating point numbers)
110 // - <type> is one of:
111 // d, x, X, o, b -- int in decimal, lowercase hex, uppercase hex, octal, binary
112 // ud, ux, uX, uo, ub -- unsigned int
113 // ld, lx, lX, lo, lb, uld, ulx, ulX, ulo, ulb -- long, unsigned long
114 // lld, llx, llX, llo, llb, ulld, ullx, ullX, ullo, ullb
115 // -- long long, unsigned long long
116 // f, g, gs -- floating point (float or double)
117 // f -- always prints trailing zeros (eg 1.0 with .2f will print 1.00)
118 // g -- omits trailing zeros and, if possible, the dot (eg 1.0 shows up as 1)
119 // gs -- is like g, but treats <precision> as number of significant
120 // digits to show (eg 0.0123 with .2gs will print 0.012)
121 // c -- character (char, short or int)
122 // s -- string (char *)
124 // w -- blank space; arg determines width
125 // To get literal curly braces, use {{ or }}.
126 static GooString
*format(const char *fmt
, ...) GOOSTRING_FORMAT
;
127 static GooString
*formatv(const char *fmt
, va_list argList
);
133 int getLength() const { return length
; }
136 char *getCString() { return s
; }
137 const char *getCString() const { return s
; }
139 // Get <i>th character.
140 char getChar(int i
) const { return s
[i
]; }
142 // Change <i>th character.
143 void setChar(int i
, char c
) { s
[i
] = c
; }
145 // Clear string to zero length.
148 // Append a character or string.
149 GooString
*append(char c
);
150 GooString
*append(GooString
*str
);
151 GooString
*append(const char *str
, int lengthA
=CALC_STRING_LEN
);
153 // Append a formatted string.
154 GooString
*appendf(const char *fmt
, ...) GOOSTRING_FORMAT
;
155 GooString
*appendfv(const char *fmt
, va_list argList
);
157 // Insert a character or string.
158 GooString
*insert(int i
, char c
);
159 GooString
*insert(int i
, GooString
*str
);
160 GooString
*insert(int i
, const char *str
, int lengthA
=CALC_STRING_LEN
);
162 // Delete a character or range of characters.
163 GooString
*del(int i
, int n
= 1);
165 // Convert string to all-upper/all-lower case.
166 GooString
*upperCase();
167 GooString
*lowerCase();
169 // Compare two strings: -1:< 0:= +1:>
170 int cmp(GooString
*str
) const;
171 int cmpN(GooString
*str
, int n
) const;
172 int cmp(const char *sA
) const;
173 int cmpN(const char *sA
, int n
) const;
175 // Return true if string ends with suffix
176 GBool
endsWith(const char *suffix
) const;
178 GBool
hasUnicodeMarker(void);
180 // Sanitizes the string so that it does
181 // not contain any ( ) < > [ ] { } / %
182 // The postscript mode also has some more strict checks
183 // The caller owns the return value
184 GooString
*sanitizedName(GBool psmode
);
187 GooString(const GooString
&other
);
188 GooString
& operator=(const GooString
&other
);
190 // You can tweak the final object size for different time/space tradeoffs.
191 // In libc malloc(), rounding is 16 so it's best to choose a value that
192 // is a multiple of 16.
194 char c
[sizeof(char*)];
199 static const int STR_FINAL_SIZE
= 32;
200 static const int STR_STATIC_SIZE
= STR_FINAL_SIZE
- sizeof(MemoryLayout
) + sizeof(char*);
202 int roundedSize(int len
);
204 char sStatic
[STR_STATIC_SIZE
];
208 void resize(int newLength
);
210 static void formatInt(long long x
, char *buf
, int bufSize
,
211 GBool zeroFill
, int width
, int base
,
212 char **p
, int *len
, GBool upperCase
= gFalse
);
214 static void formatInt(long x
, char *buf
, int bufSize
,
215 GBool zeroFill
, int width
, int base
,
216 char **p
, int *len
, GBool upperCase
= gFalse
);
219 static void formatUInt(unsigned long long x
, char *buf
, int bufSize
,
220 GBool zeroFill
, int width
, int base
,
221 char **p
, int *len
, GBool upperCase
= gFalse
);
223 static void formatUInt(Gulong x
, char *buf
, int bufSize
,
224 GBool zeroFill
, int width
, int base
,
225 char **p
, int *len
, GBool upperCase
= gFalse
);
227 static void formatDouble(double x
, char *buf
, int bufSize
, int prec
,
228 GBool trim
, char **p
, int *len
);
229 static void formatDoubleSmallAware(double x
, char *buf
, int bufSize
, int prec
,
230 GBool trim
, char **p
, int *len
);