~smspillaz/nux/nux.fix_1036521

« back to all changes in this revision

Viewing changes to NuxCore/NString.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h"
 
2
 
 
3
NAMESPACE_BEGIN
 
4
//
 
5
// Copy a string with length checking.
 
6
//warning: Behavior differs from strncpy; last character is zeroed.
 
7
//
 
8
TCHAR* Strncpy(TCHAR* Dest, t_size Size, const TCHAR* Src, t_size MaxLen)
 
9
{
 
10
    nuxAssert(MaxLen>=0);
 
11
    STRNCPY_S(Dest, Size, Src, MaxLen);
 
12
    Dest[MaxLen]=0;
 
13
    return Dest;
 
14
}
 
15
 
 
16
//
 
17
// Concatenate a string with length checking
 
18
//
 
19
TCHAR* Strncat(TCHAR* Dest, t_size Size, const TCHAR* Src, t_size MaxLen)
 
20
{
 
21
    t_size Len = StringLength(Dest);
 
22
    nuxAssert(Size >= Len);
 
23
    TCHAR* NewDest = Dest + Len;
 
24
    if((MaxLen -= Len) > 0)
 
25
    {
 
26
        Strncpy(NewDest, Size-Len, Src, MaxLen);
 
27
        NewDest[MaxLen-1] = 0;
 
28
    }
 
29
    return Dest;
 
30
}
 
31
 
 
32
 
 
33
// Search a string inside a string. Return a pointer to the beginning of the searched string if it is found. Else, return NULL;
 
34
// The shearched string must be preceded by a non alpha numeric character in Str.
 
35
 
 
36
const TCHAR* Strfind(const TCHAR* Str, const TCHAR* Find)
 
37
{
 
38
    nuxAssert(Find != NULL);
 
39
    nuxAssert(Str != NULL);
 
40
 
 
41
    if(Find == NULL || Str == NULL)
 
42
    {
 
43
        return NULL;
 
44
    }
 
45
 
 
46
    bool AlphaNum = 0;
 
47
    TCHAR First = ((*Find < TEXT('a')) || (*Find > TEXT('z'))) ? (*Find) : (*Find + TEXT('A') - TEXT('a'));
 
48
    t_size Len = StringLength(Find++) - 1;
 
49
    TCHAR chr = *Str++;
 
50
 
 
51
    while(chr)
 
52
    {
 
53
        if((chr >= TEXT('a')) && (chr <= TEXT('z')))
 
54
        {
 
55
            chr += TEXT('A') - TEXT('a');
 
56
        }
 
57
        if(!AlphaNum && (chr == First) && !TCharStringNICompare(Str, Find, Len))
 
58
        {
 
59
            return Str-1;
 
60
        }
 
61
        AlphaNum = ((chr >= TEXT('A')) && (chr <= TEXT('Z'))) || ((chr >= TEXT('0')) && (chr <= TEXT('9')));
 
62
        chr = *Str++;
 
63
    }
 
64
    return NULL;
 
65
}
 
66
 
 
67
bool IsLastChar(const TCHAR* CharString, const TCHAR Chr)
 
68
{
 
69
    nuxAssert(CharString != 0);
 
70
    if(CharString == 0)
 
71
        return false;
 
72
 
 
73
    t_size Size = StringLength(CharString);
 
74
    if(Size == 0)
 
75
        return false;
 
76
 
 
77
    if(CharString[Size-1] == Chr)
 
78
        return true;
 
79
    return false;
 
80
}
 
81
 
 
82
NString Itoa(int InNum)
 
83
{
 
84
    SQWORD      Num                                     = InNum; // This avoids having to deal with negating -MaxS32 - 1
 
85
    NString NumberString;
 
86
    const TCHAR*        NumberChar[10]          = { TEXT("0"), TEXT("1"), TEXT("2"), TEXT("3"), TEXT("4"), TEXT("5"), TEXT("6"), TEXT("7"), TEXT("8"), TEXT("9") };
 
87
    bool        bIsNumberNegative       = FALSE;
 
88
 
 
89
    // Correctly handle negative numbers and convert to positive integer.
 
90
    if(Num < 0)
 
91
    {
 
92
        bIsNumberNegative = TRUE;
 
93
        Num = -Num;
 
94
    }
 
95
 
 
96
    // Convert to string assuming base ten and a positive integer.
 
97
    do 
 
98
    {
 
99
        NumberString += NumberChar[Num % 10];
 
100
        Num /= 10;
 
101
    } while(Num);
 
102
 
 
103
    // Append sign as we're going to reverse string afterwards.
 
104
    if(bIsNumberNegative)
 
105
    {
 
106
        NumberString += TEXT("-");
 
107
    }
 
108
 
 
109
    NumberString.Reverse();
 
110
    return NumberString;
 
111
}
 
112
 
 
113
//! Duplicate a null terminated string using new[]. The resulting string is NULL terminated. Use delete[] to destroy.
 
114
TCHAR* Strdup(const TCHAR* str)
 
115
{
 
116
    if(str == 0)
 
117
        return 0;
 
118
    t_size len = StringLength(str);
 
119
    if(len>=0)
 
120
    {
 
121
        TCHAR* res = new TCHAR[len+1];
 
122
        Strncpy(res, len+1, str, len);
 
123
        return res;
 
124
    }
 
125
    return 0;
 
126
}
 
127
 
 
128
//! Duplicate a null terminated ANSICHAR string using new[]. The resulting string is NULL terminated. Use delete[] to destroy.
 
129
ANSICHAR* StrdupA( const ANSICHAR* str)
 
