Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/string/ustring.h
9902 views
1
/**************************************************************************/
2
/* ustring.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
// Note: _GODOT suffix added to header guard to avoid conflict with ICU header.
34
35
#include "core/string/char_utils.h" // IWYU pragma: export
36
#include "core/templates/cowdata.h"
37
#include "core/templates/vector.h"
38
#include "core/typedefs.h"
39
#include "core/variant/array.h"
40
41
class String;
42
template <typename T>
43
class CharStringT;
44
45
/*************************************************************************/
46
/* Utility Functions */
47
/*************************************************************************/
48
49
// Not defined by std.
50
// strlen equivalent function for char16_t * arguments.
51
constexpr size_t strlen(const char16_t *p_str) {
52
const char16_t *ptr = p_str;
53
while (*ptr != 0) {
54
++ptr;
55
}
56
return ptr - p_str;
57
}
58
59
// strlen equivalent function for char32_t * arguments.
60
constexpr size_t strlen(const char32_t *p_str) {
61
const char32_t *ptr = p_str;
62
while (*ptr != 0) {
63
++ptr;
64
}
65
return ptr - p_str;
66
}
67
68
// strlen equivalent function for wchar_t * arguments; depends on the platform.
69
constexpr size_t strlen(const wchar_t *p_str) {
70
// Use static_cast twice because reinterpret_cast is not allowed in constexpr
71
#ifdef WINDOWS_ENABLED
72
// wchar_t is 16-bit
73
return strlen(static_cast<const char16_t *>(static_cast<const void *>(p_str)));
74
#else
75
// wchar_t is 32-bit
76
return strlen(static_cast<const char32_t *>(static_cast<const void *>(p_str)));
77
#endif
78
}
79
80
// strnlen equivalent function for char16_t * arguments.
81
constexpr size_t strnlen(const char16_t *p_str, size_t p_clip_to_len) {
82
size_t len = 0;
83
while (len < p_clip_to_len && *(p_str++) != 0) {
84
len++;
85
}
86
return len;
87
}
88
89
// strnlen equivalent function for char32_t * arguments.
90
constexpr size_t strnlen(const char32_t *p_str, size_t p_clip_to_len) {
91
size_t len = 0;
92
while (len < p_clip_to_len && *(p_str++) != 0) {
93
len++;
94
}
95
return len;
96
}
97
98
// strnlen equivalent function for wchar_t * arguments; depends on the platform.
99
constexpr size_t strnlen(const wchar_t *p_str, size_t p_clip_to_len) {
100
// Use static_cast twice because reinterpret_cast is not allowed in constexpr
101
#ifdef WINDOWS_ENABLED
102
// wchar_t is 16-bit
103
return strnlen(static_cast<const char16_t *>(static_cast<const void *>(p_str)), p_clip_to_len);
104
#else
105
// wchar_t is 32-bit
106
return strnlen(static_cast<const char32_t *>(static_cast<const void *>(p_str)), p_clip_to_len);
107
#endif
108
}
109
110
template <typename L, typename R>
111
constexpr int64_t str_compare(const L *l_ptr, const R *r_ptr) {
112
while (true) {
113
const char32_t l = *l_ptr;
114
const char32_t r = *r_ptr;
115
116
if (l == 0 || l != r) {
117
return static_cast<int64_t>(l) - static_cast<int64_t>(r);
118
}
119
120
l_ptr++;
121
r_ptr++;
122
}
123
}
124
125
/*************************************************************************/
126
/* CharProxy */
127
/*************************************************************************/
128
129
template <typename T>
130
class [[nodiscard]] CharProxy {
131
friend String;
132
friend CharStringT<T>;
133
134
const int _index;
135
CowData<T> &_cowdata;
136
static constexpr T _null = 0;
137
138
_FORCE_INLINE_ CharProxy(const int &p_index, CowData<T> &p_cowdata) :
139
_index(p_index),
140
_cowdata(p_cowdata) {}
141
142
public:
143
_FORCE_INLINE_ CharProxy(const CharProxy<T> &p_other) :
144
_index(p_other._index),
145
_cowdata(p_other._cowdata) {}
146
147
_FORCE_INLINE_ operator T() const {
148
if (unlikely(_index == _cowdata.size())) {
149
return _null;
150
}
151
152
return _cowdata.get(_index);
153
}
154
155
_FORCE_INLINE_ const T *operator&() const {
156
return _cowdata.ptr() + _index;
157
}
158
159
_FORCE_INLINE_ void operator=(const T &p_other) const {
160
_cowdata.set(_index, p_other);
161
}
162
163
_FORCE_INLINE_ void operator=(const CharProxy<T> &p_other) const {
164
_cowdata.set(_index, p_other.operator T());
165
}
166
};
167
168
/*************************************************************************/
169
/* CharStringT */
170
/*************************************************************************/
171
172
template <typename T>
173
class [[nodiscard]] CharStringT {
174
CowData<T> _cowdata;
175
static constexpr T _null = 0;
176
177
public:
178
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
179
_FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
180
_FORCE_INLINE_ const T *get_data() const { return ptr() ? ptr() : &_null; }
181
182
_FORCE_INLINE_ int size() const { return _cowdata.size(); }
183
_FORCE_INLINE_ int length() const { return ptr() ? size() - 1 : 0; }
184
_FORCE_INLINE_ bool is_empty() const { return length() == 0; }
185
186
_FORCE_INLINE_ operator Span<T>() const { return Span(ptr(), length()); }
187
_FORCE_INLINE_ Span<T> span() const { return Span(ptr(), length()); }
188
189
/// Resizes the string. The given size must include the null terminator.
190
/// New characters are not initialized, and should be set by the caller.
191
_FORCE_INLINE_ Error resize_uninitialized(int64_t p_size) { return _cowdata.template resize<false>(p_size); }
192
193
_FORCE_INLINE_ T get(int p_index) const { return _cowdata.get(p_index); }
194
_FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
195
_FORCE_INLINE_ const T &operator[](int p_index) const {
196
if (unlikely(p_index == _cowdata.size())) {
197
return _null;
198
}
199
return _cowdata.get(p_index);
200
}
201
_FORCE_INLINE_ CharProxy<T> operator[](int p_index) { return CharProxy<T>(p_index, _cowdata); }
202
203
_FORCE_INLINE_ CharStringT() = default;
204
_FORCE_INLINE_ CharStringT(const CharStringT &p_str) = default;
205
_FORCE_INLINE_ CharStringT(CharStringT &&p_str) = default;
206
_FORCE_INLINE_ void operator=(const CharStringT &p_str) { _cowdata = p_str._cowdata; }
207
_FORCE_INLINE_ void operator=(CharStringT &&p_str) { _cowdata = std::move(p_str._cowdata); }
208
_FORCE_INLINE_ CharStringT(const T *p_cstr) { copy_from(p_cstr); }
209
_FORCE_INLINE_ void operator=(const T *p_cstr) { copy_from(p_cstr); }
210
211
_FORCE_INLINE_ bool operator==(const CharStringT<T> &p_other) const {
212
if (length() != p_other.length()) {
213
return false;
214
}
215
return memcmp(ptr(), p_other.ptr(), length() * sizeof(T)) == 0;
216
}
217
_FORCE_INLINE_ bool operator!=(const CharStringT<T> &p_other) const { return !(*this == p_other); }
218
_FORCE_INLINE_ bool operator<(const CharStringT<T> &p_other) const {
219
if (length() == 0) {
220
return p_other.length() != 0;
221
}
222
return str_compare(get_data(), p_other.get_data()) < 0;
223
}
224
_FORCE_INLINE_ CharStringT<T> &operator+=(T p_char) {
225
const int lhs_len = length();
226
resize_uninitialized(lhs_len + 2);
227
228
T *dst = ptrw();
229
dst[lhs_len] = p_char;
230
dst[lhs_len + 1] = _null;
231
232
return *this;
233
}
234
235
protected:
236
void copy_from(const T *p_cstr) {
237
if (!p_cstr) {
238
resize_uninitialized(0);
239
return;
240
}
241
242
size_t len = strlen(p_cstr);
243
if (len == 0) {
244
resize_uninitialized(0);
245
return;
246
}
247
248
Error err = resize_uninitialized(++len); // include terminating null char.
249
250
ERR_FAIL_COND_MSG(err != OK, "Failed to copy C-string.");
251
252
memcpy(ptrw(), p_cstr, len * sizeof(T));
253
}
254
};
255
256
template <typename T>
257
struct is_zero_constructible<CharStringT<T>> : std::true_type {};
258
259
using CharString = CharStringT<char>;
260
using Char16String = CharStringT<char16_t>;
261
262
/*************************************************************************/
263
/* String */
264
/*************************************************************************/
265
266
class [[nodiscard]] String {
267
CowData<char32_t> _cowdata;
268
static constexpr char32_t _null = 0;
269
static constexpr char32_t _replacement_char = 0xfffd;
270
271
// Known-length copy.
272
void copy_from_unchecked(const char32_t *p_char, int p_length);
273
274
// NULL-terminated c string copy - automatically parse the string to find the length.
275
void append_latin1(const char *p_cstr) {
276
append_latin1(Span(p_cstr, p_cstr ? strlen(p_cstr) : 0));
277
}
278
void append_utf32(const char32_t *p_cstr) {
279
append_utf32(Span(p_cstr, p_cstr ? strlen(p_cstr) : 0));
280
}
281
282
// wchar_t copy_from depends on the platform.
283
void append_wstring(const Span<wchar_t> &p_cstr) {
284
#ifdef WINDOWS_ENABLED
285
// wchar_t is 16-bit, parse as UTF-16
286
append_utf16((const char16_t *)p_cstr.ptr(), p_cstr.size());
287
#else
288
// wchar_t is 32-bit, copy directly
289
append_utf32((Span<char32_t> &)p_cstr);
290
#endif
291
}
292
void append_wstring(const wchar_t *p_cstr) {
293
#ifdef WINDOWS_ENABLED
294
// wchar_t is 16-bit, parse as UTF-16
295
append_utf16((const char16_t *)p_cstr);
296
#else
297
// wchar_t is 32-bit, copy directly
298
append_utf32((const char32_t *)p_cstr);
299
#endif
300
}
301
302
bool _base_is_subsequence_of(const String &p_string, bool case_insensitive) const;
303
int _count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const;
304
int _count(const char *p_string, int p_from, int p_to, bool p_case_insensitive) const;
305
String _separate_compound_words() const;
306
307
public:
308
enum {
309
npos = -1 ///<for "some" compatibility with std::string (npos is a huge value in std::string)
310
};
311
312
_FORCE_INLINE_ char32_t *ptrw() { return _cowdata.ptrw(); }
313
_FORCE_INLINE_ const char32_t *ptr() const { return _cowdata.ptr(); }
314
_FORCE_INLINE_ const char32_t *get_data() const { return ptr() ? ptr() : &_null; }
315
316
_FORCE_INLINE_ int size() const { return _cowdata.size(); }
317
_FORCE_INLINE_ int length() const { return ptr() ? size() - 1 : 0; }
318
_FORCE_INLINE_ bool is_empty() const { return length() == 0; }
319
320
_FORCE_INLINE_ operator Span<char32_t>() const { return Span(ptr(), length()); }
321
_FORCE_INLINE_ Span<char32_t> span() const { return Span(ptr(), length()); }
322
323
void remove_at(int p_index) { _cowdata.remove_at(p_index); }
324
325
_FORCE_INLINE_ void clear() { resize_uninitialized(0); }
326
327
_FORCE_INLINE_ char32_t get(int p_index) const { return _cowdata.get(p_index); }
328
_FORCE_INLINE_ void set(int p_index, const char32_t &p_elem) { _cowdata.set(p_index, p_elem); }
329
330
/// Resizes the string. The given size must include the null terminator.
331
/// New characters are not initialized, and should be set by the caller.
332
Error resize_uninitialized(int64_t p_size) { return _cowdata.resize<false>(p_size); }
333
334
_FORCE_INLINE_ const char32_t &operator[](int p_index) const {
335
if (unlikely(p_index == _cowdata.size())) {
336
return _null;
337
}
338
339
return _cowdata.get(p_index);
340
}
341
_FORCE_INLINE_ CharProxy<char32_t> operator[](int p_index) { return CharProxy<char32_t>(p_index, _cowdata); }
342
343
/* Compatibility Operators */
344
345
bool operator==(const String &p_str) const;
346
bool operator!=(const String &p_str) const;
347
String operator+(const String &p_str) const;
348
String operator+(const char *p_char) const;
349
String operator+(const wchar_t *p_char) const;
350
String operator+(const char32_t *p_char) const;
351
String operator+(char32_t p_char) const;
352
353
String &operator+=(const String &);
354
String &operator+=(char32_t p_char);
355
String &operator+=(const char *p_str);
356
String &operator+=(const wchar_t *p_str);
357
String &operator+=(const char32_t *p_str);
358
359
bool operator==(const char *p_str) const;
360
bool operator==(const wchar_t *p_str) const;
361
bool operator==(const char32_t *p_str) const;
362
bool operator==(const Span<char32_t> &p_str_range) const;
363
364
bool operator!=(const char *p_str) const;
365
bool operator!=(const wchar_t *p_str) const;
366
bool operator!=(const char32_t *p_str) const;
367
368
bool operator<(const char32_t *p_str) const;
369
bool operator<(const char *p_str) const;
370
bool operator<(const wchar_t *p_str) const;
371
372
bool operator<(const String &p_str) const;
373
bool operator<=(const String &p_str) const;
374
bool operator>(const String &p_str) const;
375
bool operator>=(const String &p_str) const;
376
377
signed char casecmp_to(const String &p_str) const;
378
signed char nocasecmp_to(const String &p_str) const;
379
signed char naturalcasecmp_to(const String &p_str) const;
380
signed char naturalnocasecmp_to(const String &p_str) const;
381
// Special sorting for file names. Names starting with `_` are put before all others except those starting with `.`, otherwise natural comparison is used.
382
signed char filecasecmp_to(const String &p_str) const;
383
signed char filenocasecmp_to(const String &p_str) const;
384
385
bool is_valid_string() const;
386
387
/* debug, error messages */
388
void print_unicode_error(const String &p_message, bool p_critical = false) const;
389
390
/* complex helpers */
391
String substr(int p_from, int p_chars = -1) const;
392
int find(const String &p_str, int p_from = 0) const; ///< return <0 if failed
393
int find(const char *p_str, int p_from = 0) const; ///< return <0 if failed
394
int find_char(char32_t p_char, int p_from = 0) const; ///< return <0 if failed
395
int findn(const String &p_str, int p_from = 0) const; ///< return <0 if failed, case insensitive
396
int findn(const char *p_str, int p_from = 0) const; ///< return <0 if failed
397
int rfind(const String &p_str, int p_from = -1) const; ///< return <0 if failed
398
int rfind(const char *p_str, int p_from = -1) const; ///< return <0 if failed
399
int rfind_char(char32_t p_char, int p_from = -1) const; ///< return <0 if failed
400
int rfindn(const String &p_str, int p_from = -1) const; ///< return <0 if failed, case insensitive
401
int rfindn(const char *p_str, int p_from = -1) const; ///< return <0 if failed
402
int findmk(const Vector<String> &p_keys, int p_from = 0, int *r_key = nullptr) const; ///< return <0 if failed
403
bool match(const String &p_wildcard) const;
404
bool matchn(const String &p_wildcard) const;
405
bool begins_with(const String &p_string) const;
406
bool begins_with(const char *p_string) const;
407
bool ends_with(const String &p_string) const;
408
bool ends_with(const char *p_string) const;
409
bool is_enclosed_in(const String &p_string) const;
410
bool is_subsequence_of(const String &p_string) const;
411
bool is_subsequence_ofn(const String &p_string) const;
412
bool is_quoted() const;
413
bool is_lowercase() const;
414
Vector<String> bigrams() const;
415
float similarity(const String &p_string) const;
416
String format(const Variant &values, const String &placeholder = "{_}") const;
417
String replace_first(const String &p_key, const String &p_with) const;
418
String replace_first(const char *p_key, const char *p_with) const;
419
String replace(const String &p_key, const String &p_with) const;
420
String replace(const char *p_key, const char *p_with) const;
421
String replace_char(char32_t p_key, char32_t p_with) const;
422
String replace_chars(const String &p_keys, char32_t p_with) const;
423
String replace_chars(const char *p_keys, char32_t p_with) const;
424
String replacen(const String &p_key, const String &p_with) const;
425
String replacen(const char *p_key, const char *p_with) const;
426
String repeat(int p_count) const;
427
String reverse() const;
428
String insert(int p_at_pos, const String &p_string) const;
429
String erase(int p_pos, int p_chars = 1) const;
430
String remove_char(char32_t p_what) const;
431
String remove_chars(const String &p_chars) const;
432
String remove_chars(const char *p_chars) const;
433
String pad_decimals(int p_digits) const;
434
String pad_zeros(int p_digits) const;
435
String trim_prefix(const String &p_prefix) const;
436
String trim_prefix(const char *p_prefix) const;
437
String trim_suffix(const String &p_suffix) const;
438
String trim_suffix(const char *p_suffix) const;
439
String lpad(int min_length, const String &character = " ") const;
440
String rpad(int min_length, const String &character = " ") const;
441
String sprintf(const Array &values, bool *error) const;
442
String quote(const String &quotechar = "\"") const;
443
String unquote() const;
444
static String num(double p_num, int p_decimals = -1);
445
static String num_scientific(double p_num);
446
static String num_scientific(float p_num);
447
static String num_real(double p_num, bool p_trailing = true);
448
static String num_real(float p_num, bool p_trailing = true);
449
static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);
450
static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false);
451
static String chr(char32_t p_char) {
452
String string;
453
string.append_utf32(Span(&p_char, 1));
454
return string;
455
}
456
static String md5(const uint8_t *p_md5);
457
static String hex_encode_buffer(const uint8_t *p_buffer, int p_len);
458
Vector<uint8_t> hex_decode() const;
459
460
bool is_numeric() const;
461
462
double to_float() const;
463
int64_t hex_to_int() const;
464
int64_t bin_to_int() const;
465
int64_t to_int() const;
466
467
static int64_t to_int(const char *p_str, int p_len = -1);
468
static int64_t to_int(const wchar_t *p_str, int p_len = -1);
469
static int64_t to_int(const char32_t *p_str, int p_len = -1, bool p_clamp = false);
470
471
static double to_float(const char *p_str);
472
static double to_float(const wchar_t *p_str, const wchar_t **r_end = nullptr);
473
static double to_float(const char32_t *p_str, const char32_t **r_end = nullptr);
474
static uint32_t num_characters(int64_t p_int);
475
476
String capitalize() const;
477
String to_camel_case() const;
478
String to_pascal_case() const;
479
String to_snake_case() const;
480
String to_kebab_case() const;
481
482
String get_with_code_lines() const;
483
int get_slice_count(const String &p_splitter) const;
484
int get_slice_count(const char *p_splitter) const;
485
String get_slice(const String &p_splitter, int p_slice) const;
486
String get_slice(const char *p_splitter, int p_slice) const;
487
String get_slicec(char32_t p_splitter, int p_slice) const;
488
489
Vector<String> split(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
490
Vector<String> split(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
491
Vector<String> rsplit(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
492
Vector<String> rsplit(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
493
Vector<String> split_spaces(int p_maxsplit = 0) const;
494
Vector<double> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
495
Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
496
Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;
497
Vector<int> split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
498
499
String join(const Vector<String> &parts) const;
500
501
static char32_t char_uppercase(char32_t p_char);
502
static char32_t char_lowercase(char32_t p_char);
503
String to_upper() const;
504
String to_lower() const;
505
506
int count(const String &p_string, int p_from = 0, int p_to = 0) const;
507
int count(const char *p_string, int p_from = 0, int p_to = 0) const;
508
int countn(const String &p_string, int p_from = 0, int p_to = 0) const;
509
int countn(const char *p_string, int p_from = 0, int p_to = 0) const;
510
511
String left(int p_len) const;
512
String right(int p_len) const;
513
String indent(const String &p_prefix) const;
514
String dedent() const;
515
String strip_edges(bool left = true, bool right = true) const;
516
String strip_escapes() const;
517
String lstrip(const String &p_chars) const;
518
String rstrip(const String &p_chars) const;
519
String get_extension() const;
520
String get_basename() const;
521
String path_join(const String &p_path) const;
522
char32_t unicode_at(int p_idx) const;
523
524
CharString ascii(bool p_allow_extended = false) const;
525
// Parse an ascii string.
526
// If any character is > 127, an error will be logged, and 0xfffd will be inserted.
527
Error append_ascii(const Span<char> &p_range);
528
static String ascii(const Span<char> &p_range) {
529
String s;
530
s.append_ascii(p_range);
531
return s;
532
}
533
CharString latin1() const { return ascii(true); }
534
void append_latin1(const Span<char> &p_cstr);
535
static String latin1(const Span<char> &p_string) {
536
String string;
537
string.append_latin1(p_string);
538
return string;
539
}
540
541
CharString utf8(Vector<uint8_t> *r_ch_length_map = nullptr) const;
542
Error append_utf8(const char *p_utf8, int p_len = -1, bool p_skip_cr = false);
543
Error append_utf8(const Span<char> &p_range, bool p_skip_cr = false) {
544
return append_utf8(p_range.ptr(), p_range.size(), p_skip_cr);
545
}
546
static String utf8(const char *p_utf8, int p_len = -1) {
547
String ret;
548
ret.append_utf8(p_utf8, p_len);
549
return ret;
550
}
551
static String utf8(const Span<char> &p_range) { return utf8(p_range.ptr(), p_range.size()); }
552
553
Char16String utf16() const;
554
Error append_utf16(const char16_t *p_utf16, int p_len = -1, bool p_default_little_endian = true);
555
Error append_utf16(const Span<char16_t> p_range, bool p_skip_cr = false) {
556
return append_utf16(p_range.ptr(), p_range.size(), p_skip_cr);
557
}
558
static String utf16(const char16_t *p_utf16, int p_len = -1) {
559
String ret;
560
ret.append_utf16(p_utf16, p_len);
561
return ret;
562
}
563
static String utf16(const Span<char16_t> &p_range) { return utf16(p_range.ptr(), p_range.size()); }
564
565
void append_utf32(const Span<char32_t> &p_cstr);
566
static String utf32(const Span<char32_t> &p_span) {
567
String string;
568
string.append_utf32(p_span);
569
return string;
570
}
571
572
static uint32_t hash(const char32_t *p_cstr, int p_len); /* hash the string */
573
static uint32_t hash(const char32_t *p_cstr); /* hash the string */
574
static uint32_t hash(const wchar_t *p_cstr, int p_len); /* hash the string */
575
static uint32_t hash(const wchar_t *p_cstr); /* hash the string */
576
static uint32_t hash(const char *p_cstr, int p_len); /* hash the string */
577
static uint32_t hash(const char *p_cstr); /* hash the string */
578
uint32_t hash() const; /* hash the string */
579
uint64_t hash64() const; /* hash the string */
580
String md5_text() const;
581
String sha1_text() const;
582
String sha256_text() const;
583
Vector<uint8_t> md5_buffer() const;
584
Vector<uint8_t> sha1_buffer() const;
585
Vector<uint8_t> sha256_buffer() const;
586
587
_FORCE_INLINE_ bool contains(const char *p_str) const { return find(p_str) != -1; }
588
_FORCE_INLINE_ bool contains(const String &p_str) const { return find(p_str) != -1; }
589
_FORCE_INLINE_ bool contains_char(char32_t p_chr) const { return find_char(p_chr) != -1; }
590
_FORCE_INLINE_ bool containsn(const char *p_str) const { return findn(p_str) != -1; }
591
_FORCE_INLINE_ bool containsn(const String &p_str) const { return findn(p_str) != -1; }
592
593
// path functions
594
bool is_absolute_path() const;
595
bool is_relative_path() const;
596
bool is_resource_file() const;
597
String path_to(const String &p_path) const;
598
String path_to_file(const String &p_path) const;
599
String get_base_dir() const;
600
String get_file() const;
601
static String humanize_size(uint64_t p_size);
602
String simplify_path() const;
603
bool is_network_share_path() const;
604
605
String xml_escape(bool p_escape_quotes = false) const;
606
String xml_unescape() const;
607
String uri_encode() const;
608
String uri_decode() const;
609
String uri_file_decode() const;
610
String c_escape() const;
611
String c_escape_multiline() const;
612
String c_unescape() const;
613
String json_escape() const;
614
Error parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const;
615
616
String property_name_encode() const;
617
618
// node functions
619
static String get_invalid_node_name_characters(bool p_allow_internal = false);
620
String validate_node_name() const;
621
String validate_ascii_identifier() const;
622
String validate_unicode_identifier() const;
623
String validate_filename() const;
624
625
bool is_valid_ascii_identifier() const;
626
bool is_valid_unicode_identifier() const;
627
bool is_valid_int() const;
628
bool is_valid_float() const;
629
bool is_valid_hex_number(bool p_with_prefix) const;
630
bool is_valid_html_color() const;
631
bool is_valid_ip_address() const;
632
bool is_valid_filename() const;
633
634
// Use `is_valid_ascii_identifier()` instead. Kept for compatibility.
635
bool is_valid_identifier() const { return is_valid_ascii_identifier(); }
636
637
/**
638
* The constructors must not depend on other overloads
639
*/
640
641
_FORCE_INLINE_ String() {}
642
_FORCE_INLINE_ String(const String &p_str) = default;
643
_FORCE_INLINE_ String(String &&p_str) = default;
644
#ifdef SIZE_EXTRA
645
_NO_INLINE_ ~String() {}
646
#endif
647
_FORCE_INLINE_ void operator=(const String &p_str) { _cowdata = p_str._cowdata; }
648
_FORCE_INLINE_ void operator=(String &&p_str) { _cowdata = std::move(p_str._cowdata); }
649
650
Vector<uint8_t> to_ascii_buffer() const;
651
Vector<uint8_t> to_utf8_buffer() const;
652
Vector<uint8_t> to_utf16_buffer() const;
653
Vector<uint8_t> to_utf32_buffer() const;
654
Vector<uint8_t> to_wchar_buffer() const;
655
Vector<uint8_t> to_multibyte_char_buffer(const String &p_encoding = String()) const;
656
657
// Constructors for NULL terminated C strings.
658
String(const char *p_cstr) {
659
append_latin1(p_cstr);
660
}
661
String(const wchar_t *p_cstr) {
662
append_wstring(p_cstr);
663
}
664
String(const char32_t *p_cstr) {
665
append_utf32(p_cstr);
666
}
667
668
// Copy assignment for NULL terminated C strings.
669
void operator=(const char *p_cstr) {
670
clear();
671
append_latin1(p_cstr);
672
}
673
void operator=(const wchar_t *p_cstr) {
674
clear();
675
append_wstring(p_cstr);
676
}
677
void operator=(const char32_t *p_cstr) {
678
clear();
679
append_utf32(p_cstr);
680
}
681
};
682
683
// Zero-constructing String initializes _cowdata.ptr() to nullptr and thus empty.
684
template <>
685
struct is_zero_constructible<String> : std::true_type {};
686
687
bool operator==(const char *p_chr, const String &p_str);
688
bool operator==(const wchar_t *p_chr, const String &p_str);
689
bool operator!=(const char *p_chr, const String &p_str);
690
bool operator!=(const wchar_t *p_chr, const String &p_str);
691
692
String operator+(const char *p_chr, const String &p_str);
693
String operator+(const wchar_t *p_chr, const String &p_str);
694
String operator+(char32_t p_chr, const String &p_str);
695
696
String itos(int64_t p_val);
697
String uitos(uint64_t p_val);
698
String rtos(double p_val);
699
String rtoss(double p_val); //scientific version
700
701
struct NoCaseComparator {
702
bool operator()(const String &p_a, const String &p_b) const {
703
return p_a.nocasecmp_to(p_b) < 0;
704
}
705
};
706
707
struct NaturalNoCaseComparator {
708
bool operator()(const String &p_a, const String &p_b) const {
709
return p_a.naturalnocasecmp_to(p_b) < 0;
710
}
711
};
712
713
struct FileNoCaseComparator {
714
bool operator()(const String &p_a, const String &p_b) const {
715
return p_a.filenocasecmp_to(p_b) < 0;
716
}
717
};
718
719
/* end of namespace */
720
721
// Tool translate (TTR and variants) for the editor UI,
722
// and doc translate for the class reference (DTR).
723
#ifdef TOOLS_ENABLED
724
// Gets parsed.
725
String TTR(const String &p_text, const String &p_context = "");
726
String TTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "");
727
String DTR(const String &p_text, const String &p_context = "");
728
String DTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "");
729
// Use for C strings.
730
#define TTRC(m_value) (m_value)
731
// Use to avoid parsing (for use later with C strings).
732
#define TTRGET(m_value) TTR(m_value)
733
734
#else
735
#define TTRC(m_value) (m_value)
736
#define TTRGET(m_value) (m_value)
737
#endif
738
739
// Use this to mark property names for editor translation.
740
// Often for dynamic properties defined in _get_property_list().
741
// Property names defined directly inside EDITOR_DEF, GLOBAL_DEF, and ADD_PROPERTY macros don't need this.
742
#define PNAME(m_value) (m_value)
743
744
// Similar to PNAME, but to mark groups, i.e. properties with PROPERTY_USAGE_GROUP.
745
// Groups defined directly inside ADD_GROUP macros don't need this.
746
// The arguments are the same as ADD_GROUP. m_prefix is only used for extraction.
747
#define GNAME(m_value, m_prefix) (m_value)
748
749
// Runtime translate for the public node API.
750
String RTR(const String &p_text, const String &p_context = "");
751
String RTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "");
752
753
/**
754
* "Extractable TRanslate". Used for strings that can appear inside an exported
755
* project (such as the ones in nodes like `FileDialog`), which are made possible
756
* to add in the POT generator. A translation context can optionally be specified
757
* to disambiguate between identical source strings in translations.
758
* When placeholders are desired, use vformat(ETR("Example: %s"), some_string)`.
759
* If a string mentions a quantity (and may therefore need a dynamic plural form),
760
* use `ETRN()` instead of `ETR()`.
761
*
762
* NOTE: This function is for string extraction only, and will just return the
763
* string it was given. The translation itself should be done internally by nodes
764
* with `atr()` instead.
765
*/
766
_FORCE_INLINE_ String ETR(const String &p_text, const String &p_context = "") {
767
return p_text;
768
}
769
770
/**
771
* "Extractable TRanslate for N items". Used for strings that can appear inside an
772
* exported project (such as the ones in nodes like `FileDialog`), which are made
773
* possible to add in the POT generator. A translation context can optionally be
774
* specified to disambiguate between identical source strings in translations.
775
* Use `ETR()` if the string doesn't need dynamic plural form. When placeholders
776
* are desired, use `vformat(ETRN("%d item", "%d items", some_integer), some_integer)`.
777
* The placeholder must be present in both strings to avoid run-time warnings in `vformat()`.
778
*
779
* NOTE: This function is for string extraction only, and will just return the
780
* string it was given. The translation itself should be done internally by nodes
781
* with `atr()` instead.
782
*/
783
_FORCE_INLINE_ String ETRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "") {
784
if (p_n == 1) {
785
return p_text;
786
}
787
return p_text_plural;
788
}
789
790
template <typename... P>
791
_FORCE_INLINE_ Vector<String> sarray(P... p_args) {
792
return Vector<String>({ String(p_args)... });
793
}
794
795