Back to index

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