00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifdef __GNUC__
00012 #pragma implementation
00013 #endif
00014
00015 #include <aconf.h>
00016 #include <stdlib.h>
00017 #include <stddef.h>
00018 #include <string.h>
00019 #include <ctype.h>
00020 #include "gtypes.h"
00021 #include "GString.h"
00022
00023 static inline int size(int len) {
00024 int delta;
00025
00026 delta = len < 256 ? 7 : 255;
00027 return ((len + 1) + delta) & ~delta;
00028 }
00029
00030 inline void GString::resize(int length1) {
00031 char *s1;
00032
00033 if (!s) {
00034 s = new char[size(length1)];
00035 } else if (size(length1) != size(length)) {
00036 s1 = new char[size(length1)];
00037 memcpy(s1, s, length + 1);
00038 delete[] s;
00039 s = s1;
00040 }
00041 }
00042
00043 GString::GString() {
00044 s = NULL;
00045 resize(length = 0);
00046 s[0] = '\0';
00047 }
00048
00049 GString::GString(const char *sA) {
00050 int n = strlen(sA);
00051
00052 s = NULL;
00053 resize(length = n);
00054 memcpy(s, sA, n + 1);
00055 }
00056
00057 GString::GString(const char *sA, int lengthA) {
00058 s = NULL;
00059 resize(length = lengthA);
00060 memcpy(s, sA, length * sizeof(char));
00061 s[length] = '\0';
00062 }
00063
00064 GString::GString(GString *str, int idx, int lengthA) {
00065 s = NULL;
00066 resize(length = lengthA);
00067 memcpy(s, str->getCString() + idx, length);
00068 s[length] = '\0';
00069 }
00070
00071 GString::GString(GString *str) {
00072 s = NULL;
00073 resize(length = str->getLength());
00074 memcpy(s, str->getCString(), length + 1);
00075 }
00076
00077 GString::GString(GString *str1, GString *str2) {
00078 int n1 = str1->getLength();
00079 int n2 = str2->getLength();
00080
00081 s = NULL;
00082 resize(length = n1 + n2);
00083 memcpy(s, str1->getCString(), n1);
00084 memcpy(s + n1, str2->getCString(), n2 + 1);
00085 }
00086
00087 GString *GString::fromInt(int x) {
00088 char buf[24];
00089 GBool neg;
00090 Guint y;
00091 int i;
00092
00093 i = 24;
00094 if (x == 0) {
00095 buf[--i] = '0';
00096 } else {
00097 if ((neg = x < 0)) {
00098 y = (Guint)-x;
00099 } else {
00100 y = (Guint)x;
00101 }
00102 while (i > 0 && y > 0) {
00103 buf[--i] = '0' + y % 10;
00104 y /= 10;
00105 }
00106 if (neg && i > 0) {
00107 buf[--i] = '-';
00108 }
00109 }
00110 return new GString(buf + i, 24 - i);
00111 }
00112
00113 GString::~GString() {
00114 delete[] s;
00115 }
00116
00117 GString *GString::clear() {
00118 s[length = 0] = '\0';
00119 resize(0);
00120 return this;
00121 }
00122
00123 GString *GString::append(char c) {
00124 resize(length + 1);
00125 s[length++] = c;
00126 s[length] = '\0';
00127 return this;
00128 }
00129
00130 GString *GString::append(GString *str) {
00131 int n = str->getLength();
00132
00133 resize(length + n);
00134 memcpy(s + length, str->getCString(), n + 1);
00135 length += n;
00136 return this;
00137 }
00138
00139 GString *GString::append(const char *str) {
00140 int n = strlen(str);
00141
00142 resize(length + n);
00143 memcpy(s + length, str, n + 1);
00144 length += n;
00145 return this;
00146 }
00147
00148 GString *GString::append(const char *str, int lengthA) {
00149 resize(length + lengthA);
00150 memcpy(s + length, str, lengthA);
00151 length += lengthA;
00152 s[length] = '\0';
00153 return this;
00154 }
00155
00156 GString *GString::insert(int i, char c) {
00157 int j;
00158
00159 resize(length + 1);
00160 for (j = length + 1; j > i; --j)
00161 s[j] = s[j-1];
00162 s[i] = c;
00163 ++length;
00164 return this;
00165 }
00166
00167 GString *GString::insert(int i, GString *str) {
00168 int n = str->getLength();
00169 int j;
00170
00171 resize(length + n);
00172 for (j = length; j >= i; --j)
00173 s[j+n] = s[j];
00174 memcpy(s+i, str->getCString(), n);
00175 length += n;
00176 return this;
00177 }
00178
00179 GString *GString::insert(int i, const char *str) {
00180 int n = strlen(str);
00181 int j;
00182
00183 resize(length + n);
00184 for (j = length; j >= i; --j)
00185 s[j+n] = s[j];
00186 memcpy(s+i, str, n);
00187 length += n;
00188 return this;
00189 }
00190
00191 GString *GString::insert(int i, const char *str, int lengthA) {
00192 int j;
00193
00194 resize(length + lengthA);
00195 for (j = length; j >= i; --j)
00196 s[j+lengthA] = s[j];
00197 memcpy(s+i, str, lengthA);
00198 length += lengthA;
00199 return this;
00200 }
00201
00202 GString *GString::del(int i, int n) {
00203 int j;
00204
00205 if (n > 0) {
00206 for (j = i; j <= length - n; ++j)
00207 s[j] = s[j + n];
00208 resize(length -= n);
00209 }
00210 return this;
00211 }
00212
00213 GString *GString::upperCase() {
00214 int i;
00215
00216 for (i = 0; i < length; ++i) {
00217 if (islower(s[i]))
00218 s[i] = toupper(s[i]);
00219 }
00220 return this;
00221 }
00222
00223 GString *GString::lowerCase() {
00224 int i;
00225
00226 for (i = 0; i < length; ++i) {
00227 if (isupper(s[i]))
00228 s[i] = tolower(s[i]);
00229 }
00230 return this;
00231 }