Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Beta/GString.cc File Reference

#include <String.h>
#include <std.h>
#include <ctype.h>
#include <limits.h>
#include <new.h>
#include <builtin.h>

Go to the source code of this file.

Defines

#define MAXStrRep_SIZE   ((1 << (sizeof(short) * CHAR_BIT - 1)) - 1)
#define MINStrRep_SIZE   16
#define MALLOC_MIN_OVERHEAD   4
#define RETURN(r)   return r
#define RETURNS(r)
#define RETURN_OBJECT(TYPE, NAME)   TYPE NAME;

Functions

void ncopy (const char *from, char *to, int n)
void ncopy0 (const char *from, char *to, int n)
void scopy (const char *from, char *to)
void revcopy (const char *from, char *to, short n)
int slen (const char *t)
StrRep * Snew (int newsiz)
StrRep * Salloc (StrRep *old, const char *src, int srclen, int newlen)
StrRep * Sresize (StrRep *old, int newlen)
StrRep * Scopy (StrRep *old, const StrRep *s)
StrRep * Scat (StrRep *old, const char *s, int srclen, const char *t, int tlen)
StrRep * Scat (StrRep *old, const char *s, int srclen, const char *t, int tlen, const char *u, int ulen)
StrRep * Sprepend (StrRep *old, const char *t, int tlen)
int scmp (const char *a, const char *b)
int ncmp (const char *a, int al, const char *b, int bl)
int fcompare (const String &x, const String &y)
int compare (const String &x, const char *b)
int compare (const String &x, const String &y)
int compare (const String &x, const SubString &y)
int compare (const SubString &x, const String &y)
int compare (const SubString &x, const SubString &y)
int compare (const SubString &x, const char *b)
int split (const String &src, String results[], int n, const String &sep)
int split (const String &src, String results[], int n, const Regex &r)
String join (String src[], int n, const String &separator)
StrRep * Sreverse (const StrRep *src, StrRep *dest)
StrRep * Supcase (const StrRep *src, StrRep *dest)
StrRep * Sdowncase (const StrRep *src, StrRep *dest)
StrRep * Scapitalize (const StrRep *src, StrRep *dest)
String replicate (char c, int n)
String replicate (const String &y, int n)
String common_prefix (const String &x, const String &y, int startpos)
String common_suffix (const String &x, const String &y, int startpos)
istream & operator>> (istream &s, String &x)
int readline (istream &s, String &x, char terminator, int discard)
ostream & operator<< (ostream &s, const SubString &x)

Variables

StrRep _nilStrRep = { 0, 1, { 0 } }
String _nilString


Define Documentation

#define MALLOC_MIN_OVERHEAD   4
 

Definition at line 114 of file GString.cc.

#define MAXStrRep_SIZE   ((1 << (sizeof(short) * CHAR_BIT - 1)) - 1)
 

Definition at line 110 of file GString.cc.

#define MINStrRep_SIZE   16
 

Definition at line 111 of file GString.cc.

#define RETURN r       return r
 

Definition at line 974 of file GString.cc.

Referenced by join().

#define RETURNS r   
 

Definition at line 975 of file GString.cc.

#define RETURN_OBJECT TYPE,
NAME       TYPE NAME;
 

Definition at line 976 of file GString.cc.

Referenced by join().


Function Documentation

StrRep* Salloc StrRep *    old,
const char *    src,
int    srclen,
int    newlen
 

Definition at line 149 of file GString.cc.

Referenced by Scapitalize(), Sdowncase(), Sreverse(), Supcase(), common_prefix(), common_suffix(), String::operator=(), and split().

00150 {
00151   if (old == &_nilStrRep) old = 0;
00152   if (srclen < 0) srclen = slen(src);
00153   if (newlen < srclen) newlen = srclen;
00154   StrRep* rep;
00155   if (old == 0 || newlen > old->sz)
00156     rep = Snew(newlen);
00157   else
00158     rep = old;
00159 
00160   rep->len = newlen;
00161   ncopy0(src, rep->s, srclen);
00162 
00163   if (old != rep && old != 0) delete old;
00164 
00165   return rep;
00166 }

