![]() |
Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members
![]() |
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.38 2002/02/08 14:27:20 fox Exp $ * 00023 ********************************************************************************/ 00024 #ifndef FXSTRING_H 00025 #define FXSTRING_H 00026 00027 00028 00029 /** 00030 * FXString provides essential string manipulation capabilities. 00031 */ 00032 class FXAPI FXString { 00033 private: 00034 FXchar* str; 00035 public: 00036 static const FXchar null[]; 00037 static const FXchar hex[17]; 00038 static const FXchar HEX[17]; 00039 public: 00040 00041 /// Create empty string 00042 FXString(); 00043 00044 /// Copy construct 00045 FXString(const FXString& s); 00046 00047 /// Construct and init 00048 FXString(const FXchar* s); 00049 00050 /// Construct and init with substring 00051 FXString(const FXchar* s,FXint n); 00052 00053 /// Construct and fill with constant 00054 FXString(FXchar c,FXint n); 00055 00056 /// Construct string from two parts 00057 FXString(const FXchar *s1,const FXchar* s2); 00058 00059 /// Size to some desired capacity 00060 void size(FXint sz); 00061 00062 /// Size of text data, 0 for empty string 00063 FXint size() const; 00064 00065 /// Length of text 00066 FXint length() const { return strlen(str); } 00067 00068 /// Get text contents 00069 const FXchar* text() const { return (const FXchar*)str; } 00070 00071 /// See if string is empty 00072 FXbool empty() const { return str[0]==0; } 00073 00074 /// Return a non-const reference to the ith character 00075 FXchar& operator[](FXint i){ return str[i]; } 00076 00077 /// Return a const reference to the ith character 00078 const FXchar& operator[](FXint i) const { return str[i]; } 00079 00080 /// Assign another string to this 00081 FXString& operator=(const FXString& s); 00082 00083 /// Assign a C-style string to this 00084 FXString& operator=(const FXchar* s); 00085 00086 /// Fill with a constant 00087 FXString& fill(FXchar c,FXint n); 00088 00089 /// Fill up to current length 00090 FXString& fill(FXchar c); 00091 00092 /// Convert to lower case 00093 FXString& lower(); 00094 00095 /// Convert to upper case 00096 FXString& upper(); 00097 00098 /// Extract partition of delimiter separated string 00099 FXString extract(FXint part,FXchar delim) const; 00100 00101 /// Extract partition of delimiter separated string 00102 FXString extract(FXint part,FXchar delim,FXchar esc) const; 00103 00104 /// Insert character at specified position 00105 FXString& insert(FXint pos,FXchar c); 00106 00107 /// Insert n characters c at specified position 00108 FXString& insert(FXint pos,FXchar c,FXint n); 00109 00110 /// Insert first n characters of string at specified position 00111 FXString& insert(FXint pos,const FXchar* s,FXint n); 00112 00113 /// Insert string at specified position 00114 FXString& insert(FXint pos,const FXString& s); 00115 00116 /// Insert string at specified position 00117 FXString& insert(FXint pos,const FXchar* s); 00118 00119 /// Prepend string with input character 00120 FXString& prepend(FXchar c); 00121 00122 /// Prepend string with n characters c 00123 FXString& prepend(FXchar c,FXint n); 00124 00125 /// Prepend string with first n characters of input string 00126 FXString& prepend(const FXchar *s,FXint n); 00127 00128 /// Prepend string with input string 00129 FXString& prepend(const FXString& s); 00130 00131 /// Prepend string with input string 00132 FXString& prepend(const FXchar *s); 00133 00134 /// Append input character to this string 00135 FXString& append(FXchar c); 00136 00137 /// Append input n characters c to this string 00138 FXString& append(FXchar c,FXint n); 00139 00140 /// Append first n characters of input string to this string 00141 FXString& append(const FXchar *s,FXint n); 00142 00143 /// Append input string to this string 00144 FXString& append(const FXString& s); 00145 00146 /// Append input string to this string 00147 FXString& append(const FXchar *s); 00148 00149 /// Replace a single character 00150 FXString& replace(FXint pos,FXchar c); 00151 00152 /// Replace the m characters at pos with n characters c 00153 FXString& replace(FXint pos,FXint m,FXchar c,FXint n); 00154 00155 /// Replaces the m characters at pos with first n characters of input string 00156 FXString& replace(FXint pos,FXint m,const FXchar *s,FXint n); 00157 00158 /// Replace the m characters at pos with input string 00159 FXString& replace(FXint pos,FXint m,const FXString& s); 00160 00161 /// Replace the m characters at pos with input string 00162 FXString& replace(FXint pos,FXint m,const FXchar *s); 00163 00164 /// Remove substring 00165 FXString& remove(FXint pos,FXint n=1); 00166 00167 /// Substitute one character by another 00168 FXString& substitute(FXchar orig,FXchar sub); 00169 00170 /// Remove leading and trailing whitespace 00171 FXString& trim(); 00172 00173 /// Remove leading whitespace 00174 FXString& trimBegin(); 00175 00176 /// Remove trailing whitespace 00177 FXString& trimEnd(); 00178 00179 /// Truncate string at pos 00180 FXString& trunc(FXint pos); 00181 00182 /// Clear 00183 FXString& clear(); 00184 00185 /// Get leftmost part 00186 FXString left(FXint n) const; 00187 00188 /// Get rightmost part 00189 FXString right(FXint n) const; 00190 00191 /// Get some part in the middle 00192 FXString mid(FXint pos,FXint n) const; 00193 00194 /** 00195 * Return all characters before the n-th occurrence of ch, 00196 * counting from the beginning of the string if n>0, or 00197 * from the end if n<0. 00198 * If the character ch is not found, the entire string, 00199 * or the empty string, is returned, respectively. 00200 * A NULL string is returned if n==0. 00201 */ 00202 FXString before(FXchar ch,FXint n=1) const; 00203 00204 /** 00205 * Return all characters after the nth occurrence of ch, 00206 * counting from the beginning of the string if n>0, or from 00207 * the end if n<0. 00208 * If the character ch is not found, the empty string, or 00209 * the entire string, is returned, respectively. 00210 * A NULL string is returned if n==0. 00211 */ 00212 FXString after(FXchar ch,FXint n=1) const; 00213 00214 /// Find a character, searching forward; return position or -1 00215 FXint findf(FXchar c,FXint pos=0) const; 00216 00217 /// Find a character, searching backward; return position or -1 00218 FXint findb(FXchar c,FXint pos=2147483647) const; 00219 00220 /// Find a substring of length n, searching forward; return position or -1 00221 FXint findf(const FXchar* substr,FXint n,FXint pos) const; 00222 00223 /// Find a substring of length n, searching backward; return position or -1 00224 FXint findb(const FXchar* substr,FXint n,FXint pos) const; 00225 00226 /// Find a substring, searching forward; return position or -1 00227 FXint findf(const FXchar* substr,FXint pos=0) const; 00228 00229 /// Find a substring, searching backward; return position or -1 00230 FXint findb(const FXchar* substr,FXint pos=2147483647) const; 00231 00232 /// Find a substring, searching forward; return position or -1 00233 FXint findf(const FXString &substr,FXint pos=0) const; 00234 00235 /// Find a substring, searching backward; return position or -1 00236 FXint findb(const FXString &substr,FXint pos=2147483647) const; 00237 00238 /// Find number of occurances of character in string 00239 FXint count(FXchar c) const; 00240 00241 /// Format a string a-la printf 00242 FXString& format(const char *fmt,...) FX_PRINTF(2,3) ; 00243 FXString& vformat(const char *fmt,va_list args); 00244 00245 /// Scan a string a-la scanf 00246 FXint scan(const char *fmt,...) const FX_SCANF(2,3) ; 00247 FXint vscan(const char *fmt,va_list args) const; 00248 00249 /// Get hash value 00250 FXint hash() const; 00251 00252 /// Compare 00253 friend FXAPI FXint compare(const FXchar *s1,const FXchar *s2); 00254 friend FXAPI FXint compare(const FXchar *s1,const FXString &s2); 00255 friend FXAPI FXint compare(const FXString &s1,const FXchar *s2); 00256 friend FXAPI FXint compare(const FXString &s1,const FXString &s2); 00257 00258 /// Compare up to n 00259 friend FXAPI FXint compare(const FXchar *s1,const FXchar *s2,FXint n); 00260 friend FXAPI FXint compare(const FXchar *s1,const FXString &s2,FXint n); 00261 friend FXAPI FXint compare(const FXString &s1,const FXchar *s2,FXint n); 00262 friend FXAPI FXint compare(const FXString &s1,const FXString &s2,FXint n); 00263 00264 /// Compare case insensitive 00265 friend FXAPI FXint comparecase(const FXchar *s1,const FXchar *s2); 00266 friend FXAPI FXint comparecase(const FXchar *s1,const FXString &s2); 00267 friend FXAPI FXint comparecase(const FXString &s1,const FXchar *s2); 00268 friend FXAPI FXint comparecase(const FXString &s1,const FXString &s2); 00269 00270 /// Compare case insensitive up to n 00271 friend FXAPI FXint comparecase(const FXchar *s1,const FXchar *s2,FXint n); 00272 friend FXAPI FXint comparecase(const FXchar *s1,const FXString &s2,FXint n); 00273 friend FXAPI FXint comparecase(const FXString &s1,const FXchar *s2,FXint n); 00274 friend FXAPI FXint comparecase(const FXString &s1,const FXString &s2,FXint n); 00275 00276 /// Comparison operators 00277 friend FXAPI FXbool operator==(const FXString &s1,const FXString &s2); 00278 friend FXAPI FXbool operator==(const FXString &s1,const FXchar *s2); 00279 friend FXAPI FXbool operator==(const FXchar *s1,const FXString &s2); 00280 00281 friend FXAPI FXbool operator!=(const FXString &s1,const FXString &s2); 00282 friend FXAPI FXbool operator!=(const FXString &s1,const FXchar *s2); 00283 friend FXAPI FXbool operator!=(const FXchar *s1,const FXString &s2); 00284 00285 friend FXAPI FXbool operator<(const FXString &s1,const FXString &s2); 00286 friend FXAPI FXbool operator<(const FXString &s1,const FXchar *s2); 00287 friend FXAPI FXbool operator<(const FXchar *s1,const FXString &s2); 00288 00289 friend FXAPI FXbool operator<=(const FXString &s1,const FXString &s2); 00290 friend FXAPI FXbool operator<=(const FXString &s1,const FXchar *s2); 00291 friend FXAPI FXbool operator<=(const FXchar *s1,const FXString &s2); 00292 00293 friend FXAPI FXbool operator>(const FXString &s1,const FXString &s2); 00294 friend FXAPI FXbool operator>(const FXString &s1,const FXchar *s2); 00295 friend FXAPI FXbool operator>(const FXchar *s1,const FXString &s2); 00296 00297 friend FXAPI FXbool operator>=(const FXString &s1,const FXString &s2); 00298 friend FXAPI FXbool operator>=(const FXString &s1,const FXchar *s2); 00299 friend FXAPI FXbool operator>=(const FXchar *s1,const FXString &s2); 00300 00301 /// Append operators 00302 FXString& operator+=(const FXString& s); 00303 FXString& operator+=(const FXchar* s); 00304 FXString& operator+=(FXchar c); 00305 00306 /// Concatenate two strings 00307 friend FXAPI FXString operator+(const FXString& s1,const FXString& s2); 00308 friend FXAPI FXString operator+(const FXString& s1,const FXchar* s2); 00309 friend FXAPI FXString operator+(const FXchar* s1,const FXString& s2); 00310 00311 /// Concatenate with single character 00312 friend FXAPI FXString operator+(const FXString& s,FXchar c); 00313 friend FXAPI FXString operator+(FXchar c,const FXString& s); 00314 00315 /// Saving to a stream 00316 friend FXAPI FXStream& operator<<(FXStream& store,const FXString& s); 00317 00318 /// Load from a stream 00319 friend FXAPI FXStream& operator>>(FXStream& store,FXString& s); 00320 00321 /// Format a string a-la printf 00322 friend FXAPI FXString FXStringFormat(const FXchar *fmt,...) FX_PRINTF(1,2) ; 00323 friend FXAPI FXString FXStringVFormat(const FXchar *fmt,va_list args); 00324 00325 /** 00326 * Convert integer number to a string, using the given number 00327 * base, which must be between 2 and 16. 00328 */ 00329 friend FXAPI FXString FXStringVal(FXint num,FXint base=10); 00330 friend FXAPI FXString FXStringVal(FXuint num,FXint base=10); 00331 00332 /** 00333 * Convert real number to a string, using the given procision and 00334 * exponential notation mode, which may be FALSE (never), TRUE (always), or 00335 * MAYBE (when needed). 00336 */ 00337 friend FXAPI FXString FXStringVal(FXfloat num,FXint prec=6,FXbool exp=MAYBE); 00338 friend FXAPI FXString FXStringVal(FXdouble num,FXint prec=6,FXbool exp=MAYBE); 00339 00340 /// Convert string to a integer number, assuming given number base 00341 friend FXAPI FXint FXIntVal(const FXString& s,FXint base=10); 00342 friend FXAPI FXuint FXUIntVal(const FXString& s,FXint base=10); 00343 00344 /// Convert string into real number 00345 friend FXAPI FXfloat FXFloatVal(const FXString& s); 00346 friend FXAPI FXdouble FXDoubleVal(const FXString& s); 00347 00348 /// Delete 00349 ~FXString(); 00350 }; 00351 00352 00353 #endif