docs
Loading...
Searching...
No Matches
String16.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
7#include <Ultralight/RefPtr.h>
8#include <stddef.h>
9
10namespace ultralight {
11
12class String8;
13class String32;
14
15namespace detail {
16 template<int> struct selector;
17 template<> struct selector<4> { typedef char16_t Char16; };
18 template<> struct selector<2> { typedef wchar_t Char16; };
19}
20
21#ifdef DISABLE_NATIVE_WCHAR_T
22/// Force Char16 type to use char16_t, used on Windows when native wchar_t support is disabled.
23typedef char16_t Char16;
24#else
25/// We use wchar_t if size == 2, otherwise use char16_t
26typedef detail::selector<sizeof(wchar_t)>::Char16 Char16;
27#endif
28
29///
30/// A null-terminated UTF-16 string container.
31///
33public:
34 /// Native character type
36
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
50 /// Move constructor
52
53 /// Destructor
55
56 /// Assign a String16 to this one, deep copy is made
57 String16& operator=(const String16& other);
58
59 /// Move assignment operator
61
62 /// Append a String16 to this one.
64
65 /// Concatenation operator
66 inline friend String16 operator+(String16 lhs, const String16& rhs) { lhs += rhs; return lhs; }
67
68 /// Get raw UTF-16 data
69 Char16* data() { return data_; }
70
71 /// Get raw UTF-16 data (const)
72 const Char16* data() const { return data_; }
73
74 /// Get raw UTF-16 data as unsigned short. This is useful on Windows if you compile without native
75 /// support for wchar_t (eg, /Zc:wchar_t-)
76 unsigned short* udata() { return reinterpret_cast<unsigned short*>(data_); }
77
78 /// Get raw UTF-16 data as unsigned short (const).
79 const unsigned short* udata() const { return reinterpret_cast<const unsigned short*>(data_); }
80
81 /// Get length in UTF-16 code units (not including null terminator).
82 size_t length() const { return length_; }
83
84 /// Get size in UTF-16 code units (synonym for length())
85 size_t size() const { return length_; }
86
87 /// Get size in bytes
88 size_t sizeBytes() const { return length_ * sizeof(Char16); }
89
90 /// Check if string is empty.
91 bool empty() const { return !data_ || length_ == 0; }
92
93 /// Get character at specific position
94 Char16& operator[](size_t pos) { return data_[pos]; }
95
96 /// Get character at specific position (const)
97 const Char16& operator[](size_t pos) const { return data_[pos]; }
98
99 /// Get a UTF-8 copy of this string
100 String8 utf8() const;
101
102 /// Get a UTF-32 copy of this string
104
105 /// Hash function
106 size_t Hash() const;
107
108 /// Comparison operator
109 bool operator<(const String16& other) const;
110
111 /// Equality operator
112 bool operator==(const String16& other) const;
113
114 /// Inequality operator
115 bool operator!=(const String16& other) const;
116
117private:
118 Char16* data_;
119 size_t length_;
120};
121
122///
123/// @brief A UTF-16 string vector.
124///
126public:
127 /// Create an empty string vector
129
130 /// Create a string vector from an existing array (a deep copy is made)
131 static RefPtr<String16Vector> Create(const String16* stringArray, size_t len);
132
133 /// Add an element to the back of the string vector
134 virtual void push_back(const String16& val) = 0;
135
136 /// Get raw String16 vector array
137 virtual String16* data() = 0;
138
139 /// Get the number of elements in vector
140 virtual size_t size() const = 0;
141
142protected:
147};
148
149} // namespace ultralight
#define UExport
Definition Exports.h:22
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:49
A nullable smart pointer.
Definition RefPtr.h:126
A null-terminated UTF-16 string container.
Definition String16.h:32
String16()
Make an empty String16.
friend String16 operator+(String16 lhs, const String16 &rhs)
Concatenation operator.
Definition String16.h:66
String16(const unsigned short *str, size_t len)
Make a String16 from raw unsigned short UTF-16 buffer with certain length.
const Char16 & operator[](size_t pos) const
Get character at specific position (const).
Definition String16.h:97
size_t size() const
Get size in UTF-16 code units (synonym for length()).
Definition String16.h:85
String16 & operator=(String16 &&other)
Move assignment operator.
const unsigned short * udata() const
Get raw UTF-16 data as unsigned short (const).
Definition String16.h:79
String8 utf8() const
Get a UTF-8 copy of this string.
bool empty() const
Check if string is empty.
Definition String16.h:91
size_t length() const
Get length in UTF-16 code units (not including null terminator).
Definition String16.h:82
Char16 & operator[](size_t pos)
Get character at specific position.
Definition String16.h:94
String32 utf32() const
Get a UTF-32 copy of this string.
String16(const String16 &other)
Make a deep copy of String16.
~String16()
Destructor.
size_t sizeBytes() const
Get size in bytes.
Definition String16.h:88
bool operator<(const String16 &other) const
Comparison operator.
bool operator==(const String16 &other) const
Equality operator.
String16 & operator=(const String16 &other)
Assign a String16 to this one, deep copy is made.
unsigned short * udata()
Get raw UTF-16 data as unsigned short.
Definition String16.h:76
String16 & operator+=(const String16 &other)
Append a String16 to this one.
size_t Hash() const
Hash function.
const Char16 * data() const
Get raw UTF-16 data (const).
Definition String16.h:72
Char16 * data()
Get raw UTF-16 data.
Definition String16.h:69
String16(String16 &&other)
Move constructor.
bool operator!=(const String16 &other) const
Inequality operator.
Char16 CharType
Native character type.
Definition String16.h:35
String16(const Char16 *str, size_t len)
Make a String16 from raw UTF-16 buffer with certain length.
String16Vector(const String16Vector &)
static RefPtr< String16Vector > Create(const String16 *stringArray, size_t len)
Create a string vector from an existing array (a deep copy is made).
virtual void push_back(const String16 &val)=0
Add an element to the back of the string vector.
virtual String16 * data()=0
Get raw String16 vector array.
void operator=(const String16Vector &)
virtual size_t size() const =0
Get the number of elements in vector.
static RefPtr< String16Vector > Create()
Create an empty string vector.
A null-terminated UTF-32 string container.
Definition String32.h:17
A null-terminated UTF-8 string container.
Definition String8.h:17
Root namespace for every public Ultralight type, function, and enumeration.
detail::selector< sizeof(wchar_t)>::Char16 Char16
We use wchar_t if size == 2, otherwise use char16_t.
Definition String16.h:26