StrRep* Scapitalize const StrRep *    src,
StrRep *    dest
 

Definition at line 1049 of file GString.cc.

Referenced by String::capitalize(), and capitalize().

01050 {
01051   int n = (short) src->len;
01052   if (src != dest) dest = Salloc(dest, src->s, n, n);
01053 
01054   char* p = dest->s;
01055   char* e = &(p[n]);
01056   for (; p < e; ++p)
01057   {
01058     int at_word = islower(*p);
01059 
01060     if (at_word)
01061       *p = toupper(*p);
01062     else 
01063       at_word = isupper(*p) || isdigit(*p);
01064 
01065     if (at_word)
01066     {
01067       while (++p < e)
01068       {
01069         if (isupper(*p))
01070           *p = tolower(*p);
01071    /* A '\'' does not break a word, so that "Nathan's" stays
01072       "Nathan's" rather than turning into "Nathan'S". */
01073         else if (!islower(*p) && !isdigit(*p) && (*p != '\''))
01074           break;
01075       }
01076     }
01077   }
01078   return dest;
01079 }

StrRep* Scat StrRep *    old,
const char *    s,
int    srclen,
const char *    t,
int    tlen,
const char *    u,
int    ulen
 

Definition at line 260 of file GString.cc.

Referenced by cat().

00262 {
00263   if (old == &_nilStrRep) old = 0;
00264   if (srclen < 0) srclen = slen(s);
00265   if (tlen < 0) tlen = slen(t);
00266   if (ulen < 0) ulen = slen(u);
00267   int newlen = srclen + tlen + ulen;
00268   StrRep* rep;
00269   if (old == 0 || newlen > old->sz || 
00270       (t >= old->s && t < &(old->s[old->len])) ||
00271       (u >= old->s && u < &(old->s[old->len])))
00272     rep = Snew(newlen);
00273   else
00274     rep = old;
00275 
00276   rep->len = newlen;
00277 
00278   ncopy(s, rep->s, srclen);
00279   ncopy(t, &(rep->s[srclen]), tlen);
00280   ncopy0(u, &(rep->s[srclen+tlen]), ulen);
00281 
00282   if (old != rep && old != 0) delete old;
00283 
00284   return rep;
00285 }

StrRep* Scat StrRep *    old,
const char *    s,
int    srclen,
const char *    t,
int    tlen
 

Definition at line 234 of file GString.cc.

00235 {
00236   if (old == &_nilStrRep) old = 0;
00237   if (srclen < 0) srclen = slen(s);
00238   if (tlen < 0) tlen = slen(t);
00239   int newlen = srclen + tlen;
00240   StrRep* rep;
00241 
00242   if (old == 0 || newlen > old->sz || 
00243       (t >= old->s && t < &(old->s[old->len]))) // beware of aliasing
00244     rep = Snew(newlen);
00245   else
00246     rep = old;
00247 
00248   rep->len = newlen;
00249 
00250   ncopy(s, rep->s, srclen);
00251   ncopy0(t, &(rep->s[srclen]), tlen);
00252 
00253   if (old != rep && old != 0) delete old;
00254 
00255   return rep;
00256 }

StrRep* Scopy StrRep *    old,
const StrRep *    s
 

Definition at line 203 of file GString.cc.

Referenced by String::operator=().

00204 {
00205   if (old == &_nilStrRep) old = 0;
00206   if (s == &_nilStrRep) s = 0;
00207   if (old == s) 
00208     return (old == 0)? &_nilStrRep : old;
00209   else if (s == 0)
00210   {
00211     old->s[0] = 0;
00212     old->len = 0;
00213     return old;
00214   }
00215   else 
00216   {
00217     StrRep* rep;
00218     int newlen = (short) s->len; // Beware of sign extension!
00219     if (old == 0 || newlen > old->sz)
00220     {
00221       if (old != 0) delete old;
00222       rep = Snew(newlen);
00223     }
00224     else
00225       rep = old;
00226     rep->len = newlen;
00227     ncopy0(s->s, rep->s, newlen);
00228     return rep;
00229   }
00230 }

