aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/shared/internal/include/ApexString.h
blob: 86cb9b8128e0eb5dbdf9e838dbbfe7cedfeda05c (plain) (blame)
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
/*
 * Copyright (c) 2008-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */

#ifndef APEX_STRING_H
#define APEX_STRING_H

#include "ApexUsingNamespace.h"
#include "PsArray.h"
#include "PsString.h"
#include "PsArray.h"
#include "PsUserAllocated.h"
#include <PxFileBuf.h>

namespace nvidia
{
namespace apex
{

/**
 * ApexSimpleString - a simple string class
 */
class ApexSimpleString : public physx::Array<char>, public UserAllocated
{
public:
	ApexSimpleString() : physx::Array<char>(), length(0)
	{
	}

	explicit ApexSimpleString(const char* cStr) : physx::Array<char>(), length(0)
	{
		if (cStr)
		{
			length = (uint32_t)strlen(cStr);
			if (length > 0)
			{
				resize(length + 1);
				nvidia::strlcpy(begin(), size(), cStr);
			}
		}
	}

	ApexSimpleString(const ApexSimpleString& other) : physx::Array<char>()
	{
		length = other.length;
		if (length > 0)
		{
			resize(length + 1);
			nvidia::strlcpy(begin(), capacity(), other.c_str());
		}
		else
		{
			resize(0);
		}
	}

	ApexSimpleString(uint32_t number, uint32_t fixedLength = 0) : length(fixedLength)
	{
		if (fixedLength)
		{
			char format[5]; format[0] = '%'; format[1] = '0';
			char buffer[10];
			if (fixedLength > 9)
			{
				PX_ASSERT(fixedLength);
				fixedLength = 9;
			}
			physx::shdfnd::snprintf(format + 2, 2, "%d", fixedLength);
			format[3] = 'd'; format[4] = '\0';
			physx::shdfnd::snprintf(buffer, 10, format, number);
			resize(length + 1);
			nvidia::strlcpy(begin(), size(), buffer);
		}
		else
		{
			char buffer[10];
			physx::shdfnd::snprintf(buffer, 10, "%d", number);
			length = 1;
			while (number >= 10)
			{
				number /= 10;
				length++;
			}
			resize(length + 1);
			nvidia::strlcpy(begin(), size(), buffer);
		}
	}

	ApexSimpleString& operator = (const ApexSimpleString& other)
	{
		length = other.length;
		if (length > 0)
		{
			resize(length + 1);
			nvidia::strlcpy(begin(), capacity(), other.c_str());
		}
		else
		{
			resize(0);
		}
		return *this;
	}

	ApexSimpleString& operator = (const char* cStr)
	{
		if (!cStr)
		{
			erase();
		}
		else
		{
			length = (uint32_t)strlen(cStr);
			if (length > 0)
			{
				resize(length + 1);
				nvidia::strlcpy(begin(), capacity(), cStr);
			}
			else
			{
				resize(0);
			}
		}
		return *this;
	}

	void truncate(uint32_t newLength)
	{
		if (newLength < length)
		{
			length = newLength;
			begin()[length] = '\0';
		}
	}

	void serialize(physx::PxFileBuf& stream) const
	{
		stream.storeDword(length);
		stream.write(begin(), length);
	}

	void deserialize(physx::PxFileBuf& stream)
	{
		uint32_t len = stream.readDword();
		if (len > 0)
		{
			resize(len + 1);
			stream.read(begin(), len);
			begin()[len] = '\0';
			length = len;
		}
		else
		{
			erase();
		}
	}

	uint32_t	len() const
	{
		return length;
	}

	/* PH: Cast operator not allowed by coding guidelines, and evil in general anyways
	operator const char* () const
	{
	return capacity() ? begin() : "";
	}
	*/
	const char* c_str() const
	{
		return capacity() > 0 ? begin() : "";
	}

	bool operator==(const ApexSimpleString& s) const
	{
		return nvidia::strcmp(c_str(), s.c_str()) == 0;
	}
	bool operator!=(const ApexSimpleString& s) const
	{
		return ! this->operator==(s);
	}
	bool operator==(const char* s) const
	{
		return nvidia::strcmp(c_str(), s) == 0;
	}
	bool operator!=(const char* s) const
	{
		return ! this->operator==(s);
	}
	bool operator < (const ApexSimpleString& s) const
	{
		return nvidia::strcmp(c_str(), s.c_str()) < 0;
	}

	ApexSimpleString& operator += (const ApexSimpleString& s)
	{
		expandTo(length + s.length);
		nvidia::strlcpy(begin() + length, capacity() - length, s.c_str());
		length += s.length;
		return *this;
	}

	ApexSimpleString& operator += (char c)
	{
		expandTo(length + 1);
		begin()[length++] = c;
		begin()[length] = '\0';
		return *this;
	}

	ApexSimpleString operator + (const ApexSimpleString& s)
	{
		ApexSimpleString sum = *this;
		sum += s;
		return sum;
	}

	ApexSimpleString& 	clear()
	{
		if (capacity())
		{
			begin()[0] = '\0';
		}
		length = 0;
		return *this;
	}

	ApexSimpleString& 	erase()
	{
		resize(0);
		return clear();
	}

	static PX_INLINE void ftoa(float f, ApexSimpleString& s)
	{
		char buf[20];
		physx::shdfnd::snprintf(buf, sizeof(buf), "%g", f);
		s = buf;
	}

	static PX_INLINE void itoa(uint32_t i, ApexSimpleString& s)
	{
		char buf[20];
		physx::shdfnd::snprintf(buf, sizeof(buf), "%i", i);
		s = buf;
	}

private:

	void expandTo(uint32_t stringCapacity)
	{
		if (stringCapacity + 1 > capacity())
		{
			resize(2 * stringCapacity + 1);
		}
	}

	uint32_t length;
};

PX_INLINE ApexSimpleString operator + (const ApexSimpleString& s1, const ApexSimpleString& s2)
{
	ApexSimpleString result = s1;
	result += s2;
	return result;
}

} // namespace apex
} // namespace nvidia

#endif // APEX_STRING_H