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 
71  U32String(const value_type *beginP, const value_type *endP) : BaseString<u32char_type_t>(beginP, endP) {}
72 
74  U32String(const U32String &str) : BaseString<u32char_type_t>(str) {}
75 
77  U32String(U32String &&str) : BaseString<u32char_type_t>(static_cast<BaseString<u32char_type_t> &&>(str)) {}
78 
80  explicit U32String(const char *str, CodePage page = kUtf8);
81 
83  U32String(const char *str, uint32 len, CodePage page = kUtf8);
84 
86  U32String(const char *beginP, const char *endP, CodePage page = kUtf8);
87 
89  U32String(const String &str, CodePage page = kUtf8);
90 
92  explicit constexpr U32String(value_type c) : BaseString<u32char_type_t>(c) {}
93 
95  U32String(size_t n, value_type c) : BaseString<u32char_type_t>(n, c) {}
96 
98  U32String &operator=(const U32String &str);
99 
101  U32String &operator=(U32String &&str);
102 
104  U32String &operator=(const String &str);
105 
107  U32String &operator=(const value_type *str);
108 
110  U32String &operator=(const char *str);
111 
113  U32String &operator=(value_type c);
114 
116  U32String &operator+=(const U32String &str);
117 
119  U32String &operator+=(const value_type *str);
120 
122  U32String &operator+=(value_type c);
123 
126 
128  bool operator==(const String &x) const;
129 
131  bool operator==(const char *x) const;
132 
134  bool operator!=(const String &x) const;
135 
137  bool operator!=(const char *x) const;
138 
140  String encode(CodePage page = kUtf8) const;
141 
144  StringEncodingResult encode(String &outString, CodePage page, char errorChar) const;
145 
152  template<class... TParam>
153  static U32String format(const U32String &fmt, TParam... param);
154 
156  static U32String format(const char *fmt, ...);
157 
162  static int vformat(const value_type *fmt, const value_type *fmtEnd, U32String &output, va_list args);
163 
165  U32String substr(size_t pos = 0, size_t len = npos) const;
166 
168  static Common::U32String decodeUTF16BE(const uint16 *start, uint len);
169 
171  static Common::U32String decodeUTF16LE(const uint16 *start, uint len);
172 
174  static Common::U32String decodeUTF16Native(const uint16 *start, uint len);
175 
177  uint16 *encodeUTF16BE(uint *len = nullptr) const;
178 
180  uint16 *encodeUTF16LE(uint *len = nullptr) const;
181 
183  uint16 *encodeUTF16Native(uint *len = nullptr) const;
184 
187 
188 private:
189  static U32String formatInternal(const U32String *fmt, ...);
190 
195  static value_type* ustr_helper_itoa(int num, value_type* str, uint base);
196 
201  static value_type* ustr_helper_uitoa(uint num, value_type* str, uint base);
202 
203  void decodeInternal(const char *str, uint32 len, CodePage page);
204  void decodeOneByte(const char *str, uint32 len, CodePage page);
205  void decodeWindows932(const char *src, uint32 len);
206  void decodeWindows936(const char *src, uint32 len);
207  void decodeWindows949(const char *src, uint32 len);
208  void decodeWindows950(const char *src, uint32 len);
209  void decodeJohab(const char *src, uint32 len);
210  void decodeUTF8(const char *str, uint32 len);
211 
212  friend class String;
213 };
214 
215 template<class... TParam>
216 inline U32String U32String::format(const U32String &fmt, TParam... param) {
217  return formatInternal(&fmt, Common::forward<TParam>(param)...);
218 }
219 
221 U32String operator+(const U32String &x, const U32String &y);
222 
224 U32String operator+(const U32String &x, U32String::value_type y);
225 
236 U32String toPrintable(const U32String &src, bool keepNewLines = true);
237 
240 } // End of namespace Common
241 
242 #endif
U32String(const value_type *str, uint32 len)
Definition: ustr.h:68
U32String(const U32String &str)
Definition: ustr.h:74
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
U32String(size_t n, value_type c)
Definition: ustr.h:95
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:216
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:71
U32String(U32String &&str)
Definition: ustr.h:77
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:92
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)