StrRep* Sdowncase const StrRep *    src,
StrRep *    dest
 

Definition at line 1039 of file GString.cc.

Referenced by String::downcase(), and downcase().

01040 {
01041   int n = (short) src->len;
01042   if (src != dest) dest = Salloc(dest, src->s, n, n);
01043   char* p = dest->s;
01044   char* e = &(p[n]);
01045   for (; p < e; ++p) if (isupper(*p)) *p = tolower(*p);
01046   return dest;
01047 }

StrRep* Snew int    newsiz [inline, static]
 

Definition at line 125 of file GString.cc.

Referenced by Salloc(), Scat(), Scopy(), Sprepend(), and Sresize().

00126 {
00127   unsigned int siz = sizeof(StrRep) + newsiz + MALLOC_MIN_OVERHEAD;
00128   unsigned int allocsiz = MINStrRep_SIZE;
00129   while (allocsiz < siz) allocsiz <<= 1;
00130   allocsiz -= MALLOC_MIN_OVERHEAD;
00131   if (allocsiz >= MAXStrRep_SIZE)
00132     (*lib_error_handler)("String", "Requested length out of range");
00133     
00134   StrRep* rep = new (operator new (allocsiz)) StrRep;
00135   rep->sz = allocsiz - sizeof(StrRep);
00136   return rep;
00137 }

StrRep* Sprepend StrRep *    old,
const char *    t,
int    tlen
 

Definition at line 289 of file GString.cc.

Referenced by String::prepend().

00290 {
00291   char* s;
00292   int srclen;
00293   if (old == &_nilStrRep || old == 0)
00294   {
00295     s = 0; old = 0; srclen = 0;
00296   }
00297   else
00298   {
00299     s = old->s; srclen = (short) old->len;
00300   }
00301   if (tlen < 0) tlen = slen(t);
00302   int newlen = srclen + tlen;
00303   StrRep* rep;
00304   if (old == 0 || newlen > old->sz || 
00305       (t >= old->s && t < &(old->s[old->len])))
00306     rep = Snew(newlen);
00307   else
00308     rep = old;
00309 
00310   rep->len = newlen;
00311 
00312   revcopy(&(s[srclen]), &(rep->s[newlen]), srclen+1);
00313   ncopy(t, rep->s, tlen);
00314 
00315   if (old != rep && old != 0) delete old;
00316 
00317   return rep;
00318 }

StrRep* Sresize StrRep *    old,
int    newlen
[static]
 

Definition at line 173 of file GString.cc.

Referenced by String::_gsub(), String::alloc(), join(), operator>>(), readline(), and replicate().

00174 {
00175   if (old == &_nilStrRep) old = 0;
00176   StrRep* rep;
00177   if (old == 0)
00178     rep = Snew(newlen);
00179   else if (newlen > old->sz)
00180   {
00181     rep = Snew(newlen);
00182     ncopy0(old->s, rep->s, old->len);
00183     delete old;
00184   }
00185   else
00186     rep = old;
00187 
00188   rep->len = newlen;
00189 
00190   return rep;
00191 }

StrRep* Sreverse const StrRep *    src,
StrRep *    dest
 

Definition at line 1009 of file GString.cc.

Referenced by String::reverse(), and reverse().

01010 {
01011   int n = (short) src->len;
01012   if (src != dest)
01013     dest = Salloc(dest, src->s, n, n);
01014   if (n > 0)
01015   {
01016     char* a = dest->s;
01017     char* b = &(a[n - 1]);
01018     while (a < b)
01019     {
01020       char t = *a;
01021       *a++ = *b;
01022       *b-- = t;
01023     }
01024   }
01025   return dest;
01026 }

