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 memcpy(s1, s, length + 1);
50 GString::GString(const char *sA) {
58 GString::GString(const char *sA, int lengthA) {
60 resize(length = lengthA);
61 memcpy(s, sA, length * sizeof(char));
65 GString::GString(GString *str, int idx, int lengthA) {
67 resize(length = lengthA);
68 memcpy(s, str->getCString() + idx, length);
72 GString::GString(GString *str) {
74 resize(length = str->getLength());
75 memcpy(s, str->getCString(), length + 1);
78 GString::GString(GString *str1, GString *str2) {
79 int n1 = str1->getLength();
80 int n2 = str2->getLength();
83 resize(length = n1 + n2);
84 memcpy(s, str1->getCString(), n1);
85 memcpy(s + n1, str2->getCString(), n2 + 1);
88 GString *GString::fromInt(int x) {
89 char buf[24]; // enough space for 64-bit ints plus a little extra
103 while (i > 0 && y > 0) {
104 buf[--i] = '0' + y % 10;
111 return new GString(buf + i, 24 - i);
114 GString::~GString() {
118 GString *GString::clear() {
119 s[length = 0] = '\0';
124 GString *GString::append(char c) {
131 GString *GString::append(GString *str) {
132 int n = str->getLength();
135 memcpy(s + length, str->getCString(), n + 1);
140 GString *GString::append(const char *str) {
144 memcpy(s + length, str, n + 1);
149 GString *GString::append(const char *str, int lengthA) {
150 resize(length + lengthA);
151 memcpy(s + length, str, lengthA);
157 GString *GString::insert(int i, char c) {
161 for (j = length + 1; j > i; --j)
168 GString *GString::insert(int i, GString *str) {
169 int n = str->getLength();
173 for (j = length; j >= i; --j)
175 memcpy(s+i, str->getCString(), n);
180 GString *GString::insert(int i, const char *str) {
185 for (j = length; j >= i; --j)
192 GString *GString::insert(int i, const char *str, int lengthA) {
195 resize(length + lengthA);
196 for (j = length; j >= i; --j)
198 memcpy(s+i, str, lengthA);
203 GString *GString::del(int i, int n) {
207 if (i + n > length) {
210 for (j = i; j <= length - n; ++j) {
218 GString *GString::upperCase() {
221 for (i = 0; i < length; ++i) {
223 s[i] = toupper(s[i]);
228 GString *GString::lowerCase() {
231 for (i = 0; i < length; ++i) {
233 s[i] = tolower(s[i]);