ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

str.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_STRING_H
00024 #define COMMON_STRING_H
00025 
00026 #include "common/scummsys.h"
00027 
00028 #include <stdarg.h>
00029 
00030 namespace Common {
00031 
00046 class String {
00047 public:
00048     static const uint32 npos = 0xFFFFFFFF;
00049 
00050     static void releaseMemoryPoolMutex();
00051 
00052     typedef char          value_type;
00058     typedef unsigned char unsigned_type;
00059     typedef char *        iterator;
00060     typedef const char *  const_iterator;
00061 
00062 protected:
00074     static const uint32 _builtinCapacity = 32 - sizeof(uint32) - sizeof(char *);
00075 
00081     uint32 _size;
00082 
00087     char  *_str;
00088 
00089 
00090     union {
00094         char _storage[_builtinCapacity];
00099         struct {
00100             mutable int *_refCount;
00101             uint32       _capacity;
00102         } _extern;
00103     };
00104 
00105     inline bool isStorageIntern() const {
00106         return _str == _storage;
00107     }
00108 
00109 public:
00111     String() : _size(0), _str(_storage) { _storage[0] = 0; }
00112 
00114     String(const char *str);
00115 
00117     String(const char *str, uint32 len);
00118 
00120     String(const char *beginP, const char *endP);
00121 
00123     String(const String &str);
00124 
00126     explicit String(char c);
00127 
00128     ~String();
00129 
00130     String &operator=(const char *str);
00131     String &operator=(const String &str);
00132     String &operator=(char c);
00133     String &operator+=(const char *str);
00134     String &operator+=(const String &str);
00135     String &operator+=(char c);
00136 
00137     bool operator==(const String &x) const;
00138     bool operator==(const char *x) const;
00139     bool operator!=(const String &x) const;
00140     bool operator!=(const char *x) const;
00141 
00142     bool operator<(const String &x) const;
00143     bool operator<=(const String &x) const;
00144     bool operator>(const String &x) const;
00145     bool operator>=(const String &x) const;
00146 
00147     bool equals(const String &x) const;
00148     bool equalsIgnoreCase(const String &x) const;
00149     int compareTo(const String &x) const;           // strcmp clone
00150     int compareToIgnoreCase(const String &x) const; // stricmp clone
00151 
00152     bool equals(const char *x) const;
00153     bool equalsIgnoreCase(const char *x) const;
00154     int compareTo(const char *x) const;             // strcmp clone
00155     int compareToIgnoreCase(const char *x) const;   // stricmp clone
00156 
00157     bool hasSuffix(const String &x) const;
00158     bool hasSuffix(const char *x) const;
00159     bool hasSuffixIgnoreCase(const String &x) const;
00160     bool hasSuffixIgnoreCase(const char *x) const;
00161 
00162     bool hasPrefix(const String &x) const;
00163     bool hasPrefix(const char *x) const;
00164     bool hasPrefixIgnoreCase(const String &x) const;
00165     bool hasPrefixIgnoreCase(const char *x) const;
00166 
00167     bool contains(const String &x) const;
00168     bool contains(const char *x) const;
00169     bool contains(char x) const;
00170 
00172     uint64 asUint64() const;
00173 
00199     bool matchString(const char *pat, bool ignoreCase = false, bool pathMode = false) const;
00200     bool matchString(const String &pat, bool ignoreCase = false, bool pathMode = false) const;
00201 
00202 
00203     inline const char *c_str() const { return _str; }
00204     inline uint size() const         { return _size; }
00205 
00206     inline bool empty() const { return (_size == 0); }
00207     char firstChar() const    { return (_size > 0) ? _str[0] : 0; }
00208     char lastChar() const     { return (_size > 0) ? _str[_size - 1] : 0; }
00209 
00210     char operator[](int idx) const {
00211         assert(_str && idx >= 0 && idx < (int)_size);
00212         return _str[idx];
00213     }
00214 
00216     void deleteLastChar();
00217 
00219     void deleteChar(uint32 p);
00220 
00222     void erase(uint32 p, uint32 len = npos);
00223 
00225     void setChar(char c, uint32 p);
00226 
00228     void insertChar(char c, uint32 p);
00229 
00231     void clear();
00232 
00234     void toLowercase();
00235 
00237     void toUppercase();
00238 
00243     void trim();
00244 
00254     void wordWrap(const uint32 maxLength);
00255 
00256     uint hash() const;
00257 
00272     // Replace 'count' bytes, starting from 'pos' with str.
00273     void replace(uint32 pos, uint32 count, const String &str);
00274     // The same as above, but accepts a C-like array of characters.
00275     void replace(uint32 pos, uint32 count, const char *str);
00276     // Replace the characters in [begin, end) with str._str.
00277     void replace(iterator begin, iterator end, const String &str);
00278     // Replace the characters in [begin, end) with str.
00279     void replace(iterator begin, iterator end, const char *str);
00280     // Replace _str[posOri, posOri + countOri) with
00281     // str._str[posDest, posDest + countDest)
00282     void replace(uint32 posOri, uint32 countOri, const String &str,
00283                     uint32 posDest, uint32 countDest);
00284     // Replace _str[posOri, posOri + countOri) with
00285     // str[posDest, posDest + countDest)
00286     void replace(uint32 posOri, uint32 countOri, const char *str,
00287                     uint32 posDest, uint32 countDest);
00295     static String format(const char *fmt, ...) GCC_PRINTF(1, 2);
00296 
00302     static String vformat(const char *fmt, va_list args);
00303 
00304 public:
00305 
00306     iterator begin() {
00307         // Since the user could potentially
00308         // change the string via the returned
00309         // iterator we have to assure we are
00310         // pointing to a unique storage.
00311         makeUnique();
00312 
00313         return _str;
00314     }
00315 
00316     iterator end() {
00317         return begin() + size();
00318     }
00319 
00320     const_iterator begin() const {
00321         return _str;
00322     }
00323 
00324     const_iterator end() const {
00325         return begin() + size();
00326     }
00327 
00328 protected:
00329     void makeUnique();
00330     void ensureCapacity(uint32 new_size, bool keep_old);
00331     void incRefCount() const;
00332     void decRefCount(int *oldRefCount);
00333     void initWithCStr(const char *str, uint32 len);
00334 };
00335 
00336 // Append two strings to form a new (temp) string
00337 String operator+(const String &x, const String &y);
00338 
00339 String operator+(const char *x, const String &y);
00340 String operator+(const String &x, const char *y);
00341 
00342 String operator+(const String &x, char y);
00343 String operator+(char x, const String &y);
00344 
00345 // Some useful additional comparison operators for Strings
00346 bool operator==(const char *x, const String &y);
00347 bool operator!=(const char *x, const String &y);
00348 
00349 // Utility functions to remove leading and trailing whitespaces
00350 extern char *ltrim(char *t);
00351 extern char *rtrim(char *t);
00352 extern char *trim(char *t);
00353 
00354 
00367 String lastPathComponent(const String &path, const char sep);
00368 
00381 String normalizePath(const String &path, const char sep);
00382 
00383 
00409 bool matchString(const char *str, const char *pat, bool ignoreCase = false, bool pathMode = false);
00410 
00419 void replace(Common::String &source, const Common::String &what, const Common::String &with);
00420 
00426 String tag2string(uint32 tag);
00427 
00443 size_t strlcpy(char *dst, const char *src, size_t size);
00444 
00463 size_t strlcat(char *dst, const char *src, size_t size);
00464 
00474 size_t strnlen(const char *src, size_t maxSize);
00475 
00481 #define tag2str(x)  Common::tag2string(x).c_str()
00482 
00483 
00484 } // End of namespace Common
00485 
00486 extern int scumm_stricmp(const char *s1, const char *s2);
00487 extern int scumm_strnicmp(const char *s1, const char *s2, uint n);
00488 extern char *scumm_strdup(const char *in);
00489 
00490 #endif


Generated on Sat Sep 14 2019 05:01:05 for ResidualVM by doxygen 1.7.1
curved edge   curved edge