130
{
 
131
    if(str == 0)
 
132
        return 0;
 
133
    int len = (int)strlen(str);
 
134
    if(len>=0)
 
135
    {
 
136
        ANSICHAR* res = new ANSICHAR[len+1];
 
137
        STRNCPY_S((char*)res, len+1, (const char*)str, len);
 
138
        return res;
 
139
    }
 
140
    return 0;
 
141
}
 
142
 
 
143
//! Duplicate a null terminated UNICHAR string using new[]. The resulting string is NULL terminated. Use delete[] to destroy.
 
144
UNICHAR* StrdupU( const UNICHAR* str)
 
145
{
 
146
    if(str == 0)
 
147
        return 0;
 
148
    int len = (int)wcslen((const wchar_t *)str);
 
149
    if(len>=0)
 
150
    {
 
151
        UNICHAR* res = new UNICHAR[len+1];
 
152
        WCSNCPY_S((wchar_t*)res, len+1, (const wchar_t*)str, len);
 
153
        return res;
 
154
    }
 
155
    return 0;
 
156
}
 
157
// /*
 
158
// * Standard string formatted print.
 
159
// */
 
160
// VARARG_BODY(int, inlSprintf, const TCHAR*, VARARG_EXTRA(TCHAR* Dest))
 
161
// {
 
162
//     int      Result = -1;
 
163
//     va_list ap;
 
164
//     va_start(ap, Fmt);
 
165
//     //@warning: make sure code using inlSprintf allocates enough memory if the below 1024 is ever changed.
 
166
//     GET_VARARGS_RESULT(Dest,1024/*!!*/,Fmt,Result);
 
167
//     return Result;
 
168
// }
 
169
 
 
170
 
 
171
t_size ValueToLiteralString(char* buffer, t_u32 len, t_u16     value) { return ToCharString(buffer, len, "%hu",     value); }
 
172
t_size ValueToLiteralString(char* buffer, t_u32 len, t_s16     value) { return ToCharString(buffer, len, "%hi",     value); }
 
173
t_size ValueToLiteralString(char* buffer, t_u32 len, t_u32     value) { return ToCharString(buffer, len, "%lu",     value); }
 
174
t_size ValueToLiteralString(char* buffer, t_u32 len, t_s32     value) { return ToCharString(buffer, len, "%li",     value); }
 
175
t_size ValueToLiteralString(char* buffer, t_u32 len, t_ulong   value) { return ToCharString(buffer, len, "%lu",     value); }
 
176
t_size ValueToLiteralString(char* buffer, t_u32 len, t_long    value) { return ToCharString(buffer, len, "%li",     value); }
 
177
t_size ValueToLiteralString(char* buffer, t_u32 len, t_u64     value) { return ToCharString(buffer, len, "%I64u",   value); }
 
178
t_size ValueToLiteralString(char* buffer, t_u32 len, t_s64     value) { return ToCharString(buffer, len, "%I64i",   value); }
 
179
t_size ValueToLiteralString(char* buffer, t_u32 len, t_float   value) { return ToCharString(buffer, len, "%.10g",   value); }
 
180
t_size ValueToLiteralString(char* buffer, t_u32 len, t_double  value) { return ToCharString(buffer, len, "%.20lg",  value); }
 
181
t_size ValueToLiteralString(char* buffer, t_u32 len, t_u8      value) { return ValueToLiteralString(buffer, len,    (t_u32) value); }
 
182
t_size ValueToLiteralString(char* buffer, t_u32 len, t_char    value) { return ValueToLiteralString(buffer, len,    (t_s32) value); }
 
183
t_size ValueToLiteralString(char* buffer, t_u32 len, t_s8      value) { return ValueToLiteralString(buffer, len,    (t_s32) value); }
 
184
 
 
185
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_u16    &value) { return FromCharString(buffer, len, "%hu", value ); }
 
186
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_s16    &value) { return FromCharString(buffer, len, "%hi", value ); }
 
187
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_u32    &value) { return FromCharString(buffer, len, "%lu", value ); }
 
188
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_s32    &value) { return FromCharString(buffer, len, "%li", value ); }
 
189
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_ulong  &value) { return FromCharString(buffer, len, "%lu", value ); }
 
190
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_long   &value) { return FromCharString(buffer, len, "%li", value ); }
 
191
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_u64    &value) { return FromCharString(buffer, len, "%I64u", value ); }
 
192
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_s64    &value) { return FromCharString(buffer, len, "%I64i", value ); }
 
193
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_float  &value) { return FromCharString(buffer, len, "%g", value ); }
 
194
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_double &value) { return FromCharString(buffer, len, "%lg", value ); }
 
195
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_u8     &value) 
 
196
{
 
197
    t_u32 tmp = 0;
 
198
    bool result;
 
199
    result = ValueFromLiteralString(buffer, len, tmp);
 
200
    value = (t_u8)tmp;
 
201
    return result;
 
202
}
 
203
 
 
204
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_char &value) 
 
205
{
 
206
    t_s32 tmp = 0;
 
207
    bool result;
 
208
    result = ValueFromLiteralString(buffer, len, tmp);
 
209
    value = (t_char)tmp;
 
210
    return result;
 
211
}
 
212
 
 
213
bool ValueFromLiteralString(const char* buffer, t_u32 len, t_s8 &value)
 
214
{
 
215
    t_s32 tmp = 0;
 
216
    bool result;
 
217
    result = ValueFromLiteralString(buffer, len, tmp);
 
218
    value = (t_s8)tmp;
 
219
    return result;
 
220
}
 
221
 
 
222
VARARG_BODY(int, Snprintf, const TCHAR*, VARARG_EXTRA(TCHAR* Dest) VARARG_EXTRA(int Size) VARARG_EXTRA(int Count))
 
