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 #include "common/ustr.h"
00029 
00030 #include <stdarg.h>
00031 
00032 namespace Common {
00033 
00034 class U32String;
00035 
00050 class String {
00051 public:
00052     static const uint32 npos = 0xFFFFFFFF;
00053 
00054     static void releaseMemoryPoolMutex();
00055 
00056     typedef char          value_type;
00062     typedef unsigned char unsigned_type;
00063     typedef char *        iterator;
00064     typedef const char *  const_iterator;
00065 
00066 protected:
00078     static const uint32 _builtinCapacity = 32 - sizeof(uint32) - sizeof(char *);
00079 
00085     uint32 _size;
00086 
00091     char  *_str;
00092 
00093 
00094     union {
00098         char _storage[_builtinCapacity];
00103         struct {
00104             mutable int *_refCount;
00105             uint32       _capacity;
00106         } _extern;
00107     };
00108 
00109     inline bool isStorageIntern() const {
00110         return _str == _storage;
00111     }
00112 
00113 public:
00115     String() : _size(0), _str(_storage) { _storage[0] = 0; }
00116 
00118     String(const char *str);
00119 
00121     String(const char *str, uint32 len);
00122 
00124     String(const char *beginP, const char *endP);
00125 
00127     String(const String &str);
00128 
00130     explicit String(char c);
00131 
00133     String(const U32String &str);
00134 
00135     ~String();
00136 
00137     String &operator=(const char *str);
00138     String &operator=(const String &str);
00139     String &operator=(char c);
00140     String &operator+=(const char *str);
00141     String &operator+=(const String &str);
00142     String &operator+=(char c);
00143 
00144     bool operator==(const String &x) const;
00145     bool operator==(const char *x) const;
00146     bool operator!=(const String &x) const;
00147     bool operator!=(const char *x) const;
00148 
00149     bool operator<(const String &x) const;
00150     bool operator<=(const String &x) const;
00151     bool operator>(const String &x) const;
00152     bool operator>=(const String &x) const;
00153 
00154     bool equals(const String &x) const;
00155     bool equalsIgnoreCase(const String &x) const;
00156     int compareTo(const String &x) const;           // strcmp clone
00157     int compareToIgnoreCase(const String &x) const; // stricmp clone
00158 
00159     bool equals(const char *x) const;
00160     bool equalsIgnoreCase(const char *x) const;
00161     int compareTo(const char *x) const;             // strcmp clone
00162     int compareToIgnoreCase(const char *x) const;   // stricmp clone
00163     int compareDictionary(const String &x) const;
00164     int compareDictionary(const char *x) const;
00165 
00166     bool hasSuffix(const String &x) const;
00167     bool hasSuffix(const char *x) const;
00168     bool hasSuffixIgnoreCase(const String &x) const;
00169     bool hasSuffixIgnoreCase(const char *x) const;
00170 
00171     bool hasPrefix(const String &x) const;
00172     bool hasPrefix(const char *x) const;
00173     bool hasPrefixIgnoreCase(const String &x) const;
00174     bool hasPrefixIgnoreCase(const char *x) const;
00175 
00176     bool contains(const String &x) const;
00177     bool contains(const char *x) const;
00178     bool contains(char x) const;
00179     bool contains(uint32 x) const;
00180 
00182     uint64 asUint64() const;
00183 
00209     bool matchString(const char *pat, bool ignoreCase = false, bool pathMode = false) const;
00210     bool matchString(const String &pat, bool ignoreCase = false, bool pathMode = false) const;
00211 
00212 
00213     inline const char *c_str() const { return _str; }
00214     inline uint size() const         { return _size; }
00215 
00216     inline bool empty() const { return (_size == 0); }
00217     char firstChar() const    { return (_size > 0) ? _str[0] : 0; }
00218     char lastChar() const     { return (_size > 0) ? _str[_size - 1] : 0; }
00219 
00220     char operator[](int idx) const {
00221         assert(_str && idx >= 0 && idx < (int)_size);
00222         return _str[idx];
00223     }
00224 
00226     void deleteLastChar();
00227 
00229     void deleteChar(uint32 p);
00230 
00232     void erase(uint32 p, uint32 len = npos);
00233 
00235     iterator erase(iterator it);
00236 
00238     void setChar(char c, uint32 p);
00239 
00241     void insertChar(char c, uint32 p);
00242 
00244     void clear();
00245 
00247     void toLowercase();
00248 
00250     void toUppercase();
00251 
00256     void trim();
00257 
00267     void wordWrap(const uint32 maxLength);
00268 
00269     uint hash() const;
00270 
00285     // Replace 'count' bytes, starting from 'pos' with str.
00286     void replace(uint32 pos, uint32 count, const String &str);
00287     // The same as above, but accepts a C-like array of characters.
00288     void replace(uint32 pos, uint32 count, const char *str);
00289     // Replace the characters in [begin, end) with str._str.
00290     void replace(iterator begin, iterator end, const String &str);
00291     // Replace the characters in [begin, end) with str.
00292     void replace(iterator begin, iterator end, const char *str);
00293     // Replace _str[posOri, posOri + countOri) with
00294     // str._str[posDest, posDest + countDest)
00295     void replace(uint32 posOri, uint32 countOri, const String &str,
00296                     uint32 posDest, uint32 countDest);
00297     // Replace _str[posOri, posOri + countOri) with
00298     // str[posDest, posDest + countDest)
00299     void replace(uint32 posOri, uint32 countOri, const char *str,
00300                     uint32 posDest, uint32 countDest);
00308     static String format(const char *fmt, ...) GCC_PRINTF(1, 2);
00309 
00315     static String vformat(const char *fmt, va_list args);
00316 
00318     size_t find(char c, size_t pos = 0) const;
00319 
00321     size_t find(const char *s) const;
00322     uint32 find(const String &str, uint32 pos = 0) const;
00323 
00325     size_t rfind(const char *s) const;
00326     size_t rfind(const String &s) const {
00327         return rfind(s.c_str());
00328     }
00329 
00331     size_t rfind(char c, size_t pos = npos) const;
00332 
00334     size_t findFirstOf(char c, size_t pos = 0) const;
00335 
00337     size_t findFirstOf(const char *chars, size_t pos = 0) const;
00338     size_t findFirstOf(const String &chars, size_t pos = 0) const {
00339         return findFirstOf(chars.c_str(), pos);
00340     }
00341 
00343     size_t findLastOf(char c, size_t pos = npos) const;
00344 
00346     size_t findLastOf(const char *chars, size_t pos = npos) const;
00347     size_t findLastOf(const String &chars, size_t pos = npos) const {
00348         return findLastOf(chars.c_str(), pos);
00349     }
00350 
00352     size_t findFirstNotOf(char c, size_t pos = 0) const;
00353 
00355     size_t findFirstNotOf(const char *chars, size_t pos = 0) const;
00356     size_t findFirstNotOf(const String &chars, size_t pos = 0) const {
00357         return findFirstNotOf(chars.c_str(), pos);
00358     }
00359 
00361     size_t findLastNotOf(char c) const;
00362 
00364     size_t findLastNotOf(const char *chars) const;
00365     size_t findLastNotOf(const String &chars) const {
00366         return findLastNotOf(chars.c_str());
00367     }
00368 
00370     String substr(size_t pos = 0, size_t len = npos) const;
00371 
00372 public:
00373 
00374     iterator begin() {
00375         // Since the user could potentially
00376         // change the string via the returned
00377         // iterator we have to assure we are
00378         // pointing to a unique storage.
00379         makeUnique();
00380 
00381         return _str;
00382     }
00383 
00384     iterator end() {
00385         return begin() + size();
00386     }
00387 
00388     const_iterator begin() const {
00389         return _str;
00390     }
00391 
00392     const_iterator end() const {
00393         return begin() + size();
00394     }
00395 
00397     U32String decode(CodePage page = kUtf8) const;
00398 
00399 protected:
00400     void makeUnique();
00401     void ensureCapacity(uint32 new_size, bool keep_old);
00402     void incRefCount() const;
00403     void decRefCount(int *oldRefCount);
00404     void initWithCStr(const char *str, uint32 len);
00405 
00406     bool pointerInOwnBuffer(const char *str) const;
00407 
00408     void decodeUTF8(U32String &dst) const;
00409     void decodeOneByte(U32String &dst, CodePage page) const;
00410 };
00411 
00412 // Append two strings to form a new (temp) string
00413 String operator+(const String &x, const String &y);
00414 
00415 String operator+(const char *x, const String &y);
00416 String operator+(const String &x, const char *y);
00417 
00418 String operator+(const String &x, char y);
00419 String operator+(char x, const String &y);
00420 
00421 // Some useful additional comparison operators for Strings
00422 bool operator==(const char *x, const String &y);
00423 bool operator!=(const char *x, const String &y);
00424 
00425 // Utility functions to remove leading and trailing whitespaces
00426 extern char *ltrim(char *t);
00427 extern char *rtrim(char *t);
00428 extern char *trim(char *t);
00429 
00430 
00443 String lastPathComponent(const String &path, const char sep);
00444 
00457 String normalizePath(const String &path, const char sep);
00458 
00459 
00485 bool matchString(const char *str, const char *pat, bool ignoreCase = false, bool pathMode = false);
00486 
00495 void replace(Common::String &source, const Common::String &what, const Common::String &with);
00496 
00502 String tag2string(uint32 tag);
00503 
00519 size_t strlcpy(char *dst, const char *src, size_t size);
00520 
00539 size_t strlcat(char *dst, const char *src, size_t size);
00540 
00550 size_t strnlen(const char *src, size_t maxSize);
00551 
00557 #define tag2str(x)  Common::tag2string(x).c_str()
00558 
00567 String toPrintable(const String &src, bool keepNewLines = true);
00568 
00569 } // End of namespace Common
00570 
00571 extern int scumm_stricmp(const char *s1, const char *s2);
00572 extern int scumm_strnicmp(const char *s1, const char *s2, uint n);
00573 extern char *scumm_strdup(const char *in);
00574 
00575 extern int scumm_compareDictionary(const char *s1, const char *s2);
00576 extern const char *scumm_skipArticle(const char *s1);
00577 
00578 extern const char *scumm_strcasestr(const char *s, const char *find);
00579 
00580 #endif


Generated on Sat Sep 19 2020 05:01:09 for ResidualVM by doxygen 1.7.1
curved edge   curved edge