filters

GString.cc

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