File: as_string.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (420 lines) | stat: -rw-r--r-- 9,538 bytes parent folder | download | duplicates (3)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2017 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/

#include "as_config.h"

#include <stdarg.h>     // va_list, va_start(), etc
#include <stdlib.h>     // strtod(), strtol()
#include <string.h>     // some compilers declare memcpy() here

#if !defined(AS_NO_MEMORY_H)
#include <memory.h>
#endif

#include "as_string.h"
#include "as_string_util.h"
#include "as_memory.h"

asCString::asCString() {
	length = 0;
	local[0] = 0;
}

// Copy constructor
asCString::asCString(const asCString &str) {
	length = 0;
	local[0] = 0;

	Assign(str.AddressOf(), str.length);
}

#ifdef AS_CAN_USE_CPP11
asCString::asCString(asCString &&str) {
	if (str.length <= 11) {
		length = str.length;
		memcpy(local, str.local, length);
		local[length] = 0;
	} else {
		dynamic = str.dynamic;
		length = str.length;
	}

	str.dynamic = 0;
	str.length = 0;
}
#endif // c++11

asCString::asCString(const char *str, size_t len) {
	length = 0;
	local[0] = 0;

	Assign(str, len);
}

asCString::asCString(const char *str) {
	length = 0;
	local[0] = 0;

	size_t len = strlen(str);
	Assign(str, len);
}

asCString::asCString(char ch) {
	length = 0;
	local[0] = 0;

	Assign(&ch, 1);
}

asCString::~asCString() {
	if (length > 11 && dynamic) {
		asDELETEARRAY(dynamic);
	}
}

char *asCString::AddressOf() {
	if (length <= 11)
		return local;
	else
		return dynamic;
}

const char *asCString::AddressOf() const {
	if (length <= 11)
		return local;
	else
		return dynamic;
}

void asCString::SetLength(size_t len) {
	Allocate(len, true);
}

void asCString::Allocate(size_t len, bool keepData) {
	// If we stored the capacity of the dynamically allocated buffer it would be possible
	// to save some memory allocations if a string decreases in size then increases again,
	// but this would require extra bytes in the string object itself, or a decrease of
	// the static buffer, which in turn would mean extra memory is needed. I've tested each
	// of these options, and it turned out that the current choice is what best balanced
	// the number of allocations against the size of the allocations.

	if (len > 11 && len > length) {
		// Allocate a new dynamic buffer if the new one is larger than the old
		char *buf = asNEWARRAY(char, len + 1);
		if (buf == 0) {
			// Out of memory. Return without modifying anything
			return;
		}

		if (keepData) {
			int l = (int)len < (int)length ? (int)len : (int)length;
			memcpy(buf, AddressOf(), l);
		}

		if (length > 11) {
			asDELETEARRAY(dynamic);
		}

		dynamic = buf;
	} else if (len <= 11 && length > 11) {
		// Free the dynamic buffer, since it is no longer needed
		char *buf = dynamic;
		if (keepData) {
			memcpy(&local, buf, len);
		}
		asDELETEARRAY(buf);
	}

	length = (int)len;

	// Make sure the buffer is null terminated
	AddressOf()[length] = 0;
}

void asCString::Assign(const char *str, size_t len) {
	Allocate(len, false);

	// Copy the string
	memcpy(AddressOf(), str, length);
	AddressOf()[length] = 0;
}

asCString &asCString::operator =(const char *str) {
	size_t len = str ? strlen(str) : 0;
	Assign(str, len);

	return *this;
}

asCString &asCString::operator =(const asCString &str) {
	Assign(str.AddressOf(), str.length);

	return *this;
}

#ifdef AS_CAN_USE_CPP11
asCString &asCString::operator =(asCString &&str) {
	if (this != &str) {
		if (length > 11 && dynamic) {
			asDELETEARRAY(dynamic);
		}

		if (str.length <= 11) {
			length = str.length;

			memcpy(local, str.local, length);
			local[length] = 0;
		} else {
			dynamic = str.dynamic;
			length = str.length;
		}

		str.dynamic = 0;
		str.length = 0;
	}

	return *this;
}
#endif // c++11

asCString &asCString::operator =(char ch) {
	Assign(&ch, 1);

	return *this;
}

void asCString::Concatenate(const char *str, size_t len) {
	asUINT oldLength = length;
	SetLength(length + len);

	memcpy(AddressOf() + oldLength, str, len);
	AddressOf()[length] = 0;
}

asCString &asCString::operator +=(const char *str) {
	size_t len = strlen(str);
	Concatenate(str, len);

	return *this;
}

asCString &asCString::operator +=(const asCString &str) {
	Concatenate(str.AddressOf(), str.length);

	return *this;
}

