Hauptseite   Klassenhierarchie   Alphabetische Liste   Übersicht   Auflistung der Dateien   Elementübersicht  

String.h

00001 // This may look like C code, but it is really -*- C++ -*-
00002 /* 
00003 Copyright (C) 1988 Free Software Foundation
00004     written by Doug Lea (dl@rocky.oswego.edu)
00005 
00006 This file is part of the GNU C++ Library.  This library is free
00007 software; you can redistribute it and/or modify it under the terms of
00008 the GNU Library General Public License as published by the Free
00009 Software Foundation; either version 2 of the License, or (at your
00010 option) any later version.  This library is distributed in the hope
00011 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00012 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00013 PURPOSE.  See the GNU Library General Public License for more details.
00014 You should have received a copy of the GNU Library General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 
00020 #ifndef _String_h
00021 #define _String_h 1
00022 
00023 
00024 // #include <iostream.h>
00025 
00026 #include "Regex.h"
00027 
00028 #undef OK
00029 
00030 struct StrRep                     // internal String representations
00031 {
00032   unsigned short    len;         // string length 
00033   unsigned short    sz;          // allocated space
00034   char              s[1];        // the string starts here 
00035                                  // (at least 1 char for trailing null)
00036                                  // allocated & expanded via non-public fcts
00037 };
00038 
00039 // primitive ops on StrReps -- nearly all String fns go through these.
00040 
00041 StrRep*     Salloc(StrRep*, const char*, int, int);
00042 StrRep*     Scopy(StrRep*, const StrRep*);
00043 StrRep*     Scat(StrRep*, const char*, int, const char*, int);
00044 StrRep*     Scat(StrRep*, const char*, int,const char*,int, const char*,int);
00045 StrRep*     Sprepend(StrRep*, const char*, int);
00046 StrRep*     Sreverse(const StrRep*, StrRep*);
00047 StrRep*     Supcase(const StrRep*, StrRep*);
00048 StrRep*     Sdowncase(const StrRep*, StrRep*);
00049 StrRep*     Scapitalize(const StrRep*, StrRep*);
00050 
00051 // These classes need to be defined in the order given
00052 
00053 class String;
00054 class SubString;
00055 
00056 class SubString
00057 {
00058   friend class      String;
00059 protected:
00060 
00061   String&           S;        // The String I'm a substring of
00062   unsigned short    pos;      // starting position in S's rep
00063   unsigned short    len;      // length of substring
00064 
00065   void              assign(const StrRep*, const char*, int = -1);
00066                     SubString(String& x, int p, int l);
00067                     SubString(const SubString& x);
00068 
00069 public:
00070 
00071 // Note there are no public constructors. SubStrings are always
00072 // created via String operations
00073 
00074                    ~SubString();
00075 
00076   SubString&        operator =  (const String&     y);
00077   SubString&        operator =  (const SubString&  y);
00078   SubString&        operator =  (const char* t);
00079   SubString&        operator =  (char        c);
00080 
00081 // return 1 if target appears anywhere in SubString; else 0
00082 
00083   int               contains(char        c) const;
00084   int               contains(const String&     y) const;
00085   int               contains(const SubString&  y) const;
00086   int               contains(const char* t) const;
00087   int               contains(const Regex&       r) const;
00088 
00089 // return 1 if target matches entire SubString
00090 
00091   int               matches(const Regex&  r) const;
00092 
00093 // IO 
00094 
00095 //  friend ostream&   operator<<(ostream& s, const SubString& x);
00096 
00097 // status
00098 
00099   unsigned int      length() const;
00100   int               empty() const;
00101   const char*       chars() const;
00102 
00103   int               OK() const; 
00104 
00105 };
00106 
00107 
00108 class String
00109 {
00110   friend class      SubString;
00111 
00112 protected:
00113   StrRep*           rep;   // Strings are pointers to their representations
00114 
00115 // some helper functions
00116 
00117   int               search(int, int, const char*, int = -1) const;
00118   int               search(int, int, char) const;
00119   int               match(int, int, int, const char*, int = -1) const;
00120   int               _gsub(const char*, int, const char* ,int);
00121   int               _gsub(const Regex&, const char*, int);
00122   SubString         _substr(int, int);
00123 
00124 public:
00125 
00126 // constructors & assignment
00127 
00128                     String();
00129                     String(const String& x);
00130                     String(const SubString&  x);
00131                     String(const char* t);
00132                     String(const char* t, int len);
00133                     String(char c);
00134 
00135                     ~String();
00136 
00137   String&           operator =  (const String&     y);
00138   String&           operator =  (const char* y);
00139   String&           operator =  (char        c);
00140   String&           operator =  (const SubString&  y);
00141 
00142 // concatenation
00143 
00144   String&           operator += (const String&     y); 
00145   String&           operator += (const SubString&  y);
00146   String&           operator += (const char* t);
00147   String&           operator += (char        c);
00148 
00149   void              prepend(const String&     y); 
00150   void              prepend(const SubString&  y);
00151   void              prepend(const char* t);
00152   void              prepend(char        c);
00153 
00154 
00155 // procedural versions:
00156 // concatenate first 2 args, store result in last arg
00157 
00158   friend inline void     cat(const String&, const String&, String&);
00159   friend inline void     cat(const String&, const SubString&, String&);
00160   friend inline void     cat(const String&, const char*, String&);
00161   friend inline void     cat(const String&, char, String&);
00162 
00163   friend inline void     cat(const SubString&, const String&, String&);
00164   friend inline void     cat(const SubString&, const SubString&, String&);
00165   friend inline void     cat(const SubString&, const char*, String&);
00166   friend inline void     cat(const SubString&, char, String&);
00167 
00168   friend inline void     cat(const char*, const String&, String&);
00169   friend inline void     cat(const char*, const SubString&, String&);
00170   friend inline void     cat(const char*, const char*, String&);
00171   friend inline void     cat(const char*, char, String&);
00172 
00173 // double concatenation, by request. (yes, there are too many versions, 
00174 // but if one is supported, then the others should be too...)
00175 // Concatenate first 3 args, store in last arg
00176 
00177   friend inline void     cat(const String&,const String&, const String&,String&);
00178   friend inline void     cat(const String&,const String&,const SubString&,String&);
00179   friend inline void     cat(const String&,const String&, const char*, String&);
00180   friend inline void     cat(const String&,const String&, char, String&);
00181   friend inline void     cat(const String&,const SubString&,const String&,String&);
00182   inline friend void     cat(const String&,const SubString&,const SubString&,String&);
00183   friend inline void     cat(const String&,const SubString&, const char*, String&);
00184   friend inline void     cat(const String&,const SubString&, char, String&);
00185   friend inline void     cat(const String&,const char*, const String&,    String&);
00186   friend inline void     cat(const String&,const char*, const SubString&, String&);
00187   friend inline void     cat(const String&,const char*, const char*, String&);
00188   friend inline void     cat(const String&,const char*, char, String&);
00189 
00190   friend inline void     cat(const char*, const String&, const String&,String&);
00191   friend inline void     cat(const char*,const String&,const SubString&,String&);
00192   friend inline void     cat(const char*,const String&, const char*, String&);
00193   friend inline void     cat(const char*,const String&, char, String&);
00194   friend inline void     cat(const char*,const SubString&,const String&,String&);
00195   friend inline void     cat(const char*,const SubString&,const SubString&,String&);
00196   friend inline void     cat(const char*,const SubString&, const char*, String&);
00197   friend inline void     cat(const char*,const SubString&, char, String&);
00198   friend inline void     cat(const char*,const char*, const String&,    String&);
00199   friend inline void     cat(const char*,const char*, const SubString&, String&);
00200   friend inline void     cat(const char*,const char*, const char*, String&);
00201   friend inline void     cat(const char*,const char*, char, String&);
00202 
00203 
00204 // searching & matching
00205 
00206 // return position of target in string or -1 for failure
00207 
00208   int               index(char        c, int startpos = 0) const;      
00209   int               index(const String&     y, int startpos = 0) const;      
00210   int               index(const SubString&  y, int startpos = 0) const;      
00211   int               index(const char* t, int startpos = 0) const;  
00212   int               index(const Regex&      r, int startpos = 0) const;       
00213 
00214 // return 1 if target appears anyhere in String; else 0
00215 
00216   int               contains(char        c) const;
00217   int               contains(const String&     y) const;
00218   int               contains(const SubString&  y) const;
00219   int               contains(const char* t) const;
00220   int               contains(const Regex&      r) const;
00221 
00222 // return 1 if target appears anywhere after position pos 
00223 // (or before, if pos is negative) in String; else 0
00224 
00225   int               contains(char        c, int pos) const;
00226   int               contains(const String&     y, int pos) const;
00227   int               contains(const SubString&  y, int pos) const;
00228   int               contains(const char* t, int pos) const;
00229   int               contains(const Regex&      r, int pos) const;
00230 
00231 // return 1 if target appears at position pos in String; else 0
00232 
00233   int               matches(char        c, int pos = 0) const;
00234   int               matches(const String&     y, int pos = 0) const;
00235   int               matches(const SubString&  y, int pos = 0) const;
00236   int               matches(const char* t, int pos = 0) const;
00237   int               matches(const Regex&      r, int pos = 0) const;
00238 
00239 //  return number of occurences of target in String
00240 
00241   int               freq(char        c) const; 
00242   int               freq(const String&     y) const;
00243   int               freq(const SubString&  y) const;
00244   int               freq(const char* t) const;
00245 
00246 // SubString extraction
00247 
00248 // Note that you can't take a substring of a const String, since
00249 // this leaves open the possiblility of indirectly modifying the
00250 // String through the SubString
00251 
00252   SubString         at(int         pos, int len);
00253   SubString         operator () (int         pos, int len); // synonym for at
00254 
00255   SubString         at(const String&     x, int startpos = 0); 
00256   SubString         at(const SubString&  x, int startpos = 0); 
00257   SubString         at(const char* t, int startpos = 0);
00258   SubString         at(char        c, int startpos = 0);
00259   SubString         at(const Regex&      r, int startpos = 0); 
00260 
00261   SubString         before(int          pos);
00262   SubString         before(const String&      x, int startpos = 0);
00263   SubString         before(const SubString&   x, int startpos = 0);
00264   SubString         before(const char*  t, int startpos = 0);
00265   SubString         before(char         c, int startpos = 0);
00266   SubString         before(const Regex&       r, int startpos = 0);
00267 
00268   SubString         through(int          pos);
00269   SubString         through(const String&      x, int startpos = 0);
00270   SubString         through(const SubString&   x, int startpos = 0);
00271   SubString         through(const char*  t, int startpos = 0);
00272   SubString         through(char         c, int startpos = 0);
00273   SubString         through(const Regex&       r, int startpos = 0);
00274 
00275   SubString         from(int          pos);
00276   SubString         from(const String&      x, int startpos = 0);
00277   SubString         from(const SubString&   x, int startpos = 0);
00278   SubString         from(const char*  t, int startpos = 0);
00279   SubString         from(char         c, int startpos = 0);
00280   SubString         from(const Regex&       r, int startpos = 0);
00281 
00282   SubString         after(int         pos);
00283   SubString         after(const String&     x, int startpos = 0);
00284   SubString         after(const SubString&  x, int startpos = 0);
00285   SubString         after(const char* t, int startpos = 0);
00286   SubString         after(char        c, int startpos = 0);
00287   SubString         after(const Regex&      r, int startpos = 0);
00288 
00289 
00290 // deletion
00291 
00292 // delete len chars starting at pos
00293   void              del(int         pos, int len);
00294 
00295 // delete the first occurrence of target after startpos
00296 
00297   void              del(const String&     y, int startpos = 0);
00298   void              del(const SubString&  y, int startpos = 0);
00299   void              del(const char* t, int startpos = 0);
00300   void              del(char        c, int startpos = 0);
00301   void              del(const Regex&      r, int startpos = 0);
00302 
00303 // global substitution: substitute all occurrences of pat with repl
00304 
00305   int               gsub(const String&     pat, const String&     repl);
00306   int               gsub(const SubString&  pat, const String&     repl);
00307   int               gsub(const char* pat, const String&     repl);
00308   int               gsub(const char* pat, const char* repl);
00309   int               gsub(const Regex&      pat, const String&     repl);
00310 
00311 // friends & utilities
00312 
00313 // split string into array res at separators; return number of elements
00314 
00315   friend int        split(const String& x, String res[], int maxn, 
00316                           const String& sep);
00317   friend int        split(const String& x, String res[], int maxn, 
00318                           const Regex&  sep);
00319 
00320   friend String     common_prefix(const String& x, const String& y, 
00321                                   int startpos = 0);
00322   friend String     common_suffix(const String& x, const String& y, 
00323                                   int startpos = -1);
00324   friend String     replicate(char        c, int n);
00325   friend String     replicate(const String&     y, int n);
00326   friend String     join(String src[], int n, const String& sep);
00327 
00328 // simple builtin transformations
00329 
00330   friend inline String     reverse(const String& x);
00331   friend inline String     upcase(const String& x);
00332   friend inline String     downcase(const String& x);
00333   friend inline String     capitalize(const String& x);
00334 
00335 // in-place versions of above
00336 
00337   void              reverse();
00338   void              upcase();
00339   void              downcase();
00340   void              capitalize();
00341 
00342 // element extraction
00343 
00344   char&             operator [] (int i);
00345   const char&       operator [] (int i) const;
00346   char              elem(int i) const;
00347   char              firstchar() const;
00348   char              lastchar() const;
00349 
00350 // conversion
00351 
00352                     operator const char*() const;
00353   const char*       chars() const;
00354 
00355 
00356 // IO
00357 /*
00358   friend inline ostream&   operator<<(ostream& s, const String& x);
00359   friend ostream&   operator<<(ostream& s, const SubString& x);
00360   friend istream&   operator>>(istream& s, String& x);
00361 
00362   friend int        readline(istream& s, String& x, 
00363                              char terminator = '\n',
00364                              int discard_terminator = 1);
00365 */
00366 // status
00367 
00368   unsigned int      length() const;
00369   int               empty() const;
00370 
00371 // preallocate some space for String
00372   void              alloc(int newsize);
00373 
00374 // report current allocation (not length!)
00375 
00376   int               allocation() const;
00377 
00378 
00379   void     error(const char* msg) const;
00380 
00381   int               OK() const;
00382 };
00383 
00384 typedef String StrTmp; // for backward compatibility
00385 
00386 // other externs
00387 
00388 int        compare(const String&    x, const String&     y);
00389 int        compare(const String&    x, const SubString&  y);
00390 int        compare(const String&    x, const char* y);
00391 int        compare(const SubString& x, const String&     y);
00392 int        compare(const SubString& x, const SubString&  y);
00393 int        compare(const SubString& x, const char* y);
00394 int        fcompare(const String&   x, const String&     y); // ignore case
00395 
00396 extern StrRep  _nilStrRep;
00397 extern String _nilString;
00398 
00399 // status reports, needed before defining other things
00400 
00401 inline unsigned int String::length() const {  return rep->len; }
00402 inline int         String::empty() const { return rep->len == 0; }
00403 inline const char* String::chars() const { return &(rep->s[0]); }
00404 inline int         String::allocation() const { return rep->sz; }
00405 
00406 inline unsigned int SubString::length() const { return len; }
00407 inline int         SubString::empty() const { return len == 0; }
00408 inline const char* SubString::chars() const { return &(S.rep->s[pos]); }
00409 
00410 
00411 // constructors
00412 
00413 inline String::String() 
00414   : rep(&_nilStrRep) {}
00415 inline String::String(const String& x) 
00416   : rep(Scopy(0, x.rep)) {}
00417 inline String::String(const char* t) 
00418   : rep(Salloc(0, t, -1, -1)) {}
00419 inline String::String(const char* t, int tlen)
00420   : rep(Salloc(0, t, tlen, tlen)) {}
00421 inline String::String(const SubString& y)
00422   : rep(Salloc(0, y.chars(), y.length(), y.length())) {}
00423 inline String::String(char c) 
00424   : rep(Salloc(0, &c, 1, 1)) {}
00425 
00426 inline String::~String() { if (rep != &_nilStrRep) delete rep; }
00427 
00428 inline SubString::SubString(const SubString& x)
00429   :S(x.S), pos(x.pos), len(x.len) {}
00430 inline SubString::SubString(String& x, int first, int l)
00431   :S(x), pos(first), len(l) {}
00432 
00433 inline SubString::~SubString() {}
00434 
00435 // assignment
00436 
00437 inline String& String::operator =  (const String& y)
00438 { 
00439   rep = Scopy(rep, y.rep);
00440   return *this;
00441 }
00442 
00443 inline String& String::operator=(const char* t)
00444 {
00445   rep = Salloc(rep, t, -1, -1);
00446   return *this;
00447 }
00448 
00449 inline String& String::operator=(const SubString&  y)
00450 {
00451   rep = Salloc(rep, y.chars(), y.length(), y.length());
00452   return *this;
00453 }
00454 
00455 inline String& String::operator=(char c)
00456 {
00457   rep = Salloc(rep, &c, 1, 1);
00458   return *this;
00459 }
00460 
00461 
00462 inline SubString& SubString::operator = (const char* ys)
00463 {
00464   assign(0, ys);
00465   return *this;
00466 }
00467 
00468 inline SubString& SubString::operator = (char ch)
00469 {
00470   assign(0, &ch, 1);
00471   return *this;
00472 }
00473 
00474 inline SubString& SubString::operator = (const String& y)
00475 {
00476   assign(y.rep, y.chars(), y.length());
00477   return *this;
00478 }
00479 
00480 inline SubString& SubString::operator = (const SubString& y)
00481 {
00482   assign(y.S.rep, y.chars(), y.length());
00483   return *this;
00484 }
00485 
00486 // Zillions of cats...
00487 
00488 inline void cat(const String& x, const String& y, String& r)
00489 {
00490   r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
00491 }
00492 
00493 inline void cat(const String& x, const SubString& y, String& r)
00494 {
00495   r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
00496 }
00497 
00498 inline void cat(const String& x, const char* y, String& r)
00499 {
00500   r.rep = Scat(r.rep, x.chars(), x.length(), y, -1);
00501 }
00502 
00503 inline void cat(const String& x, char y, String& r)
00504 {
00505   r.rep = Scat(r.rep, x.chars(), x.length(), &y, 1);
00506 }
00507 
00508 inline void cat(const SubString& x, const String& y, String& r)
00509 {
00510   r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
00511 }
00512 
00513 inline void cat(const SubString& x, const SubString& y, String& r)
00514 {
00515   r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
00516 }
00517 
00518 inline void cat(const SubString& x, const char* y, String& r)
00519 {
00520   r.rep = Scat(r.rep, x.chars(), x.length(), y, -1);
00521 }
00522 
00523 inline void cat(const SubString& x, char y, String& r)
00524 {
00525   r.rep = Scat(r.rep, x.chars(), x.length(), &y, 1);
00526 }
00527 
00528 inline void cat(const char* x, const String& y, String& r)
00529 {
00530   r.rep = Scat(r.rep, x, -1, y.chars(), y.length());
00531 }
00532 
00533 inline void cat(const char* x, const SubString& y, String& r)
00534 {
00535   r.rep = Scat(r.rep, x, -1, y.chars(), y.length());
00536 }
00537 
00538 inline void cat(const char* x, const char* y, String& r)
00539 {
00540   r.rep = Scat(r.rep, x, -1, y, -1);
00541 }
00542 
00543 inline void cat(const char* x, char y, String& r)
00544 {
00545   r.rep = Scat(r.rep, x, -1, &y, 1);
00546 }
00547 
00548 inline void cat(const String& a, const String& x, const String& y, String& r)
00549 {
00550   r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y.chars(), y.length());
00551 }
00552 
00553 inline void cat(const String& a, const String& x, const SubString& y, String& r)
00554 {
00555   r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y.chars(), y.length());
00556 }
00557 
00558 inline void cat(const String& a, const String& x, const char* y, String& r)
00559 {
00560   r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y, -1);
00561 }
00562 
00563 inline void cat(const String& a, const String& x, char y, String& r)
00564 {
00565   r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), &y, 1);
00566 }
00567 
00568 inline void cat(const String& a, const SubString& x, const String& y, String& r)
00569 {
00570   r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y.chars(), y.length());
00571 }
00572 
00573 inline void cat(const String& a, const SubString& x, const SubString& y, String& r)
00574 {
00575   r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y.chars(), y.length());
00576 }
00577 
00578 inline void cat(const String& a, const SubString& x, const char* y, String& r)
00579 {
00580   r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y, -1);
00581 }
00582 
00583 inline void cat(const String& a, const SubString& x, char y, String& r)
00584 {
00585   r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), &y, 1);
00586 }
00587 
00588 inline void cat(const String& a, const char* x, const String& y, String& r)
00589 {
00590   r.rep = Scat(r.rep, a.chars(), a.length(), x, -1, y.chars(), y.length());
00591 }
00592 
00593 inline void cat(const String& a, const char* x, const SubString& y, String& r)
00594 {
00595   r.rep = Scat(r.rep, a.chars(), a.length(), x, -1, y.chars(), y.length());
00596 }
00597 
00598 inline void cat(const String& a, const char* x, const char* y, String& r)
00599 {
00600   r.rep = Scat(r.rep, a.chars(), a.length(), x, -1, y, -1);
00601 }
00602 
00603 inline void cat(const String& a, const char* x, char y, String& r)
00604 {
00605   r.rep = Scat(r.rep, a.chars(), a.length(), x, -1, &y, 1);
00606 }
00607 
00608 
00609 inline void cat(const char* a, const String& x, const String& y, String& r)
00610 {
00611   r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y.chars(), y.length());
00612 }
00613 
00614 inline void cat(const char* a, const String& x, const SubString& y, String& r)
00615 {
00616   r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y.chars(), y.length());
00617 }
00618 
00619 inline void cat(const char* a, const String& x, const char* y, String& r)
00620 {
00621   r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y, -1);
00622 }
00623 
00624 inline void cat(const char* a, const String& x, char y, String& r)
00625 {
00626   r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), &y, 1);
00627 }
00628 
00629 inline void cat(const char* a, const SubString& x, const String& y, String& r)
00630 {
00631   r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y.chars(), y.length());
00632 }
00633 
00634 inline void cat(const char* a, const SubString& x, const SubString& y, String& r)
00635 {
00636   r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y.chars(), y.length());
00637 }
00638 
00639 inline void cat(const char* a, const SubString& x, const char* y, String& r)
00640 {
00641   r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y, -1);
00642 }
00643 
00644 inline void cat(const char* a, const SubString& x, char y, String& r)
00645 {
00646   r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), &y, 1);
00647 }
00648 
00649 inline void cat(const char* a, const char* x, const String& y, String& r)
00650 {
00651   r.rep = Scat(r.rep, a, -1, x, -1, y.chars(), y.length());
00652 }
00653 
00654 inline void cat(const char* a, const char* x, const SubString& y, String& r)
00655 {
00656   r.rep = Scat(r.rep, a, -1, x, -1, y.chars(), y.length());
00657 }
00658 
00659 inline void cat(const char* a, const char* x, const char* y, String& r)
00660 {
00661   r.rep = Scat(r.rep, a, -1, x, -1, y, -1);
00662 }
00663 
00664 inline void cat(const char* a, const char* x, char y, String& r)
00665 {
00666   r.rep = Scat(r.rep, a, -1, x, -1, &y, 1);
00667 }
00668 
00669 
00670 // operator versions
00671 
00672 inline String& String::operator +=(const String& y)
00673 {
00674   cat(*this, y, *this);
00675   return *this;
00676 }
00677 
00678 inline String& String::operator +=(const SubString& y)
00679 {
00680   cat(*this, y, *this);
00681   return *this;
00682 }
00683 
00684 inline String& String::operator += (const char* y)
00685 {
00686   cat(*this, y, *this);
00687   return *this;
00688 }
00689 
00690 inline String& String:: operator +=(char y)
00691 {
00692   cat(*this, y, *this);
00693   return *this;
00694 }
00695 
00696 
00697 inline String operator + (const String& x, const String& y)
00698 {
00699   String r;  cat(x, y, r);  return r;
00700 }
00701 
00702 inline String operator + (const String& x, const SubString& y) 
00703 {
00704   String r; cat(x, y, r); return r;
00705 }
00706 
00707 inline String operator + (const String& x, const char* y) 
00708 {
00709   String r; cat(x, y, r); return r;
00710 }
00711 
00712 inline String operator + (const String& x, char y) 
00713 {
00714   String r; cat(x, y, r); return r;
00715 }
00716 
00717 inline String operator + (const SubString& x, const String& y) 
00718 {
00719   String r; cat(x, y, r); return r;
00720 }
00721 
00722 inline String operator + (const SubString& x, const SubString& y) 
00723 {
00724   String r; cat(x, y, r); return r;
00725 }
00726 
00727 inline String operator + (const SubString& x, const char* y) 
00728 {
00729   String r; cat(x, y, r); return r;
00730 }
00731 
00732 inline String operator + (const SubString& x, char y) 
00733 {
00734   String r; cat(x, y, r); return r;
00735 }
00736 
00737 inline String operator + (const char* x, const String& y) 
00738 {
00739   String r; cat(x, y, r); return r;
00740 }
00741 
00742 inline String operator + (const char* x, const SubString& y) 
00743 {
00744   String r; cat(x, y, r); return r;
00745 }
00746 
00747 inline String reverse(const String& x) 
00748 {
00749   String r; r.rep = Sreverse(x.rep, r.rep); return r;
00750 }
00751 
00752 inline String upcase(const String& x) 
00753 {
00754   String r; r.rep = Supcase(x.rep, r.rep); return r;
00755 }
00756 
00757 inline String downcase(const String& x) 
00758 {
00759   String r; r.rep = Sdowncase(x.rep, r.rep); return r;
00760 }
00761 
00762 inline String capitalize(const String& x) 
00763 {
00764   String r; r.rep = Scapitalize(x.rep, r.rep); return r;
00765 }
00766 
00767 
00768 
00769 // prepend
00770 
00771 inline void String::prepend(const String& y)
00772 {
00773   rep = Sprepend(rep, y.chars(), y.length());
00774 }
00775 
00776 inline void String::prepend(const char* y)
00777 {
00778   rep = Sprepend(rep, y, -1); 
00779 }
00780 
00781 inline void String::prepend(char y)
00782 {
00783   rep = Sprepend(rep, &y, 1); 
00784 }
00785 
00786 inline void String::prepend(const SubString& y)
00787 {
00788   rep = Sprepend(rep, y.chars(), y.length());
00789 }
00790 
00791 // misc transformations
00792 
00793 
00794 inline void String::reverse()
00795 {
00796   rep = Sreverse(rep, rep);
00797 }
00798 
00799 
00800 inline void String::upcase()
00801 {
00802   rep = Supcase(rep, rep);
00803 }
00804 
00805 
00806 inline void String::downcase()
00807 {
00808   rep = Sdowncase(rep, rep);
00809 }
00810 
00811 
00812 inline void String::capitalize()
00813 {
00814   rep = Scapitalize(rep, rep);
00815 }
00816 
00817 // element extraction
00818 
00819 inline char&  String::operator [] (int i) 
00820 { 
00821   if (((unsigned)i) >= length()) error("invalid index");
00822   return rep->s[i];
00823 }
00824 
00825 inline const char&  String::operator [] (int i) const
00826 { 
00827   if (((unsigned)i) >= length()) error("invalid index");
00828   return rep->s[i];
00829 }
00830 
00831 inline char  String::elem (int i) const
00832 { 
00833   if (((unsigned)i) >= length()) error("invalid index");
00834   return rep->s[i];
00835 }
00836 
00837 inline char  String::firstchar() const
00838 { 
00839   return elem(0);
00840 }
00841 
00842 inline char  String::lastchar() const
00843 { 
00844   return elem(length() - 1);
00845 }
00846 
00847 // searching
00848 
00849 inline int String::index(char c, int startpos) const
00850 {
00851   return search(startpos, length(), c);
00852 }
00853 
00854 inline int String::index(const char* t, int startpos) const
00855 {   
00856   return search(startpos, length(), t);
00857 }
00858 
00859 inline int String::index(const String& y, int startpos) const
00860 {   
00861   return search(startpos, length(), y.chars(), y.length());
00862 }
00863 
00864 inline int String::index(const SubString& y, int startpos) const
00865 {   
00866   return search(startpos, length(), y.chars(), y.length());
00867 }
00868 
00869 inline int String::index(const Regex& r, int startpos) const
00870 {
00871   int unused;  return r.search(chars(), length(), unused, startpos);
00872 }
00873 
00874 inline int String::contains(char c) const
00875 {
00876   return search(0, length(), c) >= 0;
00877 }
00878 
00879 inline int String::contains(const char* t) const
00880 {   
00881   return search(0, length(), t) >= 0;
00882 }
00883 
00884 inline int String::contains(const String& y) const
00885 {   
00886   return search(0, length(), y.chars(), y.length()) >= 0;
00887 }
00888 
00889 inline int String::contains(const SubString& y) const
00890 {   
00891   return search(0, length(), y.chars(), y.length()) >= 0;
00892 }
00893 
00894 inline int String::contains(char c, int p) const
00895 {
00896   return match(p, length(), 0, &c, 1) >= 0;
00897 }
00898 
00899 inline int String::contains(const char* t, int p) const
00900 {
00901   return match(p, length(), 0, t) >= 0;
00902 }
00903 
00904 inline int String::contains(const String& y, int p) const
00905 {
00906   return match(p, length(), 0, y.chars(), y.length()) >= 0;
00907 }
00908 
00909 inline int String::contains(const SubString& y, int p) const
00910 {
00911   return match(p, length(), 0, y.chars(), y.length()) >= 0;
00912 }
00913 
00914 inline int String::contains(const Regex& r) const
00915 {
00916   int unused;  return r.search(chars(), length(), unused, 0) >= 0;
00917 }
00918 
00919 inline int String::contains(const Regex& r, int p) const
00920 {
00921   return r.match(chars(), length(), p) >= 0;
00922 }
00923 
00924 
00925 inline int String::matches(const SubString& y, int p) const
00926 {
00927   return match(p, length(), 1, y.chars(), y.length()) >= 0;
00928 }
00929 
00930 inline int String::matches(const String& y, int p) const
00931 {
00932   return match(p, length(), 1, y.chars(), y.length()) >= 0;
00933 }
00934 
00935 inline int String::matches(const char* t, int p) const
00936 {
00937   return match(p, length(), 1, t) >= 0;
00938 }
00939 
00940 inline int String::matches(char c, int p) const
00941 {
00942   return match(p, length(), 1, &c, 1) >= 0;
00943 }
00944 
00945 inline int String::matches(const Regex& r, int p) const
00946 {
00947   int l = (p < 0)? -p : length() - p;
00948   return r.match(chars(), length(), p) == l;
00949 }
00950 
00951 
00952 inline int SubString::contains(const char* t) const
00953 {   
00954   return S.search(pos, pos+len, t) >= 0;
00955 }
00956 
00957 inline int SubString::contains(const String& y) const
00958 {   
00959   return S.search(pos, pos+len, y.chars(), y.length()) >= 0;
00960 }
00961 
00962 inline int SubString::contains(const SubString&  y) const
00963 {   
00964   return S.search(pos, pos+len, y.chars(), y.length()) >= 0;
00965 }
00966 
00967 inline int SubString::contains(char c) const
00968 {
00969   return S.search(pos, pos+len, c) >= 0;
00970 }
00971 
00972 inline int SubString::contains(const Regex& r) const
00973 {
00974   int unused;  return r.search(chars(), len, unused, 0) >= 0;
00975 }
00976 
00977 inline int SubString::matches(const Regex& r) const
00978 {
00979   return r.match(chars(), len, 0) == len;
00980 }
00981 
00982 
00983 inline int String::gsub(const String& pat, const String& r)
00984 {
00985   return _gsub(pat.chars(), pat.length(), r.chars(), r.length());
00986 }
00987 
00988 inline int String::gsub(const SubString&  pat, const String& r)
00989 {
00990   return _gsub(pat.chars(), pat.length(), r.chars(), r.length());
00991 }
00992 
00993 inline int String::gsub(const Regex& pat, const String& r)
00994 {
00995   return _gsub(pat, r.chars(), r.length());
00996 }
00997 
00998 inline int String::gsub(const char* pat, const String& r)
00999 {
01000   return _gsub(pat, -1, r.chars(), r.length());
01001 }
01002 
01003 inline int String::gsub(const char* pat, const char* r)
01004 {
01005   return _gsub(pat, -1, r, -1);
01006 }
01007 
01008 
01009 /*
01010 inline  ostream& operator<<(ostream& s, const String& x)
01011 {
01012    s << x.chars(); return s;
01013 }
01014 */
01015 // a zillion comparison operators
01016 
01017 inline int operator==(const String& x, const String& y) 
01018 {
01019   return compare(x, y) == 0; 
01020 }
01021 
01022 inline int operator!=(const String& x, const String& y)
01023 {
01024   return compare(x, y) != 0; 
01025 }
01026 
01027 inline int operator>(const String& x, const String& y)
01028 {
01029   return compare(x, y) > 0; 
01030 }
01031 
01032 inline int operator>=(const String& x, const String& y)
01033 {
01034   return compare(x, y) >= 0; 
01035 }
01036 
01037 inline int operator<(const String& x, const String& y)
01038 {
01039   return compare(x, y) < 0; 
01040 }
01041 
01042 inline int operator<=(const String& x, const String& y)
01043 {
01044   return compare(x, y) <= 0; 
01045 }
01046 
01047 inline int operator==(const String& x, const SubString&  y) 
01048 {
01049   return compare(x, y) == 0; 
01050 }
01051 
01052 inline int operator!=(const String& x, const SubString&  y)
01053 {
01054   return compare(x, y) != 0; 
01055 }
01056 
01057 inline int operator>(const String& x, const SubString&  y)      
01058 {
01059   return compare(x, y) > 0; 
01060 }
01061 
01062 inline int operator>=(const String& x, const SubString&  y)
01063 {
01064   return compare(x, y) >= 0; 
01065 }
01066 
01067 inline int operator<(const String& x, const SubString&  y) 
01068 {
01069   return compare(x, y) < 0; 
01070 }
01071 
01072 inline int operator<=(const String& x, const SubString&  y)
01073 {
01074   return compare(x, y) <= 0; 
01075 }
01076 
01077 inline int operator==(const String& x, const char* t) 
01078 {
01079   return compare(x, t) == 0; 
01080 }
01081 
01082 inline int operator!=(const String& x, const char* t) 
01083 {
01084   return compare(x, t) != 0; 
01085 }
01086 
01087 inline int operator>(const String& x, const char* t)  
01088 {
01089   return compare(x, t) > 0; 
01090 }
01091 
01092 inline int operator>=(const String& x, const char* t) 
01093 {
01094   return compare(x, t) >= 0; 
01095 }
01096 
01097 inline int operator<(const String& x, const char* t)  
01098 {
01099   return compare(x, t) < 0; 
01100 }
01101 
01102 inline int operator<=(const String& x, const char* t) 
01103 {
01104   return compare(x, t) <= 0; 
01105 }
01106 
01107 inline int operator==(const SubString& x, const String& y) 
01108 {
01109   return compare(y, x) == 0; 
01110 }
01111 
01112 inline int operator!=(const SubString& x, const String& y)
01113 {
01114   return compare(y, x) != 0;
01115 }
01116 
01117 inline int operator>(const SubString& x, const String& y)      
01118 {
01119   return compare(y, x) < 0;
01120 }
01121 
01122 inline int operator>=(const SubString& x, const String& y)     
01123 {
01124   return compare(y, x) <= 0;
01125 }
01126 
01127 inline int operator<(const SubString& x, const String& y)      
01128 {
01129   return compare(y, x) > 0;
01130 }
01131 
01132 inline int operator<=(const SubString& x, const String& y)     
01133 {
01134   return compare(y, x) >= 0;
01135 }
01136 
01137 inline int operator==(const SubString& x, const SubString&  y) 
01138 {
01139   return compare(x, y) == 0; 
01140 }
01141 
01142 inline int operator!=(const SubString& x, const SubString&  y)
01143 {
01144   return compare(x, y) != 0;
01145 }
01146 
01147 inline int operator>(const SubString& x, const SubString&  y)      
01148 {
01149   return compare(x, y) > 0;
01150 }
01151 
01152 inline int operator>=(const SubString& x, const SubString&  y)
01153 {
01154   return compare(x, y) >= 0;
01155 }
01156 
01157 inline int operator<(const SubString& x, const SubString&  y) 
01158 {
01159   return compare(x, y) < 0;
01160 }
01161 
01162 inline int operator<=(const SubString& x, const SubString&  y)
01163 {
01164   return compare(x, y) <= 0;
01165 }
01166 
01167 inline int operator==(const SubString& x, const char* t) 
01168 {
01169   return compare(x, t) == 0; 
01170 }
01171 
01172 inline int operator!=(const SubString& x, const char* t) 
01173 {
01174   return compare(x, t) != 0;
01175 }
01176 
01177 inline int operator>(const SubString& x, const char* t)  
01178 {
01179   return compare(x, t) > 0; 
01180 }
01181 
01182 inline int operator>=(const SubString& x, const char* t) 
01183 {
01184   return compare(x, t) >= 0; 
01185 }
01186 
01187 inline int operator<(const SubString& x, const char* t)  
01188 {
01189   return compare(x, t) < 0; 
01190 }
01191 
01192 inline int operator<=(const SubString& x, const char* t) 
01193 {
01194   return compare(x, t) <= 0; 
01195 }
01196 
01197 
01198 // a helper needed by at, before, etc.
01199 
01200 inline SubString String::_substr(int first, int l)
01201 {
01202   if (first < 0 || (unsigned)(first + l) > length() )
01203     return SubString(_nilString, 0, 0) ;
01204   else 
01205     return SubString(*this, first, l);
01206 }
01207 
01208 #endif

Erzeugt am Tue Dec 31 22:54:54 2002 für av_convert von doxygen1.2.9.1 geschrieben von Dimitri van Heesch, © 1997-2001