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  explicit U32String(const uint32 *str) : BaseString<u32char_type_t>((const value_type *) str) {}
71  U32String(const uint32 *str, uint32 len) : BaseString<u32char_type_t>((const value_type *) str, len) {}
72  U32String(const uint32 *beginP, const uint32 *endP) : BaseString<u32char_type_t>((const value_type *) beginP, (const value_type *) endP) {}
73 
75  U32String(const value_type *beginP, const value_type *endP) : BaseString<u32char_type_t>(beginP, endP) {}
76 
78  U32String(const U32String &str) : BaseString<u32char_type_t>(str) {}
79 
81  U32String(U32String &&str) : BaseString<u32char_type_t>(static_cast<BaseString<u32char_type_t> &&>(str)) {}
82 
84  explicit U32String(const char *str, CodePage page = kUtf8);
85 
87  U32String(const char *str, uint32 len, CodePage page = kUtf8);
88 
90  U32String(const char *beginP, const char *endP, CodePage page = kUtf8);
91 
93  U32String(const String &str, CodePage page = kUtf8);
94 
96  explicit U32String(value_type c);
97 
99  U32String &operator=(const U32String &str);
100 
102  U32String &operator=(U32String &&str);
103 
105  U32String &operator=(const String &str);
106 
108  U32String &operator=(const value_type *str);
109 
111  U32String &operator=(const char *str);
112 
114  U32String &operator=(value_type c);
115 
117  U32String &operator+=(const U32String &str);
118 
120  U32String &operator+=(const value_type *str);
121 
123  U32String &operator+=(value_type c);
124 
127 
129  bool operator==(const String &x) const;
130 
132  bool operator==(const char *x) const;
133 
135  bool operator!=(const String &x) const;
136 
138  bool operator!=(const char *x) const;
139 
141  String encode(CodePage page = kUtf8) const;
142 
145  StringEncodingResult encode(String &outString, CodePage page, char errorChar) const;
146 
153  template<class... TParam>
154  static U32String format(const U32String &fmt, TParam... param);
155 
157  static U32String format(const char *fmt, ...);
158 
163  static int vformat(const value_type *fmt, const value_type *fmtEnd, U32String &output, va_list args);
164 
166  U32String substr(size_t pos = 0, size_t len = npos) const;
167 
168  const uint32 *u32_str() const {
169  return (const uint32 *) _str;
170  }
171 
173  static Common::U32String decodeUTF16BE(const uint16 *start, uint len);
174 
176  static Common::U32String decodeUTF16LE(const uint16 *start, uint len);
177 
179  static Common::U32String decodeUTF16Native(const uint16 *start, uint len);
180 
182  uint16 *encodeUTF16BE(uint *len = nullptr) const;
183 
185  uint16 *encodeUTF16LE(uint *len = nullptr) const;
186 
188  uint16 *encodeUTF16Native(uint *len = nullptr) const;
189 
192 
193 private:
194  static U32String formatInternal(const U32String *fmt, ...);
195 
200  static value_type* ustr_helper_itoa(int num, value_type* str, uint base);
201 
206  static value_type* ustr_helper_uitoa(uint num, value_type* str, uint base);
207 
208  void decodeInternal(const char *str, uint32 len, CodePage page);
209  void decodeOneByte(const char *str, uint32 len, CodePage page);
210  void decodeWindows932(const char *src, uint32 len);
211  void decodeWindows936(const char *src, uint32 len);
212  void decodeWindows949(const char *src, uint32 len);
213  void decodeWindows950(const char *src, uint32 len);
214  void decodeJohab(const char *src, uint32 len);
215  void decodeUTF8(const char *str, uint32 len);
216 
217  friend class String;
218 };
219 
220 template<class... TParam>
221 inline U32String U32String::format(const U32String &fmt, TParam... param) {
222  return formatInternal(&fmt, Common::forward<TParam>(param)...);
223 }
224 
226 U32String operator+(const U32String &x, const U32String &y);
227 
229 U32String operator+(const U32String &x, U32String::value_type y);
230 
241 U32String toPrintable(const U32String &src, bool keepNewLines = true);
242 
245 } // End of namespace Common
246 
247 #endif
U32String(const value_type *str, uint32 len)
Definition: ustr.h:68
U32String(const U32String &str)
Definition: ustr.h:78
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:221
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:75
U32String(U32String &&str)
Definition: ustr.h:81
char32_t u32char_type_t
Definition: ustr.h:41
uint16 * encodeUTF16LE(uint *len=nullptr) const
constexpr U32String()
Definition: ustr.h:62
const uint32 * u32_str() const
Definition: ustr.h:168
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)