223
{
 
224
    int Result = -1;
 
225
    GET_VARARGS_RESULT(Dest, Size, Count, Fmt, Result);
 
226
    return Result;
 
227
}
 
228
 
 
229
NString::NString()
 
230
{
 
231
 
 
232
}
 
233
 
 
234
NString::NString(const NString& s)
 
235
{
 
236
    m_string = s.m_string;
 
237
}
 
238
 
 
239
NString& NString::operator= (const NString& s)
 
240
{
 
241
    m_string = s.m_string;
 
242
    return *this;
 
243
}
 
244
 
 
245
NString::NString(const tstring& s)
 
246
{
 
247
    m_string = s;
 
248
}
 
249
 
 
250
NString::NString(const TCHAR& s)
 
251
{
 
252
    m_string = s;
 
253
}
 
254
 
 
255
NString::NString(const ANSICHAR* s)
 
256
{
 
257
#ifdef UNICODE
 
258
    if(s == 0)
 
259
        m_string = TEXT("");
 
260
    else
 
261
        m_string = ANSICHAR_TO_UNICHAR(s);
 
262
#else
 
263
    if(s == 0)
 
264
        m_string = TEXT("");
 
265
    else
 
266
        m_string = s;
 
267
#endif
 
268
}
 
269
 
 
270
NString::NString(const UNICHAR* s)
 
271
{
 
272
#ifdef UNICODE
 
273
    if(s == 0)
 
274
        m_string = TEXT("");
 
275
    else
 
276
        m_string = s;
 
277
#else
 
278
    if(s == 0)
 
279
        m_string = TEXT("");
 
280
    else
 
281
        m_string = UNICHAR_TO_ANSICHAR(s);
 
282
#endif
 
283
}
 
284
 
 
285
 
 
286
NString::~NString()
 
287
{
 
288
}
 
289
 
 
290
const tstring& NString::GetTStringRef() const
 
291
{
 
292
    return m_string;
 
293
}
 
294
 
 
295
//const TCHAR* NString::GetTChar() const
 
296
//{
 
297
//    return m_string.c_str();
 
298
//}
 
299
 
 
300
const TCHAR* NString::GetTCharPtr() const
 
301
{
 
302
    return m_string.c_str();
 
303
}
 
304
 
 
305
t_size NString::Length() const
 
306
{
 
307
    return m_string.length();
 
308
}
 
309
 
 
310
t_size NString::Size() const
 
311
{
 
312
    return m_string.size();
 
313
}
 
314
 
 
315
void NString::Clear()
 
316
{
 
317
    m_string.clear();
 
318
}
 
319
 
 
320
bool NString::IsEmpty()
 
321
{
 
322
    return m_string.empty();
 
323
}
 
324
 
 
325
void NString::Erase(t_size Pos, t_size Count)
 
326
{
 
327
    m_string.erase(Pos, Count);
 
328
}
 
329
 
 
330
NString& NString::Insert(t_size Pos, const TCHAR* Ptr)
 
331
{
 
332
    m_string.insert(Pos, Ptr);
 
333
    return *this;
 
334
}
 
335
 
 
336
NString& NString::Insert(t_size Pos, const TCHAR* Ptr, t_size Count)
 
337
{
 
338
    m_string.insert(Pos, Ptr, Count);
 
339
    return *this;
 
340
}
 
341
 
 
342
NString& NString::Insert(t_size Pos, const tstring& Str)
 
343
{
 
344
    m_string.insert(Pos, Str);
 
345
    return *this;
 
346
}
 
347
 
 
348
NString& NString::Insert(t_size Pos, const tstring& Str, t_size Offset, t_size Count)
 
349
{
 
350
    m_string.insert(Pos, Str, Offset, Count);
 
351
    return *this;
 
352
}
 
353
 
 
354
NString& NString::Insert(t_size Pos, const NString& Str)
 
355
{
 
356
    m_string.insert(Pos, Str.m_string);
 
357
    return *this;
 
358
}
 
359
 
 
360
NString& NString::Insert(t_size Pos, const NString& Str, t_size Offset, t_size Count)
 
361
{
 
362
    m_string.insert(Pos, Str.m_string, Offset, Count);
 
363
    return *this;
 
364
}
 
365
 
 
366
NString& NString::Insert(t_size Pos, int Count, const TCHAR& Ch)
 
367
{
 
368
    m_string.insert(Pos, Count, Ch);
 
369
    return *this;
 
370
}
 
371
 
 
372
const TCHAR& NString::operator[](t_size ChPos) const
 
373
{
 
374
    return m_string[ChPos];
 
375
}
 
376
 
 
377
TCHAR& NString::operator[](t_size ChPos)
 
378
{
 
379
    return m_string[ChPos];
 
380
}
 
381
 
 
382
NString& NString::Replace(t_size Pos1, t_size Num1, const TCHAR* Ptr)
 
383
{
 
384
    m_string.replace(Pos1, Num1, Ptr);
 
385
    return *this;
 
386
}
 
387
 
 
388
NString& NString::Replace(t_size Pos1, t_size Num1, const TCHAR* Ptr, t_size Num2)
 
389
{
 
390
    m_string.replace(Pos1, Num1, Ptr, Num2);
 
391
    return *this;
 
392
}
 
393
 
 
394
NString& NString::Replace(t_size Pos1, t_size Num1, const tstring& Str)
 
395
{
 
396
    m_string.replace(Pos1, Num1, Str);
 
397
    return *this;
 
398
}
 
399
 
 
400
NString& NString::Replace(t_size Pos1, t_size Num1, const tstring& Str, t_size Pos2, t_size Num2)
 
401
{
 
402
    m_string.replace(Pos1, Num1, Str, Pos2, Num2);
 
403
    return *this;
 
404
}
 
