1 //========================================================================
5 // Simple variable-length string type.
7 // Copyright 1996-2003 Glyph & Cog, LLC
9 //========================================================================
13 #ifdef USE_GCC_PRAGMAS
14 #pragma implementation
24 static inline int size(int len) {
27 delta = len < 256 ? 7 : 255;
28 return ((len + 1) + delta) & ~delta;
31 inline void GString::resize(int length1) {
35 s = new char[size(length1)];
36 } else if (size(length1) != size(length)) {
37 s1 = new char[size(length1)];
38 if (length1 < length) {
39 memcpy(s1, s, length1);
42 memcpy(s1, s, length + 1);
55 GString::GString(const char *sA) {
63 GString::GString(const char *sA, int lengthA) {
65 resize(length = lengthA);
66 memcpy(s, sA, length * sizeof(char));
70 GString::GString(GString *str, int idx, int lengthA) {
72 resize(length = lengthA);
73 memcpy(s, str->getCString() + idx, length);
77 GString::GString(GString *str) {
79 resize(length = str->getLength());
80 memcpy(s, str->getCString(), length + 1);
83 GString::GString(GString *str1, GString *str2) {
84 int n1 = str1->getLength();
85 int n2 = str2->getLength();
88 resize(length = n1 + n2);
89 memcpy(s, str1->getCString(), n1);
90 memcpy(s + n1, str2->getCString(), n2 + 1);
93 GString *GString::fromInt(int x) {
94 char buf[24]; // enough space for 64-bit ints plus a little extra
108 while (i > 0 && y > 0) {
109 buf[--i] = '0' + y % 10;
116 return new GString(buf + i, 24 - i);
119 GString::~GString() {
123 GString *GString::clear() {
124 s[length = 0] = '\0';
129 GString *GString::append(char c) {
136 GString *GString::append(GString *str) {
137 int n = str->getLength();
140 memcpy(s + length, str->getCString(), n + 1);
145 GString *GString::append(const char *str) {
149 memcpy(s + length, str, n + 1);
154 GString *GString::append(const char *str, int lengthA) {
155 resize(length + lengthA);
156 memcpy(s + length, str, lengthA);
162 GString *GString::insert(int i, char c) {
166 for (j = length + 1; j > i; --j)
173 GString *GString::insert(int i, GString *str) {
174 int n = str->getLength();
178 for (j = length; j >= i; --j)
180 memcpy(s+i, str->getCString(), n);
185 GString *GString::insert(int i, const char *str) {
190 for (j = length; j >= i; --j)
197 GString *GString::insert(int i, const char *str, int lengthA) {
200 resize(length + lengthA);
201 for (j = length; j >= i; --j)
203 memcpy(s+i, str, lengthA);
208 GString *GString::del(int i, int n) {
212 if (i + n > length) {
215 for (j = i; j <= length - n; ++j) {
223 GString *GString::upperCase() {
226 for (i = 0; i < length; ++i) {
228 s[i] = toupper(s[i]);
233 GString *GString::lowerCase() {
236 for (i = 0; i < length; ++i) {
238 s[i] = tolower(s[i]);
243 int GString::cmp(GString *str) {
249 for (i = 0, p1 = s, p2 = str->s; i < n1 && i < n2; ++i, ++p1, ++p2) {
258 int GString::cmpN(GString *str, int n) {
264 for (i = 0, p1 = s, p2 = str->s;
265 i < n1 && i < n2 && i < n;
278 int GString::cmp(const char *sA) {
283 for (i = 0, p1 = s, p2 = sA; i < n1 && *p2; ++i, ++p1, ++p2) {
298 int GString::cmpN(const char *sA, int n) {
303 for (i = 0, p1 = s, p2 = sA; i < n1 && *p2 && i < n; ++i, ++p1, ++p2) {