asCString &asCString::operator +=(char ch) {
	Concatenate(&ch, 1);

	return *this;
}

size_t asCString::GetLength() const {
	return length;
}

// Returns the length
size_t asCString::Format(const char *format, ...) {
	va_list args;
	va_start(args, format);

	const size_t startSize = 1024;
	char tmp[startSize];
	int r = asVSNPRINTF(tmp, startSize - 1, format, args);

	if (r > 0 && r < int(startSize)) {
		Assign(tmp, r);
	} else {
		// TODO: For some reason this doesn't work properly on Linux. Perhaps the
		//       problem is related to vsnprintf not keeping the state of va_arg.
		//       Perhaps I need to rewrite this in some way to keep the state
		size_t n = startSize * 2;
		asCString str; // Use temporary string in case the current buffer is a parameter
		str.Allocate(n, false);

		while ((r = asVSNPRINTF(str.AddressOf(), n, format, args)) < 0 || r >= int(n)) {
			n *= 2;
			str.Allocate(n, false);
		}

		Assign(str.AddressOf(), r);
	}

	va_end(args);

	return length;
}

char &asCString::operator [](size_t index) {
	asASSERT(index < length);

	return AddressOf()[index];
}

const char &asCString::operator [](size_t index) const {
	asASSERT(index < length);

	return AddressOf()[index];
}

asCString asCString::SubString(size_t in_start, size_t in_length) const {
	if (in_start >= GetLength() || in_length == 0)
		return asCString("");

	if (in_length == (size_t)(-1)) in_length = GetLength() - in_start;

	asCString tmp;
	tmp.Assign(AddressOf() + in_start, in_length);

	return tmp;
}

int asCString::Compare(const char *str) const {
	return asCompareStrings(AddressOf(), length, str, strlen(str));
}

int asCString::Compare(const asCString &str) const {
	return asCompareStrings(AddressOf(), length, str.AddressOf(), str.GetLength());
}

int asCString::Compare(const char *str, size_t len) const {
	return asCompareStrings(AddressOf(), length, str, len);
}

size_t asCString::RecalculateLength() {
	SetLength(strlen(AddressOf()));

	return length;
}

int asCString::FindLast(const char *str, int *count) const {
	// There is no strstr that starts from the end, so
	// we'll iterate until we find the last occurrance.
	// This shouldn't cause a performance problem because
	// it is not expected that this will be done very often,
	// and then only on quite short strings anyway.

	if (count) *count = 0;

	const char *last = 0;
	const char *curr = AddressOf() - 1;
	while ((curr = strstr(curr + 1, str)) != 0) {
		if (count)(*count)++;
		last = curr;
	}

	if (last)
		return int(last - AddressOf());

	return -1;
}

//-----------------------------------------------------------------------------
// Helper functions

bool operator ==(const asCString &a, const char *b) {
	return a.Compare(b) == 0;
}

bool operator !=(const asCString &a, const char *b) {
	return a.Compare(b) != 0;
}

bool operator ==(const asCString &a, const asCString &b) {
	return a.Compare(b) == 0;
}

bool operator !=(const asCString &a, const asCString &b) {
	return a.Compare(b) != 0;
}

bool operator ==(const char *a, const asCString &b) {
	return b.Compare(a) == 0;
}

bool operator !=(const char *a, const asCString &b) {
	return b.Compare(a) != 0;
}

bool operator <(const asCString &a, const asCString &b) {
	return a.Compare(b) < 0;
}

asCString operator +(const asCString &a, const asCString &b) {
	asCString res = a;
	res += b;

	return res;
}

asCString operator +(const char *a, const asCString &b) {
	asCString res = a;
	res += b;

	return res;
}

asCString operator +(const asCString &a, const char *b) {
	asCString res = a;
	res += b;

	return res;
}

// wrapper class

asCStringPointer::asCStringPointer()
	: string(0), length(0), cstring(0) {
}

asCStringPointer::asCStringPointer(const char *str, size_t len)
	: string(str), length(len), cstring(0) {
}

asCStringPointer::asCStringPointer(asCString *cstr)
	: string(0), length(0), cstring(cstr) {
}

const char *asCStringPointer::AddressOf() const {
	return string ? string : cstring->AddressOf();
}

size_t asCStringPointer::GetLength() const {
	return string ? length : cstring->GetLength();
}

bool asCStringPointer::operator==(const asCStringPointer &other) const {
	return asCompareStrings(AddressOf(), GetLength(), other.AddressOf(), other.GetLength()) == 0;
}

bool asCStringPointer::operator<(const asCStringPointer &other) const {
	return asCompareStrings(AddressOf(), GetLength(), other.AddressOf(), other.GetLength()) < 0;
}