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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501 | // CubitString.cpp
#include "CubitString.hpp"
#include "CubitDefines.h"
#ifdef _WIN32
#include <windows.h>
#define vsnprintf _vsnprintf
#endif
#ifdef _MSC_VER
#pragma warning ( 4 : 4291 4244 4305 4018 )
#define va_copy(dest, src) (dest = src)
#endif
#include <iomanip>
#include <cstring>
#include <stdexcept>
#include <algorithm>
#include <stdarg.h>
const size_t CubitString::npos = std::string::npos;
CubitString::CubitString()
{
}
CubitString::CubitString(const CubitString& s)
: rep(s.rep)
{
}
CubitString::~CubitString()
{
}
CubitString::CubitString(const char *s)
{
if(s)
{
rep = s;
}
}
CubitString::CubitString(const std::string& s)
: rep(s)
{
}
CubitString::CubitString(int i, char c)
: rep(i,c)
{
}
CubitString CubitString::number(double f, unsigned int max_length, unsigned int sig_digits)
{
std::stringstream str;
if(max_length)
{
str.width(max_length);
}
if(sig_digits)
{
str.precision(sig_digits);
}
str << f;
std::string ret = str.str();
size_t i = ret.find_first_not_of(' ');
if(i != std::string::npos)
ret = ret.substr(i);
// change precision to be short enough
if (max_length)
{
if(sig_digits == 0)
sig_digits = max_length;
while(ret.length() > max_length && sig_digits)
{
sig_digits--;
str.precision(sig_digits);
str.str(std::string());
str << f;
ret = str.str();
}
}
return ret.c_str();
}
CubitString CubitString::format(const char* format, ...)
{
va_list args, args2;
va_start(args, format);
// copy because first vsnprintf modifies args
va_copy(args2, args);<--- va_list 'args2' used before va_start() was called.
// how much room do we need for our string?
int num = vsnprintf(NULL, 0, format, args);
// string plus null terminator
std::vector<char> str;
str.resize(num+1);
// print string
num = vsnprintf(&str[0], num+1, format, args2);<--- va_list 'args2' used before va_start() was called.
// remove extra null terminator
str.resize(num);
va_end(args);
va_end(args2);<--- va_list 'args2' used before va_start() was called.
return str.empty() ? CubitString() : CubitString(&str[0]);
}
CubitString& CubitString::operator=(const CubitString& s)
{
rep = s.rep;
return *this;
}
#ifdef _WIN32
# define strcasecmp _stricmp
#endif
bool CubitString::compare(const CubitString& s, CaseSensitivity cs)
{
if(cs == CaseSensitive)
return this->rep == s.rep;
// someday we might need to fix this to be unicode aware
if(strcasecmp(this->rep.c_str(), s.rep.c_str()) == 0)
return true;
return false;
}
char CubitString::get_at(size_t pos) const
{
// some legacy code wants to look for the null terminator
if(pos == rep.size())
return 0;
return rep.at(pos);
}
bool CubitString::is_empty() const
{
return rep.empty();
}
size_t CubitString::length() const
{
return rep.length();
}
const char * CubitString::c_str() const
{
return rep.c_str();
}
const std::string& CubitString::str() const
{
return rep;
}
void CubitString::clear()
{
rep.clear();
}
void CubitString::resize(size_t n)
{
rep.resize(n);
}
void CubitString::resize(size_t n, char c)
{
rep.resize(n, c);
}
CubitString operator+(const CubitString& s1, const CubitString& s2)
{
return CubitString(s1) += s2;
}
CubitString operator+(const CubitString& s1, const char *c2)
{
return CubitString(s1) += c2;
}
CubitString operator+(const char *c1, const CubitString& s2)
{
return CubitString(c1) += s2;
}
CubitString& CubitString::operator+=(const CubitString& s)
{
rep += s.rep;
return *this;
}
size_t CubitString::find(const CubitString& s, size_t pos) const
{
if(pos != 0 && pos >= rep.size())
throw std::out_of_range("find index out of range");
return rep.find(s.rep, pos);
}
size_t CubitString::find_first_of(const CubitString& s, size_t pos) const
{
return rep.find_first_of(s.rep, pos);
}
size_t CubitString::find_first(char c, size_t pos) const
{
return rep.find(c, pos);
}
size_t CubitString::find_last(char c, size_t pos) const
{
return rep.find_last_of(c, pos);
}
CubitString CubitString::substr(size_t first, size_t count) const
{
if(first >= rep.size())
return CubitString();
if(count > rep.size() - first)
count = rep.size() - first;
return rep.substr(first, count).c_str();
}
void CubitString::erase(size_t pos, size_t len)
{
rep.erase(pos, len);
}
void CubitString::replace(size_t pos, size_t len, const CubitString& str)
{
rep.replace(pos, len, str.rep);
}
void CubitString::replace(const CubitString& to_find, const CubitString& to_replace)
{
size_t len = to_find.length();
size_t len2 = to_replace.length();
for(size_t idx = rep.find(to_find.c_str()); idx != npos; idx = rep.find(to_find.c_str(), idx))
{
rep.replace(idx, len, to_replace.rep);
idx += len2;
}
}
bool CubitString::ends_with(const CubitString& str) const
{
if(rep.size() >= str.rep.size())
{
size_t offset = rep.size() - str.rep.size();
return std::equal(str.rep.begin(), str.rep.end(), rep.begin() + offset);
}
return false;
}
bool CubitString::starts_with(const CubitString& str) const
{
if(rep.size() >= str.rep.size())
{
return std::equal(str.rep.begin(), str.rep.end(), rep.begin());
}
return false;
}
void CubitString::put_at(size_t pos, char c)
{
rep.at(pos) = c;
}
void CubitString::to_lower()
{
for(size_t i=0; i<rep.size(); i++)
{
rep.at(i) = tolower(rep.at(i));
}
}
void CubitString::to_upper()
{
for(size_t i=0; i<rep.size(); i++)
{
rep.at(i) = toupper(rep.at(i));
}
}
void CubitString::to_lower(char *string)
{
// convert this string to lower case
char *p = string;
while (*p != '\0')
{
*p = tolower (*p);
p++;
}
}
void CubitString::to_upper(char *string)
{
// convert this string to upper case
char *p = string;
while (*p != '\0')
{
*p = toupper (*p);
p++;
}
}
void CubitString::trim()
{
std::string::size_type start = rep.find_first_not_of(" \t\n\v\f\r");
rep.erase(0, start);
std::string::size_type end = rep.find_last_not_of(" \t\n\v\f\r");
rep.erase(end+1, std::string::npos);
}
void CubitString::simplify()
{
trim();
for(std::string::size_type i = rep.find_first_of(" \t\n\v\f\r"); i != std::string::npos;
i = rep.find_first_of(" \t\n\v\f\r", i+1))
{
std::string::size_type next_i = rep.find_first_not_of(" \t\n\v\f\r", i);
if(next_i > i+1)
{
rep.erase(i+1, next_i-i-1);
rep.at(i) = ' ';
}
}
}
void CubitString::tokenize( char delimiter, std::vector<CubitString> &strings ) const
{
std::stringstream ss(rep);
std::string s;
while(std::getline(ss, s, delimiter))
strings.push_back(CubitString(s.c_str()));
}
bool CubitString::operator==(const CubitString& s2) const
{
return rep == s2.rep;
}
bool CubitString::operator!=(const CubitString& s2) const
{
return rep != s2.rep;
}
bool operator<=(const CubitString& s1, const CubitString& s2)
{
return s1.rep <= s2.rep;
}
bool operator>=(const CubitString& s1, const CubitString& s2)
{
return s1.rep >= s2.rep;
}
bool operator<( const CubitString& s1, const CubitString& s2 )
{
return s1.rep < s2.rep;
}
bool operator>( const CubitString& s1, const CubitString& s2 )
{
return s1.rep > s2.rep;
}
#if defined(_WIN32)
std::wstring CubitString::toUtf16(const char* str)
{
// there are other methods (e.g. C++11), but since its Windows specific to support Windows APIs, just use their API.
std::wstring ret;
int sz = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
if(sz)
{
std::vector<wchar_t> result(sz);
MultiByteToWideChar(CP_UTF8, 0, str, -1, &result[0], sz);
ret = &result[0];
}
return ret;
}
std::wstring CubitString::toUtf16(const CubitString& str)
{
return toUtf16(str.c_str());
}
CubitString CubitString::toUtf8(const wchar_t* str)
{
CubitString ret;
int sz = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
if(sz)
{
std::vector<char> result(sz);
WideCharToMultiByte(CP_UTF8, 0, str, -1, &result[0], sz, NULL, NULL);
ret = &result[0];
}
return ret;
}
#endif
#if defined(_MSC_VER)
std::wstring CubitString::toNative(const char* str)
{
return toUtf16(str);
}
std::wstring CubitString::toNative(const CubitString& str)
{
return toUtf16(str);
}
#else
CubitString CubitString::toNative(const char* str)
{
return str;
}
CubitString CubitString::toNative(const CubitString& str)
{
return str;
}
#endif
std::wstring CubitString::toWide(const CubitString& str)
{
#ifdef _WIN32
return toUtf16(str);
#else
std::wstring wstr;
size_t len = mbstowcs(NULL, str.c_str(), 0) + 1;
if(len > 0)
{
std::vector<wchar_t> wchar(len);
mbstowcs(&wchar[0], str.c_str(), len);
wstr = &wchar[0];
}
return wstr;
#endif
}
CubitString CubitString::toNarrow(const std::wstring& wstr)
{
#ifdef _WIN32
return toUtf8(wstr.c_str()).c_str();
#else
CubitString str;
size_t len = wcstombs(NULL, wstr.c_str(), 0) + 1;
if(len > 0)
{
std::vector<char> chars(len);
wcstombs(&chars[0], wstr.c_str(), len);
str = &chars[0];
}
return str;
#endif
}
#ifdef TEST_STRING
void main() {
CubitString a = "Test ";
CubitString b = "String Class";
CubitString blank(' ');
CubitString c = a + b;
c+= b;
CubitString e = c;<--- Variable 'e' is assigned a value that is never used.
CubitString f = c;<--- Variable 'f' is assigned a value that is never used.
CubitString g = c;
g.put_at(1, 'Z');
cout << "G = " << g << endl;
cout << "C = " << c << endl;
c.put_at(1, 'X');
CubitString d = b;
d.put_at(1, 'Y');
}
#endif
|