docs
Loading...
Searching...
No Matches
String.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
10
11namespace ultralight {
12
13///
14/// Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
15///
16/// This class is used to represent strings in Ultralight. It can be created from a variety of
17/// string types and converted to a number of unicode string types.
18///
19/// ## Accessing string data
20///
21/// Strings are natively stored in a null-terminated UTF-8 format. You can access the UTF-8 bytes
22/// using the `utf8()` method:
23///
24/// ```
25/// String str("Hello, world!");
26///
27/// // Print the UTF-8 data (guaranteed to be null-terminated)
28/// printf("%s\n", str.utf8().data());
29/// ```
30///
32 public:
33 ///
34 /// Create empty string
35 ///
37
38 ///
39 /// Create from null-terminated UTF-8 C-string
40 ///
41 String(const char* str);
42
43 ///
44 /// Create from raw, UTF-8 string with certain length
45 ///
46 String(const char* str, size_t len);
47
48 ///
49 /// Create from existing String8 (UTF-8).
50 ///
51 String(const String8& str);
52
53 ///
54 /// Create from raw UTF-16 string with certain length
55 ///
56 String(const Char16* str, size_t len);
57
58 ///
59 /// Create from existing String16 (UTF-16)
60 ///
61 String(const String16& str);
62
63 ///
64 /// Create from existing String32 (UTF-32)
65 ///
66 String(const String32& str);
67
68 ///
69 /// Copy constructor
70 ///
71 String(const String& other);
72
73 ///
74 /// Move constructor
75 ///
76 String(String&& other);
77
78 ///
79 /// Destructor
80 ///
82
83 ///
84 /// Assign string from another, copy is made
85 ///
86 String& operator=(const String& other);
87
88 ///
89 /// Move assignment operator
90 ///
92
93 ///
94 /// Append string with another
95 ///
96 String& operator+=(const String& other);
97
98 ///
99 /// Concatenation operator
100 ///
101 inline friend String operator+(String lhs, const String& rhs) {
102 lhs += rhs;
103 return lhs;
104 }
105
106 ///
107 /// Get native UTF-8 string
108 ///
109 String8& utf8() { return str_; }
110
111 ///
112 /// Get native UTF-8 string
113 ///
114 const String8& utf8() const { return str_; }
115
116 ///
117 /// Convert to UTF-16 string
118 ///
120
121 ///
122 /// Convert to UTF-32 string
123 ///
125
126 ///
127 /// Check if string is empty or not
128 ///
129 bool empty() const { return str_.empty(); }
130
131 ///
132 /// Hash function
133 ///
134 size_t Hash() const;
135
136 ///
137 /// Comparison operator
138 ///
139 bool operator<(const String& other) const;
140
141 ///
142 /// Equality operator
143 ///
144 bool operator==(const String& other) const;
145
146 ///
147 /// Inequality operator
148 ///
149 bool operator!=(const String& other) const;
150
151 private:
152 String8 str_;
153};
154
155} // namespace ultralight
#define UExport
Definition Exports.h:22
A null-terminated UTF-16 string container.
Definition String16.h:32
A null-terminated UTF-32 string container.
Definition String32.h:17
A null-terminated UTF-8 string container.
Definition String8.h:17
String & operator=(const String &other)
Assign string from another, copy is made.
friend String operator+(String lhs, const String &rhs)
Concatenation operator.
Definition String.h:101
String & operator+=(const String &other)
Append string with another.
String(const String32 &str)
Create from existing String32 (UTF-32).
String(String &&other)
Move constructor.
bool operator==(const String &other) const
Equality operator.
String(const String &other)
Copy constructor.
bool operator!=(const String &other) const
Inequality operator.
String16 utf16() const
Convert to UTF-16 string.
String8 & utf8()
Get native UTF-8 string.
Definition String.h:109
bool empty() const
Check if string is empty or not.
Definition String.h:129
bool operator<(const String &other) const
Comparison operator.
const String8 & utf8() const
Get native UTF-8 string.
Definition String.h:114
String & operator=(String &&other)
Move assignment operator.
String32 utf32() const
Convert to UTF-32 string.
String(const char *str, size_t len)
Create from raw, UTF-8 string with certain length.
String(const String16 &str)
Create from existing String16 (UTF-16).
String()
Create empty string.
~String()
Destructor.
size_t Hash() const
Hash function.
String(const String8 &str)
Create from existing String8 (UTF-8).
String(const Char16 *str, size_t len)
Create from raw UTF-16 string with certain length.
String(const char *str)
Create from null-terminated UTF-8 C-string.
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