summaryrefslogtreecommitdiff
path: root/core/ustring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r--core/ustring.cpp1319
1 files changed, 589 insertions, 730 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp
index fbe3fcb1b2..957caf1015 100644
--- a/core/ustring.cpp
+++ b/core/ustring.cpp
@@ -28,10 +28,6 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
-#ifdef _MSC_VER
-#define _CRT_SECURE_NO_WARNINGS // to disable build-time warning which suggested to use strcpy_s instead strcpy
-#endif
-
#include "ustring.h"
#include "core/color.h"
@@ -51,6 +47,10 @@
#include <stdlib.h>
#endif
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS // to disable build-time warning which suggested to use strcpy_s instead strcpy
+#endif
+
#if defined(MINGW_ENABLED) || defined(_MSC_VER)
#define snprintf _snprintf_s
#endif
@@ -69,13 +69,11 @@ bool is_symbol(CharType c) {
}
bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) {
-
const String &s = p_s;
int beg = CLAMP(p_col, 0, s.length());
int end = beg;
if (s[beg] > 32 || beg == s.length()) {
-
bool symbol = beg < s.length() && is_symbol(s[beg]);
while (beg > 0 && s[beg - 1] > 32 && (symbol == is_symbol(s[beg - 1]))) {
@@ -85,15 +83,15 @@ bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) {
end++;
}
- if (end < s.length())
+ if (end < s.length()) {
end += 1;
+ }
r_beg = beg;
r_end = end;
return true;
} else {
-
return false;
}
}
@@ -101,7 +99,6 @@ bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) {
/** STRING **/
bool CharString::operator<(const CharString &p_right) const {
-
if (length() == 0) {
return p_right.length() != 0;
}
@@ -110,7 +107,6 @@ bool CharString::operator<(const CharString &p_right) const {
}
CharString &CharString::operator+=(char p_char) {
-
resize(size() ? size() + 1 : 2);
set(length(), 0);
set(length() - 1, p_char);
@@ -119,21 +115,19 @@ CharString &CharString::operator+=(char p_char) {
}
const char *CharString::get_data() const {
-
- if (size())
+ if (size()) {
return &operator[](0);
- else
+ } else {
return "";
+ }
}
CharString &CharString::operator=(const char *p_cstr) {
-
copy_from(p_cstr);
return *this;
}
void CharString::copy_from(const char *p_cstr) {
-
if (!p_cstr) {
resize(0);
return;
@@ -154,20 +148,18 @@ void CharString::copy_from(const char *p_cstr) {
}
void String::copy_from(const char *p_cstr) {
-
if (!p_cstr) {
-
resize(0);
return;
}
int len = 0;
const char *ptr = p_cstr;
- while (*(ptr++) != 0)
+ while (*(ptr++) != 0) {
len++;
+ }
if (len == 0) {
-
resize(0);
return;
}
@@ -177,26 +169,23 @@ void String::copy_from(const char *p_cstr) {
CharType *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) {
-
if (!p_cstr) {
-
resize(0);
return;
}
int len = 0;
const CharType *ptr = p_cstr;
- while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0)
+ while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
len++;
+ }
if (len == 0) {
-
resize(0);
return;
}
@@ -220,18 +209,18 @@ void String::copy_from_unchecked(const CharType *p_char, const int p_length) {
}
void String::copy_from(const CharType &p_char) {
-
resize(2);
set(0, p_char);
set(1, 0);
}
bool String::operator==(const String &p_str) const {
-
- if (length() != p_str.length())
+ if (length() != p_str.length()) {
return false;
- if (empty())
+ }
+ if (empty()) {
return true;
+ }
int l = length();
@@ -240,43 +229,33 @@ bool String::operator==(const String &p_str) const {
/* Compare char by char */
for (int i = 0; i < l; i++) {
-
- if (src[i] != dst[i])
+ if (src[i] != dst[i]) {
return false;
+ }
}
return true;
}
bool String::operator!=(const String &p_str) const {
-
return !(*this == p_str);
}
String String::operator+(const String &p_str) const {
-
String res = *this;
res += p_str;
return res;
}
-/*
-String String::operator+(CharType p_chr) const {
-
- String res=*this;
- res+=p_chr;
- return res;
-}
-*/
String &String::operator+=(const String &p_str) {
-
if (empty()) {
*this = p_str;
return *this;
}
- if (p_str.empty())
+ if (p_str.empty()) {
return *this;
+ }
int from = length();
@@ -287,20 +266,19 @@ String &String::operator+=(const String &p_str) {
set(length(), 0);
- for (int i = 0; i < p_str.length(); i++)
+ for (int i = 0; i < p_str.length(); i++) {
dst[from + i] = src[i];
+ }
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);
@@ -309,14 +287,15 @@ String &String::operator+=(CharType p_char) {
}
String &String::operator+=(const char *p_str) {
-
- if (!p_str || p_str[0] == 0)
+ if (!p_str || p_str[0] == 0) {
return *this;
+ }
int src_len = 0;
const char *ptr = p_str;
- while (*(ptr++) != 0)
+ while (*(ptr++) != 0) {
src_len++;
+ }
int from = length();
@@ -326,56 +305,58 @@ String &String::operator+=(const char *p_str) {
set(length(), 0);
- for (int i = 0; i < src_len; i++)
+ for (int i = 0; i < src_len; i++) {
dst[from + i] = p_str[i];
+ }
return *this;
}
void String::operator=(const char *p_str) {
-
copy_from(p_str);
}
void String::operator=(const CharType *p_str) {
-
copy_from(p_str);
}
bool String::operator==(const StrRange &p_str_range) const {
-
int len = p_str_range.len;
- if (length() != len)
+ if (length() != len) {
return false;
- if (empty())
+ }
+ if (empty()) {
return true;
+ }
const CharType *c_str = p_str_range.c_str;
const CharType *dst = &operator[](0);
/* Compare char by char */
for (int i = 0; i < len; i++) {
-
- if (c_str[i] != dst[i])
+ if (c_str[i] != dst[i]) {
return false;
+ }
}
return true;
}
bool String::operator==(const char *p_str) const {
-
int len = 0;
const char *aux = p_str;
- while (*(aux++) != 0)
+ while (*(aux++) != 0) {
len++;
+ }
- if (length() != len)
+ if (length() != len) {
return false;
- if (empty())
+ }
+ if (empty()) {
return true;
+ }
int l = length();
@@ -383,26 +364,28 @@ bool String::operator==(const char *p_str) const {
/* Compare char by char */
for (int i = 0; i < l; i++) {
-
- if (p_str[i] != dst[i])
+ if (p_str[i] != dst[i]) {
return false;
+ }
}
return true;
}
bool String::operator==(const CharType *p_str) const {
-
int len = 0;
const CharType *aux = p_str;
- while (*(aux++) != 0)
+ while (*(aux++) != 0) {
len++;
+ }
- if (length() != len)
+ if (length() != len) {
return false;
- if (empty())
+ }
+ if (empty()) {
return true;
+ }
int l = length();
@@ -410,78 +393,78 @@ bool String::operator==(const CharType *p_str) const {
/* Compare char by char */
for (int i = 0; i < l; i++) {
-
- if (p_str[i] != dst[i])
+ if (p_str[i] != dst[i]) {
return false;
+ }
}
return true;
}
bool String::operator!=(const char *p_str) const {
-
return (!(*this == p_str));
}
bool String::operator!=(const CharType *p_str) const {
-
return (!(*this == p_str));
}
bool String::operator<(const CharType *p_str) const {
-
- if (empty() && p_str[0] == 0)
+ if (empty() && p_str[0] == 0) {
return false;
- if (empty())
+ }
+ if (empty()) {
return true;
+ }
return is_str_less(c_str(), 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)
+ if (empty() && p_str[0] == 0) {
return false;
- if (empty())
+ }
+ if (empty()) {
return true;
+ }
return is_str_less(c_str(), p_str);
}
bool String::operator<(const String &p_str) const {
-
return operator<(p_str.c_str());
}
signed char String::nocasecmp_to(const String &p_str) const {
-
- if (empty() && p_str.empty())
+ if (empty() && p_str.empty()) {
return 0;
- if (empty())
+ }
+ if (empty()) {
return -1;
- if (p_str.empty())
+ }
+ if (p_str.empty()) {
return 1;
+ }
const CharType *that_str = p_str.c_str();
const CharType *this_str = c_str();
while (true) {
-
- if (*that_str == 0 && *this_str == 0)
+ if (*that_str == 0 && *this_str == 0) {
return 0; //we're equal
- else if (*this_str == 0)
+ } else if (*this_str == 0) {
return -1; //if this is empty, and the other one is not, then we're less.. I think?
- else if (*that_str == 0)
+ } else if (*that_str == 0) {
return 1; //otherwise the other one is smaller..
- else if (_find_upper(*this_str) < _find_upper(*that_str)) //more than
+ } else if (_find_upper(*this_str) < _find_upper(*that_str)) { //more than
return -1;
- else if (_find_upper(*this_str) > _find_upper(*that_str)) //less than
+ } else if (_find_upper(*this_str) > _find_upper(*that_str)) { //less than
return 1;
+ }
this_str++;
that_str++;
@@ -489,29 +472,31 @@ signed char String::nocasecmp_to(const String &p_str) const {
}
signed char String::casecmp_to(const String &p_str) const {
-
- if (empty() && p_str.empty())
+ if (empty() && p_str.empty()) {
return 0;
- if (empty())
+ }
+ if (empty()) {
return -1;
- if (p_str.empty())
+ }
+ if (p_str.empty()) {
return 1;
+ }
const CharType *that_str = p_str.c_str();
const CharType *this_str = c_str();
while (true) {
-
- if (*that_str == 0 && *this_str == 0)
+ if (*that_str == 0 && *this_str == 0) {
return 0; //we're equal
- else if (*this_str == 0)
+ } else if (*this_str == 0) {
return -1; //if this is empty, and the other one is not, then we're less.. I think?
- else if (*that_str == 0)
+ } else if (*that_str == 0) {
return 1; //otherwise the other one is smaller..
- else if (*this_str < *that_str) //more than
+ } else if (*this_str < *that_str) { //more than
return -1;
- else if (*this_str > *that_str) //less than
+ } else if (*this_str > *that_str) { //less than
return 1;
+ }
this_str++;
that_str++;
@@ -519,84 +504,87 @@ 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();
if (this_str && that_str) {
-
while (*this_str == '.' || *that_str == '.') {
- if (*this_str++ != '.')
+ if (*this_str++ != '.') {
return 1;
- if (*that_str++ != '.')
+ }
+ if (*that_str++ != '.') {
return -1;
- if (!*that_str)
+ }
+ if (!*that_str) {
return 1;
- if (!*this_str)
+ }
+ if (!*this_str) {
return -1;
+ }
}
while (*this_str) {
-
- if (!*that_str)
+ if (!*that_str) {
return 1;
- else if (IS_DIGIT(*this_str)) {
-
+ } else if (IS_DIGIT(*this_str)) {
int64_t this_int, that_int;
- if (!IS_DIGIT(*that_str))
+ if (!IS_DIGIT(*that_str)) {
return -1;
+ }
/* Compare the numbers */
- this_int = to_int(this_str);
- that_int = to_int(that_str);
+ this_int = to_int(this_str, -1, true);
+ that_int = to_int(that_str, -1, true);
- if (this_int < that_int)
+ if (this_int < that_int) {
return -1;
- else if (this_int > that_int)
+ } else if (this_int > that_int) {
return 1;
+ }
/* Skip */
- while (IS_DIGIT(*this_str))
+ while (IS_DIGIT(*this_str)) {
this_str++;
- while (IS_DIGIT(*that_str))
+ }
+ while (IS_DIGIT(*that_str)) {
that_str++;
- } else if (IS_DIGIT(*that_str))
+ }
+ } else if (IS_DIGIT(*that_str)) {
return 1;
- else {
- if (_find_upper(*this_str) < _find_upper(*that_str)) //more than
+ } else {
+ if (_find_upper(*this_str) < _find_upper(*that_str)) { //more than
return -1;
- else if (_find_upper(*this_str) > _find_upper(*that_str)) //less than
+ } else if (_find_upper(*this_str) > _find_upper(*that_str)) { //less than
return 1;
+ }
this_str++;
that_str++;
}
}
- if (*that_str)
+ if (*that_str) {
return -1;
+ }
}
return 0;
}
void String::erase(int p_pos, int p_chars) {
-
*this = left(p_pos) + substr(p_pos + p_chars, length() - ((p_pos + p_chars)));
}
String String::capitalize() const {
-
String aux = this->camelcase_to_underscore(true).replace("_", " ").strip_edges();
String cap;
for (int i = 0; i < aux.get_slice_count(" "); i++) {
-
String slice = aux.get_slicec(' ', i);
if (slice.length() > 0) {
-
slice[0] = _find_upper(slice[0]);
- if (i > 0)
+ if (i > 0) {
cap += " ";
+ }
cap += slice;
}
}
@@ -657,18 +645,19 @@ String String::get_with_code_lines() const {
}
return ret;
}
-int String::get_slice_count(String p_splitter) const {
- if (empty())
+int String::get_slice_count(String p_splitter) const {
+ if (empty()) {
return 0;
- if (p_splitter.empty())
+ }
+ if (p_splitter.empty()) {
return 0;
+ }
int pos = 0;
int slices = 1;
while ((pos = find(p_splitter, pos)) >= 0) {
-
slices++;
pos += p_splitter.length();
}
@@ -677,35 +666,37 @@ int String::get_slice_count(String p_splitter) const {
}
String String::get_slice(String p_splitter, int p_slice) const {
-
- if (empty() || p_splitter.empty())
+ if (empty() || p_splitter.empty()) {
return "";
+ }
int pos = 0;
int prev_pos = 0;
//int slices=1;
- if (p_slice < 0)
+ if (p_slice < 0) {
return "";
- if (find(p_splitter) == -1)
+ }
+ if (find(p_splitter) == -1) {
return *this;
+ }
int i = 0;
while (true) {
-
pos = find(p_splitter, pos);
- if (pos == -1)
+ if (pos == -1) {
pos = length(); //reached end
+ }
int from = prev_pos;
//int to=pos;
if (p_slice == i) {
-
return substr(from, pos - from);
}
- if (pos == length()) //reached end and no find
+ if (pos == length()) { //reached end and no find
break;
+ }
pos += p_splitter.length();
prev_pos = pos;
i++;
@@ -715,23 +706,21 @@ String String::get_slice(String p_splitter, int p_slice) const {
}
String String::get_slicec(CharType p_splitter, int p_slice) const {
-
- if (empty())
+ if (empty()) {
return String();
+ }
- if (p_slice < 0)
+ if (p_slice < 0) {
return String();
+ }
const CharType *c = this->ptr();
int i = 0;
int prev = 0;
int count = 0;
while (true) {
-
if (c[i] == 0 || c[i] == p_splitter) {
-
if (p_slice == count) {
-
return substr(prev, i - prev);
} else if (c[i] == 0) {
return String();
@@ -746,22 +735,22 @@ String String::get_slicec(CharType p_splitter, int p_slice) const {
}
Vector<String> String::split_spaces() const {
-
Vector<String> ret;
int from = 0;
int i = 0;
int len = length();
- if (len == 0)
+ if (len == 0) {
return ret;
+ }
bool inside = false;
while (true) {
-
bool empty = operator[](i) < 33;
- if (i == 0)
+ if (i == 0) {
inside = !empty;
+ }
if (!empty && !inside) {
inside = true;
@@ -769,13 +758,13 @@ Vector<String> String::split_spaces() const {
}
if (empty && inside) {
-
ret.push_back(substr(from, i - from));
inside = false;
}
- if (i == len)
+ if (i == len) {
break;
+ }
i++;
}
@@ -783,21 +772,19 @@ Vector<String> String::split_spaces() const {
}
Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
-
Vector<String> ret;
int from = 0;
int len = length();
while (true) {
-
int end = find(p_splitter, from);
- if (end < 0)
+ if (end < 0) {
end = len;
+ }
if (p_allow_empty || (end > from)) {
- if (p_maxsplit <= 0)
+ if (p_maxsplit <= 0) {
ret.push_back(substr(from, end - from));
- else {
-
+ } else {
// Put rest of the string and leave cycle.
if (p_maxsplit == ret.size()) {
ret.push_back(substr(from, len));
@@ -809,8 +796,9 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p
}
}
- if (end == len)
+ if (end == len) {
break;
+ }
from = end + p_splitter.length();
}
@@ -819,13 +807,11 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p
}
Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
-
Vector<String> ret;
const int len = length();
int remaining_len = len;
while (true) {
-
if (remaining_len < p_splitter.length() || (p_maxsplit > 0 && p_maxsplit == ret.size())) {
// no room for another splitter or hit max splits, push what's left and we're done
if (p_allow_empty || remaining_len > 0) {
@@ -855,21 +841,22 @@ Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int
}
Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
-
Vector<float> ret;
int from = 0;
int len = length();
while (true) {
-
int end = find(p_splitter, from);
- if (end < 0)
+ if (end < 0) {
end = len;
- if (p_allow_empty || (end > from))
- ret.push_back(String::to_double(&c_str()[from]));
+ }
+ if (p_allow_empty || (end > from)) {
+ ret.push_back(String::to_float(&c_str()[from]));
+ }
- if (end == len)
+ if (end == len) {
break;
+ }
from = end + p_splitter.length();
}
@@ -878,13 +865,11 @@ Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty)
}
Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
-
Vector<float> ret;
int from = 0;
int len = length();
while (true) {
-
int idx;
int end = findmk(p_splitters, from, &idx);
int spl_len = 1;
@@ -895,11 +880,12 @@ 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(&c_str()[from]));
}
- if (end == len)
+ if (end == len) {
break;
+ }
from = end + spl_len;
}
@@ -908,21 +894,22 @@ Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_
}
Vector<int> String::split_ints(const String &p_splitter, bool p_allow_empty) const {
-
Vector<int> ret;
int from = 0;
int len = length();
while (true) {
-
int end = find(p_splitter, from);
- if (end < 0)
+ if (end < 0) {
end = len;
- if (p_allow_empty || (end > from))
+ }
+ if (p_allow_empty || (end > from)) {
ret.push_back(String::to_int(&c_str()[from], end - from));
+ }
- if (end == len)
+ if (end == len) {
break;
+ }
from = end + p_splitter.length();
}
@@ -931,13 +918,11 @@ Vector<int> String::split_ints(const String &p_splitter, bool p_allow_empty) con
}
Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
-
Vector<int> ret;
int from = 0;
int len = length();
while (true) {
-
int idx;
int end = findmk(p_splitters, from, &idx);
int spl_len = 1;
@@ -947,11 +932,13 @@ Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allo
spl_len = p_splitters[idx].length();
}
- if (p_allow_empty || (end > from))
+ if (p_allow_empty || (end > from)) {
ret.push_back(String::to_int(&c_str()[from], end - from));
+ }
- if (end == len)
+ if (end == len) {
break;
+ }
from = end + spl_len;
}
@@ -971,47 +958,42 @@ String String::join(Vector<String> parts) {
}
CharType String::char_uppercase(CharType p_char) {
-
return _find_upper(p_char);
}
CharType String::char_lowercase(CharType p_char) {
-
return _find_lower(p_char);
}
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);
- if (s != t) // avoid copy on write
+ if (s != t) { // avoid copy on write
upper[i] = t;
+ }
}
return upper;
}
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);
- if (s != t) // avoid copy on write
+ if (s != t) { // avoid copy on write
lower[i] = t;
+ }
}
return lower;
}
const CharType *String::c_str() const {
-
static const CharType zero = 0;
return size() ? &operator[](0) : &zero;
@@ -1038,23 +1020,25 @@ String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
}
String String::chr(CharType p_char) {
-
CharType c[2] = { p_char, 0 };
return String(c);
}
-String String::num(double p_num, int p_decimals) {
+String String::num(double p_num, int p_decimals) {
+ if (Math::is_nan(p_num)) {
+ return "nan";
+ }
#ifndef NO_USE_STDLIB
- if (p_decimals > 16)
+ if (p_decimals > 16) {
p_decimals = 16;
+ }
char fmt[7];
fmt[0] = '%';
fmt[1] = '.';
if (p_decimals < 0) {
-
fmt[1] = 'l';
fmt[2] = 'f';
fmt[3] = 0;
@@ -1082,28 +1066,24 @@ String String::num(double p_num, int p_decimals) {
buf[255] = 0;
//destroy trailing zeroes
{
-
bool period = false;
int z = 0;
while (buf[z]) {
- if (buf[z] == '.')
+ if (buf[z] == '.') {
period = true;
+ }
z++;
}
if (period) {
z--;
while (z > 0) {
-
if (buf[z] == '0') {
-
buf[z] = 0;
} else if (buf[z] == '.') {
-
buf[z] = 0;
break;
} else {
-
break;
}
@@ -1126,7 +1106,6 @@ 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);
int digit = 0;
@@ -1137,14 +1116,12 @@ String String::num(double p_num, int p_decimals) {
int dec_max = 0;
while (true) {
-
dec *= 10.0;
dec_int = dec_int * 10 + (int)dec % 10;
dec_max = dec_max * 10 + 9;
digit++;
if (p_decimals == -1) {
-
if (digit == MAX_DIGITS) //no point in going to infinite
break;
@@ -1160,18 +1137,15 @@ String String::num(double p_num, int p_decimals) {
if (last > 5) {
if (dec_int == dec_max) {
-
dec_int = 0;
intn++;
} else {
-
dec_int++;
}
}
String decimal;
for (int i = 0; i < digit; i++) {
-
char num[2] = { 0, 0 };
num[0] = '0' + dec_int % 10;
decimal = num + decimal;
@@ -1185,7 +1159,6 @@ String String::num(double p_num, int p_decimals) {
s = "0";
else {
while (intn) {
-
CharType num = '0' + (intn % 10);
intn /= 10;
s = num + s;
@@ -1200,7 +1173,6 @@ String String::num(double p_num, int p_decimals) {
}
String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
-
bool sign = p_num < 0;
int64_t n = p_num;
@@ -1211,8 +1183,9 @@ String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
chars++;
} while (n);
- if (sign)
+ if (sign) {
chars++;
+ }
String s;
s.resize(chars + 1);
CharType *c = s.ptrw();
@@ -1230,14 +1203,14 @@ String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
n /= base;
} while (n);
- if (sign)
+ if (sign) {
c[0] = '-';
+ }
return s;
}
String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
-
uint64_t n = p_num;
int chars = 0;
@@ -1267,7 +1240,6 @@ String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
}
String String::num_real(double p_num) {
-
String s;
String sd;
/* integer part */
@@ -1279,7 +1251,6 @@ String String::num_real(double p_num) {
/* decimal part */
if ((int)p_num != p_num) {
-
double dec = p_num - (float)((int)p_num);
int digit = 0;
@@ -1289,17 +1260,18 @@ String String::num_real(double p_num) {
int dec_max = 0;
while (true) {
-
dec *= 10.0;
dec_int = dec_int * 10 + (int)dec % 10;
dec_max = dec_max * 10 + 9;
digit++;
- if ((dec - (float)((int)dec)) < 1e-6)
+ if ((dec - (float)((int)dec)) < 1e-6) {
break;
+ }
- if (digit == decimals)
+ if (digit == decimals) {
break;
+ }
}
dec *= 10;
@@ -1307,18 +1279,15 @@ String String::num_real(double p_num) {
if (last > 5) {
if (dec_int == dec_max) {
-
dec_int = 0;
intn++;
} else {
-
dec_int++;
}
}
String decimal;
for (int i = 0; i < digit; i++) {
-
char num[2] = { 0, 0 };
num[0] = '0' + dec_int % 10;
decimal = num + decimal;
@@ -1329,12 +1298,10 @@ String String::num_real(double p_num) {
sd = ".0";
}
- if (intn == 0)
-
+ if (intn == 0) {
s = "0";
- else {
+ } else {
while (intn) {
-
CharType num = '0' + (intn % 10);
intn /= 10;
s = num + s;
@@ -1342,13 +1309,16 @@ String String::num_real(double p_num) {
}
s = s + sd;
- if (neg)
+ if (neg) {
s = "-" + s;
+ }
return s;
}
String String::num_scientific(double p_num) {
-
+ if (Math::is_nan(p_num)) {
+ return "nan";
+ }
#ifndef NO_USE_STDLIB
char buf[256];
@@ -1379,33 +1349,33 @@ String String::num_scientific(double p_num) {
}
CharString String::ascii(bool p_allow_extended) const {
-
- if (!length())
+ if (!length()) {
return CharString();
+ }
CharString cs;
cs.resize(size());
- for (int i = 0; i < size(); i++)
+ for (int i = 0; i < size(); i++) {
cs[i] = operator[](i);
+ }
return cs;
}
String String::utf8(const char *p_utf8, int p_len) {
-
String ret;
ret.parse_utf8(p_utf8, p_len);
return ret;
-};
+}
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?");
- if (!p_utf8)
+ if (!p_utf8) {
return true;
+ }
String aux;
@@ -1414,13 +1384,12 @@ 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;
if (has_bom) {
-
//just skip it
- if (p_len >= 0)
+ if (p_len >= 0) {
p_len -= 3;
+ }
p_utf8 += 3;
}
}
@@ -1430,25 +1399,23 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
const char *ptrtmp_limit = &p_utf8[p_len];
int skip = 0;
while (ptrtmp != ptrtmp_limit && *ptrtmp) {
-
if (skip == 0) {
-
uint8_t c = *ptrtmp >= 0 ? *ptrtmp : uint8_t(256 + *ptrtmp);
/* Determine the number of characters in sequence */
- if ((c & 0x80) == 0)
+ 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)
+ } else if ((c & 0xFC) == 0xF8) {
skip = 4;
- else if ((c & 0xFE) == 0xFC)
+ } else if ((c & 0xFE) == 0xFC) {
skip = 5;
- else {
+ } else {
_UNICERROR("invalid skip");
return true; //invalid utf8
}
@@ -1462,7 +1429,6 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
str_size++;
} else {
-
--skip;
}
@@ -1486,23 +1452,22 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
dst[str_size] = 0;
while (cstr_size) {
-
int len = 0;
/* Determine the number of characters in sequence */
- if ((*p_utf8 & 0x80) == 0)
+ 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)
+ } else if ((*p_utf8 & 0xFC) == 0xF8) {
len = 5;
- else if ((*p_utf8 & 0xFE) == 0xFC)
+ } else if ((*p_utf8 & 0xFE) == 0xFC) {
len = 6;
- else {
+ } else {
_UNICERROR("invalid len");
return true; //invalid UTF8
@@ -1523,14 +1488,12 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
uint32_t unichar = 0;
- if (len == 1)
+ if (len == 1) {
unichar = *p_utf8;
- else {
-
+ } else {
unichar = (0xFF >> (len + 1)) & *p_utf8;
for (int i = 1; i < len; i++) {
-
if ((p_utf8[i] & 0xC0) != 0x80) {
_UNICERROR("invalid utf8");
return true; //invalid utf8
@@ -1557,19 +1520,18 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
}
CharString String::utf8() const {
-
int l = length();
- if (!l)
+ if (!l) {
return CharString();
+ }
const CharType *d = &operator[](0);
int fl = 0;
for (int i = 0; i < l; i++) {
-
uint32_t c = d[i];
- if (c <= 0x7f) // 7 bits.
+ if (c <= 0x7f) { // 7 bits.
fl += 1;
- else if (c <= 0x7ff) { // 11 bits
+ } else if (c <= 0x7ff) { // 11 bits
fl += 2;
} else if (c <= 0xffff) { // 16 bits
fl += 3;
@@ -1594,12 +1556,11 @@ CharString String::utf8() const {
#define APPEND_CHAR(m_c) *(cdst++) = m_c
for (int i = 0; i < l; i++) {
-
uint32_t c = d[i];
- if (c <= 0x7f) // 7 bits.
+ if (c <= 0x7f) { // 7 bits.
APPEND_CHAR(c);
- else if (c <= 0x7ff) { // 11 bits
+ } 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.
@@ -1643,73 +1604,31 @@ String::String(CharType p_char) {
shared=nullptr;
copy_from(p_char);
}
+
+
*/
String::String(const char *p_str) {
-
copy_from(p_str);
}
String::String(const CharType *p_str, int p_clip_to_len) {
-
copy_from(p_str, p_clip_to_len);
}
String::String(const StrRange &p_range) {
-
- if (!p_range.c_str)
+ if (!p_range.c_str) {
return;
+ }
copy_from(p_range.c_str, p_range.len);
}
-int String::hex_to_int(bool p_with_prefix) const {
-
- if (p_with_prefix && length() < 3)
+int64_t String::hex_to_int(bool p_with_prefix) const {
+ if (p_with_prefix && length() < 3) {
return 0;
-
- const CharType *s = ptr();
-
- int sign = s[0] == '-' ? -1 : 1;
-
- if (sign < 0) {
- s++;
- }
-
- if (p_with_prefix) {
- if (s[0] != '0' || s[1] != 'x')
- return 0;
- s += 2;
- }
-
- int hex = 0;
-
- while (*s) {
-
- CharType c = LOWERCASE(*s);
- int n;
- if (c >= '0' && c <= '9') {
- n = c - '0';
- } else if (c >= 'a' && c <= 'f') {
- n = (c - 'a') + 10;
- } else {
- return 0;
- }
-
- ERR_FAIL_COND_V_MSG(hex > INT32_MAX / 16, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + *this + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
- hex *= 16;
- hex += n;
- s++;
}
- return hex * sign;
-}
-
-int64_t String::hex_to_int64(bool p_with_prefix) const {
-
- if (p_with_prefix && length() < 3)
- return 0;
-
const CharType *s = ptr();
int64_t sign = s[0] == '-' ? -1 : 1;
@@ -1719,15 +1638,15 @@ int64_t String::hex_to_int64(bool p_with_prefix) const {
}
if (p_with_prefix) {
- if (s[0] != '0' || s[1] != 'x')
+ if (s[0] != '0' || s[1] != 'x') {
return 0;
+ }
s += 2;
}
int64_t hex = 0;
while (*s) {
-
CharType c = LOWERCASE(*s);
int64_t n;
if (c >= '0' && c <= '9') {
@@ -1737,8 +1656,9 @@ int64_t String::hex_to_int64(bool p_with_prefix) const {
} else {
return 0;
}
-
- ERR_FAIL_COND_V_MSG(hex > INT64_MAX / 16, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
+ // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error
+ bool overflow = ((hex > INT64_MAX / 16) && (sign == 1 || (sign == -1 && hex != (INT64_MAX >> 4) + 1))) || (sign == -1 && hex == (INT64_MAX >> 4) + 1 && c > '0');
+ 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."));
hex *= 16;
hex += n;
s++;
@@ -1747,10 +1667,10 @@ int64_t String::hex_to_int64(bool p_with_prefix) const {
return hex * sign;
}
-int64_t String::bin_to_int64(bool p_with_prefix) const {
-
- if (p_with_prefix && length() < 3)
+int64_t String::bin_to_int(bool p_with_prefix) const {
+ if (p_with_prefix && length() < 3) {
return 0;
+ }
const CharType *s = ptr();
@@ -1761,15 +1681,15 @@ int64_t String::bin_to_int64(bool p_with_prefix) const {
}
if (p_with_prefix) {
- if (s[0] != '0' || s[1] != 'b')
+ if (s[0] != '0' || s[1] != 'b') {
return 0;
+ }
s += 2;
}
int64_t binary = 0;
while (*s) {
-
CharType c = LOWERCASE(*s);
int64_t n;
if (c == '0' || c == '1') {
@@ -1777,8 +1697,9 @@ int64_t String::bin_to_int64(bool p_with_prefix) const {
} else {
return 0;
}
-
- ERR_FAIL_COND_V_MSG(binary > INT64_MAX / 2, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
+ // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error
+ bool overflow = ((binary > INT64_MAX / 2) && (sign == 1 || (sign == -1 && binary != (INT64_MAX >> 1) + 1))) || (sign == -1 && binary == (INT64_MAX >> 1) + 1 && c > '0');
+ 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."));
binary *= 2;
binary += n;
s++;
@@ -1787,55 +1708,25 @@ int64_t String::bin_to_int64(bool p_with_prefix) const {
return binary * sign;
}
-int String::to_int() const {
-
- if (length() == 0)
+int64_t String::to_int() const {
+ if (length() == 0) {
return 0;
-
- int to = (find(".") >= 0) ? find(".") : length();
-
- int integer = 0;
- int sign = 1;
-
- for (int i = 0; i < to; i++) {
-
- CharType c = operator[](i);
- if (c >= '0' && c <= '9') {
-
- ERR_FAIL_COND_V_MSG(integer > INT32_MAX / 10, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + *this + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
- integer *= 10;
- integer += c - '0';
-
- } else if (integer == 0 && c == '-') {
-
- sign = -sign;
- }
}
- return integer * sign;
-}
-
-int64_t String::to_int64() const {
-
- if (length() == 0)
- return 0;
-
int to = (find(".") >= 0) ? find(".") : length();
int64_t integer = 0;
int64_t sign = 1;
for (int i = 0; i < to; i++) {
-
CharType c = operator[](i);
if (c >= '0' && c <= '9') {
-
- ERR_FAIL_COND_V_MSG(integer > INT64_MAX / 10, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
+ 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."));
integer *= 10;
integer += c - '0';
} else if (integer == 0 && c == '-') {
-
sign = -sign;
}
}
@@ -1843,63 +1734,62 @@ int64_t String::to_int64() const {
return integer * sign;
}
-int String::to_int(const char *p_str, int p_len) {
-
+int64_t String::to_int(const char *p_str, int p_len) {
int to = 0;
- if (p_len >= 0)
+ if (p_len >= 0) {
to = p_len;
- else {
- while (p_str[to] != 0 && p_str[to] != '.')
+ } else {
+ while (p_str[to] != 0 && p_str[to] != '.') {
to++;
+ }
}
- int integer = 0;
- int sign = 1;
+ int64_t integer = 0;
+ int64_t sign = 1;
for (int i = 0; i < to; i++) {
-
char c = p_str[i];
if (c >= '0' && c <= '9') {
-
- ERR_FAIL_COND_V_MSG(integer > INT32_MAX / 10, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
+ 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 != ' ')
+ } else if (c != ' ') {
break;
+ }
}
return integer * sign;
}
bool String::is_numeric() const {
-
if (length() == 0) {
return false;
- };
+ }
int s = 0;
- if (operator[](0) == '-') ++s;
+ if (operator[](0) == '-') {
+ ++s;
+ }
bool dot = false;
for (int i = s; i < length(); i++) {
-
CharType c = operator[](i);
if (c == '.') {
if (dot) {
return false;
- };
+ }
dot = true;
}
if (c < '0' || c > '9') {
return false;
- };
- };
+ }
+ }
return true; // TODO: Use the parser below for this instead
-};
+}
template <class C>
static double built_in_strtod(const C *string, /* A decimal ASCII floating-point number,
@@ -1916,7 +1806,6 @@ static double built_in_strtod(const C *string, /* A decimal ASCII floating-point
C **endPtr = nullptr) /* If non-nullptr, store terminating Cacter's
* address here. */
{
-
static const int maxExponent = 511; /* Largest possible base 10 exponent. Any
* exponent larger than this will already
* produce underflow or overflow, so there's
@@ -2117,8 +2006,7 @@ done:
#define READING_EXP 3
#define READING_DONE 4
-double String::to_double(const char *p_str) {
-
+double String::to_float(const char *p_str) {
#ifndef NO_USE_STDLIB
return built_in_strtod<char>(p_str);
//return atof(p_str); DOES NOT WORK ON ANDROID(??)
@@ -2127,20 +2015,14 @@ double String::to_double(const char *p_str) {
#endif
}
-float String::to_float() const {
-
- return to_double();
-}
-
-double String::to_double(const CharType *p_str, const CharType **r_end) {
-
+double String::to_float(const CharType *p_str, const CharType **r_end) {
return built_in_strtod<CharType>(p_str, (CharType **)r_end);
}
-int64_t String::to_int(const CharType *p_str, int p_len) {
-
- if (p_len == 0 || !p_str[0])
+int64_t String::to_int(const CharType *p_str, int p_len, bool p_clamp) {
+ if (p_len == 0 || !p_str[0]) {
return 0;
+ }
///@todo make more exact so saving and loading does not lose precision
int64_t integer = 0;
@@ -2151,7 +2033,6 @@ int64_t String::to_int(const CharType *p_str, int p_len) {
const CharType *limit = &p_str[p_len];
while (*str && reading != READING_DONE && str != limit) {
-
CharType c = *(str++);
switch (reading) {
case READING_SIGN: {
@@ -2172,16 +2053,22 @@ int64_t String::to_int(const CharType *p_str, int p_len) {
[[fallthrough]];
}
case READING_INT: {
-
if (c >= '0' && c <= '9') {
-
if (integer > INT64_MAX / 10) {
String number("");
str = p_str;
while (*str && str != limit) {
number += *(str++);
}
- ERR_FAIL_V_MSG(sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + number + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
+ if (p_clamp) {
+ if (sign == 1) {
+ return INT64_MAX;
+ } else {
+ return INT64_MIN;
+ }
+ } else {
+ ERR_FAIL_V_MSG(sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + number + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
+ }
}
integer *= 10;
integer += c - '0';
@@ -2196,10 +2083,10 @@ int64_t String::to_int(const CharType *p_str, int p_len) {
return sign * integer;
}
-double String::to_double() const {
-
- if (empty())
+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 :(
@@ -2209,91 +2096,88 @@ double String::to_double() const {
}
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) {
+String operator+(CharType p_chr, const String &p_str) {
return (String::chr(p_chr) + p_str);
}
uint32_t String::hash(const char *p_cstr) {
-
uint32_t hashv = 5381;
uint32_t c;
- while ((c = *p_cstr++))
+ while ((c = *p_cstr++)) {
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
+ }
return hashv;
}
uint32_t String::hash(const char *p_cstr, int p_len) {
-
uint32_t hashv = 5381;
- for (int i = 0; i < p_len; i++)
+ 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 CharType *p_cstr, int p_len) {
-
uint32_t hashv = 5381;
- for (int i = 0; i < p_len; i++)
+ 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 CharType *p_cstr) {
-
uint32_t hashv = 5381;
uint32_t c;
- while ((c = *p_cstr++))
+ while ((c = *p_cstr++)) {
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
+ }
return hashv;
}
uint32_t String::hash() const {
-
/* simple djb2 hashing */
const CharType *chr = c_str();
uint32_t hashv = 5381;
uint32_t c;
- while ((c = *chr++))
+ while ((c = *chr++)) {
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
+ }
return hashv;
}
uint64_t String::hash64() const {
-
/* simple djb2 hashing */
const CharType *chr = c_str();
uint64_t hashv = 5381;
uint64_t c;
- while ((c = *chr++))
+ while ((c = *chr++)) {
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
+ }
return hashv;
}
String String::md5_text() const {
-
CharString cs = utf8();
unsigned char hash[16];
CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
@@ -2315,7 +2199,6 @@ String String::sha256_text() const {
}
Vector<uint8_t> String::md5_buffer() const {
-
CharString cs = utf8();
unsigned char hash[16];
CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
@@ -2326,7 +2209,7 @@ Vector<uint8_t> String::md5_buffer() const {
ret.write[i] = hash[i];
}
return ret;
-};
+}
Vector<uint8_t> String::sha1_buffer() const {
CharString cs = utf8();
@@ -2356,38 +2239,41 @@ Vector<uint8_t> String::sha256_buffer() const {
}
String String::insert(int p_at_pos, const String &p_string) const {
-
- if (p_at_pos < 0)
+ if (p_at_pos < 0) {
return *this;
+ }
- if (p_at_pos > length())
+ if (p_at_pos > length()) {
p_at_pos = length();
+ }
String pre;
- if (p_at_pos > 0)
+ if (p_at_pos > 0) {
pre = substr(0, p_at_pos);
+ }
String post;
- if (p_at_pos < length())
+ if (p_at_pos < length()) {
post = substr(p_at_pos, length() - p_at_pos);
+ }
return pre + p_string + post;
}
-String String::substr(int p_from, int p_chars) const {
- if (p_chars == -1)
+String String::substr(int p_from, int p_chars) const {
+ if (p_chars == -1) {
p_chars = length() - p_from;
+ }
- if (empty() || p_from < 0 || p_from >= length() || p_chars <= 0)
+ if (empty() || p_from < 0 || p_from >= length() || p_chars <= 0) {
return "";
+ }
if ((p_from + p_chars) > length()) {
-
p_chars = length() - p_from;
}
if (p_from == 0 && p_chars >= length()) {
-
return String(*this);
}
@@ -2396,47 +2282,31 @@ String String::substr(int p_from, int p_chars) const {
return s;
}
-int String::find_last(const String &p_str) const {
-
- int pos = -1;
- int findfrom = 0;
- int findres = -1;
- while ((findres = find(p_str, findfrom)) != -1) {
-
- pos = findres;
- findfrom = pos + 1;
- }
-
- return pos;
-}
-
int String::find(const String &p_str, int p_from) const {
-
- if (p_from < 0)
+ if (p_from < 0) {
return -1;
+ }
const int src_len = p_str.length();
const int len = length();
- if (src_len == 0 || len == 0)
+ if (src_len == 0 || len == 0) {
return -1; // won't find anything!
+ }
const CharType *src = c_str();
const CharType *str = p_str.c_str();
for (int i = p_from; i <= (len - src_len); i++) {
-
bool found = true;
for (int j = 0; j < src_len; j++) {
-
int read_pos = i + j;
if (read_pos >= len) {
-
ERR_PRINT("read_pos>=len");
return -1;
- };
+ }
if (src[read_pos] != str[j]) {
found = false;
@@ -2444,54 +2314,51 @@ int String::find(const String &p_str, int p_from) const {
}
}
- if (found)
+ if (found) {
return i;
+ }
}
return -1;
}
int String::find(const char *p_str, int p_from) const {
-
- if (p_from < 0)
+ if (p_from < 0) {
return -1;
+ }
const int len = length();
- if (len == 0)
+ if (len == 0) {
return -1; // won't find anything!
+ }
const CharType *src = c_str();
int src_len = 0;
- while (p_str[src_len] != '\0')
+ while (p_str[src_len] != '\0') {
src_len++;
+ }
if (src_len == 1) {
-
const char needle = p_str[0];
for (int i = p_from; i < len; i++) {
-
if (src[i] == needle) {
return i;
}
}
} else {
-
for (int i = p_from; i <= (len - src_len); i++) {
-
bool found = true;
for (int j = 0; j < src_len; j++) {
-
int read_pos = i + j;
if (read_pos >= len) {
-
ERR_PRINT("read_pos>=len");
return -1;
- };
+ }
if (src[read_pos] != p_str[j]) {
found = false;
@@ -2499,8 +2366,9 @@ int String::find(const char *p_str, int p_from) const {
}
}
- if (found)
+ if (found) {
return i;
+ }
}
}
@@ -2512,83 +2380,82 @@ int String::find_char(const CharType &p_char, int p_from) const {
}
int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
-
- if (p_from < 0)
+ if (p_from < 0) {
return -1;
- if (p_keys.size() == 0)
+ }
+ if (p_keys.size() == 0) {
return -1;
+ }
//int src_len=p_str.length();
const String *keys = &p_keys[0];
int key_count = p_keys.size();
int len = length();
- if (len == 0)
+ if (len == 0) {
return -1; // won't find anything!
+ }
const CharType *src = c_str();
for (int i = p_from; i < len; i++) {
-
bool found = true;
for (int k = 0; k < key_count; k++) {
-
found = true;
- if (r_key)
+ if (r_key) {
*r_key = k;
+ }
const CharType *cmp = keys[k].c_str();
int l = keys[k].length();
for (int j = 0; j < l; j++) {
-
int read_pos = i + j;
if (read_pos >= len) {
-
found = false;
break;
- };
+ }
if (src[read_pos] != cmp[j]) {
found = false;
break;
}
}
- if (found)
+ if (found) {
break;
+ }
}
- if (found)
+ if (found) {
return i;
+ }
}
return -1;
}
int String::findn(const String &p_str, int p_from) const {
-
- if (p_from < 0)
+ if (p_from < 0) {
return -1;
+ }
int src_len = p_str.length();
- if (src_len == 0 || length() == 0)
+ if (src_len == 0 || length() == 0) {
return -1; // won't find anything!
+ }
const CharType *srcd = c_str();
for (int i = p_from; i <= (length() - src_len); i++) {
-
bool found = true;
for (int j = 0; j < src_len; j++) {
-
int read_pos = i + j;
if (read_pos >= length()) {
-
ERR_PRINT("read_pos>=length()");
return -1;
- };
+ }
CharType src = _find_lower(srcd[read_pos]);
CharType dst = _find_lower(p_str[j]);
@@ -2599,46 +2466,46 @@ int String::findn(const String &p_str, int p_from) const {
}
}
- if (found)
+ if (found) {
return i;
+ }
}
return -1;
}
int String::rfind(const String &p_str, int p_from) const {
-
// establish a limit
int limit = length() - p_str.length();
- if (limit < 0)
+ if (limit < 0) {
return -1;
+ }
// establish a starting point
- if (p_from < 0)
+ if (p_from < 0) {
p_from = limit;
- else if (p_from > limit)
+ } else if (p_from > limit) {
p_from = limit;
+ }
int src_len = p_str.length();
int len = length();
- if (src_len == 0 || len == 0)
+ if (src_len == 0 || len == 0) {
return -1; // won't find anything!
+ }
const CharType *src = c_str();
for (int i = p_from; i >= 0; i--) {
-
bool found = true;
for (int j = 0; j < src_len; j++) {
-
int read_pos = i + j;
if (read_pos >= len) {
-
ERR_PRINT("read_pos>=len");
return -1;
- };
+ }
if (src[read_pos] != p_str[j]) {
found = false;
@@ -2646,45 +2513,46 @@ int String::rfind(const String &p_str, int p_from) const {
}
}
- if (found)
+ if (found) {
return i;
+ }
}
return -1;
}
-int String::rfindn(const String &p_str, int p_from) const {
+int String::rfindn(const String &p_str, int p_from) const {
// establish a limit
int limit = length() - p_str.length();
- if (limit < 0)
+ if (limit < 0) {
return -1;
+ }
// establish a starting point
- if (p_from < 0)
+ if (p_from < 0) {
p_from = limit;
- else if (p_from > limit)
+ } else if (p_from > limit) {
p_from = limit;
+ }
int src_len = p_str.length();
int len = length();
- if (src_len == 0 || len == 0)
+ if (src_len == 0 || len == 0) {
return -1; // won't find anything!
+ }
const CharType *src = c_str();
for (int i = p_from; i >= 0; i--) {
-
bool found = true;
for (int j = 0; j < src_len; j++) {
-
int read_pos = i + j;
if (read_pos >= len) {
-
ERR_PRINT("read_pos>=len");
return -1;
- };
+ }
CharType srcc = _find_lower(src[read_pos]);
CharType dstc = _find_lower(p_str[j]);
@@ -2695,56 +2563,59 @@ int String::rfindn(const String &p_str, int p_from) const {
}
}
- if (found)
+ if (found) {
return i;
+ }
}
return -1;
}
bool String::ends_with(const String &p_string) const {
-
- int pos = find_last(p_string);
- if (pos == -1)
+ int pos = rfind(p_string);
+ if (pos == -1) {
return false;
+ }
return pos + p_string.length() == length();
}
bool String::begins_with(const String &p_string) const {
-
- if (p_string.length() > length())
+ if (p_string.length() > length()) {
return false;
+ }
int l = p_string.length();
- if (l == 0)
+ if (l == 0) {
return true;
+ }
const CharType *src = &p_string[0];
const CharType *str = &operator[](0);
int i = 0;
for (; i < l; i++) {
-
- if (src[i] != str[i])
+ if (src[i] != str[i]) {
return false;
+ }
}
// only if i == l the p_string matches the beginning
return i == l;
}
-bool String::begins_with(const char *p_string) const {
+bool String::begins_with(const char *p_string) const {
int l = length();
- if (l == 0 || !p_string)
+ if (l == 0 || !p_string) {
return false;
+ }
const CharType *str = &operator[](0);
int i = 0;
while (*p_string && i < l) {
-
- if (*p_string != str[i])
+ if (*p_string != str[i]) {
return false;
+ }
i++;
p_string++;
}
@@ -2753,22 +2624,18 @@ bool String::begins_with(const char *p_string) const {
}
bool String::is_enclosed_in(const String &p_string) const {
-
return begins_with(p_string) && ends_with(p_string);
}
bool String::is_subsequence_of(const String &p_string) const {
-
return _base_is_subsequence_of(p_string, false);
}
bool String::is_subsequence_ofi(const String &p_string) const {
-
return _base_is_subsequence_of(p_string, true);
}
bool String::is_quoted() const {
-
return is_enclosed_in("\"") || is_enclosed_in("'");
}
@@ -2818,7 +2685,6 @@ int String::countn(const String &p_string, int p_from, int p_to) const {
}
bool String::_base_is_subsequence_of(const String &p_string, bool case_insensitive) const {
-
int len = length();
if (len == 0) {
// Technically an empty string is subsequence of any string
@@ -2911,22 +2777,21 @@ static bool _wildcard_match(const CharType *p_pattern, const CharType *p_string,
}
bool String::match(const String &p_wildcard) const {
-
- if (!p_wildcard.length() || !length())
+ if (!p_wildcard.length() || !length()) {
return false;
+ }
return _wildcard_match(p_wildcard.c_str(), c_str(), true);
}
bool String::matchn(const String &p_wildcard) const {
-
- if (!p_wildcard.length() || !length())
+ if (!p_wildcard.length() || !length()) {
return false;
+ }
return _wildcard_match(p_wildcard.c_str(), c_str(), false);
}
String String::format(const Variant &values, String placeholder) const {
-
String new_string = String(this->ptr());
if (values.get_type() == Variant::ARRAY) {
@@ -2998,20 +2863,17 @@ String String::format(const Variant &values, String placeholder) const {
}
String String::replace(const String &p_key, const String &p_with) const {
-
String new_string;
int search_from = 0;
int result = 0;
while ((result = find(p_key, search_from)) >= 0) {
-
new_string += substr(search_from, result - search_from);
new_string += p_with;
search_from = result + p_key.length();
}
if (search_from == 0) {
-
return *this;
}
@@ -3021,23 +2883,21 @@ String String::replace(const String &p_key, const String &p_with) const {
}
String String::replace(const char *p_key, const char *p_with) const {
-
String new_string;
int search_from = 0;
int result = 0;
while ((result = find(p_key, search_from)) >= 0) {
-
new_string += substr(search_from, result - search_from);
new_string += p_with;
int k = 0;
- while (p_key[k] != '\0')
+ while (p_key[k] != '\0') {
k++;
+ }
search_from = result + k;
}
if (search_from == 0) {
-
return *this;
}
@@ -3047,7 +2907,6 @@ String String::replace(const char *p_key, const char *p_with) const {
}
String String::replace_first(const String &p_key, const String &p_with) const {
-
int pos = find(p_key);
if (pos >= 0) {
return substr(0, pos) + p_with + substr(pos + p_key.length(), length());
@@ -3055,21 +2914,19 @@ String String::replace_first(const String &p_key, const String &p_with) const {
return *this;
}
-String String::replacen(const String &p_key, const String &p_with) const {
+String String::replacen(const String &p_key, const String &p_with) const {
String new_string;
int search_from = 0;
int result = 0;
while ((result = findn(p_key, search_from)) >= 0) {
-
new_string += substr(search_from, result - search_from);
new_string += p_with;
search_from = result + p_key.length();
}
if (search_from == 0) {
-
return *this;
}
@@ -3078,7 +2935,6 @@ String String::replacen(const String &p_key, const String &p_with) const {
}
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;
@@ -3086,43 +2942,45 @@ String String::repeat(int p_count) const {
new_string.resize(length() * p_count + 1);
- for (int i = 0; i < p_count; i++)
- for (int j = 0; j < length(); j++)
+ for (int i = 0; i < p_count; i++) {
+ for (int j = 0; j < length(); j++) {
new_string[i * length() + j] = src[j];
+ }
+ }
return new_string;
}
String String::left(int p_pos) const {
-
- if (p_pos <= 0)
+ if (p_pos <= 0) {
return "";
+ }
- if (p_pos >= length())
+ if (p_pos >= length()) {
return *this;
+ }
return substr(0, p_pos);
}
String String::right(int p_pos) const {
-
- if (p_pos >= length())
+ if (p_pos >= length()) {
return "";
+ }
- if (p_pos <= 0)
+ if (p_pos <= 0) {
return *this;
+ }
return substr(p_pos, (length() - p_pos));
}
CharType String::ord_at(int p_idx) const {
-
ERR_FAIL_INDEX_V(p_idx, length(), 0);
return operator[](p_idx);
}
String String::dedent() const {
-
String new_string;
String indent;
bool has_indent = false;
@@ -3131,11 +2989,11 @@ String String::dedent() const {
int indent_stop = -1;
for (int i = 0; i < length(); i++) {
-
CharType c = operator[](i);
if (c == '\n') {
- if (has_text)
+ if (has_text) {
new_string += substr(indent_stop, i - indent_stop);
+ }
new_string += "\n";
has_text = false;
line_start = i + 1;
@@ -3151,57 +3009,58 @@ String String::dedent() const {
}
if (has_indent && indent_stop < 0) {
int j = i - line_start;
- if (j >= indent.length() || c != indent[j])
+ if (j >= indent.length() || c != indent[j]) {
indent_stop = i;
+ }
}
}
}
- if (has_text)
+ if (has_text) {
new_string += substr(indent_stop, length() - indent_stop);
+ }
return new_string;
}
String String::strip_edges(bool left, bool right) const {
-
int len = length();
int beg = 0, end = len;
if (left) {
for (int i = 0; i < len; i++) {
-
- if (operator[](i) <= 32)
+ if (operator[](i) <= 32) {
beg++;
- else
+ } else {
break;
+ }
}
}
if (right) {
for (int i = (int)(len - 1); i >= 0; i--) {
-
- if (operator[](i) <= 32)
+ if (operator[](i) <= 32) {
end--;
- else
+ } else {
break;
+ }
}
}
- if (beg == 0 && end == len)
+ if (beg == 0 && end == len) {
return *this;
+ }
return substr(beg, end - beg);
}
String String::strip_escapes() const {
-
String new_string;
for (int i = 0; i < length(); i++) {
-
// Escape characters on first page of the ASCII table, before 32 (Space).
- if (operator[](i) < 32)
+ if (operator[](i) < 32) {
continue;
+ }
new_string += operator[](i);
}
@@ -3209,65 +3068,60 @@ String String::strip_escapes() const {
}
String String::lstrip(const String &p_chars) const {
-
int len = length();
int beg;
for (beg = 0; beg < len; beg++) {
-
- if (p_chars.find_char(get(beg)) == -1)
+ if (p_chars.find_char(get(beg)) == -1) {
break;
+ }
}
- if (beg == 0)
+ if (beg == 0) {
return *this;
+ }
return substr(beg, len - beg);
}
String String::rstrip(const String &p_chars) const {
-
int len = length();
int end;
for (end = len - 1; end >= 0; end--) {
-
- if (p_chars.find_char(get(end)) == -1)
+ if (p_chars.find_char(get(end)) == -1) {
break;
+ }
}
- if (end == len - 1)
+ if (end == len - 1) {
return *this;
+ }
return substr(0, end + 1);
}
String String::simplify_path() const {
-
String s = *this;
String drive;
if (s.begins_with("local://")) {
drive = "local://";
s = s.substr(8, s.length());
} else if (s.begins_with("res://")) {
-
drive = "res://";
s = s.substr(6, s.length());
} else if (s.begins_with("user://")) {
-
drive = "user://";
s = s.substr(7, s.length());
} else if (s.begins_with("/") || s.begins_with("\\")) {
-
drive = s.substr(0, 1);
s = s.substr(1, s.length() - 1);
} else {
-
int p = s.find(":/");
- if (p == -1)
+ if (p == -1) {
p = s.find(":\\");
+ }
if (p != -1 && p < s.find("/")) {
-
drive = s.substr(0, p + 2);
s = s.substr(p + 2, s.length());
}
@@ -3276,21 +3130,20 @@ String String::simplify_path() const {
s = s.replace("\\", "/");
while (true) { // in case of using 2 or more slash
String compare = s.replace("//", "/");
- if (s == compare)
+ if (s == compare) {
break;
- else
+ } else {
s = compare;
+ }
}
Vector<String> dirs = s.split("/", false);
for (int i = 0; i < dirs.size(); i++) {
-
String d = dirs[i];
if (d == ".") {
dirs.remove(i);
i--;
} else if (d == "..") {
-
if (i == 0) {
dirs.remove(i);
i--;
@@ -3305,9 +3158,9 @@ String String::simplify_path() const {
s = "";
for (int i = 0; i < dirs.size(); i++) {
-
- if (i > 0)
+ if (i > 0) {
s += "/";
+ }
s += dirs[i];
}
@@ -3315,17 +3168,16 @@ String String::simplify_path() const {
}
static int _humanize_digits(int p_num) {
-
- if (p_num < 100)
+ if (p_num < 100) {
return 2;
- else if (p_num < 1024)
+ } else if (p_num < 1024) {
return 1;
- else
+ } else {
return 0;
+ }
}
String String::humanize_size(uint64_t p_size) {
-
uint64_t _div = 1;
Vector<String> prefixes;
prefixes.push_back(RTR("B"));
@@ -3348,36 +3200,38 @@ String String::humanize_size(uint64_t p_size) {
return String::num(p_size / divisor).pad_decimals(digits) + " " + prefixes[prefix_idx];
}
-bool String::is_abs_path() const {
- if (length() > 1)
+bool String::is_abs_path() const {
+ if (length() > 1) {
return (operator[](0) == '/' || operator[](0) == '\\' || find(":/") != -1 || find(":\\") != -1);
- else if ((length()) == 1)
+ } else if ((length()) == 1) {
return (operator[](0) == '/' || operator[](0) == '\\');
- else
+ } else {
return false;
+ }
}
bool String::is_valid_identifier() const {
-
int len = length();
- if (len == 0)
+ if (len == 0) {
return false;
+ }
const wchar_t *str = &operator[](0);
for (int i = 0; i < len; i++) {
-
if (i == 0) {
- if (str[0] >= '0' && str[0] <= '9')
+ if (str[0] >= '0' && str[0] <= '9') {
return false; // no start with number plz
+ }
}
bool valid_char = (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '_';
- if (!valid_char)
+ if (!valid_char) {
return false;
+ }
}
return true;
@@ -3386,7 +3240,6 @@ bool String::is_valid_identifier() const {
//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;
@@ -3463,7 +3316,6 @@ String String::http_unescape() const {
}
String String::c_unescape() const {
-
String escaped = *this;
escaped = escaped.replace("\\a", "\a");
escaped = escaped.replace("\\b", "\b");
@@ -3481,7 +3333,6 @@ String String::c_unescape() const {
}
String String::c_escape() const {
-
String escaped = *this;
escaped = escaped.replace("\\", "\\\\");
escaped = escaped.replace("\a", "\\a");
@@ -3499,7 +3350,6 @@ String String::c_escape() const {
}
String String::c_escape_multiline() const {
-
String escaped = *this;
escaped = escaped.replace("\\", "\\\\");
escaped = escaped.replace("\"", "\\\"");
@@ -3508,7 +3358,6 @@ String String::c_escape_multiline() const {
}
String String::json_escape() const {
-
String escaped = *this;
escaped = escaped.replace("\\", "\\\\");
escaped = escaped.replace("\b", "\\b");
@@ -3523,7 +3372,6 @@ String String::json_escape() const {
}
String String::xml_escape(bool p_escape_quotes) const {
-
String str = *this;
str = str.replace("&", "&amp;");
str = str.replace("<", "&lt;");
@@ -3533,29 +3381,24 @@ String String::xml_escape(bool p_escape_quotes) const {
str = str.replace("\"", "&quot;");
}
/*
- for (int i=1;i<32;i++) {
+for (int i=1;i<32;i++) {
- char chr[2]={i,0};
- str=str.replace(chr,"&#"+String::num(i)+";");
- }*/
+ char chr[2]={i,0};
+ str=str.replace(chr,"&#"+String::num(i)+";");
+}*/
return str;
}
static _FORCE_INLINE_ int _xml_unescape(const CharType *p_src, int p_src_len, CharType *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;
for (int i = 2; i < p_src_len; i++) {
-
eat = i + 1;
CharType ct = p_src[i];
if (ct == ';') {
@@ -3573,49 +3416,50 @@ static _FORCE_INLINE_ int _xml_unescape(const CharType *p_src, int p_src_len, Ch
c |= ct;
}
- if (p_dst)
+ if (p_dst) {
*p_dst = c;
+ }
} else if (p_src_len >= 4 && p_src[1] == 'g' && p_src[2] == 't' && p_src[3] == ';') {
-
- if (p_dst)
+ if (p_dst) {
*p_dst = '>';
+ }
eat = 4;
} else if (p_src_len >= 4 && p_src[1] == 'l' && p_src[2] == 't' && p_src[3] == ';') {
-
- if (p_dst)
+ if (p_dst) {
*p_dst = '<';
+ }
eat = 4;
} else if (p_src_len >= 5 && p_src[1] == 'a' && p_src[2] == 'm' && p_src[3] == 'p' && p_src[4] == ';') {
-
- if (p_dst)
+ if (p_dst) {
*p_dst = '&';
+ }
eat = 5;
} else if (p_src_len >= 6 && p_src[1] == 'q' && p_src[2] == 'u' && p_src[3] == 'o' && p_src[4] == 't' && p_src[5] == ';') {
-
- if (p_dst)
+ if (p_dst) {
*p_dst = '"';
+ }
eat = 6;
} else if (p_src_len >= 6 && p_src[1] == 'a' && p_src[2] == 'p' && p_src[3] == 'o' && p_src[4] == 's' && p_src[5] == ';') {
-
- if (p_dst)
+ if (p_dst) {
*p_dst = '\'';
+ }
eat = 6;
} else {
-
- if (p_dst)
+ if (p_dst) {
*p_dst = *p_src;
+ }
eat = 1;
}
- if (p_dst)
+ if (p_dst) {
p_dst++;
+ }
len++;
p_src += eat;
p_src_len -= eat;
} else {
-
if (p_dst) {
*p_dst = *p_src;
p_dst++;
@@ -3630,12 +3474,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);
- if (len == 0)
+ if (len == 0) {
return String();
+ }
str.resize(len + 1);
_xml_unescape(c_str(), l, str.ptrw());
str[len] = 0;
@@ -3643,7 +3487,6 @@ String String::xml_unescape() const {
}
String String::pad_decimals(int p_digits) const {
-
String s = *this;
int c = s.find(".");
@@ -3670,7 +3513,6 @@ String String::pad_decimals(int p_digits) const {
}
String String::pad_zeros(int p_digits) const {
-
String s = *this;
int end = s.find(".");
@@ -3678,8 +3520,9 @@ String String::pad_zeros(int p_digits) const {
end = s.length();
}
- if (end == 0)
+ if (end == 0) {
return s;
+ }
int begin = 0;
@@ -3687,11 +3530,11 @@ String String::pad_zeros(int p_digits) const {
begin++;
}
- if (begin >= end)
+ if (begin >= end) {
return s;
+ }
while (end - begin < p_digits) {
-
s = s.insert(begin, "0");
end++;
}
@@ -3700,7 +3543,6 @@ String String::pad_zeros(int p_digits) const {
}
String String::trim_prefix(const String &p_prefix) const {
-
String s = *this;
if (s.begins_with(p_prefix)) {
return s.substr(p_prefix.length(), s.length() - p_prefix.length());
@@ -3709,7 +3551,6 @@ String String::trim_prefix(const String &p_prefix) const {
}
String String::trim_suffix(const String &p_suffix) const {
-
String s = *this;
if (s.ends_with(p_suffix)) {
return s.substr(0, s.length() - p_suffix.length());
@@ -3718,40 +3559,42 @@ String String::trim_suffix(const String &p_suffix) const {
}
bool String::is_valid_integer() const {
-
int len = length();
- if (len == 0)
+ if (len == 0) {
return false;
+ }
int from = 0;
- if (len != 1 && (operator[](0) == '+' || operator[](0) == '-'))
+ if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
from++;
+ }
for (int i = from; i < len; i++) {
-
- if (operator[](i) < '0' || operator[](i) > '9')
+ if (operator[](i) < '0' || operator[](i) > '9') {
return false; // no start with number plz
+ }
}
return true;
}
bool String::is_valid_hex_number(bool p_with_prefix) const {
-
int len = length();
- if (len == 0)
+ if (len == 0) {
return false;
+ }
int from = 0;
- if (len != 1 && (operator[](0) == '+' || operator[](0) == '-'))
+ if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
from++;
+ }
if (p_with_prefix) {
-
- if (len < 3)
+ if (len < 3) {
return false;
+ }
if (operator[](from) != '0' || operator[](from + 1) != 'x') {
return false;
}
@@ -3759,22 +3602,22 @@ bool String::is_valid_hex_number(bool p_with_prefix) const {
}
for (int i = from; i < len; i++) {
-
CharType c = operator[](i);
- if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
+ if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
continue;
+ }
return false;
}
return true;
-};
+}
bool String::is_valid_float() const {
-
int len = length();
- if (len == 0)
+ if (len == 0) {
return false;
+ }
int from = 0;
if (operator[](0) == '+' || operator[](0) == '-') {
@@ -3788,71 +3631,70 @@ bool String::is_valid_float() const {
bool numbers_found = false;
for (int i = from; i < len; i++) {
-
if (operator[](i) >= '0' && operator[](i) <= '9') {
-
- if (exponent_found)
+ if (exponent_found) {
exponent_values_found = true;
- else
+ } else {
numbers_found = true;
+ }
} else if (numbers_found && !exponent_found && operator[](i) == 'e') {
exponent_found = true;
} else if (!period_found && !exponent_found && operator[](i) == '.') {
period_found = true;
} else if ((operator[](i) == '-' || operator[](i) == '+') && exponent_found && !exponent_values_found && !sign_found) {
sign_found = true;
- } else
+ } else {
return false; // no start with number plz
+ }
}
return numbers_found;
}
String String::path_to_file(const String &p_path) const {
-
// Don't get base dir for src, this is expected to be a dir already.
String src = this->replace("\\", "/");
String dst = p_path.replace("\\", "/").get_base_dir();
String rel = src.path_to(dst);
- if (rel == dst) // failed
+ if (rel == dst) { // failed
return p_path;
- else
+ } else {
return rel + p_path.get_file();
+ }
}
String String::path_to(const String &p_path) const {
-
String src = this->replace("\\", "/");
String dst = p_path.replace("\\", "/");
- if (!src.ends_with("/"))
+ if (!src.ends_with("/")) {
src += "/";
- if (!dst.ends_with("/"))
+ }
+ if (!dst.ends_with("/")) {
dst += "/";
+ }
String base;
if (src.begins_with("res://") && dst.begins_with("res://")) {
-
base = "res:/";
src = src.replace("res://", "/");
dst = dst.replace("res://", "/");
} else if (src.begins_with("user://") && dst.begins_with("user://")) {
-
base = "user:/";
src = src.replace("user://", "/");
dst = dst.replace("user://", "/");
} else if (src.begins_with("/") && dst.begins_with("/")) {
-
//nothing
} else {
//dos style
String src_begin = src.get_slicec('/', 0);
String dst_begin = dst.get_slicec('/', 0);
- if (src_begin != dst_begin)
+ if (src_begin != dst_begin) {
return p_path; //impossible to do this
+ }
base = src_begin;
src = src.substr(src_begin.length(), src.length());
@@ -3867,12 +3709,15 @@ String String::path_to(const String &p_path) const {
int common_parent = 0;
while (true) {
- if (src_dirs.size() == common_parent)
+ if (src_dirs.size() == common_parent) {
break;
- if (dst_dirs.size() == common_parent)
+ }
+ if (dst_dirs.size() == common_parent) {
break;
- if (src_dirs[common_parent] != dst_dirs[common_parent])
+ }
+ if (src_dirs[common_parent] != dst_dirs[common_parent]) {
break;
+ }
common_parent++;
}
@@ -3881,27 +3726,24 @@ String String::path_to(const String &p_path) const {
String dir;
for (int i = src_dirs.size() - 1; i > common_parent; i--) {
-
dir += "../";
}
for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
-
dir += dst_dirs[i] + "/";
}
- if (dir.length() == 0)
+ if (dir.length() == 0) {
dir = "./";
+ }
return dir;
}
bool String::is_valid_html_color() const {
-
return Color::html_is_valid(*this);
}
bool String::is_valid_filename() const {
-
String stripped = strip_edges();
if (*this != stripped) {
return false;
@@ -3915,55 +3757,54 @@ bool String::is_valid_filename() const {
}
bool String::is_valid_ip_address() const {
-
if (find(":") >= 0) {
-
Vector<String> ip = split(":");
for (int i = 0; i < ip.size(); i++) {
-
String n = ip[i];
- if (n.empty())
+ if (n.empty()) {
continue;
+ }
if (n.is_valid_hex_number(false)) {
- int nint = n.hex_to_int(false);
- if (nint < 0 || nint > 0xffff)
+ int64_t nint = n.hex_to_int(false);
+ if (nint < 0 || nint > 0xffff) {
return false;
+ }
continue;
- };
- if (!n.is_valid_ip_address())
+ }
+ if (!n.is_valid_ip_address()) {
return false;
- };
+ }
+ }
} else {
Vector<String> ip = split(".");
- if (ip.size() != 4)
+ if (ip.size() != 4) {
return false;
+ }
for (int i = 0; i < ip.size(); i++) {
-
String n = ip[i];
- if (!n.is_valid_integer())
+ if (!n.is_valid_integer()) {
return false;
+ }
int val = n.to_int();
- if (val < 0 || val > 255)
+ if (val < 0 || val > 255) {
return false;
+ }
}
- };
+ }
return true;
}
bool String::is_resource_file() const {
-
return begins_with("res://") && find("::") == -1;
}
bool String::is_rel_path() const {
-
return !is_abs_path();
}
String String::get_base_dir() const {
-
int basepos = find("://");
String rs;
String base;
@@ -3976,52 +3817,52 @@ String String::get_base_dir() const {
rs = substr(1, length());
base = "/";
} else {
-
rs = *this;
}
}
- int sep = MAX(rs.find_last("/"), rs.find_last("\\"));
- if (sep == -1)
+ int sep = MAX(rs.rfind("/"), rs.rfind("\\"));
+ if (sep == -1) {
return base;
+ }
return base + rs.substr(0, sep);
}
String String::get_file() const {
-
- int sep = MAX(find_last("/"), find_last("\\"));
- if (sep == -1)
+ int sep = MAX(rfind("/"), rfind("\\"));
+ if (sep == -1) {
return *this;
+ }
return substr(sep + 1, length());
}
String String::get_extension() const {
-
- int pos = find_last(".");
- if (pos < 0 || pos < MAX(find_last("/"), find_last("\\")))
+ int pos = rfind(".");
+ if (pos < 0 || pos < MAX(rfind("/"), rfind("\\"))) {
return "";
+ }
return substr(pos + 1, length());
}
String String::plus_file(const String &p_file) const {
- if (empty())
+ if (empty()) {
return p_file;
- if (operator[](length() - 1) == '/' || (p_file.size() > 0 && p_file.operator[](0) == '/'))
+ }
+ if (operator[](length() - 1) == '/' || (p_file.size() > 0 && p_file.operator[](0) == '/')) {
return *this + p_file;
+ }
return *this + "/" + p_file;
}
String String::percent_encode() const {
-
CharString cs = utf8();
String encoded;
for (int i = 0; i < cs.length(); i++) {
uint8_t c = cs[i];
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '~' || c == '.') {
-
char p[2] = { (char)c, 0 };
encoded += p;
} else {
@@ -4036,34 +3877,34 @@ String String::percent_encode() const {
return encoded;
}
-String String::percent_decode() const {
+String String::percent_decode() const {
CharString pe;
CharString cs = utf8();
for (int i = 0; i < cs.length(); i++) {
-
uint8_t c = cs[i];
if (c == '%' && i < length() - 2) {
-
uint8_t a = LOWERCASE(cs[i + 1]);
uint8_t b = LOWERCASE(cs[i + 2]);
- if (a >= '0' && a <= '9')
+ if (a >= '0' && a <= '9') {
c = (a - '0') << 4;
- else if (a >= 'a' && a <= 'f')
+ } else if (a >= 'a' && a <= 'f') {
c = (a - 'a' + 10) << 4;
- else
+ } else {
continue;
+ }
uint8_t d = 0;
- if (b >= '0' && b <= '9')
+ if (b >= '0' && b <= '9') {
d = (b - '0');
- else if (b >= 'a' && b <= 'f')
+ } else if (b >= 'a' && b <= 'f') {
d = (b - 'a' + 10);
- else
+ } else {
continue;
+ }
c += d;
i += 2;
}
@@ -4087,31 +3928,27 @@ String String::property_name_encode() const {
}
String String::get_basename() const {
-
- int pos = find_last(".");
- if (pos < 0 || pos < MAX(find_last("/"), find_last("\\")))
+ int pos = rfind(".");
+ if (pos < 0 || pos < MAX(rfind("/"), rfind("\\"))) {
return *this;
+ }
return substr(0, pos);
}
String itos(int64_t p_val) {
-
return String::num_int64(p_val);
}
String uitos(uint64_t p_val) {
-
return String::num_uint64(p_val);
}
String rtos(double p_val) {
-
return String::num(p_val);
}
String rtoss(double p_val) {
-
return String::num_scientific(p_val);
}
@@ -4120,19 +3957,22 @@ String String::rpad(int min_length, const String &character) const {
String s = *this;
int padding = min_length - s.length();
if (padding > 0) {
- for (int i = 0; i < padding; i++)
+ for (int i = 0; i < padding; i++) {
s = s + character;
+ }
}
return s;
}
+
// Left-pad with a character.
String String::lpad(int min_length, const String &character) const {
String s = *this;
int padding = min_length - s.length();
if (padding > 0) {
- for (int i = 0; i < padding; i++)
+ for (int i = 0; i < padding; i++) {
s = character + s;
+ }
}
return s;
@@ -4182,9 +4022,14 @@ String String::sprintf(const Array &values, bool *error) const {
int base = 16;
bool capitalize = false;
switch (c) {
- case 'd': base = 10; break;
- case 'o': base = 8; break;
- case 'x': break;
+ case 'd':
+ base = 10;
+ break;
+ case 'o':
+ base = 8;
+ break;
+ case 'x':
+ break;
case 'X':
base = 16;
capitalize = true;
@@ -4225,27 +4070,40 @@ String String::sprintf(const Array &values, bool *error) const {
}
double value = values[value_index];
- String str = String::num(value, min_decimals);
+ bool is_negative = (value < 0);
+ String str = String::num(ABS(value), min_decimals);
// Pad decimals out.
str = str.pad_decimals(min_decimals);
- // Show sign
- if (show_sign && str.left(1) != "-") {
- str = str.insert(0, "+");
- }
+ int initial_len = str.length();
- // Padding
+ // Padding. Leave room for sign later if required.
+ int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars;
+ String pad_char = pad_with_zeroes ? String("0") : String(" ");
if (left_justified) {
- str = str.rpad(min_chars);
+ if (pad_with_zeroes) {
+ return "left justification cannot be used with zeros as the padding";
+ } else {
+ str = str.rpad(pad_chars_count, pad_char);
+ }
} else {
- str = str.lpad(min_chars);
+ str = str.lpad(pad_chars_count, pad_char);
+ }
+
+ // Add sign if needed.
+ if (show_sign || is_negative) {
+ String sign_char = is_negative ? "-" : "+";
+ if (left_justified) {
+ str = str.insert(0, sign_char);
+ } else {
+ str = str.insert(pad_with_zeroes ? 0 : str.length() - initial_len, sign_char);
+ }
}
formatted += str;
++value_index;
in_format = false;
-
break;
}
case 's': { // String
@@ -4420,13 +4278,14 @@ String TTR(const String &p_text) {
}
String DTR(const String &p_text) {
+ // Comes straight from the XML, so remove indentation and any trailing whitespace.
+ const String text = p_text.dedent().strip_edges();
+
if (TranslationServer::get_singleton()) {
- // Comes straight from the XML, so remove indentation and any trailing whitespace.
- const String text = p_text.dedent().strip_edges();
return TranslationServer::get_singleton()->doc_translate(text);
}
- return p_text;
+ return text;
}
#endif