405
 
 
406
NString& NString::Replace(t_size Pos1, t_size Num1, const NString& Str)
 
407
{
 
408
    m_string.replace(Pos1, Num1, Str.m_string);
 
409
    return *this;
 
410
}
 
411
 
 
412
NString& NString::Replace(t_size Pos1, t_size Num1, const NString& Str, t_size Pos2, t_size Num2)
 
413
{
 
414
    m_string.replace(Pos1, Num1, Str.m_string, Pos2, Num2);
 
415
    return *this;
 
416
}
 
417
 
 
418
NString& NString::Replace(t_size Pos1, t_size Num1, t_size Count, TCHAR Ch)
 
419
{
 
420
    m_string.replace(Pos1, Num1, Count, Ch);
 
421
    return *this;
 
422
}
 
423
 
 
424
void NString::Reverse()
 
425
{
 
426
    NString rev;
 
427
    t_size l = Length();
 
428
    for(t_size i = l-1; i >= 0; i--)
 
429
    {
 
430
        rev += m_string[i];
 
431
    }
 
432
    (*this) = rev;
 
433
}
 
434
 
 
435
NString& NString::SearchAndReplace(TCHAR ChOut, TCHAR ChIn)
 
436
{
 
437
    for(t_size i = 0; i < Length(); i++)
 
438
        if(m_string[i] == ChOut)
 
439
            m_string[i] = ChIn;
 
440
    return *this;
 
441
}
 
442
 
 
443
//! Return The last position of the substring suffix or -1 if it is not found.
 
444
t_size NString::FindLastOccurence(const TCHAR& suffix) const
 
445
{
 
446
    t_size start = 0;
 
447
    t_size pos = 0;
 
448
    do
 
449
    {
 
450
        pos = m_string.find(suffix, start);
 
451
        if(pos != tstring::npos)
 
452
            start = pos + 1;
 
453
    } while(pos != tstring::npos);
 
454
    return start-1;
 
455
}
 
456
 
 
457
//! Return The last position of the substring suffix or -1 if it is not found.
 
458
t_size NString::FindLastOccurence(const TCHAR* suffix) const
 
459
{
 
460
    t_size start = 0;
 
461
    t_size pos = 0;
 
462
    do
 
463
    {
 
464
        pos = m_string.find(suffix, start);
 
465
        if(pos != tstring::npos)
 
466
            start = pos + 1;
 
467
    } while(pos != tstring::npos);
 
468
    return start-1;
 
469
}
 
470
 
 
471
//! Return The last position of the substring suffix or -1 if it is not found.
 
472
t_size NString::FindLastOccurence(const tstring& suffix) const
 
473
{
 
474
    t_size start = 0;
 
475
    t_size pos = 0;
 
476
    do
 
477
    {
 
478
        pos = m_string.find(suffix, start);
 
479
        if(pos != tstring::npos)
 
480
            start = pos + 1;
 
481
    } while(pos != tstring::npos);
 
482
    return start-1;
 
483
}
 
484
 
 
485
//! Return The last position of the substring suffix or -1 if it is not found.
 
486
t_size NString::FindLastOccurence(const NString& suffix) const
 
487
{
 
488
    t_size start = 0;
 
489
    t_size pos = 0;
 
490
    do
 
491
    {
 
492
        pos = m_string.find(suffix.m_string, start);
 
493
        if(pos != tstring::npos)
 
494
            start = pos + 1;
 
495
    } while(pos != tstring::npos);
 
496
    return start-1;
 
497
}
 
498
 
 
499
 
 
500
//! Return the position of the first occurrence of the substring suffix or -1 if it is not found.
 
501
t_size NString::FindFirstOccurence(const TCHAR& suffix)  const
 
502
{
 
503
    t_size pos = 0;
 
504
    pos = m_string.find(suffix, pos);
 
505
    return (pos != tstring::npos)? pos : -1;
 
506
}
 
507
 
 
508
//! Return the position of the first occurrence of the substring suffix or -1 if it is not found.
 
509
t_size NString::FindFirstOccurence(const TCHAR* suffix)  const
 
510
{
 
511
    t_size pos = 0;
 
512
    pos = m_string.find(suffix, pos);
 
513
    return (pos != tstring::npos)? pos : -1;
 
514
}
 
515
 
 
516
//! Return the position of the first occurrence of the substring suffix or -1 if it is not found.
 
517
t_size NString::FindFirstOccurence(const tstring& suffix)  const
 
518
{
 
519
    t_size pos = 0;
 
520
    pos = m_string.find(suffix, pos);
 
521
    return (pos != tstring::npos)? pos : -1;
 
522
}
 
523
 
 
524
//! Return the position of the first occurrence of the substring suffix or -1 if it is not found.
 
525
t_size NString::FindFirstOccurence(const NString& suffix) const
 
526
{
 
527
    t_size pos = 0;
 
528
    pos = m_string.find(suffix.m_string, pos);
 
529
    return (pos != tstring::npos)? pos : -1;
 
530
}
 
531
 
 
532
//! Return the position of the first occurrence of the substring suffix or -1 if it is not found.
 
533
t_size NString::FindNextOccurence(const TCHAR& suffix, t_size start) const
 
534
{
 
535
    t_size pos = 0;
 
536
    pos = m_string.find(suffix, start);
 
537
    return (pos != tstring::npos)? pos : -1;
 
538
}
 
539
 
 
540
//! Return the position of the first occurrence of the substring suffix or -1 if it is not found.
 
541
t_size NString::FindNextOccurence(const TCHAR* suffix, t_size start) const
 
