Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
String16.h
Go to the documentation of this file.
1/******************************************************************************
2 * This file is a part of Ultralight, an ultra-portable web-browser engine. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2023 Ultralight, Inc. *
7 *****************************************************************************/
8#pragma once
10#include <Ultralight/RefPtr.h>
11#include <stddef.h>
12
13namespace ultralight {
14
15class String8;
16class String32;
17
18namespace detail {
19 template<int> struct selector;
20 template<> struct selector<4> { typedef char16_t Char16; };
21 template<> struct selector<2> { typedef wchar_t Char16; };
22}
23
24#ifdef DISABLE_NATIVE_WCHAR_T
25// Force Char16 type to use char16_t, used on Windows when native wchar_t support is disabled.
26typedef char16_t Char16;
27#else
28// We use wchar_t if size == 2, otherwise use char16_t
29typedef detail::selector<sizeof(wchar_t)>::Char16 Char16;
30#endif
31
32///
33/// @brief A UTF-16 string container.
34///
36public:
37 // Make an empty String16
39
40 // Make a String16 from raw UTF-16 buffer with certain length
41 String16(const Char16* str, size_t len);
42
43 // Make a String16 from raw unsigned short UTF-16 buffer with certain length. Useful on Windows
44 // when native support for wchar_t is disabled (eg, /Zc:wchar_t-).
45 String16(const unsigned short* str, size_t len);
46
47 // Make a deep copy of String16
48 String16(const String16& other);
49
51
52 // Assign a String16 to this one, deep copy is made
53 String16& operator=(const String16& other);
54
55 // Append a String16 to this one.
57
58 // Concatenation operator
59 inline friend String16 operator+(String16 lhs, const String16& rhs) { lhs += rhs; return lhs; }
60
61 // Get raw UTF-16 data
62 Char16* data() { return data_; }
63
64 // Get raw UTF-16 data (const)
65 const Char16* data() const { return data_; }
66
67 // Get raw UTF-16 data as unsigned short. This is useful on Windows if you compile without native
68 // support for wchar_t (eg, /Zc:wchar_t-)
69 unsigned short* udata() { return reinterpret_cast<unsigned short*>(data_); }
70
71 // Get raw UTF-16 data as unsigned short (const).
72 const unsigned short* udata() const { return reinterpret_cast<const unsigned short*>(data_); }
73
74 // Get length in characters.
75 size_t length() const { return length_; }
76
77 // Get size in characters (synonym for length)
78 size_t size() const { return length_; }
79
80 // Get size in bytes
81 size_t sizeBytes() const { return length_ * sizeof(Char16); }
82
83 // Check if string is empty.
84 bool empty() const { return !data_ || length_ == 0; }
85
86 // Get character at specific position
87 Char16& operator[](size_t pos) { return data_[pos]; }
88
89 // Get character at specific position (const)
90 const Char16& operator[](size_t pos) const { return data_[pos]; }
91
92 // Get a UTF-8 copy of this string
93 String8 utf8() const;
94
95 // Get a UTF-32 copy of this string
96 String32 utf32() const;
97
98private:
99 Char16* data_;
100 size_t length_;
101};
102
103///
104/// @brief A UTF-16 string vector.
105///
107public:
108 // Create an empty string vector
110
111 // Create a string vector from an existing array (a deep copy is made)
112 static RefPtr<String16Vector> Create(const String16* stringArray, size_t len);
113
114 // Add an element to the back of the string vector
115 virtual void push_back(const String16& val) = 0;
116
117 // Get raw String16 vector array
118 virtual String16* data() = 0;
119
120 // Get the number of elements in vector
121 virtual size_t size() const = 0;
122
123protected:
128};
129
130} // namespace ultralight
#define UExport
Definition Defines.h:65
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:47
A nullable smart pointer.
Definition RefPtr.h:79
A UTF-16 string container.
Definition String16.h:35
friend String16 operator+(String16 lhs, const String16 &rhs)
Definition String16.h:59
Char16 & operator[](size_t pos)
Definition String16.h:87
String16(const unsigned short *str, size_t len)
const Char16 * data() const
Definition String16.h:65
const Char16 & operator[](size_t pos) const
Definition String16.h:90
String16(const String16 &other)
unsigned short * udata()
Definition String16.h:69
String32 utf32() const
String16 & operator+=(const String16 &other)
String16 & operator=(const String16 &other)
size_t size() const
Definition String16.h:78
String8 utf8() const
size_t length() const
Definition String16.h:75
String16(const Char16 *str, size_t len)
const unsigned short * udata() const
Definition String16.h:72
Char16 * data()
Definition String16.h:62
size_t sizeBytes() const
Definition String16.h:81
bool empty() const
Definition String16.h:84
A UTF-16 string vector.
Definition String16.h:106
virtual size_t size() const =0
virtual void push_back(const String16 &val)=0
virtual String16 * data()=0
static RefPtr< String16Vector > Create()
void operator=(const String16Vector &)
static RefPtr< String16Vector > Create(const String16 *stringArray, size_t len)
String16Vector(const String16Vector &)
A UTF-32 string container.
Definition String32.h:20
A UTF-8 string container.
Definition String8.h:20
Definition App.h:14
detail::selector< sizeof(wchar_t)>::Char16 Char16
Definition String16.h:29