summaryrefslogtreecommitdiff
path: root/core/ustring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r--core/ustring.cpp1291
1 files changed, 932 insertions, 359 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp
index 5d3cf5f1a4..27dab8db6e 100644
--- a/core/ustring.cpp
+++ b/core/ustring.cpp
@@ -39,7 +39,6 @@
#include "core/ucaps.h"
#include "core/variant.h"
-#include <wchar.h>
#include <cstdint>
#ifndef NO_USE_STDLIB
@@ -62,9 +61,10 @@
#define IS_HEX_DIGIT(m_d) (((m_d) >= '0' && (m_d) <= '9') || ((m_d) >= 'a' && (m_d) <= 'f') || ((m_d) >= 'A' && (m_d) <= 'F'))
const char CharString::_null = 0;
-const CharType String::_null = 0;
+const char16_t Char16String::_null = 0;
+const char32_t String::_null = 0;
-bool is_symbol(CharType c) {
+bool is_symbol(char32_t c) {
return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' ');
}
@@ -96,9 +96,11 @@ bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) {
}
}
-/** STRING **/
+/*************************************************************************/
+/* Char16String */
+/*************************************************************************/
-bool CharString::operator<(const CharString &p_right) const {
+bool Char16String::operator<(const Char16String &p_right) const {
if (length() == 0) {
return p_right.length() != 0;
}
@@ -106,7 +108,7 @@ bool CharString::operator<(const CharString &p_right) const {
return is_str_less(get_data(), p_right.get_data());
}
-CharString &CharString::operator+=(char p_char) {
+Char16String &Char16String::operator+=(char16_t p_char) {
resize(size() ? size() + 1 : 2);
set(length(), 0);
set(length() - 1, p_char);
@@ -114,19 +116,75 @@ CharString &CharString::operator+=(char p_char) {
return *this;
}
-const char *CharString::get_data() const {
+Char16String &Char16String::operator=(const char16_t *p_cstr) {
+ copy_from(p_cstr);
+ return *this;
+}
+
+const char16_t *Char16String::get_data() const {
if (size()) {
return &operator[](0);
} else {
- return "";
+ return u"";
}
}
+void Char16String::copy_from(const char16_t *p_cstr) {
+ if (!p_cstr) {
+ resize(0);
+ return;
+ }
+
+ const char16_t *s = p_cstr;
+ for (; *s; s++) {
+ }
+ size_t len = s - p_cstr;
+
+ if (len == 0) {
+ resize(0);
+ return;
+ }
+
+ Error err = resize(++len); // include terminating null char
+
+ ERR_FAIL_COND_MSG(err != OK, "Failed to copy char16_t string.");
+
+ memcpy(ptrw(), p_cstr, len * sizeof(char16_t));
+}
+
+/*************************************************************************/
+/* CharString */
+/*************************************************************************/
+
+bool CharString::operator<(const CharString &p_right) const {
+ if (length() == 0) {
+ return p_right.length() != 0;
+ }
+
+ return is_str_less(get_data(), p_right.get_data());
+}
+
+CharString &CharString::operator+=(char p_char) {
+ resize(size() ? size() + 1 : 2);
+ set(length(), 0);
+ set(length() - 1, p_char);
+
+ return *this;
+}
+
CharString &CharString::operator=(const char *p_cstr) {
copy_from(p_cstr);
return *this;
}
+const char *CharString::get_data() const {
+ if (size()) {
+ return &operator[](0);
+ } else {
+ return "";
+ }
+}
+
void CharString::copy_from(const char *p_cstr) {
if (!p_cstr) {
resize(0);
@@ -147,7 +205,44 @@ void CharString::copy_from(const char *p_cstr) {
memcpy(ptrw(), p_cstr, len);
}
+/*************************************************************************/
+/* String */
+/*************************************************************************/
+
+//TODO: move to TextServer
+//kind of poor should be rewritten properly
+String String::word_wrap(int p_chars_per_line) const {
+ int from = 0;
+ int last_space = 0;
+ String ret;
+ for (int i = 0; i < length(); i++) {
+ if (i - from >= p_chars_per_line) {
+ if (last_space == -1) {
+ ret += substr(from, i - from + 1) + "\n";
+ } else {
+ ret += substr(from, last_space - from) + "\n";
+ i = last_space; //rewind
+ }
+ from = i + 1;
+ last_space = -1;
+ } else if (operator[](i) == ' ' || operator[](i) == '\t') {
+ last_space = i;
+ } else if (operator[](i) == '\n') {
+ ret += substr(from, i - from) + "\n";
+ from = i + 1;
+ last_space = -1;
+ }
+ }
+
+ if (from < length()) {
+ ret += substr(from, length());
+ }
+
+ return ret;
+}
+
void String::copy_from(const char *p_cstr) {
+ // copy Latin-1 encoded c-string directly
if (!p_cstr) {
resize(0);
return;
@@ -166,21 +261,22 @@ void String::copy_from(const char *p_cstr) {
resize(len + 1); // include 0
- CharType *dst = this->ptrw();
+ char32_t *dst = this->ptrw();
for (int i = 0; i < len + 1; i++) {
dst[i] = p_cstr[i];
}
}
-void String::copy_from(const CharType *p_cstr, const int p_clip_to) {
+void String::copy_from(const char *p_cstr, const int p_clip_to) {
+ // copy Latin-1 encoded c-string directly
if (!p_cstr) {
resize(0);
return;
}
int len = 0;
- const CharType *ptr = p_cstr;
+ const char *ptr = p_cstr;
while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
len++;
}
@@ -190,55 +286,117 @@ void String::copy_from(const CharType *p_cstr, const int p_clip_to) {
return;
}
- copy_from_unchecked(p_cstr, len);
-}
-
-// assumes the following have already been validated:
-// p_char != nullptr
-// p_length > 0
-// p_length <= p_char strlen
-void String::copy_from_unchecked(const CharType *p_char, const int p_length) {
- resize(p_length + 1);
- set(p_length, 0);
+ resize(len + 1); // include 0
- CharType *dst = ptrw();
+ char32_t *dst = this->ptrw();
- for (int i = 0; i < p_length; i++) {
- dst[i] = p_char[i];
+ for (int i = 0; i < len; i++) {
+ dst[i] = p_cstr[i];
}
+ dst[len] = 0;
}
-void String::copy_from(const CharType &p_char) {
+void String::copy_from(const wchar_t *p_cstr) {
+#ifdef WINDOWS_ENABLED
+ // wchar_t is 16-bit, parse as UTF-16
+ parse_utf16((const char16_t *)p_cstr);
+#else
+ // wchar_t is 32-bit, copy directly
+ copy_from((const char32_t *)p_cstr);
+#endif
+}
+
+void String::copy_from(const wchar_t *p_cstr, const int p_clip_to) {
+#ifdef WINDOWS_ENABLED
+ // wchar_t is 16-bit, parse as UTF-16
+ parse_utf16((const char16_t *)p_cstr, p_clip_to);
+#else
+ // wchar_t is 32-bit, copy directly
+ copy_from((const char32_t *)p_cstr, p_clip_to);
+#endif
+}
+
+void String::copy_from(const char32_t &p_char) {
resize(2);
- set(0, p_char);
+ if ((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff)) {
+ print_error("Unicode parsing error: Invalid unicode codepoint " + num_int64(p_char, 16) + ".");
+ set(0, 0xfffd);
+ } else {
+ set(0, p_char);
+ }
set(1, 0);
}
-bool String::operator==(const String &p_str) const {
- if (length() != p_str.length()) {
- return false;
+void String::copy_from(const char32_t *p_cstr) {
+ if (!p_cstr) {
+ resize(0);
+ return;
}
- if (empty()) {
- return true;
+
+ int len = 0;
+ const char32_t *ptr = p_cstr;
+ while (*(ptr++) != 0) {
+ len++;
}
- int l = length();
+ if (len == 0) {
+ resize(0);
+ return;
+ }
- const CharType *src = c_str();
- const CharType *dst = p_str.c_str();
+ copy_from_unchecked(p_cstr, len);
+}
- /* Compare char by char */
- for (int i = 0; i < l; i++) {
- if (src[i] != dst[i]) {
- return false;
+void String::copy_from(const char32_t *p_cstr, const int p_clip_to) {
+ if (!p_cstr) {
+ resize(0);
+ return;
+ }
+
+ int len = 0;
+ const char32_t *ptr = p_cstr;
+ while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
+ len++;
+ }
+
+ if (len == 0) {
+ resize(0);
+ return;
+ }
+
+ copy_from_unchecked(p_cstr, len);
+}
+
+// assumes the following have already been validated:
+// p_char != nullptr
+// p_length > 0
+// p_length <= p_char strlen
+void String::copy_from_unchecked(const char32_t *p_char, const int p_length) {
+ resize(p_length + 1);
+ set(p_length, 0);
+
+ char32_t *dst = ptrw();
+
+ for (int i = 0; i < p_length; i++) {
+ if ((p_char[i] >= 0xd800 && p_char[i] <= 0xdfff) || (p_char[i] > 0x10ffff)) {
+ print_error("Unicode parsing error: Invalid unicode codepoint " + num_int64(p_char[i], 16) + ".");
+ dst[i] = 0xfffd;
+ } else {
+ dst[i] = p_char[i];
}
}
+}
- return true;
+void String::operator=(const char *p_str) {
+ copy_from(p_str);
}
-bool String::operator!=(const String &p_str) const {
- return !(*this == p_str);
+void String::operator=(const char32_t *p_str) {
+ copy_from(p_str);
+}
+
+void String::operator=(const wchar_t *p_str) {
+ copy_from(p_str);
}
String String::operator+(const String &p_str) const {
@@ -247,6 +405,28 @@ String String::operator+(const String &p_str) const {
return res;
}
+String operator+(const char *p_chr, const String &p_str) {
+ String tmp = p_chr;
+ tmp += p_str;
+ return tmp;
+}
+
+String operator+(const wchar_t *p_chr, const String &p_str) {
+#ifdef WINDOWS_ENABLED
+ // wchar_t is 16-bit
+ String tmp = String::utf16((const char16_t *)p_chr);
+#else
+ // wchar_t is 32-bi
+ String tmp = (const char32_t *)p_chr;
+#endif
+ tmp += p_str;
+ return tmp;
+}
+
+String operator+(char32_t p_chr, const String &p_str) {
+ return (String::chr(p_chr) + p_str);
+}
+
String &String::operator+=(const String &p_str) {
if (empty()) {
*this = p_str;
@@ -261,8 +441,8 @@ String &String::operator+=(const String &p_str) {
resize(length() + p_str.size());
- const CharType *src = p_str.c_str();
- CharType *dst = ptrw();
+ const char32_t *src = p_str.get_data();
+ char32_t *dst = ptrw();
set(length(), 0);
@@ -273,19 +453,6 @@ String &String::operator+=(const String &p_str) {
return *this;
}
-String &String::operator+=(const CharType *p_str) {
- *this += String(p_str);
- return *this;
-}
-
-String &String::operator+=(CharType p_char) {
- resize(size() ? size() + 1 : 2);
- set(length(), 0);
- set(length() - 1, p_char);
-
- return *this;
-}
-
String &String::operator+=(const char *p_str) {
if (!p_str || p_str[0] == 0) {
return *this;
@@ -301,7 +468,7 @@ String &String::operator+=(const char *p_str) {
resize(from + src_len + 1);
- CharType *dst = ptrw();
+ char32_t *dst = ptrw();
set(length(), 0);
@@ -312,16 +479,43 @@ String &String::operator+=(const char *p_str) {
return *this;
}
-void String::operator=(const char *p_str) {
- copy_from(p_str);
+String &String::operator+=(const wchar_t *p_str) {
+#ifdef WINDOWS_ENABLED
+ // wchar_t is 16-bit
+ *this += String::utf16((const char16_t *)p_str);
+#else
+ // wchar_t is 32-bit
+ *this += String((const char32_t *)p_str);
+#endif
+ return *this;
}
-void String::operator=(const CharType *p_str) {
- copy_from(p_str);
+String &String::operator+=(const char32_t *p_str) {
+ *this += String(p_str);
+ return *this;
}
-bool String::operator==(const StrRange &p_str_range) const {
- int len = p_str_range.len;
+String &String::operator+=(char32_t p_char) {
+ resize(size() ? size() + 1 : 2);
+ set(length(), 0);
+ if ((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff)) {
+ print_error("Unicode parsing error: Invalid unicode codepoint " + num_int64(p_char, 16) + ".");
+ set(length() - 1, 0xfffd);
+ } else {
+ set(length() - 1, p_char);
+ }
+
+ return *this;
+}
+
+bool String::operator==(const char *p_str) const {
+ // compare Latin-1 encoded c-string
+ int len = 0;
+ const char *aux = p_str;
+
+ while (*(aux++) != 0) {
+ len++;
+ }
if (length() != len) {
return false;
@@ -330,12 +524,13 @@ bool String::operator==(const StrRange &p_str_range) const {
return true;
}
- const CharType *c_str = p_str_range.c_str;
- const CharType *dst = &operator[](0);
+ int l = length();
- /* Compare char by char */
- for (int i = 0; i < len; i++) {
- if (c_str[i] != dst[i]) {
+ const char32_t *dst = get_data();
+
+ // Compare char by char
+ for (int i = 0; i < l; i++) {
+ if ((char32_t)p_str[i] != dst[i]) {
return false;
}
}
@@ -343,9 +538,19 @@ bool String::operator==(const StrRange &p_str_range) const {
return true;
}
-bool String::operator==(const char *p_str) const {
+bool String::operator==(const wchar_t *p_str) const {
+#ifdef WINDOWS_ENABLED
+ // wchar_t is 16-bit, parse as UTF-16
+ return *this == String::utf16((const char16_t *)p_str);
+#else
+ // wchar_t is 32-bit, compare char by char
+ return *this == (const char32_t *)p_str;
+#endif
+}
+
+bool String::operator==(const char32_t *p_str) const {
int len = 0;
- const char *aux = p_str;
+ const char32_t *aux = p_str;
while (*(aux++) != 0) {
len++;
@@ -360,7 +565,7 @@ bool String::operator==(const char *p_str) const {
int l = length();
- const CharType *dst = c_str();
+ const char32_t *dst = get_data();
/* Compare char by char */
for (int i = 0; i < l; i++) {
@@ -372,14 +577,32 @@ bool String::operator==(const char *p_str) const {
return true;
}
-bool String::operator==(const CharType *p_str) const {
- int len = 0;
- const CharType *aux = p_str;
+bool String::operator==(const String &p_str) const {
+ if (length() != p_str.length()) {
+ return false;
+ }
+ if (empty()) {
+ return true;
+ }
- while (*(aux++) != 0) {
- len++;
+ int l = length();
+
+ const char32_t *src = get_data();
+ const char32_t *dst = p_str.get_data();
+
+ /* Compare char by char */
+ for (int i = 0; i < l; i++) {
+ if (src[i] != dst[i]) {
+ return false;
+ }
}
+ return true;
+}
+
+bool String::operator==(const StrRange &p_str_range) const {
+ int len = p_str_range.len;
+
if (length() != len) {
return false;
}
@@ -387,13 +610,12 @@ bool String::operator==(const CharType *p_str) const {
return true;
}
- int l = length();
-
- const CharType *dst = c_str();
+ const char32_t *c_str = p_str_range.c_str;
+ const char32_t *dst = &operator[](0);
/* Compare char by char */
- for (int i = 0; i < l; i++) {
- if (p_str[i] != dst[i]) {
+ for (int i = 0; i < len; i++) {
+ if (c_str[i] != dst[i]) {
return false;
}
}
@@ -401,30 +623,68 @@ bool String::operator==(const CharType *p_str) const {
return true;
}
+bool operator==(const char *p_chr, const String &p_str) {
+ return p_str == p_chr;
+}
+
+bool operator==(const wchar_t *p_chr, const String &p_str) {
+#ifdef WINDOWS_ENABLED
+ // wchar_t is 16-bit
+ return p_str == String::utf16((const char16_t *)p_chr);
+#else
+ // wchar_t is 32-bi
+ return p_str == String((const char32_t *)p_chr);
+#endif
+}
+
bool String::operator!=(const char *p_str) const {
return (!(*this == p_str));
}
-bool String::operator!=(const CharType *p_str) const {
+bool String::operator!=(const wchar_t *p_str) const {
+ return (!(*this == p_str));
+}
+
+bool String::operator!=(const char32_t *p_str) const {
return (!(*this == p_str));
}
-bool String::operator<(const CharType *p_str) const {
+bool String::operator!=(const String &p_str) const {
+ return !((*this == p_str));
+}
+
+bool String::operator<=(const String &p_str) const {
+ return (*this < p_str) || (*this == p_str);
+}
+
+bool String::operator<(const char *p_str) const {
if (empty() && p_str[0] == 0) {
return false;
}
if (empty()) {
return true;
}
-
- return is_str_less(c_str(), p_str);
+ return is_str_less(get_data(), p_str);
}
-bool String::operator<=(const String &p_str) const {
- return (*this < p_str) || (*this == p_str);
+bool String::operator<(const wchar_t *p_str) const {
+ if (empty() && p_str[0] == 0) {
+ return false;
+ }
+ if (empty()) {
+ return true;
+ }
+
+#ifdef WINDOWS_ENABLED
+ // wchar_t is 16-bit
+ return is_str_less(get_data(), String::utf16((const char16_t *)p_str).get_data());
+#else
+ // wchar_t is 32-bit
+ return is_str_less(get_data(), (const char32_t *)p_str);
+#endif
}
-bool String::operator<(const char *p_str) const {
+bool String::operator<(const char32_t *p_str) const {
if (empty() && p_str[0] == 0) {
return false;
}
@@ -432,11 +692,11 @@ bool String::operator<(const char *p_str) const {
return true;
}
- return is_str_less(c_str(), p_str);
+ return is_str_less(get_data(), p_str);
}
bool String::operator<(const String &p_str) const {
- return operator<(p_str.c_str());
+ return operator<(p_str.get_data());
}
signed char String::nocasecmp_to(const String &p_str) const {
@@ -450,8 +710,8 @@ signed char String::nocasecmp_to(const String &p_str) const {
return 1;
}
- const CharType *that_str = p_str.c_str();
- const CharType *this_str = c_str();
+ const char32_t *that_str = p_str.get_data();
+ const char32_t *this_str = get_data();
while (true) {
if (*that_str == 0 && *this_str == 0) {
@@ -482,8 +742,8 @@ signed char String::casecmp_to(const String &p_str) const {
return 1;
}
- const CharType *that_str = p_str.c_str();
- const CharType *this_str = c_str();
+ const char32_t *that_str = p_str.get_data();
+ const char32_t *this_str = get_data();
while (true) {
if (*that_str == 0 && *this_str == 0) {
@@ -504,8 +764,8 @@ signed char String::casecmp_to(const String &p_str) const {
}
signed char String::naturalnocasecmp_to(const String &p_str) const {
- const CharType *this_str = c_str();
- const CharType *that_str = p_str.c_str();
+ const char32_t *this_str = get_data();
+ const char32_t *that_str = p_str.get_data();
if (this_str && that_str) {
while (*this_str == '.' || *that_str == '.') {
@@ -527,29 +787,46 @@ signed char String::naturalnocasecmp_to(const String &p_str) const {
if (!*that_str) {
return 1;
} else if (IS_DIGIT(*this_str)) {
- int64_t this_int, that_int;
-
if (!IS_DIGIT(*that_str)) {
return -1;
}
- /* Compare the numbers */
- this_int = to_int(this_str, -1, true);
- that_int = to_int(that_str, -1, true);
-
- if (this_int < that_int) {
- return -1;
- } else if (this_int > that_int) {
- return 1;
- }
+ // Keep ptrs to start of numerical sequences
+ const char32_t *this_substr = this_str;
+ const char32_t *that_substr = that_str;
- /* Skip */
+ // Compare lengths of both numerical sequences, ignoring leading zeros
while (IS_DIGIT(*this_str)) {
this_str++;
}
while (IS_DIGIT(*that_str)) {
that_str++;
}
+ while (*this_substr == '0') {
+ this_substr++;
+ }
+ while (*that_substr == '0') {
+ that_substr++;
+ }
+ int this_len = this_str - this_substr;
+ int that_len = that_str - that_substr;
+
+ if (this_len < that_len) {
+ return -1;
+ } else if (this_len > that_len) {
+ return 1;
+ }
+
+ // If lengths equal, compare lexicographically
+ while (this_substr != this_str && that_substr != that_str) {
+ if (*this_substr < *that_substr) {
+ return -1;
+ } else if (*this_substr > *that_substr) {
+ return 1;
+ }
+ this_substr++;
+ that_substr++;
+ }
} else if (IS_DIGIT(*that_str)) {
return 1;
} else {
@@ -571,6 +848,11 @@ signed char String::naturalnocasecmp_to(const String &p_str) const {
return 0;
}
+const char32_t *String::get_data() const {
+ static const char32_t zero = 0;
+ return size() ? &operator[](0) : &zero;
+}
+
void String::erase(int p_pos, int p_chars) {
*this = left(p_pos) + substr(p_pos + p_chars, length() - ((p_pos + p_chars)));
}
@@ -593,7 +875,7 @@ String String::capitalize() const {
}
String String::camelcase_to_underscore(bool lowercase) const {
- const CharType *cstr = c_str();
+ const char32_t *cstr = get_data();
String new_string;
const char A = 'A', Z = 'Z';
const char a = 'a', z = 'z';
@@ -705,7 +987,7 @@ String String::get_slice(String p_splitter, int p_slice) const {
return ""; //no find!
}
-String String::get_slicec(CharType p_splitter, int p_slice) const {
+String String::get_slicec(char32_t p_splitter, int p_slice) const {
if (empty()) {
return String();
}
@@ -714,7 +996,7 @@ String String::get_slicec(CharType p_splitter, int p_slice) const {
return String();
}
- const CharType *c = this->ptr();
+ const char32_t *c = this->ptr();
int i = 0;
int prev = 0;
int count = 0;
@@ -851,7 +1133,7 @@ Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty)
end = len;
}
if (p_allow_empty || (end > from)) {
- ret.push_back(String::to_double(&c_str()[from]));
+ ret.push_back(String::to_float(&get_data()[from]));
}
if (end == len) {
@@ -880,7 +1162,7 @@ Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_
}
if (p_allow_empty || (end > from)) {
- ret.push_back(String::to_double(&c_str()[from]));
+ ret.push_back(String::to_float(&get_data()[from]));
}
if (end == len) {
@@ -904,7 +1186,7 @@ Vector<int> String::split_ints(const String &p_splitter, bool p_allow_empty) con
end = len;
}
if (p_allow_empty || (end > from)) {
- ret.push_back(String::to_int(&c_str()[from], end - from));
+ ret.push_back(String::to_int(&get_data()[from], end - from));
}
if (end == len) {
@@ -933,7 +1215,7 @@ Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allo
}
if (p_allow_empty || (end > from)) {
- ret.push_back(String::to_int(&c_str()[from], end - from));
+ ret.push_back(String::to_int(&get_data()[from], end - from));
}
if (end == len) {
@@ -946,7 +1228,7 @@ Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allo
return ret;
}
-String String::join(Vector<String> parts) {
+String String::join(Vector<String> parts) const {
String ret;
for (int i = 0; i < parts.size(); ++i) {
if (i > 0) {
@@ -957,11 +1239,11 @@ String String::join(Vector<String> parts) {
return ret;
}
-CharType String::char_uppercase(CharType p_char) {
+char32_t String::char_uppercase(char32_t p_char) {
return _find_upper(p_char);
}
-CharType String::char_lowercase(CharType p_char) {
+char32_t String::char_lowercase(char32_t p_char) {
return _find_lower(p_char);
}
@@ -969,8 +1251,8 @@ String String::to_upper() const {
String upper = *this;
for (int i = 0; i < upper.size(); i++) {
- const CharType s = upper[i];
- const CharType t = _find_upper(s);
+ const char32_t s = upper[i];
+ const char32_t t = _find_upper(s);
if (s != t) { // avoid copy on write
upper[i] = t;
}
@@ -983,8 +1265,8 @@ String String::to_lower() const {
String lower = *this;
for (int i = 0; i < lower.size(); i++) {
- const CharType s = lower[i];
- const CharType t = _find_lower(s);
+ const char32_t s = lower[i];
+ const char32_t t = _find_lower(s);
if (s != t) { // avoid copy on write
lower[i] = t;
}
@@ -993,38 +1275,23 @@ String String::to_lower() const {
return lower;
}
-const CharType *String::c_str() const {
- static const CharType zero = 0;
-
- return size() ? &operator[](0) : &zero;
-}
-
-String String::md5(const uint8_t *p_md5) {
- return String::hex_encode_buffer(p_md5, 16);
-}
-
-String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
- static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
-
- String ret;
- char v[2] = { 0, 0 };
-
- for (int i = 0; i < p_len; i++) {
- v[0] = hex[p_buffer[i] >> 4];
- ret += v;
- v[0] = hex[p_buffer[i] & 0xF];
- ret += v;
- }
-
- return ret;
-}
-
-String String::chr(CharType p_char) {
- CharType c[2] = { p_char, 0 };
+String String::chr(char32_t p_char) {
+ char32_t c[2] = { p_char, 0 };
return String(c);
}
String String::num(double p_num, int p_decimals) {
+ if (Math::is_nan(p_num)) {
+ return "nan";
+ }
+
+ if (Math::is_inf(p_num)) {
+ if (signbit(p_num)) {
+ return "-inf";
+ } else {
+ return "inf";
+ }
+ }
#ifndef NO_USE_STDLIB
if (p_decimals > 16) {
@@ -1103,7 +1370,7 @@ String String::num(double p_num, int p_decimals) {
/* decimal part */
if (p_decimals > 0 || (p_decimals == -1 && (int)p_num != p_num)) {
- double dec = p_num - (float)((int)p_num);
+ double dec = p_num - (double)((int)p_num);
int digit = 0;
if (p_decimals > MAX_DIGITS)
@@ -1122,7 +1389,7 @@ String String::num(double p_num, int p_decimals) {
if (digit == MAX_DIGITS) //no point in going to infinite
break;
- if ((dec - (float)((int)dec)) < 1e-6)
+ if ((dec - (double)((int)dec)) < 1e-6)
break;
}
@@ -1156,7 +1423,7 @@ String String::num(double p_num, int p_decimals) {
s = "0";
else {
while (intn) {
- CharType num = '0' + (intn % 10);
+ char32_t num = '0' + (intn % 10);
intn /= 10;
s = num + s;
}
@@ -1185,7 +1452,7 @@ String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
}
String s;
s.resize(chars + 1);
- CharType *c = s.ptrw();
+ char32_t *c = s.ptrw();
c[chars] = 0;
n = p_num;
do {
@@ -1218,7 +1485,7 @@ String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
String s;
s.resize(chars + 1);
- CharType *c = s.ptrw();
+ char32_t *c = s.ptrw();
c[chars] = 0;
n = p_num;
do {
@@ -1237,6 +1504,18 @@ String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
}
String String::num_real(double p_num) {
+ if (Math::is_nan(p_num)) {
+ return "nan";
+ }
+
+ if (Math::is_inf(p_num)) {
+ if (signbit(p_num)) {
+ return "-inf";
+ } else {
+ return "inf";
+ }
+ }
+
String s;
String sd;
/* integer part */
@@ -1248,7 +1527,7 @@ String String::num_real(double p_num) {
/* decimal part */
if ((int)p_num != p_num) {
- double dec = p_num - (float)((int)p_num);
+ double dec = p_num - (double)((int)p_num);
int digit = 0;
int decimals = MAX_DIGITS;
@@ -1262,7 +1541,7 @@ String String::num_real(double p_num) {
dec_max = dec_max * 10 + 9;
digit++;
- if ((dec - (float)((int)dec)) < 1e-6) {
+ if ((dec - (double)((int)dec)) < 1e-6) {
break;
}
@@ -1299,7 +1578,7 @@ String String::num_real(double p_num) {
s = "0";
} else {
while (intn) {
- CharType num = '0' + (intn % 10);
+ char32_t num = '0' + (intn % 10);
intn /= 10;
s = num + s;
}
@@ -1313,6 +1592,17 @@ String String::num_real(double p_num) {
}
String String::num_scientific(double p_num) {
+ if (Math::is_nan(p_num)) {
+ return "nan";
+ }
+
+ if (Math::is_inf(p_num)) {
+ if (signbit(p_num)) {
+ return "-inf";
+ } else {
+ return "inf";
+ }
+ }
#ifndef NO_USE_STDLIB
char buf[256];
@@ -1342,6 +1632,26 @@ String String::num_scientific(double p_num) {
#endif
}
+String String::md5(const uint8_t *p_md5) {
+ return String::hex_encode_buffer(p_md5, 16);
+}
+
+String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
+ static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+
+ String ret;
+ char v[2] = { 0, 0 };
+
+ for (int i = 0; i < p_len; i++) {
+ v[0] = hex[p_buffer[i] >> 4];
+ ret += v;
+ v[0] = hex[p_buffer[i] & 0xF];
+ ret += v;
+ }
+
+ return ret;
+}
+
CharString String::ascii(bool p_allow_extended) const {
if (!length()) {
return CharString();
@@ -1351,7 +1661,13 @@ CharString String::ascii(bool p_allow_extended) const {
cs.resize(size());
for (int i = 0; i < size(); i++) {
- cs[i] = operator[](i);
+ char32_t c = operator[](i);
+ if ((c <= 0x7f) || (c <= 0xff && p_allow_extended)) {
+ cs[i] = c;
+ } else {
+ print_error("Unicode parsing error: Cannot represent " + num_int64(c, 16) + " as ASCII/Latin-1 character.");
+ cs[i] = 0x20;
+ }
}
return cs;
@@ -1365,7 +1681,7 @@ String String::utf8(const char *p_utf8, int p_len) {
}
bool String::parse_utf8(const char *p_utf8, int p_len) {
-#define _UNICERROR(m_err) print_line("Unicode parsing error: " + String(m_err) + ". Is the string valid UTF-8?");
+#define _UNICERROR(m_err) print_error("Unicode parsing error: " + String(m_err) + ". Is the string valid UTF-8?");
if (!p_utf8) {
return true;
@@ -1378,9 +1694,9 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
/* HANDLE BOM (Byte Order Mark) */
if (p_len < 0 || p_len >= 3) {
- bool has_bom = uint8_t(p_utf8[0]) == 0xEF && uint8_t(p_utf8[1]) == 0xBB && uint8_t(p_utf8[2]) == 0xBF;
+ bool has_bom = uint8_t(p_utf8[0]) == 0xef && uint8_t(p_utf8[1]) == 0xbb && uint8_t(p_utf8[2]) == 0xbf;
if (has_bom) {
- //just skip it
+ //8-bit encoding, byte order has no meaning in UTF-8, just skip it
if (p_len >= 0) {
p_len -= 3;
}
@@ -1399,24 +1715,19 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
/* Determine the number of characters in sequence */
if ((c & 0x80) == 0) {
skip = 0;
- } else if ((c & 0xE0) == 0xC0) {
+ } else if ((c & 0xe0) == 0xc0) {
skip = 1;
- } else if ((c & 0xF0) == 0xE0) {
+ } else if ((c & 0xf0) == 0xe0) {
skip = 2;
- } else if ((c & 0xF8) == 0xF0) {
+ } else if ((c & 0xf8) == 0xf0) {
skip = 3;
- } else if ((c & 0xFC) == 0xF8) {
- skip = 4;
- } else if ((c & 0xFE) == 0xFC) {
- skip = 5;
} else {
- _UNICERROR("invalid skip");
+ _UNICERROR("invalid skip at " + num_int64(cstr_size));
return true; //invalid utf8
}
- if (skip == 1 && (c & 0x1E) == 0) {
- //printf("overlong rejected\n");
- _UNICERROR("overlong rejected");
+ if (skip == 1 && (c & 0x1e) == 0) {
+ _UNICERROR("overlong rejected at " + num_int64(cstr_size));
return true; //reject overlong
}
@@ -1442,7 +1753,7 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
}
resize(str_size + 1);
- CharType *dst = ptrw();
+ char32_t *dst = ptrw();
dst[str_size] = 0;
while (cstr_size) {
@@ -1451,19 +1762,14 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
/* Determine the number of characters in sequence */
if ((*p_utf8 & 0x80) == 0) {
len = 1;
- } else if ((*p_utf8 & 0xE0) == 0xC0) {
+ } else if ((*p_utf8 & 0xe0) == 0xc0) {
len = 2;
- } else if ((*p_utf8 & 0xF0) == 0xE0) {
+ } else if ((*p_utf8 & 0xf0) == 0xe0) {
len = 3;
- } else if ((*p_utf8 & 0xF8) == 0xF0) {
+ } else if ((*p_utf8 & 0xf8) == 0xf0) {
len = 4;
- } else if ((*p_utf8 & 0xFC) == 0xF8) {
- len = 5;
- } else if ((*p_utf8 & 0xFE) == 0xFC) {
- len = 6;
} else {
_UNICERROR("invalid len");
-
return true; //invalid UTF8
}
@@ -1473,7 +1779,6 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
}
if (len == 2 && (*p_utf8 & 0x1E) == 0) {
- //printf("overlong rejected\n");
_UNICERROR("no space left");
return true; //reject overlong
}
@@ -1485,24 +1790,23 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
if (len == 1) {
unichar = *p_utf8;
} else {
- unichar = (0xFF >> (len + 1)) & *p_utf8;
+ unichar = (0xff >> (len + 1)) & *p_utf8;
for (int i = 1; i < len; i++) {
- if ((p_utf8[i] & 0xC0) != 0x80) {
+ if ((p_utf8[i] & 0xc0) != 0x80) {
_UNICERROR("invalid utf8");
return true; //invalid utf8
}
- if (unichar == 0 && i == 2 && ((p_utf8[i] & 0x7F) >> (7 - len)) == 0) {
+ if (unichar == 0 && i == 2 && ((p_utf8[i] & 0x7f) >> (7 - len)) == 0) {
_UNICERROR("invalid utf8 overlong");
return true; //no overlong
}
- unichar = (unichar << 6) | (p_utf8[i] & 0x3F);
+ unichar = (unichar << 6) | (p_utf8[i] & 0x3f);
}
}
-
- //printf("char %i, len %i\n",unichar,len);
- if (sizeof(wchar_t) == 2 && unichar > 0xFFFF) {
- unichar = ' '; //too long for windows
+ if (unichar >= 0xd800 && unichar <= 0xdfff) {
+ _UNICERROR("invalid code point");
+ return CharString();
}
*(dst++) = unichar;
@@ -1511,6 +1815,7 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
}
return false;
+#undef _UNICERROR
}
CharString String::utf8() const {
@@ -1519,7 +1824,7 @@ CharString String::utf8() const {
return CharString();
}
- const CharType *d = &operator[](0);
+ const char32_t *d = &operator[](0);
int fl = 0;
for (int i = 0; i < l; i++) {
uint32_t c = d[i];
@@ -1529,13 +1834,15 @@ CharString String::utf8() const {
fl += 2;
} else if (c <= 0xffff) { // 16 bits
fl += 3;
- } else if (c <= 0x001fffff) { // 21 bits
+ } else if (c <= 0x0010ffff) { // 21 bits
fl += 4;
-
- } else if (c <= 0x03ffffff) { // 26 bits
- fl += 5;
- } else if (c <= 0x7fffffff) { // 31 bits
- fl += 6;
+ } else {
+ print_error("Unicode parsing error: Invalid unicode codepoint " + num_int64(c, 16) + ".");
+ return CharString();
+ }
+ if (c >= 0xd800 && c <= 0xdfff) {
+ print_error("Unicode parsing error: Invalid unicode codepoint " + num_int64(c, 16) + ".");
+ return CharString();
}
}
@@ -1555,35 +1862,17 @@ CharString String::utf8() const {
if (c <= 0x7f) { // 7 bits.
APPEND_CHAR(c);
} else if (c <= 0x7ff) { // 11 bits
-
APPEND_CHAR(uint32_t(0xc0 | ((c >> 6) & 0x1f))); // Top 5 bits.
APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
} else if (c <= 0xffff) { // 16 bits
-
APPEND_CHAR(uint32_t(0xe0 | ((c >> 12) & 0x0f))); // Top 4 bits.
APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Middle 6 bits.
APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
- } else if (c <= 0x001fffff) { // 21 bits
-
+ } else { // 21 bits
APPEND_CHAR(uint32_t(0xf0 | ((c >> 18) & 0x07))); // Top 3 bits.
APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper middle 6 bits.
APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
- } else if (c <= 0x03ffffff) { // 26 bits
-
- APPEND_CHAR(uint32_t(0xf8 | ((c >> 24) & 0x03))); // Top 2 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Upper middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
- } else if (c <= 0x7fffffff) { // 31 bits
-
- APPEND_CHAR(uint32_t(0xfc | ((c >> 30) & 0x01))); // Top 1 bit.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 24) & 0x3f))); // Upper upper middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Lower upper middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper lower middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower lower middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
}
}
#undef APPEND_CHAR
@@ -1592,21 +1881,191 @@ CharString String::utf8() const {
return utf8s;
}
-/*
-String::String(CharType p_char) {
+String String::utf16(const char16_t *p_utf16, int p_len) {
+ String ret;
+ ret.parse_utf16(p_utf16, p_len);
+
+ return ret;
+}
+
+bool String::parse_utf16(const char16_t *p_utf16, int p_len) {
+#define _UNICERROR(m_err) print_error("Unicode parsing error: " + String(m_err) + ". Is the string valid UTF-16?");
+
+ if (!p_utf16) {
+ return true;
+ }
+
+ String aux;
+
+ int cstr_size = 0;
+ int str_size = 0;
+
+ /* HANDLE BOM (Byte Order Mark) */
+ bool byteswap = false; // assume correct endianness if no BOM found
+ if (p_len < 0 || p_len >= 1) {
+ bool has_bom = false;
+ if (uint16_t(p_utf16[0]) == 0xfeff) { // correct BOM, read as is
+ has_bom = true;
+ byteswap = false;
+ } else if (uint16_t(p_utf16[0]) == 0xfffe) { // backwards BOM, swap bytes
+ has_bom = true;
+ byteswap = true;
+ }
+ if (has_bom) {
+ if (p_len >= 0) {
+ p_len -= 1;
+ }
+ p_utf16 += 1;
+ }
+ }
+
+ {
+ const char16_t *ptrtmp = p_utf16;
+ const char16_t *ptrtmp_limit = &p_utf16[p_len];
+ int skip = 0;
+ while (ptrtmp != ptrtmp_limit && *ptrtmp) {
+ uint32_t c = (byteswap) ? BSWAP16(*ptrtmp) : *ptrtmp;
+ if (skip == 0) {
+ if ((c & 0xfffffc00) == 0xd800) {
+ skip = 1; // lead surrogate
+ } else if ((c & 0xfffffc00) == 0xdc00) {
+ _UNICERROR("invalid utf16 surrogate at " + num_int64(cstr_size));
+ return true; // invalid UTF16
+ } else {
+ skip = 0;
+ }
+ str_size++;
+ } else {
+ if ((c & 0xfffffc00) == 0xdc00) { // trail surrogate
+ --skip;
+ } else {
+ _UNICERROR("invalid utf16 surrogate at " + num_int64(cstr_size));
+ return true; // invalid UTF16
+ }
+ }
- shared=nullptr;
- copy_from(p_char);
+ cstr_size++;
+ ptrtmp++;
+ }
+
+ if (skip) {
+ _UNICERROR("no space left");
+ return true; // not enough space
+ }
+ }
+
+ if (str_size == 0) {
+ clear();
+ return false;
+ }
+
+ resize(str_size + 1);
+ char32_t *dst = ptrw();
+ dst[str_size] = 0;
+
+ while (cstr_size) {
+ int len = 0;
+ uint32_t c = (byteswap) ? BSWAP16(*p_utf16) : *p_utf16;
+
+ if ((c & 0xfffffc00) == 0xd800) {
+ len = 2;
+ } else {
+ len = 1;
+ }
+
+ if (len > cstr_size) {
+ _UNICERROR("no space left");
+ return true; //not enough space
+ }
+
+ uint32_t unichar = 0;
+ if (len == 1) {
+ unichar = c;
+ } else {
+ uint32_t c2 = (byteswap) ? BSWAP16(p_utf16[1]) : p_utf16[1];
+ unichar = (c << 10UL) + c2 - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
+ }
+
+ *(dst++) = unichar;
+ cstr_size -= len;
+ p_utf16 += len;
+ }
+
+ return false;
+#undef _UNICERROR
}
+Char16String String::utf16() const {
+ int l = length();
+ if (!l) {
+ return Char16String();
+ }
+
+ const char32_t *d = &operator[](0);
+ int fl = 0;
+ for (int i = 0; i < l; i++) {
+ uint32_t c = d[i];
+ if (c <= 0xffff) { // 16 bits.
+ fl += 1;
+ } else if (c <= 0x10ffff) { // 32 bits.
+ fl += 2;
+ } else {
+ print_error("Unicode parsing error: Invalid unicode codepoint " + num_int64(c, 16) + ".");
+ return Char16String();
+ }
+ if (c >= 0xd800 && c <= 0xdfff) {
+ print_error("Unicode parsing error: Invalid unicode codepoint " + num_int64(c, 16) + ".");
+ return Char16String();
+ }
+ }
+
+ Char16String utf16s;
+ if (fl == 0) {
+ return utf16s;
+ }
+
+ utf16s.resize(fl + 1);
+ uint16_t *cdst = (uint16_t *)utf16s.get_data();
+
+#define APPEND_CHAR(m_c) *(cdst++) = m_c
+
+ for (int i = 0; i < l; i++) {
+ uint32_t c = d[i];
+
+ if (c <= 0xffff) { // 16 bits.
+ APPEND_CHAR(c);
+ } else { // 32 bits.
+ APPEND_CHAR(uint32_t((c >> 10) + 0xd7c0)); // lead surrogate.
+ APPEND_CHAR(uint32_t((c & 0x3ff) | 0xdc00)); // trail surrogate.
+ }
+ }
+#undef APPEND_CHAR
+ *cdst = 0; //trailing zero
-*/
+ return utf16s;
+}
String::String(const char *p_str) {
copy_from(p_str);
}
-String::String(const CharType *p_str, int p_clip_to_len) {
+String::String(const wchar_t *p_str) {
+ copy_from(p_str);
+}
+
+String::String(const char32_t *p_str) {
+ copy_from(p_str);
+}
+
+String::String(const char *p_str, int p_clip_to_len) {
+ copy_from(p_str, p_clip_to_len);
+}
+
+String::String(const wchar_t *p_str, int p_clip_to_len) {
+ copy_from(p_str, p_clip_to_len);
+}
+
+String::String(const char32_t *p_str, int p_clip_to_len) {
copy_from(p_str, p_clip_to_len);
}
@@ -1614,7 +2073,6 @@ String::String(const StrRange &p_range) {
if (!p_range.c_str) {
return;
}
-
copy_from(p_range.c_str, p_range.len);
}
@@ -1623,7 +2081,7 @@ int64_t String::hex_to_int(bool p_with_prefix) const {
return 0;
}
- const CharType *s = ptr();
+ const char32_t *s = ptr();
int64_t sign = s[0] == '-' ? -1 : 1;
@@ -1641,7 +2099,7 @@ int64_t String::hex_to_int(bool p_with_prefix) const {
int64_t hex = 0;
while (*s) {
- CharType c = LOWERCASE(*s);
+ char32_t c = LOWERCASE(*s);
int64_t n;
if (c >= '0' && c <= '9') {
n = c - '0';
@@ -1666,7 +2124,7 @@ int64_t String::bin_to_int(bool p_with_prefix) const {
return 0;
}
- const CharType *s = ptr();
+ const char32_t *s = ptr();
int64_t sign = s[0] == '-' ? -1 : 1;
@@ -1684,7 +2142,7 @@ int64_t String::bin_to_int(bool p_with_prefix) const {
int64_t binary = 0;
while (*s) {
- CharType c = LOWERCASE(*s);
+ char32_t c = LOWERCASE(*s);
int64_t n;
if (c == '0' || c == '1') {
n = c - '0';
@@ -1713,7 +2171,7 @@ int64_t String::to_int() const {
int64_t sign = 1;
for (int i = 0; i < to; i++) {
- CharType c = operator[](i);
+ char32_t c = operator[](i);
if (c >= '0' && c <= '9') {
bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
@@ -1759,6 +2217,37 @@ int64_t String::to_int(const char *p_str, int p_len) {
return integer * sign;
}
+int64_t String::to_int(const wchar_t *p_str, int p_len) {
+ int to = 0;
+ if (p_len >= 0) {
+ to = p_len;
+ } else {
+ while (p_str[to] != 0 && p_str[to] != '.') {
+ to++;
+ }
+ }
+
+ int64_t integer = 0;
+ int64_t sign = 1;
+
+ for (int i = 0; i < to; i++) {
+ wchar_t c = p_str[i];
+ if (c >= '0' && c <= '9') {
+ bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
+ ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
+ integer *= 10;
+ integer += c - '0';
+
+ } else if (c == '-' && integer == 0) {
+ sign = -sign;
+ } else if (c != ' ') {
+ break;
+ }
+ }
+
+ return integer * sign;
+}
+
bool String::is_numeric() const {
if (length() == 0) {
return false;
@@ -1770,14 +2259,13 @@ bool String::is_numeric() const {
}
bool dot = false;
for (int i = s; i < length(); i++) {
- CharType c = operator[](i);
+ char32_t c = operator[](i);
if (c == '.') {
if (dot) {
return false;
}
dot = true;
- }
- if (c < '0' || c > '9') {
+ } else if (c < '0' || c > '9') {
return false;
}
}
@@ -1939,11 +2427,11 @@ static double built_in_strtod(const C *string, /* A decimal ASCII floating-point
}
expSign = false;
}
- if (!IS_DIGIT(CharType(*p))) {
+ if (!IS_DIGIT(char32_t(*p))) {
p = pExp;
goto done;
}
- while (IS_DIGIT(CharType(*p))) {
+ while (IS_DIGIT(char32_t(*p))) {
exp = exp * 10 + (*p - '0');
p += 1;
}
@@ -2000,24 +2488,19 @@ done:
#define READING_EXP 3
#define READING_DONE 4
-double String::to_double(const char *p_str) {
-#ifndef NO_USE_STDLIB
- return built_in_strtod<char>(p_str);
-//return atof(p_str); DOES NOT WORK ON ANDROID(??)
-#else
+double String::to_float(const char *p_str) {
return built_in_strtod<char>(p_str);
-#endif
}
-float String::to_float() const {
- return to_double();
+double String::to_float(const char32_t *p_str, const char32_t **r_end) {
+ return built_in_strtod<char32_t>(p_str, (char32_t **)r_end);
}
-double String::to_double(const CharType *p_str, const CharType **r_end) {
- return built_in_strtod<CharType>(p_str, (CharType **)r_end);
+double String::to_float(const wchar_t *p_str, const wchar_t **r_end) {
+ return built_in_strtod<wchar_t>(p_str, (wchar_t **)r_end);
}
-int64_t String::to_int(const CharType *p_str, int p_len, bool p_clamp) {
+int64_t String::to_int(const char32_t *p_str, int p_len, bool p_clamp) {
if (p_len == 0 || !p_str[0]) {
return 0;
}
@@ -2027,11 +2510,11 @@ int64_t String::to_int(const CharType *p_str, int p_len, bool p_clamp) {
int64_t sign = 1;
int reading = READING_SIGN;
- const CharType *str = p_str;
- const CharType *limit = &p_str[p_len];
+ const char32_t *str = p_str;
+ const char32_t *limit = &p_str[p_len];
while (*str && reading != READING_DONE && str != limit) {
- CharType c = *(str++);
+ char32_t c = *(str++);
switch (reading) {
case READING_SIGN: {
if (c >= '0' && c <= '9') {
@@ -2081,30 +2564,11 @@ int64_t String::to_int(const CharType *p_str, int p_len, bool p_clamp) {
return sign * integer;
}
-double String::to_double() const {
+double String::to_float() const {
if (empty()) {
return 0;
}
-#ifndef NO_USE_STDLIB
- return built_in_strtod<CharType>(c_str());
-//return wcstod(c_str(),nullptr ); DOES NOT WORK ON ANDROID :(
-#else
- return built_in_strtod<CharType>(c_str());
-#endif
-}
-
-bool operator==(const char *p_chr, const String &p_str) {
- return p_str == p_chr;
-}
-
-String operator+(const char *p_chr, const String &p_str) {
- String tmp = p_chr;
- tmp += p_str;
- return tmp;
-}
-
-String operator+(CharType p_chr, const String &p_str) {
- return (String::chr(p_chr) + p_str);
+ return built_in_strtod<char32_t>(get_data());
}
uint32_t String::hash(const char *p_cstr) {
@@ -2127,7 +2591,27 @@ uint32_t String::hash(const char *p_cstr, int p_len) {
return hashv;
}
-uint32_t String::hash(const CharType *p_cstr, int p_len) {
+uint32_t String::hash(const wchar_t *p_cstr, int p_len) {
+ uint32_t hashv = 5381;
+ for (int i = 0; i < p_len; i++) {
+ hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
+ }
+
+ return hashv;
+}
+
+uint32_t String::hash(const wchar_t *p_cstr) {
+ uint32_t hashv = 5381;
+ uint32_t c;
+
+ while ((c = *p_cstr++)) {
+ hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
+ }
+
+ return hashv;
+}
+
+uint32_t String::hash(const char32_t *p_cstr, int p_len) {
uint32_t hashv = 5381;
for (int i = 0; i < p_len; i++) {
hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
@@ -2136,7 +2620,7 @@ uint32_t String::hash(const CharType *p_cstr, int p_len) {
return hashv;
}
-uint32_t String::hash(const CharType *p_cstr) {
+uint32_t String::hash(const char32_t *p_cstr) {
uint32_t hashv = 5381;
uint32_t c;
@@ -2150,7 +2634,7 @@ uint32_t String::hash(const CharType *p_cstr) {
uint32_t String::hash() const {
/* simple djb2 hashing */
- const CharType *chr = c_str();
+ const char32_t *chr = get_data();
uint32_t hashv = 5381;
uint32_t c;
@@ -2164,7 +2648,7 @@ uint32_t String::hash() const {
uint64_t String::hash64() const {
/* simple djb2 hashing */
- const CharType *chr = c_str();
+ const char32_t *chr = get_data();
uint64_t hashv = 5381;
uint64_t c;
@@ -2276,7 +2760,7 @@ String String::substr(int p_from, int p_chars) const {
}
String s = String();
- s.copy_from_unchecked(&c_str()[p_from], p_chars);
+ s.copy_from_unchecked(&get_data()[p_from], p_chars);
return s;
}
@@ -2293,8 +2777,8 @@ int String::find(const String &p_str, int p_from) const {
return -1; // won't find anything!
}
- const CharType *src = c_str();
- const CharType *str = p_str.c_str();
+ const char32_t *src = get_data();
+ const char32_t *str = p_str.get_data();
for (int i = p_from; i <= (len - src_len); i++) {
bool found = true;
@@ -2331,7 +2815,7 @@ int String::find(const char *p_str, int p_from) const {
return -1; // won't find anything!
}
- const CharType *src = c_str();
+ const char32_t *src = get_data();
int src_len = 0;
while (p_str[src_len] != '\0') {
@@ -2339,7 +2823,7 @@ int String::find(const char *p_str, int p_from) const {
}
if (src_len == 1) {
- const char needle = p_str[0];
+ const char32_t needle = p_str[0];
for (int i = p_from; i < len; i++) {
if (src[i] == needle) {
@@ -2358,7 +2842,7 @@ int String::find(const char *p_str, int p_from) const {
return -1;
}
- if (src[read_pos] != p_str[j]) {
+ if (src[read_pos] != (char32_t)p_str[j]) {
found = false;
break;
}
@@ -2373,7 +2857,7 @@ int String::find(const char *p_str, int p_from) const {
return -1;
}
-int String::find_char(const CharType &p_char, int p_from) const {
+int String::find_char(const char32_t &p_char, int p_from) const {
return _cowdata.find(p_char, p_from);
}
@@ -2394,7 +2878,7 @@ int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
return -1; // won't find anything!
}
- const CharType *src = c_str();
+ const char32_t *src = get_data();
for (int i = p_from; i < len; i++) {
bool found = true;
@@ -2403,7 +2887,7 @@ int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
if (r_key) {
*r_key = k;
}
- const CharType *cmp = keys[k].c_str();
+ const char32_t *cmp = keys[k].get_data();
int l = keys[k].length();
for (int j = 0; j < l; j++) {
@@ -2443,7 +2927,7 @@ int String::findn(const String &p_str, int p_from) const {
return -1; // won't find anything!
}
- const CharType *srcd = c_str();
+ const char32_t *srcd = get_data();
for (int i = p_from; i <= (length() - src_len); i++) {
bool found = true;
@@ -2455,8 +2939,8 @@ int String::findn(const String &p_str, int p_from) const {
return -1;
}
- CharType src = _find_lower(srcd[read_pos]);
- CharType dst = _find_lower(p_str[j]);
+ char32_t src = _find_lower(srcd[read_pos]);
+ char32_t dst = _find_lower(p_str[j]);
if (src != dst) {
found = false;
@@ -2493,7 +2977,7 @@ int String::rfind(const String &p_str, int p_from) const {
return -1; // won't find anything!
}
- const CharType *src = c_str();
+ const char32_t *src = get_data();
for (int i = p_from; i >= 0; i--) {
bool found = true;
@@ -2540,7 +3024,7 @@ int String::rfindn(const String &p_str, int p_from) const {
return -1; // won't find anything!
}
- const CharType *src = c_str();
+ const char32_t *src = get_data();
for (int i = p_from; i >= 0; i--) {
bool found = true;
@@ -2552,8 +3036,8 @@ int String::rfindn(const String &p_str, int p_from) const {
return -1;
}
- CharType srcc = _find_lower(src[read_pos]);
- CharType dstc = _find_lower(p_str[j]);
+ char32_t srcc = _find_lower(src[read_pos]);
+ char32_t dstc = _find_lower(p_str[j]);
if (srcc != dstc) {
found = false;
@@ -2587,8 +3071,8 @@ bool String::begins_with(const String &p_string) const {
return true;
}
- const CharType *src = &p_string[0];
- const CharType *str = &operator[](0);
+ const char32_t *src = &p_string[0];
+ const char32_t *str = &operator[](0);
int i = 0;
for (; i < l; i++) {
@@ -2607,11 +3091,11 @@ bool String::begins_with(const char *p_string) const {
return false;
}
- const CharType *str = &operator[](0);
+ const char32_t *str = &operator[](0);
int i = 0;
while (*p_string && i < l) {
- if (*p_string != str[i]) {
+ if ((char32_t)*p_string != str[i]) {
return false;
}
i++;
@@ -2655,7 +3139,7 @@ int String::_count(const String &p_string, int p_from, int p_to, bool p_case_ins
}
if (p_from == 0 && p_to == len) {
str = String();
- str.copy_from_unchecked(&c_str()[0], len);
+ str.copy_from_unchecked(&get_data()[0], len);
} else {
str = substr(p_from, p_to - p_from);
}
@@ -2693,14 +3177,14 @@ bool String::_base_is_subsequence_of(const String &p_string, bool case_insensiti
return false;
}
- const CharType *src = &operator[](0);
- const CharType *tgt = &p_string[0];
+ const char32_t *src = &operator[](0);
+ const char32_t *tgt = &p_string[0];
for (; *src && *tgt; tgt++) {
bool match = false;
if (case_insensitive) {
- CharType srcc = _find_lower(*src);
- CharType tgtc = _find_lower(*tgt);
+ char32_t srcc = _find_lower(*src);
+ char32_t tgtc = _find_lower(*tgt);
match = srcc == tgtc;
} else {
match = *src == *tgt;
@@ -2746,8 +3230,8 @@ float String::similarity(const String &p_string) const {
int src_size = src_bigrams.size();
int tgt_size = tgt_bigrams.size();
- float sum = src_size + tgt_size;
- float inter = 0;
+ double sum = src_size + tgt_size;
+ double inter = 0;
for (int i = 0; i < src_size; i++) {
for (int j = 0; j < tgt_size; j++) {
if (src_bigrams[i] == tgt_bigrams[j]) {
@@ -2760,7 +3244,7 @@ float String::similarity(const String &p_string) const {
return (2.0f * inter) / sum;
}
-static bool _wildcard_match(const CharType *p_pattern, const CharType *p_string, bool p_case_sensitive) {
+static bool _wildcard_match(const char32_t *p_pattern, const char32_t *p_string, bool p_case_sensitive) {
switch (*p_pattern) {
case '\0':
return !*p_string;
@@ -2779,14 +3263,14 @@ bool String::match(const String &p_wildcard) const {
return false;
}
- return _wildcard_match(p_wildcard.c_str(), c_str(), true);
+ return _wildcard_match(p_wildcard.get_data(), get_data(), true);
}
bool String::matchn(const String &p_wildcard) const {
if (!p_wildcard.length() || !length()) {
return false;
}
- return _wildcard_match(p_wildcard.c_str(), c_str(), false);
+ return _wildcard_match(p_wildcard.get_data(), get_data(), false);
}
String String::format(const Variant &values, String placeholder) const {
@@ -2936,9 +3420,10 @@ String String::repeat(int p_count) const {
ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number.");
String new_string;
- const CharType *src = this->c_str();
+ const char32_t *src = this->get_data();
new_string.resize(length() * p_count + 1);
+ new_string[length() * p_count] = 0;
for (int i = 0; i < p_count; i++) {
for (int j = 0; j < length(); j++) {
@@ -2973,7 +3458,7 @@ String String::right(int p_pos) const {
return substr(p_pos, (length() - p_pos));
}
-CharType String::ord_at(int p_idx) const {
+char32_t String::ord_at(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, length(), 0);
return operator[](p_idx);
}
@@ -2987,7 +3472,7 @@ String String::dedent() const {
int indent_stop = -1;
for (int i = 0; i < length(); i++) {
- CharType c = operator[](i);
+ char32_t c = operator[](i);
if (c == '\n') {
if (has_text) {
new_string += substr(indent_stop, i - indent_stop);
@@ -3216,7 +3701,7 @@ bool String::is_valid_identifier() const {
return false;
}
- const wchar_t *str = &operator[](0);
+ const char32_t *str = &operator[](0);
for (int i = 0; i < len; i++) {
if (i == 0) {
@@ -3235,36 +3720,14 @@ bool String::is_valid_identifier() const {
return true;
}
-//kind of poor should be rewritten properly
-
-String String::word_wrap(int p_chars_per_line) const {
- int from = 0;
- int last_space = 0;
- String ret;
- for (int i = 0; i < length(); i++) {
- if (i - from >= p_chars_per_line) {
- if (last_space == -1) {
- ret += substr(from, i - from + 1) + "\n";
- } else {
- ret += substr(from, last_space - from) + "\n";
- i = last_space; //rewind
- }
- from = i + 1;
- last_space = -1;
- } else if (operator[](i) == ' ' || operator[](i) == '\t') {
- last_space = i;
- } else if (operator[](i) == '\n') {
- ret += substr(from, i - from) + "\n";
- from = i + 1;
- last_space = -1;
- }
- }
-
- if (from < length()) {
- ret += substr(from, length());
+bool String::is_valid_string() const {
+ int l = length();
+ const char32_t *src = get_data();
+ bool valid = true;
+ for (int i = 0; i < l; i++) {
+ valid = valid && (src[i] < 0xd800 || (src[i] > 0xdfff && src[i] <= 0x10ffff));
}
-
- return ret;
+ return valid;
}
String String::http_escape() const {
@@ -3295,9 +3758,9 @@ String String::http_unescape() const {
String res;
for (int i = 0; i < length(); ++i) {
if (ord_at(i) == '%' && i + 2 < length()) {
- CharType ord1 = ord_at(i + 1);
+ char32_t ord1 = ord_at(i + 1);
if ((ord1 >= '0' && ord1 <= '9') || (ord1 >= 'A' && ord1 <= 'Z')) {
- CharType ord2 = ord_at(i + 2);
+ char32_t ord2 = ord_at(i + 2);
if ((ord2 >= '0' && ord2 <= '9') || (ord2 >= 'A' && ord2 <= 'Z')) {
char bytes[3] = { (char)ord1, (char)ord2, 0 };
res += (char)strtol(bytes, nullptr, 16);
@@ -3387,18 +3850,18 @@ for (int i=1;i<32;i++) {
return str;
}
-static _FORCE_INLINE_ int _xml_unescape(const CharType *p_src, int p_src_len, CharType *p_dst) {
+static _FORCE_INLINE_ int _xml_unescape(const char32_t *p_src, int p_src_len, char32_t *p_dst) {
int len = 0;
while (p_src_len) {
if (*p_src == '&') {
int eat = 0;
if (p_src_len >= 4 && p_src[1] == '#') {
- CharType c = 0;
+ char32_t c = 0;
for (int i = 2; i < p_src_len; i++) {
eat = i + 1;
- CharType ct = p_src[i];
+ char32_t ct = p_src[i];
if (ct == ';') {
break;
} else if (ct >= '0' && ct <= '9') {
@@ -3474,12 +3937,12 @@ static _FORCE_INLINE_ int _xml_unescape(const CharType *p_src, int p_src_len, Ch
String String::xml_unescape() const {
String str;
int l = length();
- int len = _xml_unescape(c_str(), l, nullptr);
+ int len = _xml_unescape(get_data(), l, nullptr);
if (len == 0) {
return String();
}
str.resize(len + 1);
- _xml_unescape(c_str(), l, str.ptrw());
+ _xml_unescape(get_data(), l, str.ptrw());
str[len] = 0;
return str;
}
@@ -3600,7 +4063,7 @@ bool String::is_valid_hex_number(bool p_with_prefix) const {
}
for (int i = from; i < len; i++) {
- CharType c = operator[](i);
+ char32_t c = operator[](i);
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
continue;
}
@@ -3915,7 +4378,7 @@ String String::percent_decode() const {
String String::property_name_encode() const {
// Escape and quote strings with extended ASCII or further Unicode characters
// as well as '"', '=' or ' ' (32)
- const CharType *cstr = c_str();
+ const char32_t *cstr = get_data();
for (int i = 0; cstr[i]; i++) {
if (cstr[i] == '=' || cstr[i] == '"' || cstr[i] < 33 || cstr[i] > 126) {
return "\"" + c_escape_multiline() + "\"";
@@ -3982,7 +4445,7 @@ String String::lpad(int min_length, const String &character) const {
// In case of an error, the string returned is the error description and "error" is true.
String String::sprintf(const Array &values, bool *error) const {
String formatted;
- CharType *self = (CharType *)c_str();
+ char32_t *self = (char32_t *)get_data();
bool in_format = false;
int value_index = 0;
int min_chars = 0;
@@ -3995,7 +4458,7 @@ String String::sprintf(const Array &values, bool *error) const {
*error = true;
for (; *self; self++) {
- const CharType c = *self;
+ const char32_t c = *self;
if (in_format) { // We have % - lets see what else we get.
switch (c) {
@@ -4038,11 +4501,12 @@ String String::sprintf(const Array &values, bool *error) const {
int number_len = str.length();
// Padding.
+ int pad_chars_count = (value < 0 || show_sign) ? min_chars - 1 : min_chars;
String pad_char = pad_with_zeroes ? String("0") : String(" ");
if (left_justified) {
- str = str.rpad(min_chars, pad_char);
+ str = str.rpad(pad_chars_count, pad_char);
} else {
- str = str.lpad(min_chars, pad_char);
+ str = str.lpad(pad_chars_count, pad_char);
}
// Sign.
@@ -4132,9 +4596,11 @@ String String::sprintf(const Array &values, bool *error) const {
if (values[value_index].is_num()) {
int value = values[value_index];
if (value < 0) {
- return "unsigned byte integer is lower than maximum";
- } else if (value > 255) {
- return "unsigned byte integer is greater than maximum";
+ return "unsigned integer is lower than minimum";
+ } else if (value >= 0xd800 && value <= 0xdfff) {
+ return "unsigned integer is invalid Unicode character";
+ } else if (value > 0x10ffff) {
+ return "unsigned integer is greater than maximum";
}
str = chr(values[value_index]);
} else if (values[value_index].get_type() == Variant::STRING) {
@@ -4266,32 +4732,122 @@ String String::unquote() const {
return substr(1, length() - 2);
}
+Vector<uint8_t> String::to_ascii_buffer() const {
+ const String *s = this;
+ if (s->empty()) {
+ return Vector<uint8_t>();
+ }
+ CharString charstr = s->ascii();
+
+ Vector<uint8_t> retval;
+ size_t len = charstr.length();
+ retval.resize(len);
+ uint8_t *w = retval.ptrw();
+ copymem(w, charstr.ptr(), len);
+
+ return retval;
+}
+
+Vector<uint8_t> String::to_utf8_buffer() const {
+ const String *s = this;
+ if (s->empty()) {
+ return Vector<uint8_t>();
+ }
+ CharString charstr = s->utf8();
+
+ Vector<uint8_t> retval;
+ size_t len = charstr.length();
+ retval.resize(len);
+ uint8_t *w = retval.ptrw();
+ copymem(w, charstr.ptr(), len);
+
+ return retval;
+}
+
+Vector<uint8_t> String::to_utf16_buffer() const {
+ const String *s = this;
+ if (s->empty()) {
+ return Vector<uint8_t>();
+ }
+ Char16String charstr = s->utf16();
+
+ Vector<uint8_t> retval;
+ size_t len = charstr.length() * 2;
+ retval.resize(len);
+ uint8_t *w = retval.ptrw();
+ copymem(w, (const void *)charstr.ptr(), len);
+
+ return retval;
+}
+
+Vector<uint8_t> String::to_utf32_buffer() const {
+ const String *s = this;
+ if (s->empty()) {
+ return Vector<uint8_t>();
+ }
+
+ Vector<uint8_t> retval;
+ size_t len = s->length() * 4;
+ retval.resize(len);
+ uint8_t *w = retval.ptrw();
+ copymem(w, (const void *)s->ptr(), len);
+
+ return retval;
+}
+
#ifdef TOOLS_ENABLED
-String TTR(const String &p_text) {
+String TTR(const String &p_text, const String &p_context) {
if (TranslationServer::get_singleton()) {
- return TranslationServer::get_singleton()->tool_translate(p_text);
+ return TranslationServer::get_singleton()->tool_translate(p_text, p_context);
}
return p_text;
}
-String DTR(const String &p_text) {
+String TTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
+ if (TranslationServer::get_singleton()) {
+ return TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
+ }
+
+ // Return message based on English plural rule if translation is not possible.
+ if (p_n == 1) {
+ return p_text;
+ }
+ return p_text_plural;
+}
+
+String DTR(const String &p_text, const String &p_context) {
// Comes straight from the XML, so remove indentation and any trailing whitespace.
const String text = p_text.dedent().strip_edges();
if (TranslationServer::get_singleton()) {
- return TranslationServer::get_singleton()->doc_translate(text);
+ return TranslationServer::get_singleton()->doc_translate(text, p_context);
}
return text;
}
+
+String DTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
+ const String text = p_text.dedent().strip_edges();
+ const String text_plural = p_text_plural.dedent().strip_edges();
+
+ if (TranslationServer::get_singleton()) {
+ return TranslationServer::get_singleton()->doc_translate_plural(text, text_plural, p_n, p_context);
+ }
+
+ // Return message based on English plural rule if translation is not possible.
+ if (p_n == 1) {
+ return text;
+ }
+ return text_plural;
+}
#endif
-String RTR(const String &p_text) {
+String RTR(const String &p_text, const String &p_context) {
if (TranslationServer::get_singleton()) {
- String rtr = TranslationServer::get_singleton()->tool_translate(p_text);
+ String rtr = TranslationServer::get_singleton()->tool_translate(p_text, p_context);
if (rtr == String() || rtr == p_text) {
- return TranslationServer::get_singleton()->translate(p_text);
+ return TranslationServer::get_singleton()->translate(p_text, p_context);
} else {
return rtr;
}
@@ -4299,3 +4855,20 @@ String RTR(const String &p_text) {
return p_text;
}
+
+String RTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
+ if (TranslationServer::get_singleton()) {
+ String rtr = TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
+ if (rtr == String() || rtr == p_text || rtr == p_text_plural) {
+ return TranslationServer::get_singleton()->translate_plural(p_text, p_text_plural, p_n, p_context);
+ } else {
+ return rtr;
+ }
+ }
+
+ // Return message based on English plural rule if translation is not possible.
+ if (p_n == 1) {
+ return p_text;
+ }
+ return p_text_plural;
+}