542
{
 
543
    t_size pos = 0;
 
544
    pos = m_string.find(suffix, start);
 
545
    return (pos != tstring::npos)? pos : -1;
 
546
}
 
547
 
 
548
//! Return the position of the first occurrence of the substring suffix or -1 if it is not found.
 
549
t_size NString::FindNextOccurence(const tstring& suffix, t_size start) const
 
550
{
 
551
    t_size pos = 0;
 
552
    pos = m_string.find(suffix, start);
 
553
    return (pos != tstring::npos)? pos : -1;
 
554
}
 
555
 
 
556
//! Return the position of the first occurrence of the substring suffix or -1 if it is not found.
 
557
t_size NString::FindNextOccurence(const NString& suffix, t_size start) const
 
558
{
 
559
    t_size pos = 0;
 
560
    pos = m_string.find(suffix.m_string, start);
 
561
    return (pos != tstring::npos)? pos : -1;
 
562
}
 
563
 
 
564
//! Searches through a string for the first character that matches any element of a specified string. Return -1 if it is not found.
 
565
t_size NString::FindFirstOccurenceOf(const TCHAR& str) const
 
566
{
 
567
    t_size pos = 0;
 
568
    pos = m_string.find_first_of(str, pos);
 
569
    return (pos != tstring::npos)? pos : -1;
 
570
}
 
571
//! Searches through a string for the first character that matches any element of a specified string. Return -1 if it is not found.
 
572
t_size NString::FindFirstOccurenceOf(const TCHAR* str) const
 
573
{
 
574
    t_size pos = 0;
 
575
    pos = m_string.find_first_of(str, pos);
 
576
    return (pos != tstring::npos)? pos : -1;
 
577
}
 
578
//! Searches through a string for the first character that matches any element of a specified string. Return -1 if it is not found.
 
579
t_size NString::FindFirstOccurenceOf(const tstring& str) const
 
580
{
 
581
    t_size pos = 0;
 
582
    pos = m_string.find_first_of(str, pos);
 
583
    return (pos != tstring::npos)? pos : -1;
 
584
}
 
585
//! Searches through a string for the first character that matches any element of a specified string. Return -1 if it is not found.
 
586
t_size NString::FindFirstOccurenceOf(const NString& str) const
 
587
{
 
588
    t_size pos = 0;
 
589
    pos = m_string.find_first_of(str.m_string, pos);
 
590
    return (pos != tstring::npos)? pos : -1;
 
591
}
 
592
 
 
593
//! Searches through a string for the last character that matches any element of a specified string. Return -1 if it is not found.
 
594
t_size NString::FindLastOccurenceOf(const TCHAR& str) const
 
595
{
 
596
    t_size pos = 0;
 
597
    pos = m_string.find_last_of(str, pos);
 
598
    return (pos != tstring::npos)? pos : -1;
 
599
}
 
600
//! Searches through a string for the last character that matches any element of a specified string. Return -1 if it is not found.
 
601
t_size NString::FindLastOccurenceOf(const TCHAR* str) const
 
602
{
 
603
    t_size pos = 0;
 
604
    pos = m_string.find_last_of(str, pos);
 
605
    return (pos != tstring::npos)? pos : -1;
 
606
}
 
607
//! Searches through a string for the last character that matches any element of a specified string. Return -1 if it is not found.
 
608
t_size NString::FindLastOccurenceOf(const tstring& str) const
 
609
{
 
610
    t_size pos = 0;
 
611
    pos = m_string.find_last_of(str, pos);
 
612
    return (pos != tstring::npos)? pos : -1;
 
613
}
 
614
//! Searches through a string for the last character that matches any element of a specified string. Return -1 if it is not found.
 
615
t_size NString::FindLastOccurenceOf(const NString& str) const
 
616
{
 
617
    t_size pos = 0;
 
618
    pos = m_string.find_last_of(str.m_string, pos);
 
619
    return (pos != tstring::npos)? pos : -1;
 
620
}
 
621
 
 
622
t_size NString::Find(NString str, int start)
 
623
{
 
624
    t_size pos = m_string.find(str.m_string, start);
 
625
    return (pos != tstring::npos)? pos : -1;
 
626
}
 
627
 
 
628
t_size NString::Find(TCHAR c, int start)
 
629
{
 
630
    t_size pos = m_string.find(c, start);
 
631
    return (pos != tstring::npos)? pos : -1;
 
632
}
 
633
 
 
634
bool NString::IsSuffix(const TCHAR& suffix)
 
635
{
 
636
    t_size l = m_string.length()-1;
 
637
    if(l < 0)
 
638
        return FALSE;
 
639
    t_size pos = FindLastOccurence(suffix);
 
640
    if(pos == -1)
 
641
        return FALSE;
 
642
    return (pos == l);
 
643
}
 
644
 
 
645
bool NString::IsSuffix(const TCHAR* suffix)
 
646
{
 
647
    t_size sl = StringLength(suffix);
 
648
    if(sl == 0)
 
649
        return FALSE;
 
650
    t_size l = m_string.length() - sl;
 
651
    if(l < 0)
 
652
        return FALSE;
 
653
    t_size pos = FindLastOccurence(suffix);
 
654
    if(pos == -1)
 
655
        return FALSE;
 
656
    return (pos == l);
 
657
}
 
658
 
 
659
//! Return True is the the string is terminated by the tstring 'suffix'
 
660
bool NString::IsSuffix(const tstring& suffix)
 
661
{
 
662
    t_size sl = suffix.length();
 
663
    if(sl == 0)
 
664
        return FALSE;
 
665
    t_size l = m_string.length() - sl;
 
666
    if(l < 0)
 
667
        return FALSE;
 
668
    t_size pos = FindLastOccurence(suffix);
 
669
    if(pos == -1)
 
670
        return FALSE;
 
671
    return (pos == l);
 
672
}
 