StrRep* Supcase const StrRep *    src,
StrRep *    dest
 

Definition at line 1029 of file GString.cc.

Referenced by String::upcase(), and upcase().

01030 {
01031   int n = (short) src->len;
01032   if (src != dest) dest = Salloc(dest, src->s, n, n);
01033   char* p = dest->s;
01034   char* e = &(p[n]);
01035   for (; p < e; ++p) if (islower(*p)) *p = toupper(*p);
01036   return dest;
01037 }

String common_prefix const String   x,
const String   y,
int    startpos = 0
 

Definition at line 1158 of file GString.cc.

01159 {
01160   String r;
01161   const char* xchars = x.chars();
01162   const char* ychars = y.chars();
01163   const char* xs = &(xchars[startpos]);
01164   const char* ss = xs;
01165   const char* topx = &(xchars[x.length()]);
01166   const char* ys = &(ychars[startpos]);
01167   const char* topy = &(ychars[y.length()]);
01168   int l;
01169   for (l = 0; xs < topx && ys < topy && *xs++ == *ys++; ++l);
01170   r.rep = Salloc(r.rep, ss, l, l);
01171   return r;
01172 }

String common_suffix const String   x,
const String   y,
int    startpos = -1
 

Definition at line 1174 of file GString.cc.

01175 {
01176   String r;
01177   const char* xchars = x.chars();
01178   const char* ychars = y.chars();
01179   const char* xs = &(xchars[x.length() + startpos]);
01180   const char* botx = xchars;
01181   const char* ys = &(ychars[y.length() + startpos]);
01182   const char* boty = ychars;
01183   int l;
01184   for (l = 0; xs >= botx && ys >= boty && *xs == *ys ; --xs, --ys, ++l);
01185   r.rep = Salloc(r.rep, ++xs, l, l);
01186   return r;
01187 }

int compare const SubString   x,
const char *    y
 

Definition at line 397 of file GString.cc.

00398 {
00399   if (b == 0)
00400     return x.length();
00401   else
00402   {
00403     const char* a = x.chars();
00404     int n = x.length();
00405     int diff;
00406     while (n-- > 0) if ((diff = *a++ - *b++) != 0) return diff;
00407     return (*b == 0) ? 0 : -1;
00408   }
00409 }

int compare const SubString   x,
const SubString   y
 

Definition at line 392 of file GString.cc.

00393 {
00394   return ncmp(x.chars(), x.length(), y.chars(), y.length());
00395 }

int compare const SubString   x,
const String   y
 

Definition at line 387 of file GString.cc.

00388 {
00389   return ncmp(x.chars(), x.length(), y.chars(), y.length());
00390 }

int compare const String   x,
const SubString   y
 

Definition at line 382 of file GString.cc.

00383 {
00384   return ncmp(x.chars(), x.length(), y.chars(), y.length());
00385 }

int compare const String   x,
const String   y
 

Definition at line 377 of file GString.cc.

00378 {
00379   return scmp(x.chars(), y.chars());
00380 }

int compare const String   x,
const char *    y
 

Definition at line 372 of file GString.cc.

00373 {
00374   return scmp(x.chars(), b);
00375 }

int fcompare const String   x,
const String   y
 

Definition at line 344 of file GString.cc.

00345 {
00346   const char* a = x.chars();
00347   const char* b = y.chars();
00348   int al = x.length();
00349   int bl = y.length();
00350   int n = (al <= bl)? al : bl;
00351   int diff = 0;
00352   while (n-- > 0)
00353   {
00354     char ac = *a++;
00355     char bc = *b++;
00356     if ((diff = ac - bc) != 0)
00357     {
00358       if (ac >= 'a' && ac <= 'z')
00359         ac = ac - 'a' + 'A';
00360       if (bc >= 'a' && bc <= 'z')
00361         bc = bc - 'a' + 'A';
00362       if ((diff = ac - bc) != 0)
00363         return diff;
00364     }
00365   }
00366   return al - bl;
00367 }

