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 
00198     bool matchString(const char *pat, bool ignoreCase = false, bool pathMode = false) const;
00199     bool matchString(const String &pat, bool ignoreCase = false, bool pathMode = false) const;
00200 
00201 
00202     inline const char *c_str() const { return _str; }
00203     inline uint size() const         { return _size; }
00204 
00205     inline bool empty() const { return (_size == 0); }
00206     char firstChar() const    { return (_size > 0) ? _str[0] : 0; }
00207     char lastChar() const     { return (_size > 0) ? _str[_size - 1] : 0; }
00208 
00209     char operator[](int idx) const {
00210         assert(_str && idx >= 0 && idx < (int)_size);
00211         return _str[idx];
00212     }
00213 
00215     void deleteLastChar();
00216 
00218     void deleteChar(uint32 p);
00219 
00221     void erase(uint32 p, uint32 len = npos);
00222 
00224     void setChar(char c, uint32 p);
00225 
00227     void insertChar(char c, uint32 p);
00228 
00230     void clear();
00231 
00233     void toLowercase();
00234 
00236     void toUppercase();
00237 
00242     void trim();
00243 
00253     void wordWrap(const uint32 maxLength);
00254 
00255     uint hash() const;
00256 
00271     // Replace 'count' bytes, starting from 'pos' with str.
00272     void replace(uint32 pos, uint32 count, const String &str);
00273     // The same as above, but accepts a C-like array of characters.
00274     void replace(uint32 pos, uint32 count, const char *str);
00275     // Replace the characters in [begin, end) with str._str.
00276     void replace(iterator begin, iterator end, const String &str);
00277     // Replace the characters in [begin, end) with str.
00278     void replace(iterator begin, iterator end, const char *str);
00279     // Replace _str[posOri, posOri + countOri) with
00280     // str._str[posDest, posDest + countDest)
00281     void replace(uint32 posOri, uint32 countOri, const String &str,
00282                     uint32 posDest, uint32 countDest);
00283     // Replace _str[posOri, posOri + countOri) with
00284     // str[posDest, posDest + countDest)
00285     void replace(uint32 posOri, uint32 countOri, const char *str,
00286                     uint32 posDest, uint32 countDest);
00294     static String format(const char *fmt, ...) GCC_PRINTF(1, 2);
00295 
00301     static String vformat(const char *fmt, va_list args);
00302 
00303 public:
00304 
00305     iterator begin() {
00306         // Since the user could potentially
00307         // change the string via the returned
00308         // iterator we have to assure we are
00309         // pointing to a unique storage.
00310         makeUnique();
00311 
00312         return _str;
00313     }
00314 
00315     iterator end() {
00316         return begin() + size();
00317     }
00318 
00319     const_iterator begin() const {
00320         return _str;
00321     }
00322 
00323     const_iterator end() const {
00324         return begin() + size();
00325     }
00326 
00327 protected:
00328     void makeUnique();
00329     void ensureCapacity(uint32 new_size, bool keep_old);
00330     void incRefCount() const;
00331     void decRefCount(int *oldRefCount);
00332     void initWithCStr(const char *str, uint32 len);
00333 };
00334 
00335 // Append two strings to form a new (temp) string
00336 String operator+(const String &x, const String &y);
00337 
00338 String operator+(const char *x, const String &y);
00339 String operator+(const String &x, const char *y);
00340 
00341 String operator+(const String &x, char y);
00342 String operator+(char x, const String &y);
00343 
00344 // Some useful additional comparison operators for Strings
00345 bool operator==(const char *x, const String &y);
00346 bool operator!=(const char *x, const String &y);
00347 
00348 // Utility functions to remove leading and trailing whitespaces
00349 extern char *ltrim(char *t);
00350 extern char *rtrim(char *t);
00351 extern char *trim(char *t);
00352 
00353 
00366 String lastPathComponent(const String &path, const char sep);
00367 
00380 String normalizePath(const String &path, const char sep);
00381 
00382 
00408 bool matchString(const char *str, const char *pat, bool ignoreCase = false, bool pathMode = false);
00409 
00418 void replace(Common::String &source, const Common::String &what, const Common::String &with);
00419 
00425 String tag2string(uint32 tag);
00426 
00442 size_t strlcpy(char *dst, const char *src, size_t size);
00443 
00462 size_t strlcat(char *dst, const char *src, size_t size);
00463 
00473 size_t strnlen(const char *src, size_t maxSize);
00474 
00480 #define tag2str(x)  Common::tag2string(x).c_str()
00481 
00482 
00483 } // End of namespace Common
00484 
00485 extern int scumm_stricmp(const char *s1, const char *s2);
00486 extern int scumm_strnicmp(const char *s1, const char *s2, uint n);
00487 extern char *scumm_strdup(const char *in);
00488 
00489 #endif


Generated on Sat May 18 2019 05:01:21 for ResidualVM by doxygen 1.7.1
curved edge   curved edge