673
 
 
674
//! Return True is the the string is terminated by the NString 'suffix'
 
675
bool NString::IsSuffix(const NString& suffix)
 
676
{
 
677
    t_size sl = suffix.Length();
 
678
    if(sl == 0)
 
679
        return FALSE;
 
680
    t_size l = m_string.length() - sl;
 
681
    if(l < 0)
 
682
        return FALSE;
 
683
    t_size pos = FindLastOccurence(suffix);
 
684
    if(pos == -1)
 
685
        return FALSE;
 
686
    return (pos == l);
 
687
}
 
688
 
 
689
//! Return True if the string start with the character contained in prefix
 
690
bool NString::IsPrefix(const TCHAR& prefix)
 
691
{
 
692
    t_size l = m_string.length()-1;
 
693
    if(l < 0)
 
694
        return FALSE;
 
695
    t_size pos = FindFirstOccurence(prefix);
 
696
    if(pos == -1)
 
697
        return FALSE;
 
698
    return (pos == 0);
 
699
}
 
700
 
 
701
//! Return True if the string start with the character string contained in prefix
 
702
bool NString::IsPrefix(const TCHAR* prefix)
 
703
{
 
704
    t_size sl = StringLength(prefix);
 
705
    if(sl == 0)
 
706
        return FALSE;
 
707
    t_size pos = FindFirstOccurence(prefix);
 
708
    if(pos == -1)
 
709
        return FALSE;
 
710
    return (pos == 0);
 
711
}
 
712
 
 
713
//! Return True if the string start with the tstring contained in prefix
 
714
bool NString::IsPrefix(const tstring& prefix)
 
715
{
 
716
    t_size sl = prefix.length();
 
717
    if(sl == 0)
 
718
        return FALSE;
 
719
    t_size pos = FindFirstOccurence(prefix);
 
720
    if(pos == -1)
 
721
        return FALSE;
 
722
    return (pos == 0);
 
723
}
 
724
//! Return True if the string start with the NString contained in prefix
 
725
bool NString::IsPrefix(const NString& prefix)
 
726
{
 
727
    t_size sl = prefix.Length();
 
728
    if(sl == 0)
 
729
        return FALSE;
 
730
    t_size pos = FindFirstOccurence(prefix);
 
731
    if(pos == -1)
 
732
        return FALSE;
 
733
    return (pos == 0);
 
734
}
 
735
 
 
736
//! Return an NString without the character contained in suffix
 
737
void NString::RemoveSuffix(const TCHAR& suffix)
 
738
{
 
739
    if(IsSuffix(suffix))
 
740
    {
 
741
        t_size pos = FindLastOccurence(suffix);
 
742
        *this = NString(m_string.substr(0, pos));
 
743
    }
 
744
}
 
745
 
 
746
//! Return an NString without the character string 'suffix'
 
747
void NString::RemoveSuffix(const TCHAR* suffix)
 
748
{
 
749
    if(IsSuffix(suffix))
 
750
    {
 
751
        t_size pos = FindLastOccurence(suffix);
 
752
        *this = NString(m_string.substr(0, pos));
 
753
    }
 
754
}
 
755
 
 
756
//! Return an NString without the tstring 'suffix'
 
757
void NString::RemoveSuffix(const tstring& suffix)
 
758
{
 
759
    if(IsSuffix(suffix))
 
760
    {
 
761
        t_size pos = FindLastOccurence(suffix);
 
762
        *this = NString(m_string.substr(0, pos));
 
763
    }
 
764
}
 
765
 
 
766
//! Return an NString without the NString 'suffix'
 
767
void NString::RemoveSuffix(const NString& suffix)
 
768
{
 
769
    if(IsSuffix(suffix))
 
770
    {
 
771
        t_size pos = FindLastOccurence(suffix);
 
772
        *this = NString(m_string.substr(0, pos));
 
773
    }
 
774
}
 
775
 
 
776
//! Return an NString striped out of the prefix contained in prefix
 
777
void NString::RemovePrefix(const TCHAR& prefix)
 
778
{
 
779
    if(IsPrefix(prefix))
 
780
    {
 
781
        *this = NString(m_string.substr(1));
 
782
    }
 
783
}
 
784
 
 
785
//! Return an NString striped out of the prefix contained in prefix
 
786
void NString::RemovePrefix(const TCHAR* prefix)
 
787
{
 
788
    if(IsPrefix(prefix))
 
789
    {
 
790
        t_size l = StringLength(prefix);
 
791
        *this = NString(m_string.substr(l));
 
792
    }
 
793
}
 
794
 
 
795
//! Return an NString striped out of the prefix contained in prefix
 
796
void NString::RemovePrefix(const tstring& prefix)
 
797
{
 
798
    if(IsPrefix(prefix))
 
799
    {
 
800
        t_size l = prefix.length();
 
801
        *this = NString(m_string.substr(l));
 
802
    }
 
803
}
 
804
 
 
805
//! Return an NString striped out of the prefix contained in prefix
 
806
void NString::RemovePrefix(const NString& prefix)
 
807
{
 
808
    if(IsPrefix(prefix))
 
809
    {
 
810
        t_size l = prefix.Length();
 
811
        *this = NString(m_string.substr(l));
 
812
    }
 
813
}
 
814
 
 
815
NString NString::GetSubString(t_size count) const
 
816
{
 
817
    nuxAssert(count >= 0);
 
818
    return NString(m_string.substr(0, count));
 
819
}
 
820
 
 
821
NString NString::GetSubString(t_size start, t_size count) const
 
