Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXString.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *                           S t r i n g   O b j e c t                           *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2004 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXString.h,v 1.66 2004/02/08 17:17:34 fox Exp $                          *
00023 ********************************************************************************/
00024 #ifndef FXSTRING_H
00025 #define FXSTRING_H
00026 
00027 namespace FX {
00028 
00029 
00030 /**
00031 * FXString provides essential string manipulation capabilities.
00032 */
00033 class FXAPI FXString {
00034 private:
00035   FXchar* str;
00036 public:
00037   static const FXchar null[];
00038   static const FXchar hex[17];
00039   static const FXchar HEX[17];
00040 public:
00041 
00042   /// Create empty string
00043   FXString();
00044 
00045   /// Copy construct
00046   FXString(const FXString& s);
00047 
00048   /// Construct and init
00049   FXString(const FXchar* s);
00050 
00051   /// Construct and init with substring
00052   FXString(const FXchar* s,FXint n);
00053 
00054   /// Construct and fill with constant
00055   FXString(FXchar c,FXint n);
00056 
00057   /// Construct string from two parts
00058   FXString(const FXchar* s1,const FXchar* s2);
00059 
00060   /// Change the length of the string to len
00061   void length(FXint len);
00062 
00063   /// Length of text
00064   FXint length() const { return ((FXint*)str)[-1]; }
00065 
00066   /// Get text contents
00067   const FXchar* text() const { return (const FXchar*)str; }
00068 
00069   /// See if string is empty
00070   FXbool empty() const { return (((FXint*)str)[-1]==0); }
00071 
00072   /// Return a non-const reference to the ith character
00073   FXchar& operator[](FXint i){ return str[i]; }
00074 
00075   /// Return a const reference to the ith character
00076   const FXchar& operator[](FXint i) const { return str[i]; }
00077 
00078   /// Assign another string to this
00079   FXString& operator=(const FXString& s);
00080 
00081   /// Assign a C-style string to this
00082   FXString& operator=(const FXchar* s);
00083 
00084   /// Fill with a constant
00085   FXString& fill(FXchar c,FXint n);
00086 
00087   /// Fill up to current length
00088   FXString& fill(FXchar c);
00089 
00090   /// Convert to lower case
00091   FXString& lower();
00092 
00093   /// Convert to upper case
00094   FXString& upper();
00095 
00096   /// Return num partition(s) of string separated by delimiter delim
00097   FXString section(FXchar delim,FXint start,FXint num=1) const;
00098 
00099   /// Return num partition(s) of string separated by delimiters in delim
00100   FXString section(const FXchar* delim,FXint n,FXint start,FXint num=1) const;
00101 
00102   /// Return num partition(s) of string separated by delimiters in delim
00103   FXString section(const FXchar* delim,FXint start,FXint num=1) const;
00104 
00105   /// Return num partition(s) of string separated by delimiters in delim
00106   FXString section(const FXString& delim,FXint start,FXint num=1) const;
00107 
00108   /// Assign character c to this string
00109   FXString& assign(FXchar c);
00110 
00111   /// Assign n characters c to this string
00112   FXString& assign(FXchar c,FXint n);
00113 
00114   /// Assign first n characters of string s to this string
00115   FXString& assign(const FXchar *s,FXint n);
00116 
00117   /// Assign string s to this string
00118   FXString& assign(const FXString& s);
00119 
00120   /// Assign string s to this string
00121   FXString& assign(const FXchar* s);
00122 
00123   /// Insert character at specified position
00124   FXString& insert(FXint pos,FXchar c);
00125 
00126   /// Insert n characters c at specified position
00127   FXString& insert(FXint pos,FXchar c,FXint n);
00128 
00129   /// Insert first n characters of string at specified position
00130   FXString& insert(FXint pos,const FXchar* s,FXint n);
00131 
00132   /// Insert string at specified position
00133   FXString& insert(FXint pos,const FXString& s);
00134 
00135   /// Insert string at specified position
00136   FXString& insert(FXint pos,const FXchar* s);
00137 
00138   /// Prepend string with input character
00139   FXString& prepend(FXchar c);
00140 
00141   /// Prepend string with n characters c
00142   FXString& prepend(FXchar c,FXint n);
00143 
00144   /// Prepend string with first n characters of input string
00145   FXString& prepend(const FXchar* s,FXint n);
00146 
00147   /// Prepend string with input string
00148   FXString& prepend(const FXString& s);
00149 
00150   /// Prepend string with input string
00151   FXString& prepend(const FXchar* s);
00152 
00153   /// Append input character to this string
00154   FXString& append(FXchar c);
00155 
00156   /// Append input n characters c to this string
00157   FXString& append(FXchar c,FXint n);
00158 
00159   /// Append first n characters of input string to this string
00160   FXString& append(const FXchar* s,FXint n);
00161 
00162   /// Append input string to this string
00163   FXString& append(const FXString& s);
00164 
00165   /// Append input string to this string
00166   FXString& append(const FXchar* s);
00167 
00168   /// Replace a single character
00169   FXString& replace(FXint pos,FXchar c);
00170 
00171   /// Replace the m characters at pos with n characters c
00172   FXString& replace(FXint pos,FXint m,FXchar c,FXint n);
00173 
00174   /// Replaces the m characters at pos with first n characters of input string
00175   FXString& replace(FXint pos,FXint m,const FXchar* s,FXint n);
00176 
00177   /// Replace the m characters at pos with input string
00178   FXString& replace(FXint pos,FXint m,const FXString& s);
00179 
00180   /// Replace the m characters at pos with input string
00181   FXString& replace(FXint pos,FXint m,const FXchar* s);
00182 
00183   /// Remove substring
00184   FXString& remove(FXint pos,FXint n=1);
00185 
00186   /// Return number of occurrences of ch in string
00187   FXint contains(FXchar ch);
00188 
00189   /// Return number of occurrences of string sub in string
00190   FXint contains(const FXchar* sub,FXint n);
00191 
00192   /// Return number of occurrences of string sub in string
00193   FXint contains(const FXchar* sub);
00194 
00195   /// Return number of occurrences of string sub in string
00196   FXint contains(const FXString& sub);
00197 
00198   /// Substitute one character by another
00199   FXString& substitute(FXchar org,FXchar sub,FXbool all=TRUE);
00200 
00201   /// Substitute one string by another
00202   FXString& substitute(const FXchar* org,FXint olen,const FXchar *rep,FXint rlen,FXbool all=TRUE);
00203 
00204   /// Substitute one string by another
00205   FXString& substitute(const FXchar* org,const FXchar *rep,FXbool all=TRUE);
00206 
00207   /// Substitute one string by another
00208   FXString& substitute(const FXString& org,const FXString& rep,FXbool all=TRUE);
00209 
00210   /// Simplify whitespace in string
00211   FXString& simplify();
00212 
00213   /// Remove leading and trailing whitespace
00214   FXString& trim();
00215 
00216   /// Remove leading whitespace
00217   FXString& trimBegin();
00218 
00219   /// Remove trailing whitespace
00220   FXString& trimEnd();
00221 
00222   /// Truncate string at pos
00223   FXString& trunc(FXint pos);
00224 
00225   /// Clear
00226   FXString& clear();
00227 
00228   /// Get left most part
00229   FXString left(FXint n) const;
00230 
00231   /// Get right most part
00232   FXString right(FXint n) const;
00233 
00234   /// Get some part in the middle
00235   FXString mid(FXint pos,FXint n) const;
00236 
00237   /**
00238   * Return all characters before the n-th occurrence of ch,
00239   * searching from the beginning of the string. If the character
00240   * is not found, return the entire string.  If n<=0, return
00241   * the empty string.
00242   */
00243   FXString before(FXchar ch,FXint n=1) const;
00244 
00245   /**
00246   * Return all characters before the n-th occurrence of ch,
00247   * searching from the end of the string. If the character
00248   * is not found, return the empty string. If n<=0, return
00249   * the entire string.
00250   */
00251   FXString rbefore(FXchar ch,FXint n=1) const;
00252 
00253   /**
00254   * Return all characters after the nth occurrence of ch,
00255   * searching from the beginning of the string. If the character
00256   * is not found, return the empty string.  If n<=0, return
00257   * the entire string.
00258   */
00259   FXString after(FXchar ch,FXint n=1) const;
00260 
00261   /**
00262   * Return all characters after the nth occurrence of ch,
00263   * searching from the end of the string. If the character
00264   * is not found, return the entire string. If n<=0, return
00265   * the empty string.
00266   */
00267   FXString rafter(FXchar ch,FXint n=1) const;
00268 
00269   /// Find a character, searching forward; return position or -1
00270   FXint find(FXchar c,FXint pos=0) const;
00271 
00272   /// Find a character, searching backward; return position or -1
00273   FXint rfind(FXchar c,FXint pos=2147483647) const;
00274 
00275   // Find n-th occurrence of character, searching forward; return position or -1
00276   FXint find(FXchar c,FXint pos,FXint n) const;
00277 
00278   // Find n-th occurrence of character, searching backward; return position or -1
00279   FXint rfind(FXchar c,FXint pos,FXint n) const;
00280 
00281   /// Find a substring of length n, searching forward; return position or -1
00282   FXint find(const FXchar* substr,FXint n,FXint pos) const;
00283 
00284   /// Find a substring of length n, searching backward; return position or -1
00285   FXint rfind(const FXchar* substr,FXint n,FXint pos) const;
00286 
00287   /// Find a substring, searching forward; return position or -1
00288   FXint find(const FXchar* substr,FXint pos=0) const;
00289 
00290   /// Find a substring, searching backward; return position or -1
00291   FXint rfind(const FXchar* substr,FXint pos=2147483647) const;
00292 
00293   /// Find a substring, searching forward; return position or -1
00294   FXint find(const FXString& substr,FXint pos=0) const;
00295 
00296   /// Find a substring, searching backward; return position or -1
00297   FXint rfind(const FXString& substr,FXint pos=2147483647) const;
00298 
00299   /// Find first character in the set of size n, starting from pos; return position or -1
00300   FXint find_first_of(const FXchar* set,FXint n,FXint pos) const;
00301 
00302   /// Find first character in the set, starting from pos; return position or -1
00303   FXint find_first_of(const FXchar* set,FXint pos=0) const;
00304 
00305   /// Find first character in the set, starting from pos; return position or -1
00306   FXint find_first_of(const FXString& set,FXint pos=0) const;
00307 
00308   /// Find first character, starting from pos; return position or -1
00309   FXint find_first_of(FXchar c,FXint pos=0) const;
00310 
00311   /// Find last character in the set of size n, starting from pos; return position or -1
00312   FXint find_last_of(const FXchar* set,FXint n,FXint pos) const;
00313 
00314   /// Find last character in the set, starting from pos; return position or -1
00315   FXint find_last_of(const FXchar* set,FXint pos=2147483647) const;
00316 
00317   /// Find last character in the set, starting from pos; return position or -1
00318   FXint find_last_of(const FXString& set,FXint pos=2147483647) const;
00319 
00320   /// Find last character, starting from pos; return position or -1
00321   FXint find_last_of(FXchar c,FXint pos=0) const;
00322 
00323   /// Find first character NOT in the set of size n, starting from pos; return position or -1
00324   FXint find_first_not_of(const FXchar* set,FXint n,FXint pos) const;
00325 
00326   /// Find first character NOT in the set, starting from pos; return position or -1
00327   FXint find_first_not_of(const FXchar* set,FXint pos=0) const;
00328 
00329   /// Find first character NOT in the set, starting from pos; return position or -1
00330   FXint find_first_not_of(const FXString& set,FXint pos=0) const;
00331 
00332   /// Find first character NOT equal to c, starting from pos; return position or -1
00333   FXint find_first_not_of(FXchar c,FXint pos=0) const;
00334 
00335   /// Find last character NOT in the set of size n, starting from pos; return position or -1
00336   FXint find_last_not_of(const FXchar* set,FXint n,FXint pos) const;
00337 
00338   /// Find last character NOT in the set, starting from pos; return position or -1
00339   FXint find_last_not_of(const FXchar* set,FXint pos=2147483647) const;
00340 
00341   /// Find last character NOT in the set, starting from pos; return position or -1
00342   FXint find_last_not_of(const FXString& set,FXint pos=2147483647) const;
00343 
00344   /// Find last character NOT equal to c, starting from pos; return position or -1
00345   FXint find_last_not_of(FXchar c,FXint pos=0) const;
00346 
00347   /// Find number of occurrences of character in string
00348   FXint count(FXchar c) const;
00349 
00350   /// Format a string a-la printf
00351   FXString& format(const char* fmt,...) FX_PRINTF(2,3) ;
00352   FXString& vformat(const char* fmt,va_list args);
00353 
00354   /// Scan a string a-la scanf
00355   FXint scan(const char* fmt,...) const FX_SCANF(2,3) ;
00356   FXint vscan(const char* fmt,va_list args) const;
00357 
00358   /// Get hash value
00359   FXuint hash() const;
00360 
00361   /// Compare
00362   friend FXAPI FXint compare(const FXchar* s1,const FXchar* s2);
00363   friend FXAPI FXint compare(const FXchar* s1,const FXString& s2);
00364   friend FXAPI FXint compare(const FXString& s1,const FXchar* s2);
00365   friend FXAPI FXint compare(const FXString& s1,const FXString& s2);
00366 
00367   /// Compare up to n
00368   friend FXAPI FXint compare(const FXchar* s1,const FXchar* s2,FXint n);
00369   friend FXAPI FXint compare(const FXchar* s1,const FXString& s2,FXint n);
00370   friend FXAPI FXint compare(const FXString& s1,const FXchar* s2,FXint n);
00371   friend FXAPI FXint compare(const FXString& s1,const FXString& s2,FXint n);
00372 
00373   /// Compare case insensitive
00374   friend FXAPI FXint comparecase(const FXchar* s1,const FXchar* s2);
00375   friend FXAPI FXint comparecase(const FXchar* s1,const FXString& s2);
00376   friend FXAPI FXint comparecase(const FXString& s1,const FXchar* s2);
00377   friend FXAPI FXint comparecase(const FXString& s1,const FXString& s2);
00378 
00379   /// Compare case insensitive up to n
00380   friend FXAPI FXint comparecase(const FXchar* s1,const FXchar* s2,FXint n);
00381   friend FXAPI FXint comparecase(const FXchar* s1,const FXString& s2,FXint n);
00382   friend FXAPI FXint comparecase(const FXString& s1,const FXchar* s2,FXint n);
00383   friend FXAPI FXint comparecase(const FXString& s1,const FXString& s2,FXint n);
00384 
00385   /// Comparison operators
00386   friend FXAPI FXbool operator==(const FXString& s1,const FXString& s2);
00387   friend FXAPI FXbool operator==(const FXString& s1,const FXchar* s2);
00388   friend FXAPI FXbool operator==(const FXchar* s1,const FXString& s2);
00389 
00390   friend FXAPI FXbool operator!=(const FXString& s1,const FXString& s2);
00391   friend FXAPI FXbool operator!=(const FXString& s1,const FXchar* s2);
00392   friend FXAPI FXbool operator!=(const FXchar* s1,const FXString& s2);
00393 
00394   friend FXAPI FXbool operator<(const FXString& s1,const FXString& s2);
00395   friend FXAPI FXbool operator<(const FXString& s1,const FXchar* s2);
00396   friend FXAPI FXbool operator<(const FXchar* s1,const FXString& s2);
00397 
00398   friend FXAPI FXbool operator<=(const FXString& s1,const FXString& s2);
00399   friend FXAPI FXbool operator<=(const FXString& s1,const FXchar* s2);
00400   friend FXAPI FXbool operator<=(const FXchar* s1,const FXString& s2);
00401 
00402   friend FXAPI FXbool operator>(const FXString& s1,const FXString& s2);
00403   friend FXAPI FXbool operator>(const FXString& s1,const FXchar* s2);
00404   friend FXAPI FXbool operator>(const FXchar* s1,const FXString& s2);
00405 
00406   friend FXAPI FXbool operator>=(const FXString& s1,const FXString& s2);
00407   friend FXAPI FXbool operator>=(const FXString& s1,const FXchar* s2);
00408   friend FXAPI FXbool operator>=(const FXchar* s1,const FXString& s2);
00409 
00410   /// Append operators
00411   FXString& operator+=(const FXString& s);
00412   FXString& operator+=(const FXchar* s);
00413   FXString& operator+=(FXchar c);
00414 
00415   /// Concatenate two strings
00416   friend FXAPI FXString operator+(const FXString& s1,const FXString& s2);
00417   friend FXAPI FXString operator+(const FXString& s1,const FXchar* s2);
00418   friend FXAPI FXString operator+(const FXchar* s1,const FXString& s2);
00419 
00420   /// Concatenate with single character
00421   friend FXAPI FXString operator+(const FXString& s,FXchar c);
00422   friend FXAPI FXString operator+(FXchar c,const FXString& s);
00423 
00424   /// Saving to a stream
00425   friend FXAPI FXStream& operator<<(FXStream& store,const FXString& s);
00426 
00427   /// Load from a stream
00428   friend FXAPI FXStream& operator>>(FXStream& store,FXString& s);
00429 
00430   /// Format a string a-la printf
00431   friend FXAPI FXString FXStringFormat(const FXchar* fmt,...) FX_PRINTF(1,2) ;
00432   friend FXAPI FXString FXStringVFormat(const FXchar* fmt,va_list args);
00433 
00434   /**
00435   * Convert integer number to a string, using the given number
00436   * base, which must be between 2 and 16.
00437   */
00438   friend FXAPI FXString FXStringVal(FXint num,FXint base=10);
00439   friend FXAPI FXString FXStringVal(FXuint num,FXint base=10);
00440 
00441   /**
00442   * Convert real number to a string, using the given procision and
00443   * exponential notation mode, which may be FALSE (never), TRUE (always), or
00444   * MAYBE (when needed).
00445   */
00446   friend FXAPI FXString FXStringVal(FXfloat num,FXint prec=6,FXbool exp=MAYBE);
00447   friend FXAPI FXString FXStringVal(FXdouble num,FXint prec=6,FXbool exp=MAYBE);
00448 
00449   /// Convert string to a integer number, assuming given number base
00450   friend FXAPI FXint FXIntVal(const FXString& s,FXint base=10);
00451   friend FXAPI FXuint FXUIntVal(const FXString& s,FXint base=10);
00452 
00453   /// Convert string into real number
00454   friend FXAPI FXfloat FXFloatVal(const FXString& s);
00455   friend FXAPI FXdouble FXDoubleVal(const FXString& s);
00456 
00457   /// Escape special characters in a string
00458   friend FXAPI FXString escape(const FXString& s);
00459 
00460   /// Unescape special characters in a string
00461   friend FXAPI FXString unescape(const FXString& s);
00462 
00463   /// Swap two strings
00464   friend FXAPI void swap(FXString& a,FXString& b){ FXchar *t=a.str; a.str=b.str; b.str=t; }
00465 
00466   /// Delete
00467  ~FXString();
00468   };
00469 
00470 
00471 }
00472 
00473 #endif

Copyright © 1997-2004 Jeroen van der Zijp