summaryrefslogtreecommitdiffhomepage
path: root/ovr_sdk_win_23.0.0/LibOVRKernel/Src/Kernel/OVR_String.h
blob: 228b020127fe3bf994b1cfb91f986d3eeeb69ad5 (plain)
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/************************************************************************************

Filename    :   OVR_String.h
Content     :   String UTF8 string implementation with copy-on-write semantics
                (thread-safe for assignment but not modification).
Created     :   September 19, 2012
Notes       :

Copyright   :   Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.

Licensed under the Oculus Master SDK License Version 1.0 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

https://developer.oculus.com/licenses/oculusmastersdk-1.0

Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#ifndef OVR_String_h
#define OVR_String_h

#include <stdarg.h>
#include <string>
#include "OVR_Alg.h"
#include "OVR_Allocator.h"
#include "OVR_Atomic.h"
#include "OVR_Std.h"
#include "OVR_Types.h"
#include "OVR_UTF8Util.h"

namespace OVR {

class String;
class StringBuffer;

// Special/default null-terminated length argument
const size_t StringIsNullTerminated = size_t(-1);

//-----------------------------------------------------------------------------------
// ***** String Class

// String is UTF8 based string class with copy-on-write implementation
// for assignment.

class String : public std::string {
 public:
  typedef std::string inherited;

  // Constructors / Destructors.
  String() {}

  String(const char* data) {
    if (data)
      inherited::assign(data);
  }

  String(const char* data1, const char* pdata2, const char* pdata3 = nullptr) {
    if (data1)
      inherited::append(data1);
    if (pdata2)
      inherited::append(pdata2);
    if (pdata3)
      inherited::append(pdata3);
  }

  String(const char* data, size_t buflen) {
    if (data)
      inherited::assign(data, buflen);
  }

  String(const String& src) {
    inherited::assign(src.data(), src.length());
  }

  String(const std::string& src) {
    inherited::assign(src.data(), src.length());
  }

  explicit String(const wchar_t* data) {
    if (data)
      String::operator=(data); // Need to do UCS2->UTF8 conversion
  }

  void Clear() {
    inherited::clear();
  }

  operator const char*() const {
    return inherited::c_str();
  }

  const char* ToCStr() const {
    return inherited::c_str();
  }

  // Byte length.
  size_t GetSize() const {
    return inherited::size();
  }

  bool IsEmpty() const {
    return inherited::empty();
  }

  // Returns number of Unicode codepoints (not byte length).
  size_t GetLength() const {
    return (size_t)UTF8Util::GetLength(inherited::data(), inherited::size());
  }

  // Return Unicode codepoint count.
  int GetLengthI() const {
    return (int)UTF8Util::GetLength(inherited::data(), inherited::size());
  }

  // Return Unicode codepoint at the specified codepoint index.
  uint32_t GetCharAt(size_t index) const {
    return UTF8Util::GetCharAt(index, inherited::data(), inherited::size());
  }

  // Get first Unicode codepoint.
  uint32_t Front() const {
    return UTF8Util::GetCharAt(0, inherited::data(), inherited::size());
  }

  // Get last Unicode codepoint.
  uint32_t Back() const {
    OVR_ASSERT(!inherited::empty());
    return GetCharAt(GetSize() - 1);
  }

  // Append a Unicode codepoint.
  void AppendChar(uint32_t ch) {
    char buff[8]; // Max possible UTF8 sequence is 6 bytes.
    intptr_t encodeSize = 0;
    UTF8Util::EncodeChar(buff, &encodeSize, ch);
    inherited::append(buff, encodeSize);
  }

  // Append Unicode codepoints
  void AppendString(const wchar_t* pstr, size_t len = StringIsNullTerminated) {
    if (pstr) {
      if (len == StringIsNullTerminated)
        len = wcslen(pstr);

      while (len-- > 0)
        AppendChar(*pstr++);
    }
  }

  // Append UTF8 stirng of a given byte size.
  void AppendString(const char* putf8str, size_t utf8StrSz = StringIsNullTerminated) {
    if (putf8str) {
      if (utf8StrSz == StringIsNullTerminated)
        inherited::append(putf8str);
      else
        inherited::append(putf8str, utf8StrSz);
    }
  }

  // Assigns string with known byte size.
  void AssignString(const char* putf8str, size_t size) {
    if (putf8str || (size == 0))
      inherited::assign(putf8str, size);
  }

  // Remove 'removeLength' Unicode codepoints starting at the 'posAt' Unicode codepoint.
  void Remove(size_t posAt, size_t removeLength = 1) {
    size_t length = GetLength(); // Unicode size.

    if (posAt < length) {
      size_t oldSize = inherited::size(); // Byte size.

      if ((posAt + removeLength) > length)
        removeLength = (length - posAt);

      intptr_t bytePos = UTF8Util::GetByteIndex(posAt, inherited::data(), oldSize);

      intptr_t removeSize =
          UTF8Util::GetByteIndex(removeLength, inherited::data() + bytePos, oldSize - bytePos);

      inherited::erase(bytePos, removeSize);
    }
  }

  // Removes the last Unicode codepoint.
  void PopBack() {
    if (!inherited::empty())
      Remove(GetLength() - 1, 1);
  }

  // Returns a String that's a substring of this.
  //  start is the index of the first Unicode codepoint you want to include.
  //  end is the index one past the last Unicode codepoint you want to include.
  String Substring(size_t start, size_t end) const {
    size_t length = GetLength();

    if ((start >= length) || (start >= end))
      return String();

    if (end > length)
      end = length;

    // Get position of starting character and size
    intptr_t byteStart = UTF8Util::GetByteIndex(start, inherited::data(), inherited::size());
    intptr_t byteSize = UTF8Util::GetByteIndex(
        end - start, inherited::data() + byteStart, inherited::size() - byteStart);

    return String(inherited::data() + byteStart, (size_t)byteSize);
  }

  // Insert a UTF8 string at the Unicode codepoint posAt.
  String& Insert(const char* substr, size_t posAt, size_t strSize = StringIsNullTerminated) {
    if (substr) {
      if (strSize == StringIsNullTerminated)
        strSize = strlen(substr);

      size_t byteIndex = UTF8Util::GetByteIndex(posAt, inherited::c_str(), inherited::size());

      if (byteIndex > inherited::size())
        byteIndex = inherited::size();

      inherited::insert(byteIndex, substr, strSize);
    }

    return *this;
  }

  // UTF8 compare
  static int CompareNoCase(const char* a, const char* b) {
    return OVR_stricmp(a, b);
  }

  // UTF8 compare
  static int CompareNoCase(const char* a, const char* b, size_t byteSize) {
    return OVR_strnicmp(a, b, byteSize);
  }

  // Hash function, case-insensitive
  static size_t BernsteinHashFunctionCIS(const void* pdataIn, size_t size, size_t seed = 5381) {
    const uint8_t* pdata = (const uint8_t*)pdataIn;
    size_t h = seed;
    while (size > 0) {
      size--;
      h = ((h << 5) + h) ^ OVR_tolower(pdata[size]);
    }
    return h;
  }

  // Hash function, case-sensitive
  static size_t BernsteinHashFunction(const void* pdataIn, size_t size, size_t seed = 5381) {
    const uint8_t* pdata = (const uint8_t*)pdataIn;
    size_t h = seed;
    while (size > 0) {
      size--;
      h = ((h << 5) + h) ^ (unsigned)pdata[size];
    }
    return h;
  }

  // Absolute paths can star with:
  //  - protocols:        'file://', 'http://'
  //  - windows drive:    'c:\'
  //  - UNC share name:   '\\share'
  //  - unix root         '/'
  static bool HasAbsolutePath(const char* path);

  static bool HasExtension(const char* path);

  static bool HasProtocol(const char* path);

  bool HasAbsolutePath() const {
    return HasAbsolutePath(inherited::c_str());
  }

  bool HasExtension() const {
    return HasExtension(inherited::c_str());
  }

  bool HasProtocol() const {
    return HasProtocol(inherited::c_str());
  }

  String GetProtocol() const; // Returns protocol, if any, with trailing '://'.

  String GetPath() const; // Returns path with trailing '/'.

  String GetFilename() const; // Returns filename, including extension.

  String GetExtension() const; // Returns extension with a dot.

  void StripProtocol(); // Strips front protocol, if any, from the string.

  void StripExtension(); // Strips off trailing extension.

  void operator=(const char* str) {
    if (str)
      inherited::assign(str);
    else
      inherited::clear();
  }

  void operator=(const wchar_t* str) {
    inherited::clear();

    while (str && *str)
      AppendChar(*str++);
  }

  void operator=(const String& src) {
    inherited::assign(src.data(), src.size());
  }

  void operator+=(const String& src) {
    inherited::append(src.data(), src.size());
  }

  void operator+=(const char* psrc) {
    if (psrc)
      inherited::append(psrc);
  }

  void operator+=(const wchar_t* psrc) {
    if (psrc)
      AppendString(psrc);
  }

  // Append a Unicode codepoint.
  // Note that this function seems amiss by taking char as an argument instead of uint32_t or
  // wchar_t.
  void operator+=(char ch) {
    AppendChar(ch);
  }

  String operator+(const char* str) const {
    String temp(*this);
    if (str)
      temp += str;
    return temp;
  }

  String operator+(const String& src) const {
    String temp(*this);
    temp += src;
    return temp;
  }

  bool operator==(const String& str) const {
    return (OVR_strcmp(inherited::c_str(), str.c_str()) == 0);
  }

  bool operator!=(const String& str) const {
    return !operator==(str);
  }

  bool operator==(const char* str) const {
    return OVR_strcmp(inherited::c_str(), (str ? str : "")) == 0;
  }

  bool operator!=(const char* str) const {
    return !operator==(str);
  }

  bool operator<(const char* pstr) const {
    return OVR_strcmp(inherited::c_str(), (pstr ? pstr : "")) < 0;
  }

  bool operator<(const String& str) const {
    return *this < str.c_str();
  }

  bool operator>(const char* pstr) const {
    return OVR_strcmp(inherited::c_str(), (pstr ? pstr : "")) > 0;
  }

  bool operator>(const String& str) const {
    return *this > str.c_str();
  }

  int CompareNoCase(const char* pstr) const {
    return CompareNoCase(inherited::c_str(), (pstr ? pstr : ""));
  }

  int CompareNoCase(const String& str) const {
    return CompareNoCase(inherited::c_str(), str.c_str());
  }

  int CompareNoCaseStartsWith(const String& str) const {
    // Problem: the original version of this used GetLength, which seems like a bug because
    // CompareNoCase takes a byte length. Need to look back in the OVR_String.h history to see if
    // the bug has always been there.
    return CompareNoCase(inherited::c_str(), str.c_str(), str.size());
  }

  // Accesses raw bytes
  const char& operator[](int index) const {
    OVR_ASSERT(index >= 0 && (size_t)index < inherited::size());
    return inherited::operator[](index);
  }

  const char& operator[](size_t index) const {
    OVR_ASSERT(index < inherited::size());
    return inherited::operator[](index);
  }

  struct NoCaseKey {
    const String* pStr;
    NoCaseKey(const String& str) : pStr(&str){};
  };

  bool operator==(const NoCaseKey& strKey) const {
    return (CompareNoCase(ToCStr(), strKey.pStr->ToCStr()) == 0);
  }

  bool operator!=(const NoCaseKey& strKey) const {
    return !(CompareNoCase(ToCStr(), strKey.pStr->ToCStr()) == 0);
  }

  // Hash functor used for strings.
  struct HashFunctor {
    size_t operator()(const String& str) const {
      return String::BernsteinHashFunction(str.data(), str.size());
    }
  };

  // Case-insensitive hash functor used for strings. Supports additional
  // lookup based on NoCaseKey.
  struct NoCaseHashFunctor {
    size_t operator()(const String& str) const {
      return String::BernsteinHashFunctionCIS(str.data(), str.size());
    }
    size_t operator()(const NoCaseKey& key) const {
      return String::BernsteinHashFunctionCIS(key.pStr->c_str(), key.pStr->size());
    }
  };
};

//-----------------------------------------------------------------------------------
// ***** String Buffer used for Building Strings

class StringBuffer {
  char* pData;
  size_t Size;
  size_t BufferSize;
  size_t GrowSize;
  mutable bool LengthIsSize;

 public:
  // Constructors / Destructor.
  StringBuffer();
  explicit StringBuffer(size_t growSize);
  StringBuffer(const char* data);
  StringBuffer(const char* data, size_t buflen);
  StringBuffer(const String& src);
  StringBuffer(const StringBuffer& src);
  explicit StringBuffer(const wchar_t* data);
  ~StringBuffer();

  // Modify grow size used for growing/shrinking the buffer.
  size_t GetGrowSize() const {
    return GrowSize;
  }
  void SetGrowSize(size_t growSize);

  // *** General Functions
  // Does not release memory, just sets Size to 0
  void Clear();

  // For casting to a pointer to char.
  operator const char*() const {
    return (pData) ? pData : "";
  }
  // Pointer to raw buffer.
  const char* ToCStr() const {
    return (pData) ? pData : "";
  }

  // Returns number of bytes.
  size_t GetSize() const {
    return Size;
  }
  // Tells whether or not the string is empty.
  bool IsEmpty() const {
    return GetSize() == 0;
  }

  // Returns  number of characters
  size_t GetLength() const;

  // Returns  character at the specified index
  uint32_t GetCharAt(size_t index) const;
  uint32_t GetFirstCharAt(size_t index, const char** offset) const;
  uint32_t GetNextChar(const char** offset) const;
  uint32_t Front() const {
    return GetCharAt(0);
  }
  uint32_t Back() const {
    return GetCharAt(GetSize() - 1);
  }

  //  Resize the string to the new size
  void Resize(size_t _size);
  void Reserve(size_t _size);

  // Appends a character
  void AppendChar(uint32_t ch);

  // Append a string
  void AppendString(const wchar_t* pstr, size_t len = StringIsNullTerminated);
  void AppendString(const char* putf8str, size_t utf8StrSz = StringIsNullTerminated);
  void AppendFormatV(const char* format, va_list argList);
  void AppendFormat(const char* format, ...);

  // Assigned a string with dynamic data (copied through initializer).
  // void        AssignString(const InitStruct& src, size_t size);

  // Inserts substr at posAt
  void Insert(const char* substr, size_t posAt, size_t len = StringIsNullTerminated);
  // Inserts character at posAt
  size_t InsertCharAt(uint32_t c, size_t posAt);

  // Assignment
  void operator=(const char* str);
  void operator=(const wchar_t* str);
  void operator=(const String& src);
  void operator=(const StringBuffer& src);

  // Addition
  void operator+=(const String& src) {
    AppendString(src.ToCStr(), src.GetSize());
  }
  void operator+=(const char* psrc) {
    AppendString(psrc);
  }
  void operator+=(const wchar_t* psrc) {
    AppendString(psrc);
  }
  void operator+=(char ch) {
    AppendChar(ch);
  }
  // String   operator +  (const char* str) const ;
  // String   operator +  (const String& src)  const ;

  // Accesses raw bytes
  char& operator[](size_t index) {
    OVR_ASSERT(index < GetSize());
    return pData[index];
  }

  const char& operator[](size_t index) const {
    OVR_ASSERT(index < GetSize());
    return pData[index];
  }
};

// Returns a std::string that was initialized via printf-style formatting.
// The behavior is undefined if the specified format or arguments are invalid.
// Example usage:
//     std::string s = StringVsprintf("Hello %s", "world");
std::string StringVsprintf(const char* format, va_list args);

// Returns a std::string that was appended to via printf-style formatting.
// The behavior is undefined if the specified format or arguments are invalid.
// Example usage:
//     AppendSprintf(s, "appended %s", "hello world");
std::string& AppendSprintf(std::string& s, const char* format, ...);

// Convert a UTF8 String object to a wchar_t UCS (Unicode) std::basic_string object.
// The C++11 Standard Library has similar functionality, but it's not supported by earlier
// versions of Visual Studio. To consider: Add support for this when available.
// length is the strlen of pUTF8. If not specified then it is calculated automatically.
// Returns an empty string in the case that the UTF8 is malformed.
std::wstring UTF8StringToUCSString(const char* pUTF8, size_t length = StringIsNullTerminated);
std::wstring UTF8StringToUCSString(const std::string& sUTF8);
std::wstring OVRStringToUCSString(const String& sOVRUTF8);

// Convert a wchar_t UCS (Unicode) std::basic_string object to a UTF8 std::basic_string object.
// The C++11 Standard Library has similar functionality, but it's not supported by earlier
// versions of Visual Studio. To consider: Add support for this when available.
// length is the strlen of pUCS. If not specified then it is calculated automatically.
// Returns an empty string in the case that the UTF8 is malformed.
std::string UCSStringToUTF8String(const wchar_t* pUCS, size_t length = StringIsNullTerminated);
std::string UCSStringToUTF8String(const std::wstring& sUCS);
String UCSStringToOVRString(const wchar_t* pUCS, size_t length = StringIsNullTerminated);
String UCSStringToOVRString(const std::wstring& sUCS);

} // namespace OVR

#endif