String join String    src[],
int    n,
const String   separator
 

Definition at line 979 of file GString.cc.

00980 {
00981   RETURN_OBJECT(String,x)
00982   String sep = separator;
00983   int xlen = 0;
00984   int i;
00985   for (i = 0; i < n; ++i)
00986     xlen += src[i].length();
00987   xlen += (n - 1) * sep.length();
00988 
00989   x.rep = Sresize (x.rep, xlen);
00990 
00991   int j = 0;
00992   
00993   for (i = 0; i < n - 1; ++i)
00994   {
00995     ncopy(src[i].chars(), &(x.rep->s[j]), src[i].length());
00996     j += src[i].length();
00997     ncopy(sep.chars(), &(x.rep->s[j]), sep.length());
00998     j += sep.length();
00999   }
01000   ncopy0(src[i].chars(), &(x.rep->s[j]), src[i].length());
01001   RETURN(x);
01002 }

int ncmp const char *    a,
int    al,
const char *    b,
int    bl
[inline, static]
 

Definition at line 336 of file GString.cc.

Referenced by compare().

00337 {
00338   int n = (al <= bl)? al : bl;
00339   int diff;
00340   while (n-- > 0) if ((diff = *a++ - *b++) != 0) return diff;
00341   return al - bl;
00342 }

void ncopy const char *    from,
char *    to,
int    n
[inline, static]
 

Definition at line 66 of file GString.cc.

Referenced by Scat(), Sprepend(), String::_gsub(), join(), and replicate().

00067 {
00068   if (from != to) while (--n >= 0) *to++ = *from++;
00069 }

void ncopy0 const char *    from,
char *    to,
int    n
[inline, static]
 

Definition at line 72 of file GString.cc.

Referenced by Salloc(), Scat(), Scopy(), Sresize(), String::_gsub(), String::del(), and join().

00073 {
00074   if (from != to) 
00075   {
00076     while (--n >= 0) *to++ = *from++;
00077     *to = 0;
00078   }
00079   else
00080     to[n] = 0;
00081 }

ostream& operator<< ostream &    s,
const SubString   x
 

Definition at line 1247 of file GString.cc.

01248 { 
01249   const char* a = x.chars();
01250   const char* lasta = &(a[x.length()]);
01251   while (a < lasta)
01252     s.put(*a++);
01253   return(s);
01254 }

istream& operator>> istream &    s,
String   x
 

Definition at line 1193 of file GString.cc.

01194 {
01195   if (!s.ipfx(0) || (!(s.flags() & ios::skipws) && !ws(s)))
01196   {
01197     s.clear(ios::failbit|s.rdstate()); // Redundant if using GNU iostreams.
01198     return s;
01199   }
01200   int ch;
01201   int i = 0;
01202   x.rep = Sresize(x.rep, 20);
01203   register streambuf *sb = s.rdbuf();
01204   while ((ch = sb->sbumpc()) != EOF)
01205   {
01206     if (isspace(ch))
01207       break;
01208     if (i >= x.rep->sz - 1)
01209       x.rep = Sresize(x.rep, i+1);
01210     x.rep->s[i++] = ch;
01211   }
01212   x.rep->s[i] = 0;
01213   x.rep->len = i;
01214   int new_state = s.rdstate();
01215   if (i == 0) new_state |= ios::failbit;
01216   if (ch == EOF) new_state |= ios::eofbit;
01217   s.clear(new_state);
01218   return s;
01219 }

int readline istream &    s,
String   x,
char    terminator = '\n',
int    discard = 1
 

Definition at line 1221 of file GString.cc.