822
{
 
823
    nuxAssert(start >= 0);
 
824
    nuxAssert(count >= 0);
 
825
    return NString(m_string.substr(start, count));
 
826
}
 
827
 
 
828
NString NString::Mid(t_size count) const
 
829
{
 
830
    return GetSubString(count);
 
831
}
 
832
 
 
833
NString NString::Mid(t_size start, t_size count) const
 
834
{
 
835
    return GetSubString(start, count);
 
836
}
 
837
 
 
838
NString NString::Left(t_size N) const
 
839
{
 
840
    if(N >= Length())
 
841
        return *this;
 
842
 
 
843
    return GetSubString(0, N);
 
844
}
 
845
 
 
846
NString NString::Right(t_size N) const
 
847
{
 
848
    if(N >= Length())
 
849
        return *this;
 
850
 
 
851
    return GetSubString(Length()-N, N);
 
852
}
 
853
 
 
854
NString NString::Trim() const
 
855
{
 
856
    return TrimLeft().TrimRight();
 
857
}
 
858
 
 
859
NString NString::TrimLeft() const
 
860
{
 
861
    t_size n = 0;
 
862
    t_size L = Length()-1;
 
863
    while(n <= L)
 
864
    {
 
865
        if(IsWhitespaceChar(m_string[n]))
 
866
        {
 
867
            n++;
 
868
        }
 
869
        else
 
870
        {
 
871
            break;
 
872
        }
 
873
    }
 
874
    return GetSubString(n, Length()-n);
 
875
}
 
876
 
 
877
NString NString::TrimRight() const
 
878
{
 
879
    t_size L = Length()-1;
 
880
    while(0 <= L)
 
881
    {
 
882
        if(IsWhitespaceChar(m_string[L]))
 
883
        {
 
884
            L--;
 
885
        }
 
886
        else
 
887
        {
 
888
            break;
 
889
        }
 
890
    }
 
891
    return GetSubString(0, Length()+1);
 
892
}
 
893
 
 
894
NString NString::TrimLeft(NString str) const
 
895
{
 
896
    t_size n = 0;
 
897
    t_size L = Length();
 
898
    if(L == 0)
 
899
        return *this;
 
900
 
 
901
    while(n < L)
 
902
    {
 
903
        bool trim = false;
 
904
        t_size sz = str.Length();
 
905
        for(t_size i = 0; i < sz; i++)
 
906
        {
 
907
            if(m_string[n] == str[i])
 
908
            {
 
909
                trim = true;
 
910
                break;
 
911
            }
 
912
        }
 
913
        if(trim)
 
914
        {
 
915
            n++;
 
916
        }
 
917
        else
 
918
        {
 
919
            break;
 
920
        }
 
921
    }
 
922
    return GetSubString(n, Length()-n);
 
923
}
 
924
 
 
925
NString NString::TrimRight(NString str) const
 
926
{
 
927
    t_size L = Length();
 
928
    if(L == 0)
 
929
        return *this;
 
930
 
 
931
    L = L - 1;
 
932
    while(0 <= L)
 
933
    {
 
934
        bool trim = false;
 
935
        t_size sz = str.Length();
 
936
        for(t_size i = 0; i < sz; i++)
 
937
        {
 
938
            if(m_string[L] == str[i])
 
939
            {
 
940
                trim = true;
 
941
                break;
 
942
            }
 
943
        }
 
944
        if(trim)
 
945
        {
 
946
            L--;
 
947
        }
 
948
        else
 
949
        {
 
950
            break;
 
951
        }
 
952
    }
 
953
    return GetSubString(0, L+1);
 
954
}
 
955
 
 
956
const TCHAR* NString::operator () () const
 
957
{
 
958
    if(Size() == 0)
 
959
        return 0;
 
960
    return GetTCharPtr();
 
961
}
 
962
 
 
963
const TCHAR* NString::operator * () const
 
964
{
 
965
    if(Size() == 0)
 
966
        return 0;
 
967
    return GetTCharPtr();
 
968
}
 
969
 
 
970
// NString::operator const TCHAR*() const
 
971
// {
 
972
//     return (const TCHAR*)GetTCharPtr();
 
973
// }
 
974
 
 
975
NString& NString::operator += (const TCHAR& sufix)
 
976
{
 
977
    m_string += sufix;
 
978
    return *this;
 
979
}
 
980
 
 
981
NString& NString::operator += (const TCHAR* sufix)
 
982
{
 
983
    m_string += sufix;
 
984
    return *this;
 
985
}
 
986
 
 
987
NString& NString::operator += (const tstring sufix)
 
988
{
 
989
    m_string += sufix;
 
990
    return *this;
 
991
}
 
992
 
 
993
NString& NString::operator += (const NString sufix)
 
994
{
 
995
    m_string += sufix.m_string;
 
996
    return *this;
 
997
}
 
998
 
 
999
void NString::SplitAtFirstOccurenceOf(const TCHAR* SplitString, NString& Left, NString& Right)
 
1000
{
 
1001
    t_size start = FindFirstOccurence(SplitString);
 
1002
    if(start!=-1)
 
1003
    {
 
1004
        t_size size = StringLength(SplitString);
 
1005
        Left = GetSubString(0, start);
 
1006
        Right = GetSubString(start+size, Length()-(start+size));
 
1007
    }
 
1008
    else
 
1009
    {
 
1010
        Left = *this;
 
1011
        Right = "";
 
1012
    }
 
1013
}
 
1014
 
 
1015
void NString::SplitAtFirstOccurenceOf(const TCHAR& SplitChar, NString& Left, NString& Right)
 
1016
{
 
1017
    SplitAtFirstOccurenceOf(NString(SplitChar), Left, Right);
 
1018
}
 
