File: ustr.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (316 lines) | stat: -rw-r--r-- 6,948 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/ustr.h"
#include "common/str.h"
#include "common/memorypool.h"
#include "common/util.h"

namespace Common {

U32String::U32String(const char *str, Common::CodePage page) : BaseString<u32char_type_t>() {
	if (str == nullptr) {
		_storage[0] = 0;
		_size = 0;
	} else {
		decodeInternal(str, strlen(str), page);
	}
}

U32String::U32String(const char *str, uint32 len, Common::CodePage page) : BaseString<u32char_type_t>() {
	decodeInternal(str, len, page);
}

U32String::U32String(const char *beginP, const char *endP, Common::CodePage page) : BaseString<u32char_type_t>() {
	assert(endP >= beginP);
	decodeInternal(beginP, endP - beginP, page);
}

U32String::U32String(const String &str, Common::CodePage page) : BaseString<u32char_type_t>() {
	decodeInternal(str.c_str(), str.size(), page);
}

U32String::U32String(u32char_type_t c) : BaseString<u32char_type_t>() {
	_storage[0] = c;
	_storage[1] = 0;

	_size = (c == 0) ? 0 : 1;
}

U32String &U32String::operator=(const U32String &str) {
	assign(str);
	return *this;
}

U32String &U32String::operator=(U32String &&str) {
	assign(static_cast<U32String &&>(str));
	return *this;
}

U32String &U32String::operator=(const String &str) {
	clear();
	decodeInternal(str.c_str(), str.size(), Common::kUtf8);
	return *this;
}

U32String &U32String::operator=(const value_type *str) {
	assign(str);
	return *this;
}

U32String &U32String::operator=(const char *str) {
	clear();
	decodeInternal(str, strlen(str), Common::kUtf8);
	return *this;
}

U32String &U32String::operator=(value_type c) {
	assign(c);
	return *this;
}

U32String &U32String::operator+=(const value_type *str) {
	assignAppend(str);
	return *this;
}

U32String &U32String::operator+=(const U32String &str) {
	assignAppend(str);
	return *this;
}

U32String &U32String::operator+=(value_type c) {
	assignAppend(c);
	return *this;
}

bool U32String::operator==(const String &x) const {
	return equalsC(x.c_str());
}

bool U32String::operator==(const char *x) const {
	return equalsC(x);
}

bool U32String::operator!=(const String &x) const {
	return !equalsC(x.c_str());
}

bool U32String::operator!=(const char *x) const {
	return !equalsC(x);
}

U32String operator+(const U32String &x, const U32String &y) {
	U32String temp(x);
	temp += y;
	return temp;
}

U32String operator+(const U32String &x, const U32String::value_type y) {
	U32String temp(x);
	temp += y;
	return temp;
}

U32String U32String::substr(size_t pos, size_t len) const {
	if (pos >= _size)
		return U32String();
	else if (len == npos)
		return U32String(_str + pos);
	else
		return U32String(_str + pos, MIN((size_t)_size - pos, len));
}

U32String U32String::formatInternal(const U32String *fmt, ...) {
	U32String output;

	va_list va;
	va_start(va, fmt);
	U32String::vformat(fmt->c_str(), fmt->c_str() + fmt->size(), output, va);
	va_end(va);

	return output;
}

U32String U32String::format(const char *fmt, ...) {
	U32String output;

	Common::U32String fmtU32(fmt);
	va_list va;
	va_start(va, fmt);
	U32String::vformat(fmtU32.c_str(), fmtU32.c_str() + fmtU32.size(),
			   output, va);
	va_end(va);

	return output;
}

int U32String::vformat(const value_type *fmt, const value_type *fmtEnd, U32String &output, va_list args) {
	int int_temp;
	uint uint_temp;
	char *string_temp;

	value_type ch;
	value_type *u32string_temp;

	value_type buffer[512];

	const value_type *start = fmt;

	while (fmt != fmtEnd) {
		if (*fmt == '%') {
			// Copy all characters since the last argument
			if (fmt != start)
				output.append(start, fmt);

			switch (ch = *++fmt) {
			case 'S':
				u32string_temp = va_arg(args, value_type *);

				output += u32string_temp;
				break;
			case 's':
				string_temp = va_arg(args, char *);

				output += Common::U32String(string_temp, kUtf8);
				break;
			case 'i':
			// fallthrough intended
			case 'd':
				int_temp = va_arg(args, int);
				ustr_helper_itoa(int_temp, buffer, 10);

				output += buffer;
				break;
			case 'u':
				uint_temp = va_arg(args, uint);
				ustr_helper_uitoa(uint_temp, buffer, 10);

				output += buffer;
				break;
			case 'c':
				//char is promoted to int when passed through '...'
				int_temp = va_arg(args, int);

				output += int_temp;
				break;
			case '%':
				output += '%';
				break;
			default:
				warning("Unexpected formatting type for U32String::Format.");
				break;
			}

			start = ++fmt;
		} else {
			// We attempt to copy as many characters as possible in one go.
			++fmt;
		}
	}

	// Append any remaining characters
	if (fmt != start)
		output.append(start, fmt);

	return output.size();
}

U32String::value_type* U32String::ustr_helper_itoa(int num, value_type* str, uint base) {
	if (num < 0) {
		str[0] = '-';
		ustr_helper_uitoa(-num, str + 1, base);
	} else {
		ustr_helper_uitoa(num, str, base);
	}

	return str;
}

U32String::value_type* U32String::ustr_helper_uitoa(uint num, value_type* str, uint base) {
	int i = 0;

	if (num) {
		// go digit by digit
		while (num != 0) {
			int rem = num % base;
			str[i++] = rem + '0';
			num /= base;
		}
	} else {
		str[i++] = '0';
	}

	// append string terminator
	str[i] = '\0';
	int k = 0;
	int j = i - 1;

	// reverse the string
	while (k < j) {
		value_type temp = str[k];
		str[k] = str[j];
		str[j] = temp;
		k++;
		j--;
	}

	return str;
}

U32String toPrintable(const U32String &in, bool keepNewLines) {
	U32String res;

	const char *tr = "\x01\x01\x02\x03\x04\x05\x06" "a"
				  //"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
					   "b" "t" "n" "v" "f" "r\x0e\x0f"
					"\x10\x11\x12\x13\x14\x15\x16\x17"
					"\x18\x19\x1a" "e\x1c\x1d\x1e\x1f";

	for (const u32char_type_t *p = in.c_str(); *p; p++) {
		if (*p == '\n') {
			if (keepNewLines)
				res += *p;
			else
				res += U32String("\\n");

			continue;
		}

		if (*p < 0x20 || *p == '\'' || *p == '\"' || *p == '\\') {
			res += '\\';

			if (*p < 0x20) {
				if (tr[*p] < 0x20)
					res += Common::String::format("x%02x", *p);
				else
					res += tr[*p];
			} else {
				res += *p;	// We will escape it
			}
		} else
			res += *p;
	}

	return res;
}

} // End of namespace Common