1 //========================================================================
2 //
3 // GString.h
4 //
5 // Simple variable-length string type.
6 //
7 // Copyright 1996-2003 Glyph & Cog, LLC
8 //
9 //========================================================================
10 
11 #ifndef GSTRING_H
12 #define GSTRING_H
13 
14 #include <aconf.h>
15 
16 #ifdef USE_GCC_PRAGMAS
17 #pragma interface
18 #endif
19 
20 #include <limits.h> // for LLONG_MAX and ULLONG_MAX
21 #include <stdarg.h>
22 #include "gtypes.h"
23 
24 class GString {
25 public:
26 
27   // Create an empty string.
28   GString();
29 
30   // Create a string from a C string.
31   GString(const char *sA);
32 
33   // Create a string from <lengthA> chars at <sA>.  This string
34   // can contain null characters.
35   GString(const char *sA, int lengthA);
36 
37   // Create a string from <lengthA> chars at <idx> in <str>.
38   GString(GString *str, int idx, int lengthA);
39 
40   // Copy a string.
41   GString(GString *str);
copy()42   GString *copy() { return new GString(this); }
43 
44   // Concatenate two strings.
45   GString(GString *str1, GString *str2);
46 
47   // Convert an integer to a string.
48   static GString *fromInt(int x);
49 
50   // Create a formatted string.  Similar to printf, but without the
51   // string overflow issues.  Formatting elements consist of:
52   //     {<arg>:[<width>][.<precision>]<type>}
53   // where:
54   // - <arg> is the argument number (arg 0 is the first argument
55   //   following the format string) -- NB: args must be first used in
56   //   order; they can be reused in any order
57   // - <width> is the field width -- negative to reverse the alignment;
58   //   starting with a leading zero to zero-fill (for integers)
59   // - <precision> is the number of digits to the right of the decimal
60   //   point (for floating point numbers)
61   // - <type> is one of:
62   //     d, x, o, b -- int in decimal, hex, octal, binary
63   //     ud, ux, uo, ub -- unsigned int
64   //     ld, lx, lo, lb, uld, ulx, ulo, ulb -- long, unsigned long
65   //     lld, llx, llo, llb, ulld, ullx, ullo, ullb
66   //         -- long long, unsigned long long
67   //     f, g -- double
68   //     c -- char
69   //     s -- string (char *)
70   //     t -- GString *
71   //     w -- blank space; arg determines width
72   // To get literal curly braces, use {{ or }}.
73   static GString *format(const char *fmt, ...);
74   static GString *formatv(const char *fmt, va_list argList);
75 
76   // Destructor.
77   ~GString();
78 
79   // Get length.
getLength()80   int getLength() { return length; }
81 
82   // Get C string.
getCString()83   char *getCString() { return s; }
84 
85   // Get <i>th character.
getChar(int i)86   char getChar(int i) { return s[i]; }
87 
88   // Change <i>th character.
setChar(int i,char c)89   void setChar(int i, char c) { s[i] = c; }
90 
91   // Clear string to zero length.
92   GString *clear();
93 
94   // Append a character or string.
95   GString *append(char c);
96   GString *append(GString *str);
97   GString *append(const char *str);
98   GString *append(const char *str, int lengthA);
99 
100   // Append a formatted string.
101   GString *appendf(const char *fmt, ...);
102   GString *appendfv(const char *fmt, va_list argList);
103 
104   // Insert a character or string.
105   GString *insert(int i, char c);
106   GString *insert(int i, GString *str);
107   GString *insert(int i, const char *str);
108   GString *insert(int i, const char *str, int lengthA);
109 
110   // Delete a character or range of characters.
111   GString *del(int i, int n = 1);
112 
113   // Convert string to all-upper/all-lower case.
114   GString *upperCase();
115   GString *lowerCase();
116 
117   // Compare two strings:  -1:<  0:=  +1:>
118   int cmp(GString *str);
119   int cmpN(GString *str, int n);
120   int cmp(const char *sA);
121   int cmpN(const char *sA, int n);
122 
123 private:
124 
125   int length;
126   char *s;
127 
128   void resize(int length1);
129 #ifdef LLONG_MAX
130   static void formatInt(long long x, char *buf, int bufSize,
131 			GBool zeroFill, int width, int base,
132 			const char **p, int *len);
133 #else
134   static void formatInt(long x, char *buf, int bufSize,
135 			GBool zeroFill, int width, int base,
136 			const char **p, int *len);
137 #endif
138 #ifdef ULLONG_MAX
139   static void formatUInt(unsigned long long x, char *buf, int bufSize,
140 			 GBool zeroFill, int width, int base,
141 			 const char **p, int *len);
142 #else
143   static void formatUInt(Gulong x, char *buf, int bufSize,
144 			 GBool zeroFill, int width, int base,
145 			 const char **p, int *len);
146 #endif
147   static void formatDouble(double x, char *buf, int bufSize, int prec,
148 			   GBool trim, const char **p, int *len);
149 };
150 
151 #endif
152