01222 {
01223   if (!s.ipfx(0))
01224     return 0;
01225   int ch;
01226   int i = 0;
01227   x.rep = Sresize(x.rep, 80);
01228   register streambuf *sb = s.rdbuf();
01229   while ((ch = sb->sbumpc()) != EOF)
01230   {
01231     if (ch != terminator || !discard)
01232     {
01233       if (i >= x.rep->sz - 1)
01234         x.rep = Sresize(x.rep, i+1);
01235       x.rep->s[i++] = ch;
01236     }
01237     if (ch == terminator)
01238       break;
01239   }
01240   x.rep->s[i] = 0;
01241   x.rep->len = i;
01242   if (ch == EOF) s.clear(ios::eofbit|s.rdstate());
01243   return i;
01244 }

String replicate const String   y,
int    n
 

Definition at line 1143 of file GString.cc.

01144 {
01145   String w;
01146   int len = y.length();
01147   w.rep = Sresize(w.rep, n * len);
01148   char* p = w.rep->s;
01149   while (n-- > 0)
01150   {
01151     ncopy(y.chars(), p, len);
01152     p += len;
01153   }
01154   *p = 0;
01155   return w;
01156 }

String replicate char    c,
int    n
 

Definition at line 1133 of file GString.cc.

01134 {
01135   String w;
01136   w.rep = Sresize(w.rep, n);
01137   char* p = w.rep->s;
01138   while (n-- > 0) *p++ = c;
01139   *p = 0;
01140   return w;
01141 }

void revcopy const char *    from,
char *    to,
short    n
[inline, static]
 

Definition at line 90 of file GString.cc.

Referenced by Sprepend().

00091 {
00092   if (from != 0) while (--n >= 0) *to-- = *from--;
00093 }

int scmp const char *    a,
const char *    b
[inline, static]
 

Definition at line 323 of file GString.cc.

Referenced by compare().

00324 {
00325   if (b == 0)
00326     return *a != 0;
00327   else
00328   {
00329     int diff = 0;
00330     while ((diff = *a - *b++) == 0 && *a++ != 0);
00331     return diff;
00332   }
00333 }

void scopy const char *    from,
char *    to
[inline, static]
 

Definition at line 84 of file GString.cc.

00085 {
00086   if (from != 0) while((*to++ = *from++) != 0);
00087 }

int slen const char *    t [inline, static]
 

Definition at line 96 of file GString.cc.

Referenced by Salloc(), Scat(), Sprepend(), String::_gsub(), String::after(), String::at(), String::before(), String::del(), String::from(), String::match(), String::search(), and String::through().

00097 {
00098   if (t == 0)
00099     return 0;
00100   else
00101   {
00102     const char* a = t;
00103     while (*a++ != 0);
00104     return a - 1 - t;
00105   }
00106 }

int split const String   src,
String    results[],
int    n,
const Regex &    r
 

Definition at line 948 of file GString.cc.

00949 {
00950   String x = src;
00951   const char* s = x.chars();
00952   int sl = x.length();
00953   int i = 0;
00954   int pos = 0;
00955   int p, matchlen;
00956   while (i < n && pos < sl)
00957   {
00958     p = r.search(s, sl, matchlen, pos);
00959     if (p < 0)
00960       p = sl;
00961     results[i].rep = Salloc(results[i].rep, &(s[pos]), p - pos, p - pos);
00962     i++;
00963     pos = p + matchlen;
00964   }
00965   return i;
00966 }

int split const String   src,
String    results[],
int    n,
const String   sep
 

Definition at line 929 of file GString.cc.

00930 {
00931   String x = src;
00932   const char* s = x.chars();
00933   int sl = x.length();
00934   int i = 0;
00935   int pos = 0;
00936   while (i < n && pos < sl)
00937   {
00938     int p = x.search(pos, sl, sep.chars(), sep.length());
00939     if (p < 0)
00940       p = sl;
00941     results[i].rep = Salloc(results[i].rep, &(s[pos]), p - pos, p - pos);
00942     i++;
00943     pos = p + sep.length();
00944   }
00945   return i;
00946 }


Variable Documentation

StrRep _nilStrRep = { 0, 1, { 0 } }
 

Definition at line 46 of file GString.cc.

String _nilString
 

Definition at line 47 of file GString.cc.


Generated on Sun Oct 14 18:46:52 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001