Loading...
Searching...
No Matches
String16.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2024 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/// A null-terminated UTF-16 string container.
34///
36public:
37 // Native character type
39
40 // Make an empty String16
42
43 // Make a String16 from raw UTF-16 buffer with certain length
44 String16(const Char16* str, size_t len);
45
46 // Make a String16 from raw unsigned short UTF-16 buffer with certain length. Useful on Windows
47 // when native support for wchar_t is disabled (eg, /Zc:wchar_t-).
48 String16(const unsigned short* str, size_t len);
49
50 // Make a deep copy of String16
51 String16(const String16& other);
52
53 // Move constructor
55
56 // Destructor
58
59 // Assign a String16 to this one, deep copy is made
60 String16& operator=(const String16& other);
61
62 // Move assignment operator
64
65 // Append a String16 to this one.
67
68 // Concatenation operator
69 inline friend String16 operator+(String16 lhs, const String16& rhs) { lhs += rhs; return lhs; }
70
71 // Get raw UTF-16 data
72 Char16* data() { return data_; }
73
74 // Get raw UTF-16 data (const)
75 const Char16* data() const { return data_; }
76
77 // Get raw UTF-16 data as unsigned short. This is useful on Windows if you compile without native
78 // support for wchar_t (eg, /Zc:wchar_t-)
79 unsigned short* udata() { return reinterpret_cast<unsigned short*>(data_); }
80
81 // Get raw UTF-16 data as unsigned short (const).
82 const unsigned short* udata() const { return reinterpret_cast<const unsigned short*>(data_); }
83
84 // Get length in characters.
85 size_t length() const { return length_; }
86
87 // Get size in characters (synonym for length)
88 size_t size() const { return length_; }
89
90 // Get size in bytes
91 size_t sizeBytes() const { return length_ * sizeof(Char16); }
92
93 // Check if string is empty.
94 bool empty() const { return !data_ || length_ == 0; }
95
96 // Get character at specific position
97 Char16& operator[](size_t pos) { return data_[pos]; }
98
99 // Get character at specific position (const)
100 const Char16& operator[](size_t pos) const { return data_[pos]; }
101
102 // Get a UTF-8 copy of this string
103 String8 utf8() const;
104
105 // Get a UTF-32 copy of this string
107
108 // Hash function
109 size_t Hash() const;
110
111 // Comparison operator
112 bool operator<(const String16& other) const;
113
114 // Equality operator
115 bool operator==(const String16& other) const;
116
117 // Inequality operator
118 bool operator!=(const String16& other) const;
119
120private:
121 Char16* data_;
122 size_t length_;
123};
124
125///
126/// @brief A UTF-16 string vector.
127///
129public:
130 // Create an empty string vector
132
133 // Create a string vector from an existing array (a deep copy is made)
134 static RefPtr<String16Vector> Create(const String16* stringArray, size_t len);
135
136 // Add an element to the back of the string vector
137 virtual void push_back(const String16& val) = 0;
138
139 // Get raw String16 vector array
140 virtual String16* data() = 0;
141
142 // Get the number of elements in vector
143 virtual size_t size() const = 0;
144
145protected:
150};
151
152} // namespace ultralight
#define UExport
Definition Exports.h:25
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 null-terminated UTF-16 string container.
Definition String16.h:35
friend String16 operator+(String16 lhs, const String16 &rhs)
Definition String16.h:69
String16(const unsigned short *str, size_t len)
const Char16 & operator[](size_t pos) const
Definition String16.h:100
size_t size() const
Definition String16.h:88
String16 & operator=(String16 &&other)
const unsigned short * udata() const
Definition String16.h:82
String8 utf8() const
bool empty() const
Definition String16.h:94
size_t length() const
Definition String16.h:85
Char16 & operator[](size_t pos)
Definition String16.h:97
String32 utf32() const
String16(const String16 &other)
size_t sizeBytes() const
Definition String16.h:91
bool operator<(const String16 &other) const
bool operator==(const String16 &other) const
String16 & operator=(const String16 &other)
unsigned short * udata()
Definition String16.h:79
String16 & operator+=(const String16 &other)
size_t Hash() const
const Char16 * data() const
Definition String16.h:75
Char16 * data()
Definition String16.h:72
String16(String16 &&other)
bool operator!=(const String16 &other) const
Char16 CharType
Definition String16.h:38
String16(const Char16 *str, size_t len)
A UTF-16 string vector.
Definition String16.h:128
String16Vector(const String16Vector &)
static RefPtr< String16Vector > Create(const String16 *stringArray, size_t len)
virtual void push_back(const String16 &val)=0
virtual String16 * data()=0
void operator=(const String16Vector &)
virtual size_t size() const =0
static RefPtr< String16Vector > Create()
A null-terminated UTF-32 string container.
Definition String32.h:20
A null-terminated UTF-8 string container.
Definition String8.h:20
Definition App.h:14
detail::selector< sizeof(wchar_t)>::Char16 Char16
Definition String16.h:29