1 //========================================================================
5 // Simple variable-length string type.
7 // Copyright 1996-2002 Glyph & Cog, LLC
9 //========================================================================
12 #pragma implementation
23 static inline int size(int len) {
26 delta = len < 256 ? 7 : 255;
27 return ((len + 1) + delta) & ~delta;
30 inline void GString::resize(int length1) {
34 s = new char[size(length1)];
35 } else if (size(length1) != size(length)) {
36 s1 = new char[size(length1)];
37 memcpy(s1, s, length + 1);
49 GString::GString(const char *sA) {
57 GString::GString(const char *sA, int lengthA) {
59 resize(length = lengthA);
60 memcpy(s, sA, length * sizeof(char));
64 GString::GString(GString *str, int idx, int lengthA) {
66 resize(length = lengthA);
67 memcpy(s, str->getCString() + idx, length);
71 GString::GString(GString *str) {
73 resize(length = str->getLength());
74 memcpy(s, str->getCString(), length + 1);
77 GString::GString(GString *str1, GString *str2) {
78 int n1 = str1->getLength();
79 int n2 = str2->getLength();
82 resize(length = n1 + n2);
83 memcpy(s, str1->getCString(), n1);
84 memcpy(s + n1, str2->getCString(), n2 + 1);
87 GString *GString::fromInt(int x) {
88 char buf[24]; // enough space for 64-bit ints plus a little extra
102 while (i > 0 && y > 0) {
103 buf[--i] = '0' + y % 10;
110 return new GString(buf + i, 24 - i);
113 GString::~GString() {
117 GString *GString::clear() {
118 s[length = 0] = '\0';
123 GString *GString::append(char c) {
130 GString *GString::append(GString *str) {
131 int n = str->getLength();
134 memcpy(s + length, str->getCString(), n + 1);
139 GString *GString::append(const char *str) {
143 memcpy(s + length, str, n + 1);
148 GString *GString::append(const char *str, int lengthA) {
149 resize(length + lengthA);
150 memcpy(s + length, str, lengthA);
156 GString *GString::insert(int i, char c) {
160 for (j = length + 1; j > i; --j)
167 GString *GString::insert(int i, GString *str) {
168 int n = str->getLength();
172 for (j = length; j >= i; --j)
174 memcpy(s+i, str->getCString(), n);
179 GString *GString::insert(int i, const char *str) {
184 for (j = length; j >= i; --j)
191 GString *GString::insert(int i, const char *str, int lengthA) {
194 resize(length + lengthA);
195 for (j = length; j >= i; --j)
197 memcpy(s+i, str, lengthA);
202 GString *GString::del(int i, int n) {
206 for (j = i; j <= length - n; ++j)
213 GString *GString::upperCase() {
216 for (i = 0; i < length; ++i) {
218 s[i] = toupper(s[i]);
223 GString *GString::lowerCase() {
226 for (i = 0; i < length; ++i) {
228 s[i] = tolower(s[i]);