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>
24 // To see a description of the changes please see the Changelog file that
25 // came with your tarball or type make ChangeLog if you are building from git
27 //========================================================================
32 #ifdef USE_GCC_PRAGMAS
36 #include <limits.h> // for LLONG_MAX and ULLONG_MAX
38 /* <limits.h> and/or the compiler may or may not define these. */
39 /* Minimum and maximum values a `signed long long int' can hold. */
41 # define LLONG_MAX 9223372036854775807LL
44 # define LLONG_MIN (-LLONG_MAX - 1LL)
47 /* Maximum value an `unsigned long long int' can hold. (Minimum is 0.) */
49 # define ULLONG_MAX 18446744073709551615ULL
53 #include <stdlib.h> // for NULL
57 # define GOOSTRING_FORMAT __attribute__((__annotate__("gooformat")))
59 # define GOOSTRING_FORMAT
65 // a special value telling that the length of the string is not given
66 // so it must be calculated from the strings
67 static const int CALC_STRING_LEN
= -1;
69 // Create an empty string.
72 // Create a string from a C string.
73 explicit GooString(const char *sA
);
75 // Create a string from <lengthA> chars at <sA>. This string
76 // can contain null characters.
77 GooString(const char *sA
, int lengthA
);
79 // Create a string from <lengthA> chars at <idx> in <str>.
80 GooString(GooString
*str
, int idx
, int lengthA
);
82 // Set content of a string to concatination of <s1> and <s2>. They can both
83 // be NULL. if <s1Len> or <s2Len> is CALC_STRING_LEN, then length of the string
84 // will be calculated with strlen(). Otherwise we assume they are a valid
85 // length of string (or its substring)
86 GooString
* Set(const char *s1
, int s1Len
=CALC_STRING_LEN
, const char *s2
=NULL
, int s2Len
=CALC_STRING_LEN
);
89 explicit GooString(const GooString
*str
);
90 GooString
*copy() const { return new GooString(this); }
92 // Concatenate two strings.
93 GooString(GooString
*str1
, GooString
*str2
);
95 // Convert an integer to a string.
96 static GooString
*fromInt(int x
);
98 // Create a formatted string. Similar to printf, but without the
99 // string overflow issues. Formatting elements consist of:
100 // {<arg>:[<width>][.<precision>]<type>}
102 // - <arg> is the argument number (arg 0 is the first argument
103 // following the format string) -- NB: args must be first used in
104 // order; they can be reused in any order
105 // - <width> is the field width -- negative to reverse the alignment;
106 // starting with a leading zero to zero-fill (for integers)
107 // - <precision> is the number of digits to the right of the decimal
108 // point (for floating point numbers)
109 // - <type> is one of:
110 // d, x, X, o, b -- int in decimal, lowercase hex, uppercase hex, octal, binary
111 // ud, ux, uX, uo, ub -- unsigned int
112 // ld, lx, lX, lo, lb, uld, ulx, ulX, ulo, ulb -- long, unsigned long
113 // lld, llx, llX, llo, llb, ulld, ullx, ullX, ullo, ullb
114 // -- long long, unsigned long long
115 // f, g, gs -- floating point (float or double)
116 // f -- always prints trailing zeros (eg 1.0 with .2f will print 1.00)
117 // g -- omits trailing zeros and, if possible, the dot (eg 1.0 shows up as 1)
118 // gs -- is like g, but treats <precision> as number of significant
119 // digits to show (eg 0.0123 with .2gs will print 0.012)
120 // c -- character (char, short or int)
121 // s -- string (char *)
123 // w -- blank space; arg determines width
124 // To get literal curly braces, use {{ or }}.
125 static GooString
*format(const char *fmt
, ...) GOOSTRING_FORMAT
;
126 static GooString
*formatv(const char *fmt
, va_list argList
);
132 int getLength() { return length
; }
135 char *getCString() const { return s
; }
137 // Get <i>th character.
138 char getChar(int i
) { return s
[i
]; }
140 // Change <i>th character.
141 void setChar(int i
, char c
) { s
[i
] = c
; }
143 // Clear string to zero length.
146 // Append a character or string.
147 GooString
*append(char c
);
148 GooString
*append(GooString
*str
);
149 GooString
*append(const char *str
, int lengthA
=CALC_STRING_LEN
);
151 // Append a formatted string.
152 GooString
*appendf(const char *fmt
, ...) GOOSTRING_FORMAT
;
153 GooString
*appendfv(const char *fmt
, va_list argList
);
155 // Insert a character or string.
156 GooString
*insert(int i
, char c
);
157 GooString
*insert(int i
, GooString
*str
);
158 GooString
*insert(int i
, const char *str
, int lengthA
=CALC_STRING_LEN
);
160 // Delete a character or range of characters.
161 GooString
*del(int i
, int n
= 1);
163 // Convert string to all-upper/all-lower case.
164 GooString
*upperCase();
165 GooString
*lowerCase();
167 // Compare two strings: -1:< 0:= +1:>
168 int cmp(GooString
*str
) const;
169 int cmpN(GooString
*str
, int n
) const;
170 int cmp(const char *sA
) const;
171 int cmpN(const char *sA
, int n
) const;
173 // Return true if string ends with suffix
174 GBool
endsWith(const char *suffix
) const;
176 GBool
hasUnicodeMarker(void);
178 // Sanitizes the string so that it does
179 // not contain any ( ) < > [ ] { } / %
180 // The postscript mode also has some more strict checks
181 // The caller owns the return value
182 GooString
*sanitizedName(GBool psmode
);
185 GooString(const GooString
&other
);
186 GooString
& operator=(const GooString
&other
);
188 // you can tweak this number for a different speed/memory usage tradeoffs.
189 // In libc malloc() rounding is 16 so it's best to choose a value that
190 // results in sizeof(GooString) be a multiple of 16.
191 // 24 makes sizeof(GooString) to be 32.
192 static const int STR_STATIC_SIZE
= 24;
194 int roundedSize(int len
);
196 char sStatic
[STR_STATIC_SIZE
];
200 void resize(int newLength
);
202 static void formatInt(long long x
, char *buf
, int bufSize
,
203 GBool zeroFill
, int width
, int base
,
204 char **p
, int *len
, GBool upperCase
= gFalse
);
206 static void formatInt(long x
, char *buf
, int bufSize
,
207 GBool zeroFill
, int width
, int base
,
208 char **p
, int *len
, GBool upperCase
= gFalse
);
211 static void formatUInt(unsigned long long x
, char *buf
, int bufSize
,
212 GBool zeroFill
, int width
, int base
,
213 char **p
, int *len
, GBool upperCase
= gFalse
);
215 static void formatUInt(Gulong x
, char *buf
, int bufSize
,
216 GBool zeroFill
, int width
, int base
,
217 char **p
, int *len
, GBool upperCase
= gFalse
);
219 static void formatDouble(double x
, char *buf
, int bufSize
, int prec
,
220 GBool trim
, char **p
, int *len
);
221 static void formatDoubleSmallAware(double x
, char *buf
, int bufSize
, int prec
,
222 GBool trim
, char **p
, int *len
);