ScummVM API documentation
ustr.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_USTR_H
23 #define COMMON_USTR_H
24 
25 #include "common/scummsys.h"
26 #include "common/util.h"
27 #include "common/str-enc.h"
28 #include "common/str-base.h"
29 
30 namespace Common {
31 
41 class String;
42 
55 typedef char32_t u32char_type_t;
56 
57 class U32String : public BaseString<u32char_type_t> {
58 public:
59  typedef uint32 unsigned_type;
60 public:
62  constexpr U32String() : BaseString<u32char_type_t>() {}
63 
65  explicit U32String(const value_type *str) : BaseString<u32char_type_t>(str) {}
66 
68  U32String(const value_type *str, uint32 len) : BaseString<u32char_type_t>(str, len) {}
69 
70  WARN_DEPRECATED("Use u32char_type_t instead of uint32")
71  explicit U32String(const uint32 *str) : BaseString<u32char_type_t>((const value_type *) str) {}
72  WARN_DEPRECATED("Use u32char_type_t instead of uint32")
73  U32String(const uint32 *str, uint32 len) : BaseString<u32char_type_t>((const value_type *) str, len) {}
74  WARN_DEPRECATED("Use u32char_type_t instead of uint32")
75  U32String(const uint32 *beginP, const uint32 *endP) : BaseString<u32char_type_t>((const value_type *) beginP, (const value_type *) endP) {}
76 
78  U32String(const value_type *beginP, const value_type *endP) : BaseString<u32char_type_t>(beginP, endP) {}
79 
81  U32String(const U32String &str) : BaseString<u32char_type_t>(str) {}
82 
84  U32String(U32String &&str) : BaseString<u32char_type_t>(static_cast<BaseString<u32char_type_t> &&>(str)) {}
85 
87  explicit U32String(const char *str, CodePage page = kUtf8);
88 
90  U32String(const char *str, uint32 len, CodePage page = kUtf8);
91 
93  U32String(const char *beginP, const char *endP, CodePage page = kUtf8);
94 
96  U32String(const String &str, CodePage page = kUtf8);
97 
99  explicit constexpr U32String(value_type c) : BaseString<u32char_type_t>(c) {}
100 
102  U32String &operator=(const U32String &str);
103 
105  U32String &operator=(U32String &&str);
106 
108  U32String &operator=(const String &str);
109 
111  U32String &operator=(const value_type *str);
112 
114  U32String &operator=(const char *str);
115 
117  U32String &operator=(value_type c);
118 
120  U32String &operator+=(const U32String &str);
121 
123  U32String &operator+=(const value_type *str);
124 
126  U32String &operator+=(value_type c);
127 
130 
132  bool operator==(const String &x) const;
133 
135  bool operator==(const char *x) const;
136 
138  bool operator!=(const String &x) const;
139 
141  bool operator!=(const char *x) const;
142 
144  String encode(CodePage page = kUtf8) const;
145 
148  StringEncodingResult encode(String &outString, CodePage page, char errorChar) const;
149 
156  template<class... TParam>
157  static U32String format(const U32String &fmt, TParam... param);
158 
160  static U32String format(const char *fmt, ...);
161 
166  static int vformat(const value_type *fmt, const value_type *fmtEnd, U32String &output, va_list args);
167 
169  U32String substr(size_t pos = 0, size_t len = npos) const;
170 
171  WARN_DEPRECATED("Use c_str() instead")
172  const uint32 *u32_str() const {
173  return (const uint32 *) _str;
174  }
175 
177  static Common::U32String decodeUTF16BE(const uint16 *start, uint len);
178 
180  static Common::U32String decodeUTF16LE(const uint16 *start, uint len);
181 
183  static Common::U32String decodeUTF16Native(const uint16 *start, uint len);
184 
186  uint16 *encodeUTF16BE(uint *len = nullptr) const;
187 
189  uint16 *encodeUTF16LE(uint *len = nullptr) const;
190 
192  uint16 *encodeUTF16Native(uint *len = nullptr) const;
193 
196 
197 private:
198  static U32String formatInternal(const U32String *fmt, ...);
199 
204  static value_type* ustr_helper_itoa(int num, value_type* str, uint base);
205 
210  static value_type* ustr_helper_uitoa(uint num, value_type* str, uint base);
211 
212  void decodeInternal(const char *str, uint32 len, CodePage page);
213  void decodeOneByte(const char *str, uint32 len, CodePage page);
214  void decodeWindows932(const char *src, uint32 len);
215  void decodeWindows936(const char *src, uint32 len);
216  void decodeWindows949(const char *src, uint32 len);
217  void decodeWindows950(const char *src, uint32 len);
218  void decodeJohab(const char *src, uint32 len);
219  void decodeUTF8(const char *str, uint32 len);
220 
221  friend class String;
222 };
223 
224 template<class... TParam>
225 inline U32String U32String::format(const U32String &fmt, TParam... param) {
226  return formatInternal(&fmt, Common::forward<TParam>(param)...);
227 }
228 
230 U32String operator+(const U32String &x, const U32String &y);
231 
233 U32String operator+(const U32String &x, U32String::value_type y);
234 
245 U32String toPrintable(const U32String &src, bool keepNewLines = true);
246 
249 } // End of namespace Common
250 
251 #endif
U32String(const value_type *str, uint32 len)
Definition: ustr.h:68
U32String(const U32String &str)
Definition: ustr.h:81
Definition: str.h:59
static int vformat(const value_type *fmt, const value_type *fmtEnd, U32String &output, va_list args)
static Common::U32String decodeUTF16Native(const uint16 *start, uint len)
bool operator==(const String &x) const
U32String transcodeChineseT2S() const
uint16 * encodeUTF16Native(uint *len=nullptr) const
uint32 unsigned_type
Definition: ustr.h:59
U32String & operator+=(const U32String &str)
Definition: str-base.h:32
U32String & operator=(const U32String &str)
String encode(CodePage page=kUtf8) const
uint16 * encodeUTF16BE(uint *len=nullptr) const
String toPrintable(const String &src, bool keepNewLines=true)
static U32String format(const U32String &fmt, TParam... param)
Definition: ustr.h:225
Definition: ustr.h:57
Definition: algorithm.h:29
DBCSString operator+(const DBCSString &x, const DBCSString &y)
U32String(const value_type *beginP, const value_type *endP)
Definition: ustr.h:78
U32String(U32String &&str)
Definition: ustr.h:84
char32_t u32char_type_t
Definition: ustr.h:41
uint16 * encodeUTF16LE(uint *len=nullptr) const
constexpr U32String()
Definition: ustr.h:62
constexpr U32String(value_type c)
Definition: ustr.h:99
const uint32 * u32_str() const
Definition: ustr.h:172
value_type * _str
Definition: str-base.h:60
U32String substr(size_t pos=0, size_t len=npos) const
U32String(const value_type *str)
Definition: ustr.h:65
bool operator!=(const String &x) const
static Common::U32String decodeUTF16BE(const uint16 *start, uint len)
static Common::U32String decodeUTF16LE(const uint16 *start, uint len)