ScummVM API documentation
str.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_STRING_H
23 #define COMMON_STRING_H
24 
25 #include "common/scummsys.h"
26 #include "common/str-enc.h"
27 #include "common/ustr.h"
28 #include "common/str-base.h"
29 
30 #include <stdarg.h>
31 
32 namespace Common {
33 
43 class U32String;
44 
59 class String : public BaseString<char> {
60 public:
66  typedef unsigned char unsigned_type;
67 
69  constexpr String() : BaseString<char>() {}
70 
72  String(const char *str) : BaseString<char>(str) {}
73 
75  String(const char *str, uint32 len) : BaseString<char>(str, len) {}
76 
78  String(const char *beginP, const char *endP) : BaseString<char>(beginP, endP) {}
79 
81  String(const String &str) : BaseString<char>(str) {}
82 
84  String(String &&str) : BaseString<char>(static_cast<BaseString<char> &&>(str)) {}
85 
87  explicit constexpr String(value_type c) : BaseString<char>(c) {}
88 
90  String(size_t n, value_type c) : BaseString<char>(n, c) {}
91 
93  String(const U32String &str, CodePage page = kUtf8);
94 
95  String &operator=(const char *str);
96  String &operator=(const String &str);
97  String &operator=(String &&str);
98  String &operator=(char c);
99  String &operator+=(const char *str);
100  String &operator+=(const String &str);
101  String &operator+=(char c);
102 
103  bool equalsIgnoreCase(const String &x) const;
104  int compareToIgnoreCase(const String &x) const; // stricmp clone
105 
106  bool equalsIgnoreCase(const char *x) const;
107  int compareToIgnoreCase(const char *x) const; // stricmp clone
108  int compareDictionary(const String &x) const;
109  int compareDictionary(const char *x) const;
110 
111  bool hasSuffix(const String &x) const;
112  bool hasSuffix(const char *x) const;
113  bool hasSuffixIgnoreCase(const String &x) const;
114  bool hasSuffixIgnoreCase(const char *x) const;
115 
116  bool hasPrefix(const String &x) const;
117  bool hasPrefix(const char *x) const;
118  bool hasPrefixIgnoreCase(const String &x) const;
119  bool hasPrefixIgnoreCase(const char *x) const;
120 
122  bool contains(const String &x) const;
123  bool contains(const char *x) const;
124 
150  bool matchString(const char *pat, bool ignoreCase = false, const char *wildcardExclusions = NULL) const;
151  bool matchString(const String &pat, bool ignoreCase = false, const char *wildcardExclusions = NULL) const;
152 
158  static String format(MSVC_PRINTF const char *fmt, ...) GCC_PRINTF(1, 2);
159 
165  static String vformat(const char *fmt, va_list args);
166 
168  String substr(size_t pos = 0, size_t len = npos) const;
169 
171  String forEachLine(String(*func)(const String, va_list args), ...) const;
172 
174  U32String decode(CodePage page = kUtf8) const;
175 
176 protected:
177  StringEncodingResult encodeUTF8(const U32String &src, char errorChar);
178  StringEncodingResult encodeWindows932(const U32String &src, char errorChar);
179  StringEncodingResult encodeWindows936(const U32String &src, char errorChar);
180  StringEncodingResult encodeWindows949(const U32String &src, char errorChar);
181  StringEncodingResult encodeWindows950(const U32String &src, bool translit, char errorChar);
182  StringEncodingResult encodeJohab(const U32String &src, char errorChar);
183  StringEncodingResult encodeOneByte(const U32String &src, CodePage page, bool translit, char errorChar);
184  StringEncodingResult encodeInternal(const U32String &src, CodePage page, char errorChar);
185  StringEncodingResult translitChar(U32String::value_type point, char errorChar);
186 
187  friend class U32String;
188 };
189 
190 // Append two strings to form a new (temp) string
191 String operator+(const String &x, const String &y);
192 
193 String operator+(const char *x, const String &y);
194 String operator+(const String &x, const char *y);
195 
196 String operator+(const String &x, char y);
197 String operator+(char x, const String &y);
198 
199 // Some useful additional comparison operators for Strings
200 bool operator==(const char *x, const String &y);
201 bool operator!=(const char *x, const String &y);
202 
203 // Utility functions to remove leading and trailing whitespaces
204 extern char *ltrim(char *t);
205 extern char *rtrim(char *t);
206 extern char *trim(char *t);
207 
208 
221 String lastPathComponent(const String &path, const char sep);
222 
235 String firstPathComponents(const String &path, const char sep);
236 
249 String normalizePath(const String &path, const char sep);
250 
251 
277 bool matchString(const char *str, const char *pat, bool ignoreCase = false, const char *wildcardExclusions = NULL);
278 
287 void replace(Common::String &source, const Common::String &what, const Common::String &with);
288 
297 String tag2string(uint32 tag, bool nonPrintable = false);
298 
310 void strcpy_s(char *dst, size_t size, const char *src);
311 
322 template<typename T, size_t N>
323 FORCEINLINE void strcpy_s(T (&dst)[N], const char *src) {
324  STATIC_ASSERT(sizeof(T) == sizeof(char), T_is_not_compatible_with_char);
325  strcpy_s((char *)dst, N, src);
326 }
327 
340 void strcat_s(char *dst, size_t size, const char *src);
341 
353 template<typename T, size_t N>
354 FORCEINLINE void strcat_s(T (&dst)[N], const char *src) {
355  STATIC_ASSERT(sizeof(T) == sizeof(char), T_is_not_compatible_with_char);
356  strcat_s((char *)dst, N, src);
357 }
358 
366 int vsprintf_s(char *dst, size_t size, const char *format, va_list ap) GCC_PRINTF(3, 0);
367 
375 template<typename T, size_t N>
376 FORCEINLINE GCC_PRINTF(2, 0) int vsprintf_s(T (&dst)[N], const char *format, va_list ap) {
377  STATIC_ASSERT(sizeof(T) == sizeof(char), T_is_not_compatible_with_char);
378  return vsprintf_s((char *)dst, N, format, ap);
379 }
380 
388 int sprintf_s(char *dst, size_t size, MSVC_PRINTF const char *format, ...) GCC_PRINTF(3, 4);
389 
397 template<typename T, size_t N>
398 inline GCC_PRINTF(2, 3) int sprintf_s(T (&dst)[N], MSVC_PRINTF const char *format, ...) {
399  STATIC_ASSERT(sizeof(T) == sizeof(char), T_is_not_compatible_with_char);
400  int ret;
401  va_list ap;
402  va_start(ap, format);
403  ret = vsprintf_s((char *)dst, N, format, ap);
404  va_end(ap);
405  return ret;
406 }
407 
423 size_t strlcpy(char *dst, const char *src, size_t size);
424 
443 size_t strlcat(char *dst, const char *src, size_t size);
444 
454 size_t strnlen(const char *src, size_t maxSize);
455 
461 #define tag2str(x) Common::tag2string(x).c_str()
462 
468 #define tag2strP(x) Common::tag2string(x, true).c_str()
469 
478 String toPrintable(const String &src, bool keepNewLines = true);
479 
483 String percentEncodeString(const String &src);
484 
487 } // End of namespace Common
488 
489 extern int scumm_stricmp(const char *s1, const char *s2);
490 extern int scumm_strnicmp(const char *s1, const char *s2, uint n);
491 extern char *scumm_strdup(const char *in);
492 
493 extern int scumm_compareDictionary(const char *s1, const char *s2);
494 extern const char *scumm_skipArticle(const char *s1);
495 
496 extern const char *scumm_strcasestr(const char *s, const char *find);
497 
498 #endif
String percentEncodeString(const String &src)
String lastPathComponent(const String &path, const char sep)
Definition: str.h:59
String(String &&str)
Definition: str.h:84
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
bool matchString(const char *pat, bool ignoreCase=false, const char *wildcardExclusions=NULL) const
constexpr String(value_type c)
Definition: str.h:87
size_t strlcat(char *dst, const char *src, size_t size)
size_t strlcpy(char *dst, const char *src, size_t size)
constexpr String()
Definition: str.h:69
String forEachLine(String(*func)(const String, va_list args),...) const
uint32 find(value_type x, uint32 pos=0) const
Definition: str-base.h:32
int sprintf_s(char *dst, size_t size, MSVC_PRINTF const char *format,...) GCC_PRINTF(3
static String static String vformat(const char *fmt, va_list args)
String firstPathComponents(const String &path, const char sep)
int FORCEINLINE GCC_PRINTF(2, 0) int vsprintf_s(T(&dst)[N]
String toPrintable(const String &src, bool keepNewLines=true)
String normalizePath(const String &path, const char sep)
Definition: ustr.h:57
String(const char *str)
Definition: str.h:72
int vsprintf_s(char *dst, size_t size, const char *format, va_list ap) GCC_PRINTF(3
Definition: algorithm.h:29
String(const char *beginP, const char *endP)
Definition: str.h:78
DBCSString operator+(const DBCSString &x, const DBCSString &y)
String(const String &str)
Definition: str.h:81
String substr(size_t pos=0, size_t len=npos) const
String(const char *str, uint32 len)
Definition: str.h:75
void replace(uint32 pos, uint32 count, const BaseString &str)
String tag2string(uint32 tag, bool nonPrintable=false)
void strcpy_s(char *dst, size_t size, const char *src)
size_t strnlen(const char *src, size_t maxSize)
U32String decode(CodePage page=kUtf8) const
void strcat_s(char *dst, size_t size, const char *src)
String(size_t n, value_type c)
Definition: str.h:90
unsigned char unsigned_type
Definition: str.h:66