Initial revision
[swftools.git] / pdf2swf / xpdf / GString.cc
1 //========================================================================
2 //
3 // GString.cc
4 //
5 // Simple variable-length string type.
6 //
7 // Copyright 1996 Derek B. Noonburg
8 //
9 //========================================================================
10
11 #ifdef __GNUC__
12 #pragma implementation
13 #endif
14
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include "gtypes.h"
20 #include "GString.h"
21
22 static inline int size(int len) {
23   int delta;
24
25   delta = len < 256 ? 7 : 255;
26   return ((len + 1) + delta) & ~delta;
27 }
28
29 inline void GString::resize(int length1) {
30   char *s1;
31
32   if (!s) {
33     s = new char[size(length1)];
34   } else if (size(length1) != size(length)) {
35     s1 = new char[size(length1)];
36     memcpy(s1, s, length + 1);
37     delete[] s;
38     s = s1;
39   }
40 }
41
42 GString::GString() {
43   s = NULL;
44   resize(length = 0);
45   s[0] = '\0';
46 }
47
48 GString::GString(const char *s1) {
49   int n = strlen(s1);
50
51   s = NULL;
52   resize(length = n);
53   memcpy(s, s1, n + 1);
54 }
55
56 GString::GString(const char *s1, int length1) {
57   s = NULL;
58   resize(length = length1);
59   memcpy(s, s1, length * sizeof(char));
60   s[length] = '\0';
61 }
62
63 GString::GString(GString *str) {
64   s = NULL;
65   resize(length = str->getLength());
66   memcpy(s, str->getCString(), length + 1);
67 }
68
69 GString::GString(GString *str1, GString *str2) {
70   int n1 = str1->getLength();
71   int n2 = str2->getLength();
72
73   s = NULL;
74   resize(length = n1 + n2);
75   memcpy(s, str1->getCString(), n1);
76   memcpy(s + n1, str2->getCString(), n2 + 1);
77 }
78
79 GString *GString::fromInt(int x) {
80   char buf[24]; // enough space for 64-bit ints plus a little extra
81   GBool neg;
82   Guint y;
83   int i;
84
85   i = 24;
86   if (x == 0) {
87     buf[--i] = '0';
88   } else {
89     if ((neg = x < 0)) {
90       y = (Guint)-x;
91     } else {
92       y = (Guint)x;
93     }
94     while (i > 0 && y > 0) {
95       buf[--i] = '0' + y % 10;
96       y /= 10;
97     }
98     if (neg && i > 0) {
99       buf[--i] = '-';
100     }
101   }
102   return new GString(buf + i, 24 - i);
103 }
104
105 GString::~GString() {
106   delete[] s;
107 }
108
109 GString *GString::clear() {
110   s[length = 0] = '\0';
111   resize(0);
112   return this;
113 }
114
115 GString *GString::append(char c) {
116   resize(length + 1);
117   s[length++] = c;
118   s[length] = '\0';
119   return this;
120 }
121
122 GString *GString::append(GString *str) {
123   int n = str->getLength();
124
125   resize(length + n);
126   memcpy(s + length, str->getCString(), n + 1);
127   length += n;
128   return this;
129 }
130
131 GString *GString::append(const char *str) {
132   int n = strlen(str);
133
134   resize(length + n);
135   memcpy(s + length, str, n + 1);
136   length += n;
137   return this;
138 }
139
140 GString *GString::append(const char *str, int length1) {
141   resize(length + length1);
142   memcpy(s + length, str, length1);
143   length += length1;
144   s[length] = '\0';
145   return this;
146 }
147
148 GString *GString::insert(int i, char c) {
149   int j;
150
151   resize(length + 1);
152   for (j = length + 1; j > i; --j)
153     s[j] = s[j-1];
154   s[i] = c;
155   ++length;
156   return this;
157 }
158
159 GString *GString::insert(int i, GString *str) {
160   int n = str->getLength();
161   int j;
162
163   resize(length + n);
164   for (j = length; j >= i; --j)
165     s[j+n] = s[j];
166   memcpy(s+i, str->getCString(), n);
167   length += n;
168   return this;
169 }
170
171 GString *GString::insert(int i, const char *str) {
172   int n = strlen(str);
173   int j;
174
175   resize(length + n);
176   for (j = length; j >= i; --j)
177     s[j+n] = s[j];
178   memcpy(s+i, str, n);
179   length += n;
180   return this;
181 }
182
183 GString *GString::insert(int i, const char *str, int length1) {
184   int j;
185
186   resize(length + length1);
187   for (j = length; j >= i; --j)
188     s[j+length1] = s[j];
189   memcpy(s+i, str, length1);
190   length += length1;
191   return this;
192 }
193
194 GString *GString::del(int i, int n) {
195   int j;
196
197   if (n > 0) {
198     for (j = i; j <= length - n; ++j)
199       s[j] = s[j + n];
200     resize(length -= n);
201   }
202   return this;
203 }
204
205 GString *GString::upperCase() {
206   int i;
207
208   for (i = 0; i < length; ++i) {
209     if (islower(s[i]))
210       s[i] = toupper(s[i]);
211   }
212   return this;
213 }
214
215 GString *GString::lowerCase() {
216   int i;
217
218   for (i = 0; i < length; ++i) {
219     if (isupper(s[i]))
220       s[i] = tolower(s[i]);
221   }
222   return this;
223 }