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,2002 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.60 2002/10/01 06:58:46 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   /// Substitute one character by another
00187   FXString& substitute(FXchar orig,FXchar sub);
00188 
00189   /// Simplify whitespace in string
00190   FXString& simplify();
00191 
00192   /// Remove leading and trailing whitespace
00193   FXString& trim();
00194 
00195   /// Remove leading whitespace
00196   FXString& trimBegin();
00197 
00198   /// Remove trailing whitespace
00199   FXString& trimEnd();
00200 
00201   /// Truncate string at pos
00202   FXString& trunc(FXint pos);
00203 
00204   /// Clear
00205   FXString& clear();
00206 
00207   /// Get leftmost part
00208   FXString left(FXint n) const;
00209 
00210   /// Get rightmost part
00211   FXString right(FXint n) const;
00212 
00213   /// Get some part in the middle
00214   FXString mid(FXint pos,FXint n) const;
00215 
00216   /**
00217   * Return all characters before the n-th occurrence of ch,
00218   * searching from the beginning of the string. If the character
00219   * is not found, return the entire string.  If n<=0, return
00220   * the empty string.
00221   */
00222   FXString before(FXchar ch,FXint n=1) const;
00223 
00224   /**
00225   * Return all characters before the n-th occurrence of ch,
00226   * searching from the end of the string. If the character
00227   * is not found, return the empty string. If n<=0, return
00228   * the entire string.
00229   */
00230   FXString rbefore(FXchar ch,FXint n=1) const;
00231 
00232   /**
00233   * Return all characters after the nth occurrence of ch,
00234   * searching from the beginning of the string. If the character
00235   * is not found, return the empty string.  If n<=0, return
00236   * the entire string.
00237   */
00238   FXString after(FXchar ch,FXint n=1) const;
00239 
00240   /**
00241   * Return all characters after the nth occurrence of ch,
00242   * searching from the end of the string. If the character
00243   * is not found, return the entire string. If n<=0, return
00244   * the empty string.
00245   */
00246   FXString rafter(FXchar ch,FXint n=1) const;
00247 
00248   /// Find a character, searching forward; return position or -1
00249   FXint find(FXchar c,FXint pos=0) const;
00250 
00251   /// Find a character, searching backward; return position or -1
00252   FXint rfind(FXchar c,FXint pos=2147483647) const;
00253 
00254   // Find n-th occurrence of character, searching forward; return position or -1
00255   FXint find(FXchar c,FXint pos,FXint n) const;
00256 
00257   // Find n-th occurrence of character, searching backward; return position or -1
00258   FXint rfind(FXchar c,FXint pos,FXint n) const;
00259 
00260   /// Find a substring of length n, searching forward; return position or -1
00261   FXint find(const FXchar* substr,FXint n,FXint pos) const;
00262 
00263   /// Find a substring of length n, searching backward; return position or -1
00264   FXint rfind(const FXchar* substr,FXint n,FXint pos) const;
00265 
00266   /// Find a substring, searching forward; return position or -1
00267   FXint find(const FXchar* substr,FXint pos=0) const;
00268 
00269   /// Find a substring, searching backward; return position or -1
00270   FXint rfind(const FXchar* substr,FXint pos=2147483647) const;
00271 
00272   /// Find a substring, searching forward; return position or -1
00273   FXint find(const FXString &substr,FXint pos=0) const;
00274 
00275   /// Find a substring, searching backward; return position or -1
00276   FXint rfind(const FXString &substr,FXint pos=2147483647) const;
00277 
00278   /// Find first character in the set of size n, starting from pos; return position or -1
00279   FXint find_first_of(const FXchar *set,FXint n,FXint pos) const;
00280 
00281   /// Find first character in the set, starting from pos; return position or -1
00282   FXint find_first_of(const FXchar *set,FXint pos=0) const;
00283 
00284   /// Find first character in the set, starting from pos; return position or -1
00285   FXint find_first_of(const FXString &set,FXint pos=0) const;
00286 
00287   /// Find first character, starting from pos; return position or -1
00288   FXint find_first_of(FXchar c,FXint pos=0) const;
00289 
00290   /// Find last character in the set of size n, starting from pos; return position or -1
00291   FXint find_last_of(const FXchar *set,FXint n,FXint pos) const;
00292 
00293   /// Find last character in the set, starting from pos; return position or -1
00294   FXint find_last_of(const FXchar *set,FXint pos=2147483647) const;
00295 
00296   /// Find last character in the set, starting from pos; return position or -1
00297   FXint find_last_of(const FXString &set,FXint pos=2147483647) const;
00298 
00299   /// Find last character, starting from pos; return position or -1
00300   FXint find_last_of(FXchar c,FXint pos=0) const;
00301 
00302   /// Find first character NOT in the set of size n, starting from pos; return position or -1
00303   FXint find_first_not_of(const FXchar *set,FXint n,FXint pos) const;
00304 
00305   /// Find first character NOT in the set, starting from pos; return position or -1
00306   FXint find_first_not_of(const FXchar *set,FXint pos=0) const;
00307 
00308   /// Find first character NOT in the set, starting from pos; return position or -1
00309   FXint find_first_not_of(const FXString &set,FXint pos=0) const;
00310 
00311   /// Find first character NOT equal to c, starting from pos; return position or -1
00312   FXint find_first_not_of(FXchar c,FXint pos=0) const;
00313 
00314   /// Find last character NOT in the set of size n, starting from pos; return position or -1
00315   FXint find_last_not_of(const FXchar *set,FXint n,FXint pos) const;
00316 
00317   /// Find last character NOT in the set, starting from pos; return position or -1
00318   FXint find_last_not_of(const FXchar *set,FXint pos=2147483647) const;
00319 
00320   /// Find last character NOT in the set, starting from pos; return position or -1
00321   FXint find_last_not_of(const FXString &set,FXint pos=2147483647) const;
00322 
00323   /// Find last character NOT equal to c, starting from pos; return position or -1
00324   FXint find_last_not_of(FXchar c,FXint pos=0) const;
00325 
00326   /// Find number of occurances of character in string
00327   FXint count(FXchar c) const;
00328 
00329   /// Format a string a-la printf
00330   FXString& format(const char *fmt,...) FX_PRINTF(2,3) ;
00331   FXString& vformat(const char *fmt,va_list args);
00332 
00333   /// Scan a string a-la scanf
00334   FXint scan(const char *fmt,...) const FX_SCANF(2,3) ;
00335   FXint vscan(const char *fmt,va_list args) const;
00336 
00337   /// Get hash value
00338   FXuint hash() const;
00339 
00340   /// Compare
00341   friend FXAPI FXint compare(const FXchar *s1,const FXchar *s2);
00342   friend FXAPI FXint compare(const FXchar *s1,const FXString &s2);
00343   friend FXAPI FXint compare(const FXString &s1,const FXchar *s2);
00344   friend FXAPI FXint compare(const FXString &s1,const FXString &s2);
00345 
00346   /// Compare up to n
00347   friend FXAPI FXint compare(const FXchar *s1,const FXchar *s2,FXint n);
00348   friend FXAPI FXint compare(const FXchar *s1,const FXString &s2,FXint n);
00349   friend FXAPI FXint compare(const FXString &s1,const FXchar *s2,FXint n);
00350   friend FXAPI FXint compare(const FXString &s1,const FXString &s2,FXint n);
00351 
00352   /// Compare case insensitive
00353   friend FXAPI FXint comparecase(const FXchar *s1,const FXchar *s2);
00354   friend FXAPI FXint comparecase(const FXchar *s1,const FXString &s2);
00355   friend FXAPI FXint comparecase(const FXString &s1,const FXchar *s2);
00356   friend FXAPI FXint comparecase(const FXString &s1,const FXString &s2);
00357 
00358   /// Compare case insensitive up to n
00359   friend FXAPI FXint comparecase(const FXchar *s1,const FXchar *s2,FXint n);
00360   friend FXAPI FXint comparecase(const FXchar *s1,const FXString &s2,FXint n);
00361   friend FXAPI FXint comparecase(const FXString &s1,const FXchar *s2,FXint n);
00362   friend FXAPI FXint comparecase(const FXString &s1,const FXString &s2,FXint n);
00363 
00364   /// Comparison operators
00365   friend FXAPI FXbool operator==(const FXString &s1,const FXString &s2);
00366   friend FXAPI FXbool operator==(const FXString &s1,const FXchar *s2);
00367   friend FXAPI FXbool operator==(const FXchar *s1,const FXString &s2);
00368 
00369   friend FXAPI FXbool operator!=(const FXString &s1,const FXString &s2);
00370   friend FXAPI FXbool operator!=(const FXString &s1,const FXchar *s2);
00371   friend FXAPI FXbool operator!=(const FXchar *s1,const FXString &s2);
00372 
00373   friend FXAPI FXbool operator<(const FXString &s1,const FXString &s2);
00374   friend FXAPI FXbool operator<(const FXString &s1,const FXchar *s2);
00375   friend FXAPI FXbool operator<(const FXchar *s1,const FXString &s2);
00376 
00377   friend FXAPI FXbool operator<=(const FXString &s1,const FXString &s2);
00378   friend FXAPI FXbool operator<=(const FXString &s1,const FXchar *s2);
00379   friend FXAPI FXbool operator<=(const FXchar *s1,const FXString &s2);
00380 
00381   friend FXAPI FXbool operator>(const FXString &s1,const FXString &s2);
00382   friend FXAPI FXbool operator>(const FXString &s1,const FXchar *s2);
00383   friend FXAPI FXbool operator>(const FXchar *s1,const FXString &s2);
00384 
00385   friend FXAPI FXbool operator>=(const FXString &s1,const FXString &s2);
00386   friend FXAPI FXbool operator>=(const FXString &s1,const FXchar *s2);
00387   friend FXAPI FXbool operator>=(const FXchar *s1,const FXString &s2);
00388 
00389   /// Append operators
00390   FXString& operator+=(const FXString& s);
00391   FXString& operator+=(const FXchar* s);
00392   FXString& operator+=(FXchar c);
00393 
00394   /// Concatenate two strings
00395   friend FXAPI FXString operator+(const FXString& s1,const FXString& s2);
00396   friend FXAPI FXString operator+(const FXString& s1,const FXchar* s2);
00397   friend FXAPI FXString operator+(const FXchar* s1,const FXString& s2);
00398 
00399   /// Concatenate with single character
00400   friend FXAPI FXString operator+(const FXString& s,FXchar c);
00401   friend FXAPI FXString operator+(FXchar c,const FXString& s);
00402 
00403   /// Saving to a stream
00404   friend FXAPI FXStream& operator<<(FXStream& store,const FXString& s);
00405 
00406   /// Load from a stream
00407   friend FXAPI FXStream& operator>>(FXStream& store,FXString& s);
00408 
00409   /// Format a string a-la printf
00410   friend FXAPI FXString FXStringFormat(const FXchar *fmt,...) FX_PRINTF(1,2) ;
00411   friend FXAPI FXString FXStringVFormat(const FXchar *fmt,va_list args);
00412 
00413   /**
00414   * Convert integer number to a string, using the given number
00415   * base, which must be between 2 and 16.
00416   */
00417   friend FXAPI FXString FXStringVal(FXint num,FXint base=10);
00418   friend FXAPI FXString FXStringVal(FXuint num,FXint base=10);
00419 
00420   /**
00421   * Convert real number to a string, using the given procision and
00422   * exponential notation mode, which may be FALSE (never), TRUE (always), or
00423   * MAYBE (when needed).
00424   */
00425   friend FXAPI FXString FXStringVal(FXfloat num,FXint prec=6,FXbool exp=MAYBE);
00426   friend FXAPI FXString FXStringVal(FXdouble num,FXint prec=6,FXbool exp=MAYBE);
00427 
00428   /// Convert string to a integer number, assuming given number base
00429   friend FXAPI FXint FXIntVal(const FXString& s,FXint base=10);
00430   friend FXAPI FXuint FXUIntVal(const FXString& s,FXint base=10);
00431 
00432   /// Convert string into real number
00433   friend FXAPI FXfloat FXFloatVal(const FXString& s);
00434   friend FXAPI FXdouble FXDoubleVal(const FXString& s);
00435 
00436   /// Escape special characters in a string
00437   friend FXAPI FXString escape(const FXString& s);
00438 
00439   /// Unescape special characters in a string
00440   friend FXAPI FXString unescape(const FXString& s);
00441 
00442   /// Swap two strings
00443   friend FXAPI void swap(FXString& a,FXString& b){ FXchar *t=a.str; a.str=b.str; b.str=t; }
00444 
00445   /// Delete
00446  ~FXString();
00447   };
00448 
00449 }
00450 
00451 #endif