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 
00171     uint32 find(const String &str, uint32 pos = 0) const;
00172 
00174     uint64 asUint64() const;
00175 
00201     bool matchString(const char *pat, bool ignoreCase = false, bool pathMode = false) const;
00202     bool matchString(const String &pat, bool ignoreCase = false, bool pathMode = false) const;
00203 
00204 
00205     inline const char *c_str() const { return _str; }
00206     inline uint size() const         { return _size; }
00207 
00208     inline bool empty() const { return (_size == 0); }
00209     char firstChar() const    { return (_size > 0) ? _str[0] : 0; }
00210     char lastChar() const     { return (_size > 0) ? _str[_size - 1] : 0; }
00211 
00212     char operator[](int idx) const {
00213         assert(_str && idx >= 0 && idx < (int)_size);
00214         return _str[idx];
00215     }
00216 
00218     void deleteLastChar();
00219 
00221     void deleteChar(uint32 p);
00222 
00224     void erase(uint32 p, uint32 len = npos);
00225 
00227     void setChar(char c, uint32 p);
00228 
00230     void insertChar(char c, uint32 p);
00231 
00233     void clear();
00234 
00236     void toLowercase();
00237 
00239     void toUppercase();
00240 
00245     void trim();
00246 
00256     void wordWrap(const uint32 maxLength);
00257 
00258     uint hash() const;
00259 
00274     // Replace 'count' bytes, starting from 'pos' with str.
00275     void replace(uint32 pos, uint32 count, const String &str);
00276     // The same as above, but accepts a C-like array of characters.
00277     void replace(uint32 pos, uint32 count, const char *str);
00278     // Replace the characters in [begin, end) with str._str.
00279     void replace(iterator begin, iterator end, const String &str);
00280     // Replace the characters in [begin, end) with str.
00281     void replace(iterator begin, iterator end, const char *str);
00282     // Replace _str[posOri, posOri + countOri) with
00283     // str._str[posDest, posDest + countDest)
00284     void replace(uint32 posOri, uint32 countOri, const String &str,
00285                     uint32 posDest, uint32 countDest);
00286     // Replace _str[posOri, posOri + countOri) with
00287     // str[posDest, posDest + countDest)
00288     void replace(uint32 posOri, uint32 countOri, const char *str,
00289                     uint32 posDest, uint32 countDest);
00297     static String format(const char *fmt, ...) GCC_PRINTF(1, 2);
00298 
00304     static String vformat(const char *fmt, va_list args);
00305 
00306 public:
00307 
00308     iterator begin() {
00309         // Since the user could potentially
00310         // change the string via the returned
00311         // iterator we have to assure we are
00312         // pointing to a unique storage.
00313         makeUnique();
00314 
00315         return _str;
00316     }
00317 
00318     iterator end() {
00319         return begin() + size();
00320     }
00321 
00322     const_iterator begin() const {
00323         return _str;
00324     }
00325 
00326     const_iterator end() const {
00327         return begin() + size();
00328     }
00329 
00330 protected:
00331     void makeUnique();
00332     void ensureCapacity(uint32 new_size, bool keep_old);
00333     void incRefCount() const;
00334     void decRefCount(int *oldRefCount);
00335     void initWithCStr(const char *str, uint32 len);
00336 };
00337 
00338 // Append two strings to form a new (temp) string
00339 String operator+(const String &x, const String &y);
00340 
00341 String operator+(const char *x, const String &y);
00342 String operator+(const String &x, const char *y);
00343 
00344 String operator+(const String &x, char y);
00345 String operator+(char x, const String &y);
00346 
00347 // Some useful additional comparison operators for Strings
00348 bool operator==(const char *x, const String &y);
00349 bool operator!=(const char *x, const String &y);
00350 
00351 // Utility functions to remove leading and trailing whitespaces
00352 extern char *ltrim(char *t);
00353 extern char *rtrim(char *t);
00354 extern char *trim(char *t);
00355 
00356 
00369 String lastPathComponent(const String &path, const char sep);
00370 
00383 String normalizePath(const String &path, const char sep);
00384 
00385 
00411 bool matchString(const char *str, const char *pat, bool ignoreCase = false, bool pathMode = false);
00412 
00421 void replace(Common::String &source, const Common::String &what, const Common::String &with);
00422 
00428 String tag2string(uint32 tag);
00429 
00445 size_t strlcpy(char *dst, const char *src, size_t size);
00446 
00465 size_t strlcat(char *dst, const char *src, size_t size);
00466 
00476 size_t strnlen(const char *src, size_t maxSize);
00477 
00483 #define tag2str(x)  Common::tag2string(x).c_str()
00484 
00485 
00486 } // End of namespace Common
00487 
00488 extern int scumm_stricmp(const char *s1, const char *s2);
00489 extern int scumm_strnicmp(const char *s1, const char *s2, uint n);
00490 extern char *scumm_strdup(const char *in);
00491 
00492 #endif


Generated on Sat Feb 15 2020 05:00:48 for ResidualVM by doxygen 1.7.1
curved edge   curved edge