00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <string.h>
00031 #include <ctype.h>
00032 #include "xalloc.h"
00033 #include "string.hpp"
00034
00035
00036
00037 String::String()
00038 {
00039 buf=xstrdup("");
00040 }
00041
00042
00043
00044 String::String(const String& x)
00045 {
00046 buf=xstrdup(x);
00047 }
00048
00049
00050
00051 String::String(const char* t)
00052 {
00053 buf=xstrdup(t);
00054 }
00055
00056
00057
00058 String::String(const char* t, int len)
00059 {
00060 int l=strlen(t);
00061 if (l>len) l=len;
00062 buf=(char*)xmalloc(l+1);
00063 strncpy(buf,t,l);
00064 buf[l]='\0';
00065 }
00066
00067
00068
00069 String::String(char c)
00070 {
00071 buf=(char*)xmalloc(2);
00072 buf[0]=c;
00073 }
00074
00075
00076
00077 String::~String()
00078 {
00079 if (buf) xfree(buf);
00080 }
00081
00082
00083
00084 String& String::operator = (const String& y)
00085 {
00086 char *tmp=xstrdup(y.buf);
00087 if (buf) xfree(buf);
00088 buf=tmp;
00089 return *this;
00090 }
00091
00092
00093
00094 String& String::operator = (const char* y)
00095 {
00096 char *tmp=xstrdup(y);
00097 if (buf) xfree(buf);
00098 buf=tmp;
00099 return *this;
00100 }
00101
00102
00103
00104 String& String::operator = (char c)
00105 {
00106 char *tmp=(char*)xmalloc(2);
00107 tmp[0]=c;
00108 tmp[1]='\0';
00109 if (buf) xfree(buf);
00110 buf=tmp;
00111 return *this;
00112 }
00113
00114
00115
00116 String& String::operator += (const String& y)
00117 {
00118 return operator +=((const char*)y);
00119 }
00120
00121
00122
00123 String& String::operator += (const char* t)
00124 {
00125 char *tmp;
00126 tmp=(char*)xmalloc((buf?strlen(buf):0)+(t?strlen(t):0)+1);
00127 tmp[0]='\0';
00128 if (buf) { strcat(tmp,buf); xfree(buf); }
00129 if (t) strcat(tmp,t);
00130 buf=tmp;
00131 return *this;
00132 }
00133
00134
00135
00136 String& String::operator += (char c)
00137 {
00138 char *tmp;
00139 tmp=(char*)xmalloc((buf?strlen(buf):0)+2);
00140 tmp[0]='\0';
00141 if (buf) { strcat(tmp,buf); xfree(buf); }
00142 size_t l=strlen(tmp);
00143 tmp[l+1]='\0';
00144 tmp[l]=c;
00145 buf=tmp;
00146 return *this;
00147 }
00148
00149
00150
00151 char& String::operator [] (int i)
00152 {
00153 return buf[i];
00154 }
00155
00156
00157
00158 const char& String::operator [] (int i) const
00159 {
00160 return buf[i];
00161 }
00162
00163
00164
00165 String::operator const char*() const
00166 {
00167 return buf;
00168 }
00169
00170
00171
00172 const char* String::chars() const
00173 {
00174 return buf;
00175 }
00176
00177
00178
00179 unsigned int String::length() const
00180 {
00181 return strlen(buf);
00182 }
00183
00184
00185
00186 int String::empty() const
00187 {
00188 return (buf[0]=='\0');
00189 }
00190
00191
00192
00193 int String::OK() const
00194 {
00195 return buf!=NULL;
00196 }
00197
00198
00199
00200 int String::index(char c, int pos) const
00201 {
00202 const char *x=strchr(buf+pos,c);
00203 if (x) return (int)(buf-(char*)x);
00204 return -1;
00205 }
00206
00207
00208
00209 int String::index(const String& y, int pos) const
00210 {
00211 return index(y.buf,pos);
00212 }
00213
00214
00215
00216 int String::index(const char* t, int pos) const
00217 {
00218 const char *x=strstr(buf+pos,t);
00219 if (x) return (int)(buf-(char*)x);
00220 return -1;
00221 }
00222
00223
00224
00225 int String::contains(char c) const { return strchr(buf,c)!=NULL; }
00226
00227
00228
00229 int String::contains(const String& y) const { return strstr(buf,y.buf)!=NULL; }
00230
00231
00232
00233 int String::contains(const char* t) const { return strstr(buf,t)!=NULL; }
00234
00235
00236
00237 int String::contains(char c, int pos) const
00238 {
00239 if (pos>=0) return strchr(buf+pos,c)!=NULL;
00240 char *x=strchr(buf,c);
00241 if (!x) return 0;
00242 return (buf-x <= pos);
00243 }
00244
00245
00246
00247 int String::contains(const String& y, int pos) const
00248 {
00249 return contains(y.buf,pos);
00250 }
00251
00252
00253
00254 int String::contains(const char* t, int pos) const
00255 {
00256 if (pos>=0) return strstr(buf+pos,t)!=NULL;
00257 char *x=strstr(buf,t);
00258 if (!x) return 0;
00259 return (buf-x <= pos);
00260 }
00261
00262
00263
00264 String operator + (const String& x, const String& y) { String r(x); r+=y; return r; }
00265 String operator + (const String& x, const char* y) { String r(x); r+=y; return r; }
00266 String operator + (const String& x, char y) { String r(x); r+=y; return r; }
00267 String operator + (const char* x, const String& y) { String r(x); r+=y; return r; }
00268
00269
00270 int compare(const String& x, const String& y) { return strcmp(x,y); }
00271 int compare(const String& x, const char* y) { return strcmp(x,y); }
00272
00273 int operator==(const String& x, const String& y) { return compare(x, y) == 0; }
00274 int operator!=(const String& x, const String& y) { return compare(x, y) != 0; }
00275 int operator>(const String& x, const String& y) { return compare(x, y) > 0; }
00276 int operator>=(const String& x, const String& y) { return compare(x, y) >= 0; }
00277 int operator<(const String& x, const String& y) { return compare(x, y) < 0; }
00278 int operator<=(const String& x, const String& y) { return compare(x, y) <= 0; }
00279 int operator==(const String& x, const char* t) { return compare(x, t) == 0; }
00280 int operator!=(const String& x, const char* t) { return compare(x, t) != 0; }
00281 int operator>(const String& x, const char* t) { return compare(x, t) > 0; }
00282 int operator>=(const String& x, const char* t) { return compare(x, t) >= 0; }
00283 int operator<(const String& x, const char* t) { return compare(x, t) < 0; }
00284 int operator<=(const String& x, const char* t) { return compare(x, t) <= 0; }
00285
00286
00287
00288 void String::upcase()
00289 {
00290 char *s=buf;
00291 while (*s)
00292 {
00293 *s=toupper(*s);
00294 s++;
00295 }
00296 }
00297
00298
00299
00300 String upcase(const String& x) { String s(x); s.upcase(); return s; }
00301
00302
00303
00304 int String::gsub(const char* pat, const char* repl)
00305 {
00306 size_t l=strlen(pat);
00307 char *from=buf;
00308 int n=0;
00309 char *s;
00310 String dest;
00311
00312 while ( (s=strstr(from,pat))!=NULL ) {
00313 n++;
00314 String tmp(from,(int)(s-from));
00315 dest += tmp+repl;
00316 from = s+l;
00317 }
00318 dest += from;
00319 xfree(buf);
00320 buf=dest.buf;
00321 dest.buf=NULL;
00322 return n;
00323 }
00324
00325
00326 String String::before(int pos)
00327 {
00328 if (pos<0) return String(buf);
00329 if (pos>0) return String(buf,pos-1);
00330 else return String();
00331 }
00332
00333 String String::before(const String& x, int startpos) { return before(index(x,startpos)); }
00334 String String::before(const char* t, int startpos) { return before(index(t,startpos)); }
00335 String String::before(char c, int startpos) { return before(index(c,startpos)); }
00336 String String::after(int pos)
00337 {
00338 if (pos<0) return String(buf);
00339 String tmp(buf+pos+1);
00340 return tmp;
00341 }
00342
00343 String String::after(const String& x, int startpos) { return after(index(x,startpos)); }
00344 String String::after(const char* t, int startpos) { return after(index(t,startpos)); }
00345 String String::after(char c, int startpos) { return after(index(c,startpos)); }
00346
00347