1019
 
 
1020
void NString::SplitAtFirstOccurenceOf(const NString& SplitString, NString& Left, NString& Right)
 
1021
{
 
1022
    SplitAtFirstOccurenceOf(SplitString.GetTCharPtr(), Left, Right);
 
1023
}
 
1024
 
 
1025
void NString::SplitAtLastOccurenceOf(const TCHAR* SplitString, NString& Left, NString& Right)
 
1026
{
 
1027
    t_size start = FindLastOccurence(SplitString);
 
1028
    if(start!=-1)
 
1029
    {
 
1030
        t_size size = StringLength(SplitString);
 
1031
        Left = GetSubString(0, start);
 
1032
        Right = GetSubString(start+size, Length()-(start+size));
 
1033
    }
 
1034
    else
 
1035
    {
 
1036
        Left = *this;
 
1037
        Right = "";
 
1038
    }
 
1039
}
 
1040
 
 
1041
void NString::SplitAtLastOccurenceOf(const TCHAR& SplitChar, NString& Left, NString& Right)
 
1042
{
 
1043
    SplitAtLastOccurenceOf(NString(SplitChar), Left, Right);
 
1044
}
 
1045
 
 
1046
void NString::SplitAtLastOccurenceOf(const NString& SplitString, NString& Left, NString& Right)
 
1047
{
 
1048
    SplitAtLastOccurenceOf(SplitString.GetTCharPtr(), Left, Right);
 
1049
}
 
1050
 
 
1051
void NString::ParseToArray(std::vector<NString>& StringArray, const NString& delimiter)
 
1052
{
 
1053
    NString Left;
 
1054
    NString Right;
 
1055
    NString Temp;
 
1056
    SplitAtFirstOccurenceOf(delimiter, Left, Temp);
 
1057
    if(Left.Size())
 
1058
    {
 
1059
        Left = Left.Trim();
 
1060
        StringArray.push_back(Left);
 
1061
    }
 
1062
    Right = Temp;
 
1063
    while(Right.Size())
 
1064
    {
 
1065
        Right.SplitAtFirstOccurenceOf(delimiter, Left, Temp);
 
1066
        if(Left.Size())
 
1067
        {
 
1068
            Left = Left.Trim();
 
1069
            StringArray.push_back(Left);
 
1070
        }
 
1071
        Right = Temp;
 
1072
    }
 
1073
}
 
1074
 
 
1075
TCHAR NString::GetFirstChar()
 
1076
{
 
1077
    if(IsEmpty())
 
1078
        return 0;
 
1079
    return m_string[0];
 
1080
}
 
1081
 
 
1082
TCHAR NString::GetLastChar()
 
1083
{
 
1084
    if(IsEmpty())
 
1085
        return 0;
 
1086
    return m_string[Size()-1];
 
1087
}
 
1088
 
 
1089
bool operator != (const NString& left, const NString& right)
 
1090
{
 
1091
    return left.m_string != right.m_string;
 
1092
}
 
1093
bool operator == (const NString& left, const NString& right)
 
1094
{
 
1095
    return left.m_string == right.m_string;
 
1096
}
 
1097
bool operator <  (const NString& left, const NString& right)
 
1098
{
 
1099
    return left.m_string < right.m_string;
 
1100
}
 
1101
bool operator <= (const NString& left, const NString& right)
 
1102
{
 
1103
    return left.m_string <= right.m_string;
 
1104
}
 
1105
bool operator >  (const NString& left, const NString& right)
 
1106
{
 
1107
    return left.m_string > right.m_string;
 
1108
}
 
1109
bool operator >= (const NString& left, const NString& right)
 
1110
{
 
1111
    return left.m_string >= right.m_string;
 
1112
}
 
1113
 
 
1114
NString operator+ (const NString& left, const NString& right)
 
1115
{
 
1116
    return NString(left.m_string + right.m_string);
 
1117
}
 
1118
 
 
1119
NString operator+ (const NString& left, const TCHAR* right)
 
1120
{
 
1121
    return NString(left.m_string + right);
 
1122
}
 
1123
 
 
1124
NString operator+ (const NString& left, const TCHAR right)
 
1125
{
 
1126
    return NString(left.m_string + right);
 
1127
}
 
1128
 
 
1129
NString operator+ (const TCHAR* left, const NString& right)
 
1130
{
 
1131
    return NString(left + right.m_string);
 
1132
}
 
1133
 
 
1134
NString operator+ (const TCHAR left, const NString& right)
 
1135
{
 
1136
    return NString(left + right.m_string);
 
1137
}
 
1138
 
 
1139
tostream& operator << (tostream& o, const NString& s)
 
1140
{
 
1141
    return o << s.m_string;
 
1142
}
 
1143
 
 
1144
VARARG_BODY(NString, NString::Printf, const TCHAR*, VARARG_NONE)
 
1145
{
 
1146
    t_u32  BufferSize  = 1024;
 
1147
    TCHAR* Buffer      = NULL;
 
1148
    t_u32  Result      = -1;
 
1149
 
 
1150
    while(Result == -1)
 
1151
    {
 
1152
        Buffer = (TCHAR*) Realloc(Buffer, BufferSize * sizeof(TCHAR));
 
1153
        GET_VARARGS_RESULT(Buffer, BufferSize, BufferSize-1, Fmt, Result);
 
1154
        BufferSize *= 2;
 
1155
    };
 
1156
    Buffer[Result] = 0;
 
1157
 
 
1158
    NString ResultString(Buffer);
 
1159
    free(Buffer);
 
1160
 
 
1161
    return ResultString;
 
1162
}
 
1163
 
 
1164
NAMESPACE_END