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 String(char c);
88 
90  String(const U32String &str, CodePage page = kUtf8);
91 
92  String &operator=(const char *str);
93  String &operator=(const String &str);
94  String &operator=(String &&str);
95  String &operator=(char c);
96  String &operator+=(const char *str);
97  String &operator+=(const String &str);
98  String &operator+=(char c);
99 
100  bool equalsIgnoreCase(const String &x) const;
101  int compareToIgnoreCase(const String &x) const; // stricmp clone
102 
103  bool equalsIgnoreCase(const char *x) const;
104  int compareToIgnoreCase(const char *x) const; // stricmp clone
105  int compareDictionary(const String &x) const;
106  int compareDictionary(const char *x) const;
107 
108  bool hasSuffix(const String &x) const;
109  bool hasSuffix(const char *x) const;
110  bool hasSuffixIgnoreCase(const String &x) const;
111  bool hasSuffixIgnoreCase(const char *x) const;
112 
113  bool hasPrefix(const String &x) const;
114  bool hasPrefix(const char *x) const;
115  bool hasPrefixIgnoreCase(const String &x) const;
116  bool hasPrefixIgnoreCase(const char *x) const;
117 
119  bool contains(const String &x) const;
120  bool contains(const char *x) const;
121 
147  bool matchString(const char *pat, bool ignoreCase = false, const char *wildcardExclusions = NULL) const;
148  bool matchString(const String &pat, bool ignoreCase = false, const char *wildcardExclusions = NULL) const;
149 
155  static String format(MSVC_PRINTF const char *fmt, ...) GCC_PRINTF(1, 2);
156 
162  static String vformat(const char *fmt, va_list args);
163 
165  String substr(size_t pos = 0, size_t len = npos) const;
166 
168  String forEachLine(String(*func)(const String, va_list args), ...) const;
169 
171  U32String decode(CodePage page = kUtf8) const;
172 
173 protected:
174  StringEncodingResult encodeUTF8(const U32String &src, char errorChar);
175  StringEncodingResult encodeWindows932(const U32String &src, char errorChar);
176  StringEncodingResult encodeWindows936(const U32String &src, char errorChar);
177  StringEncodingResult encodeWindows949(const U32String &src, char errorChar);
178  StringEncodingResult encodeWindows950(const U32String &src, bool translit, char errorChar);
179  StringEncodingResult encodeJohab(const U32String &src, char errorChar);
180  StringEncodingResult encodeOneByte(const U32String &src, CodePage page, bool translit, char errorChar);
181  StringEncodingResult encodeInternal(const U32String &src, CodePage page, char errorChar);
182  StringEncodingResult translitChar(U32String::value_type point, char errorChar);
183 
184  friend class U32String;
185 };
186 
187 // Append two strings to form a new (temp) string
188 String operator+(const String &x, const String &y);
189 
190 String operator+(const char *x, const String &y);
191 String operator+(const String &x, const char *y);
192 
193 String operator+(const String &x, char y);
194 String operator+(char x, const String &y);
195 
196 // Some useful additional comparison operators for Strings
197 bool operator==(const char *x, const String &y);
198 bool operator!=(const char *x, const String &y);
199 
200 // Utility functions to remove leading and trailing whitespaces
201 extern char *ltrim(char *t);
202 extern char *rtrim(char *t);
203 extern char *trim(char *t);
204 
205 
218 String lastPathComponent(const String &path, const char sep);
219 
232 String firstPathComponents(const String &path, const char sep);
233 
246 String normalizePath(const String &path, const char sep);
247 
248 
274 bool matchString(const char *str, const char *pat, bool ignoreCase = false, const char *wildcardExclusions = NULL);
275 
284 void replace(Common::String &source, const Common::String &what, const Common::String &with);
285 
294 String tag2string(uint32 tag, bool nonPrintable = false);
295 
307 void strcpy_s(char *dst, size_t size, const char *src);
308 
319 template<typename T, size_t N>
320 FORCEINLINE void strcpy_s(T (&dst)[N], const char *src) {
321  STATIC_ASSERT(sizeof(T) == sizeof(char), T_is_not_compatible_with_char);
322  strcpy_s((char *)dst, N, src);
323 }
324 
337 void strcat_s(char *dst, size_t size, const char *src);
338 
350 template<typename T, size_t N>
351 FORCEINLINE void strcat_s(T (&dst)[N], const char *src) {
352  STATIC_ASSERT(sizeof(T) == sizeof(char), T_is_not_compatible_with_char);
353  strcat_s((char *)dst, N, src);
354 }
355 
363 int vsprintf_s(char *dst, size_t size, const char *format, va_list ap) GCC_PRINTF(3, 0);
364 
372 template<typename T, size_t N>
373 FORCEINLINE GCC_PRINTF(2, 0) int vsprintf_s(T (&dst)[N], const char *format, va_list ap) {
374  STATIC_ASSERT(sizeof(T) == sizeof(char), T_is_not_compatible_with_char);
375  return vsprintf_s((char *)dst, N, format, ap);
376 }
377 
385 int sprintf_s(char *dst, size_t size, MSVC_PRINTF const char *format, ...) GCC_PRINTF(3, 4);
386 
394 template<typename T, size_t N>
395 inline GCC_PRINTF(2, 3) int sprintf_s(T (&dst)[N], MSVC_PRINTF const char *format, ...) {
396  STATIC_ASSERT(sizeof(T) == sizeof(char), T_is_not_compatible_with_char);
397  int ret;
398  va_list ap;
399  va_start(ap, format);
400  ret = vsprintf_s((char *)dst, N, format, ap);
401  va_end(ap);
402  return ret;
403 }
404 
420 size_t strlcpy(char *dst, const char *src, size_t size);
421 
440 size_t strlcat(char *dst, const char *src, size_t size);
441 
451 size_t strnlen(const char *src, size_t maxSize);
452 
458 #define tag2str(x) Common::tag2string(x).c_str()
459 
465 #define tag2strP(x) Common::tag2string(x, true).c_str()
466 
475 String toPrintable(const String &src, bool keepNewLines = true);
476 
480 String percentEncodeString(const String &src);
481 
484 } // End of namespace Common
485 
486 extern int scumm_stricmp(const char *s1, const char *s2);
487 extern int scumm_strnicmp(const char *s1, const char *s2, uint n);
488 extern char *scumm_strdup(const char *in);
489 
490 extern int scumm_compareDictionary(const char *s1, const char *s2);
491 extern const char *scumm_skipArticle(const char *s1);
492 
493 extern const char *scumm_strcasestr(const char *s, const char *find);
494 
495 #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
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)
unsigned char unsigned_type
Definition: str.h:66