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
32namespace Common {
33
43class U32String;
44
59class String : public BaseString<char> {
60public:
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
121 using BaseString<value_type>::contains;
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
176protected:
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
191String operator+(const String &x, const String &y);
192
193String operator+(const char *x, const String &y);
194String operator+(const String &x, const char *y);
195
196String operator+(const String &x, char y);
197String operator+(char x, const String &y);
198
199// Some useful additional comparison operators for Strings
200bool operator==(const char *x, const String &y);
201bool operator!=(const char *x, const String &y);
202
203// Utility functions to remove leading and trailing whitespaces
204extern char *ltrim(char *t);
205extern char *rtrim(char *t);
206extern char *trim(char *t);
207
208
221String lastPathComponent(const String &path, const char sep);
222
235String firstPathComponents(const String &path, const char sep);
236
249String normalizePath(const String &path, const char sep);
250
251
277bool matchString(const char *str, const char *pat, bool ignoreCase = false, const char *wildcardExclusions = NULL);
278
287void replace(Common::String &source, const Common::String &what, const Common::String &with);
288
297String tag2string(uint32 tag, bool nonPrintable = false);
298
310void strcpy_s(char *dst, size_t size, const char *src);
311
322template<typename T, size_t N>
323FORCEINLINE 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
340void strcat_s(char *dst, size_t size, const char *src);
341
353template<typename T, size_t N>
354FORCEINLINE 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
366int vsprintf_s(char *dst, size_t size, const char *format, va_list ap) GCC_PRINTF(3, 0);
367
375template<typename T, size_t N>
376FORCEINLINE 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
388int sprintf_s(char *dst, size_t size, MSVC_PRINTF const char *format, ...) GCC_PRINTF(3, 4);
389
397template<typename T, size_t N>
398inline 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
423size_t strlcpy(char *dst, const char *src, size_t size);
424
443size_t strlcat(char *dst, const char *src, size_t size);
444
454size_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
478String toPrintable(const String &src, bool keepNewLines = true);
479
484
487} // End of namespace Common
488
489extern int scumm_stricmp(const char *s1, const char *s2);
490extern int scumm_strnicmp(const char *s1, const char *s2, uint n);
491extern char *scumm_strdup(const char *in);
492
493extern int scumm_compareDictionary(const char *s1, const char *s2);
494extern const char *scumm_skipArticle(const char *s1);
495
496extern const char *scumm_strcasestr(const char *s, const char *find);
497
498#endif
Definition: str-base.h:32
void replace(uint32 pos, uint32 count, const BaseString &str)
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
unsigned char unsigned_type
Definition: str.h:66
U32String decode(CodePage page=kUtf8) const
String(String &&str)
Definition: str.h:84
String substr(size_t pos=0, size_t len=npos) const
String(const String &str)
Definition: str.h:81
constexpr String(value_type c)
Definition: str.h:87
String(const char *str)
Definition: str.h:72
String(const char *beginP, const char *endP)
Definition: str.h:78
static String static String vformat(const char *fmt, va_list args)
String(const char *str, uint32 len)
Definition: str.h:75
String(size_t n, value_type c)
Definition: str.h:90
String forEachLine(String(*func)(const String, va_list args),...) const
constexpr String()
Definition: str.h:69
String(const U32String &str, CodePage page=kUtf8)
bool matchString(const char *pat, bool ignoreCase=false, const char *wildcardExclusions=NULL) const
Definition: ustr.h:57
In find(In first, In last, const T &v)
Definition: algorithm.h:225
size_t strnlen(const char *src, size_t maxSize)
String lastPathComponent(const String &path, const char sep)
size_t strlcat(char *dst, const char *src, size_t size)
int vsprintf_s(char *dst, size_t size, const char *format, va_list ap) GCC_PRINTF(3
String tag2string(uint32 tag, bool nonPrintable=false)
String percentEncodeString(const String &src)
int sprintf_s(char *dst, size_t size, MSVC_PRINTF const char *format,...) GCC_PRINTF(3
int FORCEINLINE GCC_PRINTF(2, 0) int vsprintf_s(T(&dst)[N]
void strcat_s(char *dst, size_t size, const char *src)
String toPrintable(const String &src, bool keepNewLines=true)
size_t strlcpy(char *dst, const char *src, size_t size)
void strcpy_s(char *dst, size_t size, const char *src)
String firstPathComponents(const String &path, const char sep)
String normalizePath(const String &path, const char sep)
Definition: algorithm.h:29