1 //========================================================================
5 // Simple variable-length string type.
7 // Copyright 1996 Derek B. Noonburg
9 //========================================================================
12 #pragma implementation
22 static inline int size(int len) {
25 delta = len < 256 ? 7 : 255;
26 return ((len + 1) + delta) & ~delta;
29 inline void GString::resize(int length1) {
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);
48 GString::GString(const char *s1) {
56 GString::GString(const char *s1, int length1) {
58 resize(length = length1);
59 memcpy(s, s1, length * sizeof(char));
63 GString::GString(GString *str) {
65 resize(length = str->getLength());
66 memcpy(s, str->getCString(), length + 1);
69 GString::GString(GString *str1, GString *str2) {
70 int n1 = str1->getLength();
71 int n2 = str2->getLength();
74 resize(length = n1 + n2);
75 memcpy(s, str1->getCString(), n1);
76 memcpy(s + n1, str2->getCString(), n2 + 1);
79 GString *GString::fromInt(int x) {
80 char buf[24]; // enough space for 64-bit ints plus a little extra
94 while (i > 0 && y > 0) {
95 buf[--i] = '0' + y % 10;
102 return new GString(buf + i, 24 - i);
105 GString::~GString() {
109 GString *GString::clear() {
110 s[length = 0] = '\0';
115 GString *GString::append(char c) {
122 GString *GString::append(GString *str) {
123 int n = str->getLength();
126 memcpy(s + length, str->getCString(), n + 1);
131 GString *GString::append(const char *str) {
135 memcpy(s + length, str, n + 1);
140 GString *GString::append(const char *str, int length1) {
141 resize(length + length1);
142 memcpy(s + length, str, length1);
148 GString *GString::insert(int i, char c) {
152 for (j = length + 1; j > i; --j)
159 GString *GString::insert(int i, GString *str) {
160 int n = str->getLength();
164 for (j = length; j >= i; --j)
166 memcpy(s+i, str->getCString(), n);
171 GString *GString::insert(int i, const char *str) {
176 for (j = length; j >= i; --j)
183 GString *GString::insert(int i, const char *str, int length1) {
186 resize(length + length1);
187 for (j = length; j >= i; --j)
189 memcpy(s+i, str, length1);
194 GString *GString::del(int i, int n) {
198 for (j = i; j <= length - n; ++j)
205 GString *GString::upperCase() {
208 for (i = 0; i < length; ++i) {
210 s[i] = toupper(s[i]);
215 GString *GString::lowerCase() {
218 for (i = 0; i < length; ++i) {
220 s[i] = tolower(s[i]);