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 #include "common/str-enc.h"
00028 
00029 #include <stdarg.h>
00030 
00031 namespace Common {
00032 
00033 class U32String;
00034 
00049 class String {
00050 public:
00051     static const uint32 npos = 0xFFFFFFFF;
00052 
00053     static void releaseMemoryPoolMutex();
00054 
00055     typedef char          value_type;
00061     typedef unsigned char unsigned_type;
00062     typedef char *        iterator;
00063     typedef const char *  const_iterator;
00064 
00065 protected:
00077     static const uint32 _builtinCapacity = 32 - sizeof(uint32) - sizeof(char *);
00078 
00084     uint32 _size;
00085 
00090     char  *_str;
00091 
00092 
00093     union {
00097         char _storage[_builtinCapacity];
00102         struct {
00103             mutable int *_refCount;
00104             uint32       _capacity;
00105         } _extern;
00106     };
00107 
00108     inline bool isStorageIntern() const {
00109         return _str == _storage;
00110     }
00111 
00112 public:
00114     String() : _size(0), _str(_storage) { _storage[0] = 0; }
00115 
00117     String(const char *str);
00118 
00120     String(const char *str, uint32 len);
00121 
00123     String(const char *beginP, const char *endP);
00124 
00126     String(const String &str);
00127 
00129     explicit String(char c);
00130 
00131     ~String();
00132 
00133     String &operator=(const char *str);
00134     String &operator=(const String &str);
00135     String &operator=(char c);
00136     String &operator+=(const char *str);
00137     String &operator+=(const String &str);
00138     String &operator+=(char c);
00139 
00140     bool operator==(const String &x) const;
00141     bool operator==(const char *x) const;
00142     bool operator!=(const String &x) const;
00143     bool operator!=(const char *x) const;
00144 
00145     bool operator<(const String &x) const;
00146     bool operator<=(const String &x) const;
00147     bool operator>(const String &x) const;
00148     bool operator>=(const String &x) const;
00149 
00150     bool equals(const String &x) const;
00151     bool equalsIgnoreCase(const String &x) const;
00152     int compareTo(const String &x) const;           // strcmp clone
00153     int compareToIgnoreCase(const String &x) const; // stricmp clone
00154 
00155     bool equals(const char *x) const;
00156     bool equalsIgnoreCase(const char *x) const;
00157     int compareTo(const char *x) const;             // strcmp clone
00158     int compareToIgnoreCase(const char *x) const;   // stricmp clone
00159     int compareDictionary(const String &x) const;
00160     int compareDictionary(const char *x) const;
00161 
00162     bool hasSuffix(const String &x) const;
00163     bool hasSuffix(const char *x) const;
00164     bool hasSuffixIgnoreCase(const String &x) const;
00165     bool hasSuffixIgnoreCase(const char *x) const;
00166 
00167     bool hasPrefix(const String &x) const;
00168     bool hasPrefix(const char *x) const;
00169     bool hasPrefixIgnoreCase(const String &x) const;
00170     bool hasPrefixIgnoreCase(const char *x) const;
00171 
00172     bool contains(const String &x) const;
00173     bool contains(const char *x) const;
00174     bool contains(char x) const;
00175 
00177     uint64 asUint64() const;
00178 
00204     bool matchString(const char *pat, bool ignoreCase = false, bool pathMode = false) const;
00205     bool matchString(const String &pat, bool ignoreCase = false, bool pathMode = false) const;
00206 
00207 
00208     inline const char *c_str() const { return _str; }
00209     inline uint size() const         { return _size; }
00210 
00211     inline bool empty() const { return (_size == 0); }
00212     char firstChar() const    { return (_size > 0) ? _str[0] : 0; }
00213     char lastChar() const     { return (_size > 0) ? _str[_size - 1] : 0; }
00214 
00215     char operator[](int idx) const {
00216         assert(_str && idx >= 0 && idx < (int)_size);
00217         return _str[idx];
00218     }
00219 
00221     void deleteLastChar();
00222 
00224     void deleteChar(uint32 p);
00225 
00227     void erase(uint32 p, uint32 len = npos);
00228 
00230     iterator erase(iterator it);
00231 
00233     void setChar(char c, uint32 p);
00234 
00236     void insertChar(char c, uint32 p);
00237 
00239     void clear();
00240 
00242     void toLowercase();
00243 
00245     void toUppercase();
00246 
00251     void trim();
00252 
00262     void wordWrap(const uint32 maxLength);
00263 
00264     uint hash() const;
00265 
00280     // Replace 'count' bytes, starting from 'pos' with str.
00281     void replace(uint32 pos, uint32 count, const String &str);
00282     // The same as above, but accepts a C-like array of characters.
00283     void replace(uint32 pos, uint32 count, const char *str);
00284     // Replace the characters in [begin, end) with str._str.
00285     void replace(iterator begin, iterator end, const String &str);
00286     // Replace the characters in [begin, end) with str.
00287     void replace(iterator begin, iterator end, const char *str);
00288     // Replace _str[posOri, posOri + countOri) with
00289     // str._str[posDest, posDest + countDest)
00290     void replace(uint32 posOri, uint32 countOri, const String &str,
00291                     uint32 posDest, uint32 countDest);
00292     // Replace _str[posOri, posOri + countOri) with
00293     // str[posDest, posDest + countDest)
00294     void replace(uint32 posOri, uint32 countOri, const char *str,
00295                     uint32 posDest, uint32 countDest);
00303     static String format(const char *fmt, ...) GCC_PRINTF(1, 2);
00304 
00310     static String vformat(const char *fmt, va_list args);
00311 
00313     size_t find(char c, size_t pos = 0) const;
00314 
00316     size_t find(const char *s) const;
00317     uint32 find(const String &str, uint32 pos = 0) const;
00318 
00320     size_t rfind(const char *s) const;
00321     size_t rfind(const String &s) const {
00322         return rfind(s.c_str());
00323     }
00324 
00326     size_t rfind(char c, size_t pos = npos) const;
00327 
00329     size_t findFirstOf(char c, size_t pos = 0) const;
00330 
00332     size_t findFirstOf(const char *chars, size_t pos = 0) const;
00333     size_t findFirstOf(const String &chars, size_t pos = 0) const {
00334         return findFirstOf(chars.c_str(), pos);
00335     }
00336 
00338     size_t findLastOf(char c, size_t pos = npos) const;
00339 
00341     size_t findLastOf(const char *chars, size_t pos = npos) const;
00342     size_t findLastOf(const String &chars, size_t pos = npos) const {
00343         return findLastOf(chars.c_str(), pos);
00344     }
00345 
00347     size_t findFirstNotOf(char c, size_t pos = 0) const;
00348 
00350     size_t findFirstNotOf(const char *chars, size_t pos = 0) const;
00351     size_t findFirstNotOf(const String &chars, size_t pos = 0) const {
00352         return findFirstNotOf(chars.c_str(), pos);
00353     }
00354 
00356     size_t findLastNotOf(char c) const;
00357 
00359     size_t findLastNotOf(const char *chars) const;
00360     size_t findLastNotOf(const String &chars) const {
00361         return findLastNotOf(chars.c_str());
00362     }
00363 
00365     String substr(size_t pos = 0, size_t len = npos) const;
00366 
00367 public:
00368 
00369     iterator begin() {
00370         // Since the user could potentially
00371         // change the string via the returned
00372         // iterator we have to assure we are
00373         // pointing to a unique storage.
00374         makeUnique();
00375 
00376         return _str;
00377     }
00378 
00379     iterator end() {
00380         return begin() + size();
00381     }
00382 
00383     const_iterator begin() const {
00384         return _str;
00385     }
00386 
00387     const_iterator end() const {
00388         return begin() + size();
00389     }
00390 
00392     U32String decode(CodePage page = kUtf8) const;
00393 
00394 protected:
00395     void makeUnique();
00396     void ensureCapacity(uint32 new_size, bool keep_old);
00397     void incRefCount() const;
00398     void decRefCount(int *oldRefCount);
00399     void initWithCStr(const char *str, uint32 len);
00400 
00401     void decodeUTF8(U32String &dst) const;
00402     void decodeOneByte(U32String &dst, CodePage page) const;
00403 };
00404 
00405 // Append two strings to form a new (temp) string
00406 String operator+(const String &x, const String &y);
00407 
00408 String operator+(const char *x, const String &y);
00409 String operator+(const String &x, const char *y);
00410 
00411 String operator+(const String &x, char y);
00412 String operator+(char x, const String &y);
00413 
00414 // Some useful additional comparison operators for Strings
00415 bool operator==(const char *x, const String &y);
00416 bool operator!=(const char *x, const String &y);
00417 
00418 // Utility functions to remove leading and trailing whitespaces
00419 extern char *ltrim(char *t);
00420 extern char *rtrim(char *t);
00421 extern char *trim(char *t);
00422 
00423 
00436 String lastPathComponent(const String &path, const char sep);
00437 
00450 String normalizePath(const String &path, const char sep);
00451 
00452 
00478 bool matchString(const char *str, const char *pat, bool ignoreCase = false, bool pathMode = false);
00479 
00488 void replace(Common::String &source, const Common::String &what, const Common::String &with);
00489 
00495 String tag2string(uint32 tag);
00496 
00512 size_t strlcpy(char *dst, const char *src, size_t size);
00513 
00532 size_t strlcat(char *dst, const char *src, size_t size);
00533 
00543 size_t strnlen(const char *src, size_t maxSize);
00544 
00550 #define tag2str(x)  Common::tag2string(x).c_str()
00551 
00560 String toPrintable(const String &src, bool keepNewLines = true);
00561 
00562 } // End of namespace Common
00563 
00564 extern int scumm_stricmp(const char *s1, const char *s2);
00565 extern int scumm_strnicmp(const char *s1, const char *s2, uint n);
00566 extern char *scumm_strdup(const char *in);
00567 
00568 extern int scumm_compareDictionary(const char *s1, const char *s2);
00569 extern const char *scumm_skipArticle(const char *s1);
00570 
00571 extern const char *scumm_strcasestr(const char *s, const char *find);
00572 
00573 #endif


Generated on Sat Jul 4 2020 05:01:14 for ResidualVM by doxygen 1.7.1
curved edge   curved edge