2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* ustring.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2019-01-01 12:53:14 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "ustring.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/color.h"
|
2019-07-02 03:06:52 +02:00
|
|
|
#include "core/math/crypto_core.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
|
|
|
#include "core/os/memory.h"
|
|
|
|
#include "core/print_string.h"
|
|
|
|
#include "core/translation.h"
|
|
|
|
#include "core/ucaps.h"
|
|
|
|
#include "core/variant.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-16 08:04:19 +01:00
|
|
|
#include <wchar.h>
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#ifndef NO_USE_STDLIB
|
|
|
|
#include <stdio.h>
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <stdlib.h>
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
2015-11-19 22:09:57 +01:00
|
|
|
|
|
|
|
#if defined(MINGW_ENABLED) || defined(_MSC_VER)
|
2018-10-04 15:38:52 +02:00
|
|
|
#define snprintf _snprintf_s
|
2015-11-19 22:09:57 +01:00
|
|
|
#endif
|
|
|
|
|
2017-01-16 08:04:19 +01:00
|
|
|
#define MAX_DIGITS 6
|
2017-03-05 16:44:50 +01:00
|
|
|
#define UPPERCASE(m_c) (((m_c) >= 'a' && (m_c) <= 'z') ? ((m_c) - ('a' - 'A')) : (m_c))
|
|
|
|
#define LOWERCASE(m_c) (((m_c) >= 'A' && (m_c) <= 'Z') ? ((m_c) + ('a' - 'A')) : (m_c))
|
2017-05-11 21:07:59 +02:00
|
|
|
#define IS_DIGIT(m_d) ((m_d) >= '0' && (m_d) <= '9')
|
|
|
|
#define IS_HEX_DIGIT(m_d) (((m_d) >= '0' && (m_d) <= '9') || ((m_d) >= 'a' && (m_d) <= 'f') || ((m_d) >= 'A' && (m_d) <= 'F'))
|
2017-02-21 04:05:15 +01:00
|
|
|
|
2019-01-04 17:01:54 +01:00
|
|
|
const char CharString::_null = 0;
|
|
|
|
const CharType String::_null = 0;
|
|
|
|
|
2017-12-23 09:59:54 +01:00
|
|
|
bool is_symbol(CharType c) {
|
|
|
|
return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || 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]))) {
|
|
|
|
beg--;
|
|
|
|
}
|
|
|
|
while (end < s.length() && s[end + 1] > 32 && (symbol == is_symbol(s[end + 1]))) {
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end < s.length())
|
|
|
|
end += 1;
|
|
|
|
|
|
|
|
r_beg = beg;
|
|
|
|
r_end = end;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/** STRING **/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool CharString::operator<(const CharString &p_right) const {
|
2017-02-21 04:05:15 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (length() == 0) {
|
|
|
|
return p_right.length() != 0;
|
2017-02-21 04:05:15 +01:00
|
|
|
}
|
|
|
|
|
2017-12-16 15:31:30 +01:00
|
|
|
return is_str_less(get_data(), p_right.get_data());
|
2017-02-21 04:05:15 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
CharString &CharString::operator+=(char p_char) {
|
|
|
|
|
|
|
|
resize(size() ? size() + 1 : 2);
|
|
|
|
set(length(), 0);
|
|
|
|
set(length() - 1, p_char);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
const char *CharString::get_data() const {
|
|
|
|
|
|
|
|
if (size())
|
|
|
|
return &operator[](0);
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:57:22 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len = strlen(p_cstr);
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
resize(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resize(len + 1); // include terminating null char
|
|
|
|
|
|
|
|
strcpy(ptrw(), p_cstr);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void String::copy_from(const char *p_cstr) {
|
|
|
|
|
2017-06-25 15:57:06 +02:00
|
|
|
if (!p_cstr) {
|
|
|
|
|
|
|
|
resize(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = 0;
|
|
|
|
const char *ptr = p_cstr;
|
|
|
|
while (*(ptr++) != 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
len++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
resize(0);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
resize(len + 1); // include 0
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-11-25 04:07:54 +01:00
|
|
|
CharType *dst = this->ptrw();
|
2015-06-30 16:59:00 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < len + 1; i++) {
|
2015-06-30 16:59:00 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dst[i] = p_cstr[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 23:51:07 +02:00
|
|
|
void String::copy_from(const CharType *p_cstr, const int p_clip_to) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-06-25 15:57:06 +02:00
|
|
|
if (!p_cstr) {
|
|
|
|
|
|
|
|
resize(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = 0;
|
|
|
|
const CharType *ptr = p_cstr;
|
2018-08-06 23:51:07 +02:00
|
|
|
while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
len++;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
resize(0);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-07-24 17:06:49 +02:00
|
|
|
copy_from_unchecked(p_cstr, len);
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-07-24 17:06:49 +02:00
|
|
|
// assumes the following have already been validated:
|
|
|
|
// p_char != NULL
|
|
|
|
// p_length > 0
|
|
|
|
// p_length <= p_char strlen
|
2018-08-06 23:51:07 +02:00
|
|
|
void String::copy_from_unchecked(const CharType *p_char, const int p_length) {
|
2018-07-24 17:06:49 +02:00
|
|
|
resize(p_length + 1);
|
|
|
|
set(p_length, 0);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-12-16 01:44:18 +01:00
|
|
|
CharType *dst = ptrw();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-07-24 17:06:49 +02:00
|
|
|
for (int i = 0; i < p_length; i++) {
|
|
|
|
dst[i] = p_char[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void String::copy_from(const CharType &p_char) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
resize(2);
|
2017-03-05 16:44:50 +01:00
|
|
|
set(0, p_char);
|
|
|
|
set(1, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::operator==(const String &p_str) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (length() != p_str.length())
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
if (empty())
|
|
|
|
return true;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int l = length();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
const CharType *src = c_str();
|
|
|
|
const CharType *dst = p_str.c_str();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/* Compare char by char */
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < l; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src[i] != dst[i])
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::operator!=(const String &p_str) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return !(*this == p_str);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::operator+(const String &p_str) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String res = *this;
|
|
|
|
res += p_str;
|
2014-02-10 02:10:30 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
String String::operator+(CharType p_chr) const {
|
|
|
|
|
|
|
|
String res=*this;
|
|
|
|
res+=p_chr;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
*/
|
2017-03-05 16:44:50 +01:00
|
|
|
String &String::operator+=(const String &p_str) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (empty()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
*this = p_str;
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_str.empty())
|
|
|
|
return *this;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = length();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
resize(length() + p_str.size());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
const CharType *src = p_str.c_str();
|
2018-12-16 01:44:18 +01:00
|
|
|
CharType *dst = ptrw();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
set(length(), 0);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_str.length(); i++)
|
|
|
|
dst[from + i] = src[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String &String::operator+=(const CharType *p_str) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*this += String(p_str);
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String &String::operator+=(CharType p_char) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
resize(size() ? size() + 1 : 2);
|
|
|
|
set(length(), 0);
|
|
|
|
set(length() - 1, p_char);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String &String::operator+=(const char *p_str) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!p_str || p_str[0] == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int src_len = 0;
|
|
|
|
const char *ptr = p_str;
|
|
|
|
while (*(ptr++) != 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
src_len++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = length();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
resize(from + src_len + 1);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-12-16 01:44:18 +01:00
|
|
|
CharType *dst = ptrw();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
set(length(), 0);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < src_len; i++)
|
|
|
|
dst[from + i] = p_str[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::operator=(const char *p_str) {
|
|
|
|
|
|
|
|
copy_from(p_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::operator=(const CharType *p_str) {
|
|
|
|
|
|
|
|
copy_from(p_str);
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
bool String::operator==(const StrRange &p_str_range) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
int len = p_str_range.len;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (length() != len)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
if (empty())
|
|
|
|
return true;
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
const CharType *c_str = p_str_range.c_str;
|
2016-06-24 17:39:58 +02:00
|
|
|
const CharType *dst = &operator[](0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* Compare char by char */
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < len; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c_str[i] != dst[i])
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::operator==(const char *p_str) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = 0;
|
|
|
|
const char *aux = p_str;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*(aux++) != 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
len++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (length() != len)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
if (empty())
|
|
|
|
return true;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int l = length();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
const CharType *dst = c_str();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/* Compare char by char */
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < l; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_str[i] != dst[i])
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::operator==(const CharType *p_str) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = 0;
|
|
|
|
const CharType *aux = p_str;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*(aux++) != 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
len++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (length() != len)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
if (empty())
|
|
|
|
return true;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int l = length();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
const CharType *dst = c_str();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/* Compare char by char */
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < l; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_str[i] != dst[i])
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::operator!=(const char *p_str) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (!(*this == p_str));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool String::operator!=(const CharType *p_str) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (!(*this == p_str));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool String::operator<(const CharType *p_str) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (empty() && p_str[0] == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
if (empty())
|
|
|
|
return true;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-12-16 15:31:30 +01:00
|
|
|
return is_str_less(c_str(), p_str);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
bool String::operator<=(const String &p_str) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (*this < p_str) || (*this == p_str);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool String::operator<(const char *p_str) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (empty() && p_str[0] == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
if (empty())
|
|
|
|
return true;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-12-16 15:31:30 +01:00
|
|
|
return is_str_less(c_str(), p_str);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
bool String::operator<(const String &p_str) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return operator<(p_str.c_str());
|
|
|
|
}
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
signed char String::nocasecmp_to(const String &p_str) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (empty() && p_str.empty())
|
|
|
|
return 0;
|
|
|
|
if (empty())
|
|
|
|
return -1;
|
|
|
|
if (p_str.empty())
|
|
|
|
return 1;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *that_str = p_str.c_str();
|
|
|
|
const CharType *this_str = c_str();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
while (true) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (*that_str == 0 && *this_str == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0; //we're equal
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (*this_str == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1; //if this is empty, and the other one is not, then we're less.. I think?
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (*that_str == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 1; //otherwise the other one is smaller..
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (_find_upper(*this_str) < _find_upper(*that_str)) //more than
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (_find_upper(*this_str) > _find_upper(*that_str)) //less than
|
2014-02-10 02:10:30 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
this_str++;
|
|
|
|
that_str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
signed char String::casecmp_to(const String &p_str) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (empty() && p_str.empty())
|
|
|
|
return 0;
|
|
|
|
if (empty())
|
|
|
|
return -1;
|
|
|
|
if (p_str.empty())
|
|
|
|
return 1;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *that_str = p_str.c_str();
|
|
|
|
const CharType *this_str = c_str();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
while (true) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (*that_str == 0 && *this_str == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0; //we're equal
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (*this_str == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1; //if this is empty, and the other one is not, then we're less.. I think?
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (*that_str == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 1; //otherwise the other one is smaller..
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (*this_str < *that_str) //more than
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (*this_str > *that_str) //less than
|
2014-02-10 02:10:30 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
this_str++;
|
|
|
|
that_str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 21:07:59 +02:00
|
|
|
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) {
|
2017-05-29 04:56:40 +02:00
|
|
|
|
|
|
|
while (*this_str == '.' || *that_str == '.') {
|
|
|
|
if (*this_str++ != '.')
|
|
|
|
return 1;
|
|
|
|
if (*that_str++ != '.')
|
|
|
|
return -1;
|
|
|
|
if (!*that_str)
|
|
|
|
return 1;
|
|
|
|
if (!*this_str)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-11 21:07:59 +02:00
|
|
|
while (*this_str) {
|
|
|
|
|
|
|
|
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);
|
|
|
|
that_int = to_int(that_str);
|
|
|
|
|
|
|
|
if (this_int < that_int)
|
|
|
|
return -1;
|
|
|
|
else if (this_int > that_int)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Skip */
|
|
|
|
while (IS_DIGIT(*this_str))
|
|
|
|
this_str++;
|
|
|
|
while (IS_DIGIT(*that_str))
|
|
|
|
that_str++;
|
|
|
|
} else if (IS_DIGIT(*that_str))
|
|
|
|
return 1;
|
|
|
|
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
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
this_str++;
|
|
|
|
that_str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*that_str)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void String::erase(int p_pos, int p_chars) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*this = left(p_pos) + substr(p_pos + p_chars, length() - ((p_pos + p_chars)));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String String::capitalize() const {
|
|
|
|
|
2017-11-14 21:32:37 +01:00
|
|
|
String aux = this->camelcase_to_underscore(true).replace("_", " ").strip_edges();
|
2014-02-10 02:10:30 +01:00
|
|
|
String cap;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < aux.get_slice_count(" "); i++) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String slice = aux.get_slicec(' ', i);
|
|
|
|
if (slice.length() > 0) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
slice[0] = _find_upper(slice[0]);
|
|
|
|
if (i > 0)
|
|
|
|
cap += " ";
|
|
|
|
cap += slice;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return cap;
|
|
|
|
}
|
2015-03-23 06:08:07 +01:00
|
|
|
|
2016-02-17 19:26:22 +01:00
|
|
|
String String::camelcase_to_underscore(bool lowercase) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *cstr = c_str();
|
2016-02-17 19:26:22 +01:00
|
|
|
String new_string;
|
2015-03-23 06:08:07 +01:00
|
|
|
const char A = 'A', Z = 'Z';
|
2016-02-17 19:26:22 +01:00
|
|
|
const char a = 'a', z = 'z';
|
|
|
|
int start_index = 0;
|
2015-03-23 06:08:07 +01:00
|
|
|
|
2017-08-31 23:30:35 +02:00
|
|
|
for (int i = 1; i < this->size(); i++) {
|
2016-02-17 19:26:22 +01:00
|
|
|
bool is_upper = cstr[i] >= A && cstr[i] <= Z;
|
2017-04-23 23:03:16 +02:00
|
|
|
bool is_number = cstr[i] >= '0' && cstr[i] <= '9';
|
2016-02-17 19:26:22 +01:00
|
|
|
bool are_next_2_lower = false;
|
2018-09-30 06:33:21 +02:00
|
|
|
bool is_next_lower = false;
|
|
|
|
bool is_next_number = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool was_precedent_upper = cstr[i - 1] >= A && cstr[i - 1] <= Z;
|
2017-04-23 23:03:16 +02:00
|
|
|
bool was_precedent_number = cstr[i - 1] >= '0' && cstr[i - 1] <= '9';
|
2015-03-23 06:08:07 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i + 2 < this->size()) {
|
|
|
|
are_next_2_lower = cstr[i + 1] >= a && cstr[i + 1] <= z && cstr[i + 2] >= a && cstr[i + 2] <= z;
|
2015-03-23 06:08:07 +01:00
|
|
|
}
|
|
|
|
|
2018-09-30 06:33:21 +02:00
|
|
|
if (i + 1 < this->size()) {
|
|
|
|
is_next_lower = cstr[i + 1] >= a && cstr[i + 1] <= z;
|
|
|
|
is_next_number = cstr[i + 1] >= '0' && cstr[i + 1] <= '9';
|
|
|
|
}
|
|
|
|
|
2019-02-12 21:10:08 +01:00
|
|
|
const bool cond_a = is_upper && !was_precedent_upper && !was_precedent_number;
|
|
|
|
const bool cond_b = was_precedent_upper && is_upper && are_next_2_lower;
|
|
|
|
const bool cond_c = is_number && !was_precedent_number;
|
2018-09-30 06:33:21 +02:00
|
|
|
const bool can_break_number_letter = is_number && !was_precedent_number && is_next_lower;
|
|
|
|
const bool can_break_letter_number = !is_number && was_precedent_number && (is_next_lower || is_next_number);
|
|
|
|
|
2019-02-12 21:10:08 +01:00
|
|
|
bool should_split = cond_a || cond_b || cond_c || can_break_number_letter || can_break_letter_number;
|
2016-02-17 19:26:22 +01:00
|
|
|
if (should_split) {
|
|
|
|
new_string += this->substr(start_index, i - start_index) + "_";
|
|
|
|
start_index = i;
|
|
|
|
}
|
|
|
|
}
|
2015-03-23 06:08:07 +01:00
|
|
|
|
2016-02-17 19:26:22 +01:00
|
|
|
new_string += this->substr(start_index, this->size() - start_index);
|
|
|
|
return lowercase ? new_string.to_lower() : new_string;
|
2015-03-23 06:08:07 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int String::get_slice_count(String p_splitter) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (empty())
|
|
|
|
return 0;
|
|
|
|
if (p_splitter.empty())
|
|
|
|
return 0;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int pos = 0;
|
|
|
|
int slices = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while ((pos = find(p_splitter, pos)) >= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
slices++;
|
2017-03-05 16:44:50 +01:00
|
|
|
pos += p_splitter.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return slices;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::get_slice(String p_splitter, int p_slice) const {
|
|
|
|
|
|
|
|
if (empty() || p_splitter.empty())
|
|
|
|
return "";
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int pos = 0;
|
|
|
|
int prev_pos = 0;
|
2017-01-14 12:26:56 +01:00
|
|
|
//int slices=1;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_slice < 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
2017-03-05 16:44:50 +01:00
|
|
|
if (find(p_splitter) == -1)
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int i = 0;
|
|
|
|
while (true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = find(p_splitter, pos);
|
|
|
|
if (pos == -1)
|
|
|
|
pos = length(); //reached end
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = prev_pos;
|
2017-01-14 12:26:56 +01:00
|
|
|
//int to=pos;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_slice == i) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return substr(from, pos - from);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pos == length()) //reached end and no find
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
pos += p_splitter.length();
|
|
|
|
prev_pos = pos;
|
2014-02-10 02:10:30 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""; //no find!
|
|
|
|
}
|
|
|
|
|
2015-06-29 05:29:49 +02:00
|
|
|
String String::get_slicec(CharType p_splitter, int p_slice) const {
|
|
|
|
|
|
|
|
if (empty())
|
|
|
|
return String();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_slice < 0)
|
2015-06-29 05:29:49 +02:00
|
|
|
return String();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *c = this->ptr();
|
|
|
|
int i = 0;
|
|
|
|
int prev = 0;
|
|
|
|
int count = 0;
|
|
|
|
while (true) {
|
2015-06-29 05:29:49 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c[i] == 0 || c[i] == p_splitter) {
|
2015-06-29 05:29:49 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_slice == count) {
|
2015-06-29 05:29:49 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return substr(prev, i - prev);
|
|
|
|
} else if (c[i] == 0) {
|
2016-12-21 18:20:35 +01:00
|
|
|
return String();
|
2015-06-29 05:29:49 +02:00
|
|
|
} else {
|
|
|
|
count++;
|
2017-03-05 16:44:50 +01:00
|
|
|
prev = i + 1;
|
2015-06-29 05:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 00:44:27 +02:00
|
|
|
Vector<String> String::split_spaces() const {
|
|
|
|
|
|
|
|
Vector<String> ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
|
|
|
int i = 0;
|
2014-10-10 00:44:27 +02:00
|
|
|
int len = length();
|
2018-01-15 22:40:41 +01:00
|
|
|
if (len == 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool inside = false;
|
2014-10-10 00:44:27 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2014-10-10 00:44:27 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool empty = operator[](i) < 33;
|
2014-10-10 00:44:27 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i == 0)
|
|
|
|
inside = !empty;
|
2014-10-10 00:44:27 +02:00
|
|
|
|
|
|
|
if (!empty && !inside) {
|
2017-03-05 16:44:50 +01:00
|
|
|
inside = true;
|
|
|
|
from = i;
|
2014-10-10 00:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty && inside) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ret.push_back(substr(from, i - from));
|
|
|
|
inside = false;
|
2014-10-10 00:44:27 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i == len)
|
2014-10-10 00:44:27 +02:00
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-12 03:14:38 +01:00
|
|
|
Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector<String> ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int end = find(p_splitter, from);
|
|
|
|
if (end < 0)
|
|
|
|
end = len;
|
2017-12-12 03:14:38 +01:00
|
|
|
if (p_allow_empty || (end > from)) {
|
|
|
|
if (p_maxsplit <= 0)
|
|
|
|
ret.push_back(substr(from, end - from));
|
2019-07-07 23:08:51 +02:00
|
|
|
else {
|
2017-12-12 03:14:38 +01:00
|
|
|
|
|
|
|
// Put rest of the string and leave cycle.
|
|
|
|
if (p_maxsplit == ret.size()) {
|
|
|
|
ret.push_back(substr(from, len));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, push items until positive limit is reached.
|
|
|
|
ret.push_back(substr(from, end - from));
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (end == len)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
from = end + p_splitter.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-15 20:23:58 +01:00
|
|
|
Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
|
|
|
|
|
|
|
|
Vector<String> ret;
|
|
|
|
const int len = length();
|
2018-07-17 20:45:29 +02:00
|
|
|
int remaining_len = len;
|
2017-12-15 20:23:58 +01:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
2018-07-17 20:45:29 +02:00
|
|
|
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) {
|
|
|
|
ret.push_back(substr(0, remaining_len));
|
2017-12-15 20:23:58 +01:00
|
|
|
}
|
2018-07-17 20:45:29 +02:00
|
|
|
break;
|
2017-12-15 20:23:58 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 20:45:29 +02:00
|
|
|
int left_edge = rfind(p_splitter, remaining_len - p_splitter.length());
|
|
|
|
|
|
|
|
if (left_edge < 0) {
|
|
|
|
// no more splitters, we're done
|
|
|
|
ret.push_back(substr(0, remaining_len));
|
2017-12-15 20:23:58 +01:00
|
|
|
break;
|
2018-07-17 20:45:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int substr_start = left_edge + p_splitter.length();
|
|
|
|
if (p_allow_empty || substr_start < remaining_len) {
|
|
|
|
ret.push_back(substr(substr_start, remaining_len - substr_start));
|
|
|
|
}
|
2017-12-15 20:23:58 +01:00
|
|
|
|
2018-07-17 20:45:29 +02:00
|
|
|
remaining_len = left_edge;
|
2017-12-15 20:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.invert();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector<float> ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int end = find(p_splitter, from);
|
|
|
|
if (end < 0)
|
|
|
|
end = len;
|
|
|
|
if (p_allow_empty || (end > from))
|
2015-01-03 15:06:53 +01:00
|
|
|
ret.push_back(String::to_double(&c_str()[from]));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (end == len)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
from = end + p_splitter.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector<float> ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int idx;
|
2017-03-05 16:44:50 +01:00
|
|
|
int end = findmk(p_splitters, from, &idx);
|
|
|
|
int spl_len = 1;
|
|
|
|
if (end < 0) {
|
|
|
|
end = len;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
spl_len = p_splitters[idx].length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_allow_empty || (end > from)) {
|
2015-01-03 15:06:53 +01:00
|
|
|
ret.push_back(String::to_double(&c_str()[from]));
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (end == len)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
from = end + spl_len;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<int> String::split_ints(const String &p_splitter, bool p_allow_empty) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector<int> ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int end = find(p_splitter, from);
|
|
|
|
if (end < 0)
|
|
|
|
end = len;
|
|
|
|
if (p_allow_empty || (end > from))
|
|
|
|
ret.push_back(String::to_int(&c_str()[from], end - from));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (end == len)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
from = end + p_splitter.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector<int> ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int idx;
|
2017-03-05 16:44:50 +01:00
|
|
|
int end = findmk(p_splitters, from, &idx);
|
|
|
|
int spl_len = 1;
|
|
|
|
if (end < 0) {
|
|
|
|
end = len;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
spl_len = p_splitters[idx].length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_allow_empty || (end > from))
|
|
|
|
ret.push_back(String::to_int(&c_str()[from], end - from));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (end == len)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
from = end + spl_len;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-02 17:01:43 +02:00
|
|
|
String String::join(Vector<String> parts) {
|
|
|
|
String ret;
|
|
|
|
for (int i = 0; i < parts.size(); ++i) {
|
|
|
|
if (i > 0) {
|
|
|
|
ret += *this;
|
|
|
|
}
|
|
|
|
ret += parts[i];
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
CharType String::char_uppercase(CharType p_char) {
|
|
|
|
|
|
|
|
return _find_upper(p_char);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType String::char_lowercase(CharType p_char) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return _find_lower(p_char);
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::to_upper() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String upper = *this;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < upper.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-07-22 00:55:52 +02:00
|
|
|
const CharType s = upper[i];
|
|
|
|
const CharType t = _find_upper(s);
|
2017-12-11 23:38:07 +01:00
|
|
|
if (s != t) // avoid copy on write
|
|
|
|
upper[i] = t;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return upper;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::to_lower() const {
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
String lower = *this;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
for (int i = 0; i < lower.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-07-22 00:55:52 +02:00
|
|
|
const CharType s = lower[i];
|
|
|
|
const CharType t = _find_lower(s);
|
2017-12-11 23:38:07 +01:00
|
|
|
if (s != t) // avoid copy on write
|
|
|
|
lower[i] = t;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
return lower;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *String::c_str() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const CharType zero = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return size() ? &operator[](0) : &zero;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String String::md5(const uint8_t *p_md5) {
|
2016-06-17 09:55:16 +02:00
|
|
|
return String::hex_encode_buffer(p_md5, 16);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-17 09:55:16 +02:00
|
|
|
String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
|
2017-03-05 16:44:50 +01:00
|
|
|
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-17 09:55:16 +02:00
|
|
|
String ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
char v[2] = { 0, 0 };
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::chr(CharType p_char) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType c[2] = { p_char, 0 };
|
2014-02-10 02:10:30 +01:00
|
|
|
return String(c);
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::num(double p_num, int p_decimals) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#ifndef NO_USE_STDLIB
|
|
|
|
|
2018-03-06 20:40:29 +01:00
|
|
|
if (p_decimals > 16)
|
|
|
|
p_decimals = 16;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
char fmt[7];
|
2017-03-05 16:44:50 +01:00
|
|
|
fmt[0] = '%';
|
|
|
|
fmt[1] = '.';
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_decimals < 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
fmt[1] = 'l';
|
|
|
|
fmt[2] = 'f';
|
|
|
|
fmt[3] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (p_decimals < 10) {
|
|
|
|
fmt[2] = '0' + p_decimals;
|
|
|
|
fmt[3] = 'l';
|
|
|
|
fmt[4] = 'f';
|
|
|
|
fmt[5] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
fmt[2] = '0' + (p_decimals / 10);
|
|
|
|
fmt[3] = '0' + (p_decimals % 10);
|
|
|
|
fmt[4] = 'l';
|
|
|
|
fmt[5] = 'f';
|
|
|
|
fmt[6] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
char buf[256];
|
|
|
|
|
2015-11-20 00:19:44 +01:00
|
|
|
#if defined(__GNUC__) || defined(_MSC_VER)
|
2017-03-05 16:44:50 +01:00
|
|
|
snprintf(buf, 256, fmt, p_num);
|
2014-02-10 02:10:30 +01:00
|
|
|
#else
|
2017-03-05 16:44:50 +01:00
|
|
|
sprintf(buf, fmt, p_num);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
buf[255] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
//destroy trailing zeroes
|
|
|
|
{
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool period = false;
|
|
|
|
int z = 0;
|
|
|
|
while (buf[z]) {
|
|
|
|
if (buf[z] == '.')
|
|
|
|
period = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
z++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (period) {
|
|
|
|
z--;
|
2017-03-05 16:44:50 +01:00
|
|
|
while (z > 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (buf[z] == '0') {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
buf[z] = 0;
|
|
|
|
} else if (buf[z] == '.') {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
buf[z] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
z--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
#else
|
|
|
|
|
|
|
|
String s;
|
|
|
|
String sd;
|
|
|
|
/* integer part */
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool neg = p_num < 0;
|
|
|
|
p_num = ABS(p_num);
|
|
|
|
int intn = (int)p_num;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* decimal part */
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_decimals > 0 || (p_decimals == -1 && (int)p_num != p_num)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
double dec = p_num - (float)((int)p_num);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int digit = 0;
|
|
|
|
if (p_decimals > MAX_DIGITS)
|
|
|
|
p_decimals = MAX_DIGITS;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int dec_int = 0;
|
|
|
|
int dec_max = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dec *= 10.0;
|
|
|
|
dec_int = dec_int * 10 + (int)dec % 10;
|
|
|
|
dec_max = dec_max * 10 + 9;
|
2014-02-10 02:10:30 +01:00
|
|
|
digit++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_decimals == -1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (digit == MAX_DIGITS) //no point in going to infinite
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((dec - (float)((int)dec)) < 1e-6)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (digit == p_decimals)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
dec *= 10;
|
|
|
|
int last = (int)dec % 10;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (last > 5) {
|
|
|
|
if (dec_int == dec_max) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dec_int = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
intn++;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
dec_int++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String decimal;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < digit; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
char num[2] = { 0, 0 };
|
|
|
|
num[0] = '0' + dec_int % 10;
|
|
|
|
decimal = num + decimal;
|
|
|
|
dec_int /= 10;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
sd = '.' + decimal;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (intn == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
s = "0";
|
2014-02-10 02:10:30 +01:00
|
|
|
else {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (intn) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType num = '0' + (intn % 10);
|
|
|
|
intn /= 10;
|
|
|
|
s = num + s;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
s = s + sd;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (neg)
|
2017-03-05 16:44:50 +01:00
|
|
|
s = "-" + s;
|
2014-02-10 02:10:30 +01:00
|
|
|
return s;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:42:36 +01:00
|
|
|
String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool sign = p_num < 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-01-13 21:04:30 +01:00
|
|
|
int64_t n = p_num;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int chars = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
do {
|
2017-03-05 16:44:50 +01:00
|
|
|
n /= base;
|
2014-02-10 02:10:30 +01:00
|
|
|
chars++;
|
2017-03-05 16:44:50 +01:00
|
|
|
} while (n);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (sign)
|
|
|
|
chars++;
|
|
|
|
String s;
|
2017-03-05 16:44:50 +01:00
|
|
|
s.resize(chars + 1);
|
2017-11-25 04:07:54 +01:00
|
|
|
CharType *c = s.ptrw();
|
2017-03-05 16:44:50 +01:00
|
|
|
c[chars] = 0;
|
2018-01-13 21:04:30 +01:00
|
|
|
n = p_num;
|
2014-02-10 02:10:30 +01:00
|
|
|
do {
|
2018-01-13 21:04:30 +01:00
|
|
|
int mod = ABS(n % base);
|
2015-02-01 19:42:36 +01:00
|
|
|
if (mod >= 10) {
|
|
|
|
char a = (capitalize_hex ? 'A' : 'a');
|
2017-03-05 16:44:50 +01:00
|
|
|
c[--chars] = a + (mod - 10);
|
2015-02-01 19:42:36 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
c[--chars] = '0' + mod;
|
2015-02-01 19:42:36 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
n /= base;
|
|
|
|
} while (n);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (sign)
|
2017-03-05 16:44:50 +01:00
|
|
|
c[0] = '-';
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-02-22 13:13:51 +01:00
|
|
|
String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
|
|
|
|
|
|
|
|
uint64_t n = p_num;
|
|
|
|
|
|
|
|
int chars = 0;
|
|
|
|
do {
|
|
|
|
n /= base;
|
|
|
|
chars++;
|
|
|
|
} while (n);
|
|
|
|
|
|
|
|
String s;
|
|
|
|
s.resize(chars + 1);
|
|
|
|
CharType *c = s.ptrw();
|
|
|
|
c[chars] = 0;
|
|
|
|
n = p_num;
|
|
|
|
do {
|
2018-04-18 22:20:39 +02:00
|
|
|
int mod = n % base;
|
2018-02-22 13:13:51 +01:00
|
|
|
if (mod >= 10) {
|
|
|
|
char a = (capitalize_hex ? 'A' : 'a');
|
|
|
|
c[--chars] = a + (mod - 10);
|
|
|
|
} else {
|
|
|
|
c[--chars] = '0' + mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
n /= base;
|
|
|
|
} while (n);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String String::num_real(double p_num) {
|
|
|
|
|
|
|
|
String s;
|
|
|
|
String sd;
|
|
|
|
/* integer part */
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool neg = p_num < 0;
|
|
|
|
p_num = ABS(p_num);
|
|
|
|
int intn = (int)p_num;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* decimal part */
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((int)p_num != p_num) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
double dec = p_num - (float)((int)p_num);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int digit = 0;
|
|
|
|
int decimals = MAX_DIGITS;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int dec_int = 0;
|
|
|
|
int dec_max = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dec *= 10.0;
|
|
|
|
dec_int = dec_int * 10 + (int)dec % 10;
|
|
|
|
dec_max = dec_max * 10 + 9;
|
2014-02-10 02:10:30 +01:00
|
|
|
digit++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((dec - (float)((int)dec)) < 1e-6)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (digit == decimals)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dec *= 10;
|
|
|
|
int last = (int)dec % 10;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (last > 5) {
|
|
|
|
if (dec_int == dec_max) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dec_int = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
intn++;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
dec_int++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String decimal;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < digit; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
char num[2] = { 0, 0 };
|
|
|
|
num[0] = '0' + dec_int % 10;
|
|
|
|
decimal = num + decimal;
|
|
|
|
dec_int /= 10;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
sd = '.' + decimal;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
sd = ".0";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (intn == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
s = "0";
|
2014-02-10 02:10:30 +01:00
|
|
|
else {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (intn) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType num = '0' + (intn % 10);
|
|
|
|
intn /= 10;
|
|
|
|
s = num + s;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
s = s + sd;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (neg)
|
2017-03-05 16:44:50 +01:00
|
|
|
s = "-" + s;
|
2014-02-10 02:10:30 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::num_scientific(double p_num) {
|
|
|
|
|
|
|
|
#ifndef NO_USE_STDLIB
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
|
2015-11-20 00:19:44 +01:00
|
|
|
#if defined(__GNUC__) || defined(_MSC_VER)
|
2017-03-05 16:44:50 +01:00
|
|
|
snprintf(buf, 256, "%lg", p_num);
|
2014-02-10 02:10:30 +01:00
|
|
|
#else
|
2017-03-05 16:44:50 +01:00
|
|
|
sprintf(buf, "%.16lg", p_num);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
buf[255] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
#else
|
|
|
|
|
|
|
|
return String::num(p_num);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CharString String::ascii(bool p_allow_extended) const {
|
|
|
|
|
|
|
|
if (!length())
|
|
|
|
return CharString();
|
|
|
|
|
|
|
|
CharString cs;
|
|
|
|
cs.resize(size());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < size(); i++)
|
|
|
|
cs[i] = operator[](i);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::utf8(const char *p_utf8, int p_len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
ret.parse_utf8(p_utf8, p_len);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::parse_utf8(const char *p_utf8, int p_len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-08-24 09:35:07 +02:00
|
|
|
#define _UNICERROR(m_err) print_line("Unicode error: " + String(m_err));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-02-22 19:27:13 +01:00
|
|
|
if (!p_utf8)
|
|
|
|
return true;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String aux;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int cstr_size = 0;
|
|
|
|
int str_size = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* HANDLE BOM (Byte Order Mark) */
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_len < 0 || p_len >= 3) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool has_bom = uint8_t(p_utf8[0]) == 0xEF && uint8_t(p_utf8[1]) == 0xBB && uint8_t(p_utf8[2]) == 0xBF;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (has_bom) {
|
|
|
|
|
|
|
|
//just skip it
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_len >= 0)
|
|
|
|
p_len -= 3;
|
|
|
|
p_utf8 += 3;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *ptrtmp = p_utf8;
|
|
|
|
const char *ptrtmp_limit = &p_utf8[p_len];
|
|
|
|
int skip = 0;
|
|
|
|
while (ptrtmp != ptrtmp_limit && *ptrtmp) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (skip == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
uint8_t c = *ptrtmp;
|
|
|
|
|
|
|
|
/* Determine the number of characters in sequence */
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((c & 0x80) == 0)
|
|
|
|
skip = 0;
|
|
|
|
else if ((c & 0xE0) == 0xC0)
|
|
|
|
skip = 1;
|
|
|
|
else if ((c & 0xF0) == 0xE0)
|
|
|
|
skip = 2;
|
|
|
|
else if ((c & 0xF8) == 0xF0)
|
|
|
|
skip = 3;
|
|
|
|
else if ((c & 0xFC) == 0xF8)
|
|
|
|
skip = 4;
|
|
|
|
else if ((c & 0xFE) == 0xFC)
|
|
|
|
skip = 5;
|
2014-02-10 02:10:30 +01:00
|
|
|
else {
|
|
|
|
_UNICERROR("invalid skip");
|
|
|
|
return true; //invalid utf8
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (skip == 1 && (c & 0x1E) == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
//printf("overlong rejected\n");
|
|
|
|
_UNICERROR("overlong rejected");
|
|
|
|
return true; //reject overlong
|
|
|
|
}
|
|
|
|
|
|
|
|
str_size++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
--skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
cstr_size++;
|
|
|
|
ptrtmp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip) {
|
|
|
|
_UNICERROR("no space left");
|
|
|
|
return true; //not enough spac
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (str_size == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
resize(str_size + 1);
|
2018-12-16 01:44:18 +01:00
|
|
|
CharType *dst = ptrw();
|
2017-03-05 16:44:50 +01:00
|
|
|
dst[str_size] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
while (cstr_size) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
/* Determine the number of characters in sequence */
|
|
|
|
if ((*p_utf8 & 0x80) == 0)
|
|
|
|
len = 1;
|
|
|
|
else if ((*p_utf8 & 0xE0) == 0xC0)
|
|
|
|
len = 2;
|
|
|
|
else if ((*p_utf8 & 0xF0) == 0xE0)
|
|
|
|
len = 3;
|
|
|
|
else if ((*p_utf8 & 0xF8) == 0xF0)
|
|
|
|
len = 4;
|
|
|
|
else if ((*p_utf8 & 0xFC) == 0xF8)
|
|
|
|
len = 5;
|
|
|
|
else if ((*p_utf8 & 0xFE) == 0xFC)
|
|
|
|
len = 6;
|
2014-02-10 02:10:30 +01:00
|
|
|
else {
|
|
|
|
_UNICERROR("invalid len");
|
|
|
|
|
|
|
|
return true; //invalid UTF8
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len > cstr_size) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_UNICERROR("no space left");
|
|
|
|
return true; //not enough space
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 2 && (*p_utf8 & 0x1E) == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
//printf("overlong rejected\n");
|
|
|
|
_UNICERROR("no space left");
|
|
|
|
return true; //reject overlong
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the first character */
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t unichar = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (len == 1)
|
2017-03-05 16:44:50 +01:00
|
|
|
unichar = *p_utf8;
|
2014-02-10 02:10:30 +01:00
|
|
|
else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
unichar = (0xFF >> (len + 1)) & *p_utf8;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 1; i < len; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if ((p_utf8[i] & 0xC0) != 0x80) {
|
|
|
|
_UNICERROR("invalid utf8");
|
|
|
|
return true; //invalid utf8
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (unichar == 0 && i == 2 && ((p_utf8[i] & 0x7F) >> (7 - len)) == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_UNICERROR("invalid utf8 overlong");
|
|
|
|
return true; //no overlong
|
|
|
|
}
|
|
|
|
unichar = (unichar << 6) | (p_utf8[i] & 0x3F);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//printf("char %i, len %i\n",unichar,len);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (sizeof(wchar_t) == 2 && unichar > 0xFFFF) {
|
|
|
|
unichar = ' '; //too long for windows
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*(dst++) = unichar;
|
2017-03-05 16:44:50 +01:00
|
|
|
cstr_size -= len;
|
2014-02-10 02:10:30 +01:00
|
|
|
p_utf8 += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CharString String::utf8() const {
|
|
|
|
|
|
|
|
int l = length();
|
|
|
|
if (!l)
|
|
|
|
return CharString();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *d = &operator[](0);
|
|
|
|
int fl = 0;
|
|
|
|
for (int i = 0; i < l; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t c = d[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
if (c <= 0x7f) // 7 bits.
|
2017-03-05 16:44:50 +01:00
|
|
|
fl += 1;
|
|
|
|
else if (c <= 0x7ff) { // 11 bits
|
|
|
|
fl += 2;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else if (c <= 0xffff) { // 16 bits
|
2017-03-05 16:44:50 +01:00
|
|
|
fl += 3;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else if (c <= 0x001fffff) { // 21 bits
|
2017-03-05 16:44:50 +01:00
|
|
|
fl += 4;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else if (c <= 0x03ffffff) { // 26 bits
|
2017-03-05 16:44:50 +01:00
|
|
|
fl += 5;
|
|
|
|
} else if (c <= 0x7fffffff) { // 31 bits
|
|
|
|
fl += 6;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CharString utf8s;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (fl == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return utf8s;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
utf8s.resize(fl + 1);
|
|
|
|
uint8_t *cdst = (uint8_t *)utf8s.get_data();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define APPEND_CHAR(m_c) *(cdst++) = m_c
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < l; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t c = d[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (c <= 0x7f) // 7 bits.
|
2017-03-05 16:44:50 +01:00
|
|
|
APPEND_CHAR(c);
|
|
|
|
else if (c <= 0x7ff) { // 11 bits
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
APPEND_CHAR(uint32_t(0xc0 | ((c >> 6) & 0x1f))); // Top 5 bits.
|
|
|
|
APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
|
2014-02-10 02:10:30 +01:00
|
|
|
} else if (c <= 0xffff) { // 16 bits
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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.
|
2014-02-10 02:10:30 +01:00
|
|
|
} else if (c <= 0x001fffff) { // 21 bits
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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.
|
2014-02-10 02:10:30 +01:00
|
|
|
} else if (c <= 0x03ffffff) { // 26 bits
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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.
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef APPEND_CHAR
|
2017-03-05 16:44:50 +01:00
|
|
|
*cdst = 0; //trailing zero
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return utf8s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
String::String(CharType p_char) {
|
|
|
|
|
|
|
|
shared=NULL;
|
|
|
|
copy_from(p_char);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
String::String(const char *p_str) {
|
|
|
|
|
|
|
|
copy_from(p_str);
|
|
|
|
}
|
2018-07-25 03:11:03 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String::String(const CharType *p_str, int p_clip_to_len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
copy_from(p_str, p_clip_to_len);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String::String(const StrRange &p_range) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!p_range.c_str)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
copy_from(p_range.c_str, p_range.len);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 14:58:00 +02:00
|
|
|
int String::hex_to_int(bool p_with_prefix) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-04-18 22:20:39 +02:00
|
|
|
if (p_with_prefix && length() < 3)
|
2016-10-20 14:58:00 +02:00
|
|
|
return 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *s = ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int sign = s[0] == '-' ? -1 : 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (sign < 0) {
|
2017-01-09 00:58:39 +01:00
|
|
|
s++;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-20 14:58:00 +02:00
|
|
|
if (p_with_prefix) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (s[0] != '0' || s[1] != 'x')
|
2016-10-20 14:58:00 +02:00
|
|
|
return 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
s += 2;
|
2018-04-18 22:20:39 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int hex = 0;
|
2017-01-09 00:58:39 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*s) {
|
2017-01-09 00:58:39 +01:00
|
|
|
|
|
|
|
CharType c = LOWERCASE(*s);
|
|
|
|
int n;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
n = c - '0';
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
n = (c - 'a') + 10;
|
2017-01-09 00:58:39 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
hex *= 16;
|
|
|
|
hex += n;
|
2017-01-09 00:58:39 +01:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return hex * sign;
|
2017-01-09 00:58:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t String::hex_to_int64(bool p_with_prefix) const {
|
|
|
|
|
2018-04-18 22:20:39 +02:00
|
|
|
if (p_with_prefix && length() < 3)
|
2017-01-09 00:58:39 +01:00
|
|
|
return 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *s = ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int64_t sign = s[0] == '-' ? -1 : 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (sign < 0) {
|
2017-01-09 00:58:39 +01:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_with_prefix) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (s[0] != '0' || s[1] != 'x')
|
2017-01-09 00:58:39 +01:00
|
|
|
return 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
s += 2;
|
2018-04-18 22:20:39 +02:00
|
|
|
}
|
2017-01-09 00:58:39 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int64_t hex = 0;
|
2017-01-09 00:58:39 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*s) {
|
2017-01-09 00:58:39 +01:00
|
|
|
|
|
|
|
CharType c = LOWERCASE(*s);
|
|
|
|
int64_t n;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
n = c - '0';
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
n = (c - 'a') + 10;
|
2017-01-09 00:58:39 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
hex *= 16;
|
|
|
|
hex += n;
|
2017-01-09 00:58:39 +01:00
|
|
|
s++;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return hex * sign;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:43:48 +02:00
|
|
|
int64_t String::bin_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;
|
|
|
|
|
|
|
|
if (sign < 0) {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_with_prefix) {
|
|
|
|
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') {
|
|
|
|
n = c - '0';
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
binary *= 2;
|
|
|
|
binary += n;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return binary * sign;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int String::to_int() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (length() == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int to = (find(".") >= 0) ? find(".") : length();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int integer = 0;
|
|
|
|
int sign = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < to; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType c = operator[](i);
|
|
|
|
if (c >= '0' && c <= '9') {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
integer *= 10;
|
|
|
|
integer += c - '0';
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (integer == 0 && c == '-') {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sign = -sign;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return integer * sign;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t String::to_int64() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (length() == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int to = (find(".") >= 0) ? find(".") : length();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int64_t integer = 0;
|
|
|
|
int64_t sign = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < to; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType c = operator[](i);
|
|
|
|
if (c >= '0' && c <= '9') {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
integer *= 10;
|
|
|
|
integer += c - '0';
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (integer == 0 && c == '-') {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sign = -sign;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return integer * sign;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int String::to_int(const char *p_str, int p_len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int to = 0;
|
|
|
|
if (p_len >= 0)
|
|
|
|
to = p_len;
|
2016-01-03 21:14:28 +01:00
|
|
|
else {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (p_str[to] != 0 && p_str[to] != '.')
|
2016-01-03 21:14:28 +01:00
|
|
|
to++;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int integer = 0;
|
|
|
|
int sign = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < to; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
char c = p_str[i];
|
|
|
|
if (c >= '0' && c <= '9') {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
integer *= 10;
|
|
|
|
integer += c - '0';
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (c == '-' && integer == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sign = -sign;
|
|
|
|
} else if (c != ' ')
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return integer * sign;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool String::is_numeric() const {
|
|
|
|
|
|
|
|
if (length() == 0) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
int s = 0;
|
|
|
|
if (operator[](0) == '-') ++s;
|
|
|
|
bool dot = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = s; i < length(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class C>
|
|
|
|
static double built_in_strtod(const C *string, /* A decimal ASCII floating-point number,
|
2014-02-10 02:10:30 +01:00
|
|
|
* optionally preceded by white space. Must
|
|
|
|
* have form "-I.FE-X", where I is the integer
|
|
|
|
* part of the mantissa, F is the fractional
|
|
|
|
* part of the mantissa, and X is the
|
|
|
|
* exponent. Either of the signs may be "+",
|
|
|
|
* "-", or omitted. Either I or F may be
|
|
|
|
* omitted, or both. The decimal point isn't
|
|
|
|
* necessary unless F is present. The "E" may
|
|
|
|
* actually be an "e". E and X may both be
|
|
|
|
* omitted (but not just one). */
|
2017-03-05 16:44:50 +01:00
|
|
|
C **endPtr = NULL) /* If non-NULL, store terminating Cacter's
|
2014-02-10 02:10:30 +01:00
|
|
|
* address here. */
|
|
|
|
{
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const int maxExponent = 511; /* Largest possible base 10 exponent. Any
|
2014-02-10 02:10:30 +01:00
|
|
|
* exponent larger than this will already
|
|
|
|
* produce underflow or overflow, so there's
|
|
|
|
* no need to worry about additional digits.
|
|
|
|
*/
|
2017-03-05 16:44:50 +01:00
|
|
|
static const double powersOf10[] = { /* Table giving binary powers of 10. Entry */
|
|
|
|
10., /* is 10^2^i. Used to convert decimal */
|
|
|
|
100., /* exponents into floating-point numbers. */
|
|
|
|
1.0e4,
|
|
|
|
1.0e8,
|
|
|
|
1.0e16,
|
|
|
|
1.0e32,
|
|
|
|
1.0e64,
|
|
|
|
1.0e128,
|
|
|
|
1.0e256
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int sign, expSign = false;
|
|
|
|
double fraction, dblExp;
|
|
|
|
const double *d;
|
2018-10-03 14:05:17 +02:00
|
|
|
const C *p;
|
|
|
|
int c;
|
2017-03-05 16:44:50 +01:00
|
|
|
int exp = 0; /* Exponent read from "EX" field. */
|
|
|
|
int fracExp = 0; /* Exponent that derives from the fractional
|
2017-09-22 05:58:29 +02:00
|
|
|
* part. Under normal circumstances, it is
|
2014-02-10 02:10:30 +01:00
|
|
|
* the negative of the number of digits in F.
|
|
|
|
* However, if I is very long, the last digits
|
|
|
|
* of I get dropped (otherwise a long I with a
|
|
|
|
* large negative exponent could cause an
|
|
|
|
* unnecessary overflow on I alone). In this
|
|
|
|
* case, fracExp is incremented one for each
|
|
|
|
* dropped digit. */
|
2017-03-05 16:44:50 +01:00
|
|
|
int mantSize; /* Number of digits in mantissa. */
|
|
|
|
int decPt; /* Number of mantissa digits BEFORE decimal
|
2014-02-10 02:10:30 +01:00
|
|
|
* point. */
|
2017-03-05 16:44:50 +01:00
|
|
|
const C *pExp; /* Temporarily holds location of exponent in
|
2014-02-10 02:10:30 +01:00
|
|
|
* string. */
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
/*
|
2014-02-10 02:10:30 +01:00
|
|
|
* Strip off leading blanks and check for a sign.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p = string;
|
|
|
|
while (*p == ' ' || *p == '\t' || *p == '\n') {
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
if (*p == '-') {
|
|
|
|
sign = true;
|
|
|
|
p += 1;
|
|
|
|
} else {
|
|
|
|
if (*p == '+') {
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
sign = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-02-10 02:10:30 +01:00
|
|
|
* Count the number of digits in the mantissa (including the decimal
|
|
|
|
* point), and also locate the decimal point.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
decPt = -1;
|
|
|
|
for (mantSize = 0;; mantSize += 1) {
|
|
|
|
c = *p;
|
|
|
|
if (!IS_DIGIT(c)) {
|
|
|
|
if ((c != '.') || (decPt >= 0)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
decPt = mantSize;
|
|
|
|
}
|
|
|
|
p += 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
/*
|
2014-02-10 02:10:30 +01:00
|
|
|
* Now suck up the digits in the mantissa. Use two integers to collect 9
|
|
|
|
* digits each (this is faster than using floating-point). If the mantissa
|
|
|
|
* has more than 18 digits, ignore the extras, since they can't affect the
|
|
|
|
* value anyway.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pExp = p;
|
|
|
|
p -= mantSize;
|
|
|
|
if (decPt < 0) {
|
|
|
|
decPt = mantSize;
|
|
|
|
} else {
|
|
|
|
mantSize -= 1; /* One of the digits was the point. */
|
|
|
|
}
|
|
|
|
if (mantSize > 18) {
|
|
|
|
fracExp = decPt - 18;
|
|
|
|
mantSize = 18;
|
|
|
|
} else {
|
|
|
|
fracExp = decPt - mantSize;
|
|
|
|
}
|
|
|
|
if (mantSize == 0) {
|
|
|
|
fraction = 0.0;
|
|
|
|
p = string;
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
int frac1, frac2;
|
|
|
|
|
|
|
|
frac1 = 0;
|
|
|
|
for (; mantSize > 9; mantSize -= 1) {
|
|
|
|
c = *p;
|
|
|
|
p += 1;
|
|
|
|
if (c == '.') {
|
|
|
|
c = *p;
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
frac1 = 10 * frac1 + (c - '0');
|
|
|
|
}
|
|
|
|
frac2 = 0;
|
|
|
|
for (; mantSize > 0; mantSize -= 1) {
|
|
|
|
c = *p;
|
|
|
|
p += 1;
|
|
|
|
if (c == '.') {
|
|
|
|
c = *p;
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
frac2 = 10 * frac2 + (c - '0');
|
|
|
|
}
|
|
|
|
fraction = (1.0e9 * frac1) + frac2;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
/*
|
2014-02-10 02:10:30 +01:00
|
|
|
* Skim off the exponent.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p = pExp;
|
|
|
|
if ((*p == 'E') || (*p == 'e')) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p += 1;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (*p == '-') {
|
|
|
|
expSign = true;
|
|
|
|
p += 1;
|
|
|
|
} else {
|
|
|
|
if (*p == '+') {
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
expSign = false;
|
|
|
|
}
|
|
|
|
if (!IS_DIGIT(CharType(*p))) {
|
|
|
|
p = pExp;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
while (IS_DIGIT(CharType(*p))) {
|
|
|
|
exp = exp * 10 + (*p - '0');
|
|
|
|
p += 1;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (expSign) {
|
|
|
|
exp = fracExp - exp;
|
|
|
|
} else {
|
|
|
|
exp = fracExp + exp;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
/*
|
2014-02-10 02:10:30 +01:00
|
|
|
* Generate a floating-point number that represents the exponent. Do this
|
|
|
|
* by processing the exponent one bit at a time to combine many powers of
|
|
|
|
* 2 of 10. Then combine the exponent with the fraction.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (exp < 0) {
|
|
|
|
expSign = true;
|
|
|
|
exp = -exp;
|
|
|
|
} else {
|
|
|
|
expSign = false;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (exp > maxExponent) {
|
|
|
|
exp = maxExponent;
|
|
|
|
WARN_PRINT("Exponent too high");
|
|
|
|
}
|
|
|
|
dblExp = 1.0;
|
|
|
|
for (d = powersOf10; exp != 0; exp >>= 1, ++d) {
|
|
|
|
if (exp & 01) {
|
|
|
|
dblExp *= *d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (expSign) {
|
|
|
|
fraction /= dblExp;
|
|
|
|
} else {
|
|
|
|
fraction *= dblExp;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
done:
|
|
|
|
if (endPtr != NULL) {
|
|
|
|
*endPtr = (C *)p;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (sign) {
|
|
|
|
return -fraction;
|
|
|
|
}
|
|
|
|
return fraction;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define READING_SIGN 0
|
|
|
|
#define READING_INT 1
|
|
|
|
#define READING_DEC 2
|
|
|
|
#define READING_EXP 3
|
|
|
|
#define READING_DONE 4
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
double String::to_double(const char *p_str) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#ifndef NO_USE_STDLIB
|
|
|
|
return built_in_strtod<char>(p_str);
|
2017-03-05 16:44:50 +01:00
|
|
|
//return atof(p_str); DOES NOT WORK ON ANDROID(??)
|
2014-02-10 02:10:30 +01:00
|
|
|
#else
|
|
|
|
return built_in_strtod<char>(p_str);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
float String::to_float() const {
|
|
|
|
|
|
|
|
return to_double();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
double String::to_double(const CharType *p_str, const CharType **r_end) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return built_in_strtod<CharType>(p_str, (CharType **)r_end);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int64_t String::to_int(const CharType *p_str, int p_len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_len == 0 || !p_str[0])
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
///@todo make more exact so saving and loading does not lose precision
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int64_t integer = 0;
|
|
|
|
int64_t sign = 1;
|
|
|
|
int reading = READING_SIGN;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *str = p_str;
|
|
|
|
const CharType *limit = &p_str[p_len];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*str && reading != READING_DONE && str != limit) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType c = *(str++);
|
|
|
|
switch (reading) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case READING_SIGN: {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
reading = READING_INT;
|
2014-02-10 02:10:30 +01:00
|
|
|
// let it fallthrough
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (c == '-') {
|
|
|
|
sign = -1;
|
|
|
|
reading = READING_INT;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (c == '+') {
|
|
|
|
sign = 1;
|
|
|
|
reading = READING_INT;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case READING_INT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c >= '0' && c <= '9') {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
integer *= 10;
|
|
|
|
integer += c - '0';
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
reading = READING_DONE;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return sign * integer;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double String::to_double() const {
|
|
|
|
|
|
|
|
if (empty())
|
|
|
|
return 0;
|
|
|
|
#ifndef NO_USE_STDLIB
|
|
|
|
return built_in_strtod<CharType>(c_str());
|
2017-03-05 16:44:50 +01:00
|
|
|
//return wcstod(c_str(),NULL); DOES NOT WORK ON ANDROID :(
|
2014-02-10 02:10:30 +01:00
|
|
|
#else
|
|
|
|
return built_in_strtod<CharType>(c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool operator==(const char *p_chr, const String &p_str) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return p_str == p_chr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String operator+(const char *p_chr, const String &p_str) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String tmp = p_chr;
|
|
|
|
tmp += p_str;
|
2014-02-10 02:10:30 +01:00
|
|
|
return tmp;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
String operator+(CharType p_chr, const String &p_str) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (String::chr(p_chr) + p_str);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t String::hash(const char *p_cstr) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t hashv = 5381;
|
|
|
|
uint32_t c;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
while ((c = *p_cstr++))
|
|
|
|
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
return hashv;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t String::hash(const char *p_cstr, int p_len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
uint32_t hashv = 5381;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_len; i++)
|
2014-02-10 02:10:30 +01:00
|
|
|
hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
|
|
|
|
|
|
|
|
return hashv;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t String::hash(const CharType *p_cstr, int p_len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
uint32_t hashv = 5381;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_len; i++)
|
2014-02-10 02:10:30 +01:00
|
|
|
hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
|
|
|
|
|
|
|
|
return hashv;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t String::hash(const CharType *p_cstr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
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 {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/* simple djb2 hashing */
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *chr = c_str();
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t hashv = 5381;
|
|
|
|
uint32_t c;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
while ((c = *chr++))
|
|
|
|
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return hashv;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t String::hash64() const {
|
|
|
|
|
|
|
|
/* simple djb2 hashing */
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *chr = c_str();
|
2014-02-10 02:10:30 +01:00
|
|
|
uint64_t hashv = 5381;
|
|
|
|
uint64_t c;
|
|
|
|
|
|
|
|
while ((c = *chr++))
|
|
|
|
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
|
|
|
|
|
|
|
return hashv;
|
|
|
|
}
|
|
|
|
|
2014-03-14 02:57:24 +01:00
|
|
|
String String::md5_text() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharString cs = utf8();
|
2019-07-02 03:06:52 +02:00
|
|
|
unsigned char hash[16];
|
|
|
|
CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
|
|
|
|
return String::hex_encode_buffer(hash, 16);
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-07-02 16:07:02 +02:00
|
|
|
String String::sha1_text() const {
|
|
|
|
CharString cs = utf8();
|
|
|
|
unsigned char hash[20];
|
|
|
|
CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
|
|
|
|
return String::hex_encode_buffer(hash, 20);
|
|
|
|
}
|
|
|
|
|
2016-06-17 09:55:16 +02:00
|
|
|
String String::sha256_text() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
CharString cs = utf8();
|
2016-06-17 09:55:16 +02:00
|
|
|
unsigned char hash[32];
|
2019-07-02 03:06:52 +02:00
|
|
|
CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
|
2016-06-17 09:55:16 +02:00
|
|
|
return String::hex_encode_buffer(hash, 32);
|
|
|
|
}
|
|
|
|
|
2014-08-02 03:10:38 +02:00
|
|
|
Vector<uint8_t> String::md5_buffer() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharString cs = utf8();
|
2019-07-02 03:06:52 +02:00
|
|
|
unsigned char hash[16];
|
|
|
|
CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
Vector<uint8_t> ret;
|
|
|
|
ret.resize(16);
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 16; i++) {
|
2019-07-02 03:06:52 +02:00
|
|
|
ret.write[i] = hash[i];
|
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2019-07-02 16:07:02 +02:00
|
|
|
Vector<uint8_t> String::sha1_buffer() const {
|
|
|
|
CharString cs = utf8();
|
|
|
|
unsigned char hash[20];
|
|
|
|
CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
|
|
|
|
|
|
|
|
Vector<uint8_t> ret;
|
|
|
|
ret.resize(20);
|
|
|
|
for (int i = 0; i < 20; i++) {
|
|
|
|
ret.write[i] = hash[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-23 18:57:45 +02:00
|
|
|
Vector<uint8_t> String::sha256_buffer() const {
|
|
|
|
CharString cs = utf8();
|
|
|
|
unsigned char hash[32];
|
2019-07-02 03:06:52 +02:00
|
|
|
CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
|
2016-06-23 18:57:45 +02:00
|
|
|
|
|
|
|
Vector<uint8_t> ret;
|
|
|
|
ret.resize(32);
|
|
|
|
for (int i = 0; i < 32; i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
ret.write[i] = hash[i];
|
2016-06-23 18:57:45 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
String String::insert(int p_at_pos, const String &p_string) const {
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_at_pos < 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_at_pos > length())
|
|
|
|
p_at_pos = length();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String pre;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_at_pos > 0)
|
|
|
|
pre = substr(0, p_at_pos);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String post;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_at_pos < length())
|
|
|
|
post = substr(p_at_pos, length() - p_at_pos);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return pre + p_string + post;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::substr(int p_from, int p_chars) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-05-03 14:21:04 +02:00
|
|
|
if (p_chars == -1)
|
|
|
|
p_chars = length() - p_from;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (empty() || p_from < 0 || p_from >= length() || p_chars <= 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((p_from + p_chars) > length()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_chars = length() - p_from;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
if (p_from == 0 && p_chars >= length()) {
|
|
|
|
|
|
|
|
return String(*this);
|
|
|
|
}
|
|
|
|
|
2018-07-24 17:06:49 +02:00
|
|
|
String s = String();
|
|
|
|
s.copy_from_unchecked(&c_str()[p_from], p_chars);
|
|
|
|
return s;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
int String::find_last(const String &p_str) const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int pos = -1;
|
|
|
|
int findfrom = 0;
|
|
|
|
int findres = -1;
|
|
|
|
while ((findres = find(p_str, findfrom)) != -1) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = findres;
|
|
|
|
findfrom = pos + 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return pos;
|
|
|
|
}
|
2017-12-11 23:38:07 +01:00
|
|
|
|
|
|
|
int String::find(const String &p_str, int p_from) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_from < 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
const int src_len = p_str.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
const int len = length();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src_len == 0 || len == 0)
|
2018-01-18 21:37:17 +01:00
|
|
|
return -1; // won't find anything!
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
const CharType *src = c_str();
|
2017-12-11 23:38:07 +01:00
|
|
|
const CharType *str = p_str.c_str();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = p_from; i <= (len - src_len); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found = true;
|
|
|
|
for (int j = 0; j < src_len; j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int read_pos = i + j;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (read_pos >= len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_PRINT("read_pos>=len");
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
if (src[read_pos] != str[j]) {
|
2017-03-05 16:44:50 +01:00
|
|
|
found = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
int String::find(const char *p_str, int p_from) const {
|
|
|
|
|
|
|
|
if (p_from < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
const int len = length();
|
|
|
|
|
|
|
|
if (len == 0)
|
2018-01-18 21:37:17 +01:00
|
|
|
return -1; // won't find anything!
|
2017-12-11 23:38:07 +01:00
|
|
|
|
|
|
|
const CharType *src = c_str();
|
|
|
|
|
|
|
|
int 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;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:24:29 +01:00
|
|
|
int String::find_char(const CharType &p_char, int p_from) const {
|
2019-01-07 18:02:55 +01:00
|
|
|
return _cowdata.find(p_char, p_from);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_from < 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_keys.size() == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
//int src_len=p_str.length();
|
2017-03-05 16:44:50 +01:00
|
|
|
const String *keys = &p_keys[0];
|
|
|
|
int key_count = p_keys.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0)
|
2018-01-18 21:37:17 +01:00
|
|
|
return -1; // won't find anything!
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
const CharType *src = c_str();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = p_from; i < len; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found = true;
|
|
|
|
for (int k = 0; k < key_count; k++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
found = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (r_key)
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_key = k;
|
|
|
|
const CharType *cmp = keys[k].c_str();
|
|
|
|
int l = keys[k].length();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < l; j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int read_pos = i + j;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (read_pos >= len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
found = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src[read_pos] != cmp[j]) {
|
|
|
|
found = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
int String::findn(const String &p_str, int p_from) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_from < 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int src_len = p_str.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src_len == 0 || length() == 0)
|
2018-01-18 21:37:17 +01:00
|
|
|
return -1; // won't find anything!
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
const CharType *srcd = c_str();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = p_from; i <= (length() - src_len); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found = true;
|
|
|
|
for (int j = 0; j < src_len; j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int read_pos = i + j;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (read_pos >= length()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_PRINT("read_pos>=length()");
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType src = _find_lower(srcd[read_pos]);
|
|
|
|
CharType dst = _find_lower(p_str[j]);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src != dst) {
|
|
|
|
found = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
int String::rfind(const String &p_str, int p_from) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-22 05:58:29 +02:00
|
|
|
// establish a limit
|
2017-03-05 16:44:50 +01:00
|
|
|
int limit = length() - p_str.length();
|
|
|
|
if (limit < 0)
|
2014-09-21 06:43:42 +02:00
|
|
|
return -1;
|
|
|
|
|
2017-09-22 05:58:29 +02:00
|
|
|
// establish a starting point
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_from < 0)
|
|
|
|
p_from = limit;
|
|
|
|
else if (p_from > limit)
|
|
|
|
p_from = limit;
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int src_len = p_str.length();
|
|
|
|
int len = length();
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src_len == 0 || len == 0)
|
2017-09-22 05:58:29 +02:00
|
|
|
return -1; // won't find anything!
|
2014-09-21 06:43:42 +02:00
|
|
|
|
|
|
|
const CharType *src = c_str();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = p_from; i >= 0; i--) {
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found = true;
|
|
|
|
for (int j = 0; j < src_len; j++) {
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int read_pos = i + j;
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (read_pos >= len) {
|
2014-09-21 06:43:42 +02:00
|
|
|
|
|
|
|
ERR_PRINT("read_pos>=len");
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src[read_pos] != p_str[j]) {
|
|
|
|
found = false;
|
2014-09-21 06:43:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2017-12-11 23:38:07 +01:00
|
|
|
int String::rfindn(const String &p_str, int p_from) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-22 05:58:29 +02:00
|
|
|
// establish a limit
|
2017-03-05 16:44:50 +01:00
|
|
|
int limit = length() - p_str.length();
|
|
|
|
if (limit < 0)
|
2014-09-21 06:43:42 +02:00
|
|
|
return -1;
|
|
|
|
|
2017-09-22 05:58:29 +02:00
|
|
|
// establish a starting point
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_from < 0)
|
|
|
|
p_from = limit;
|
|
|
|
else if (p_from > limit)
|
|
|
|
p_from = limit;
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int src_len = p_str.length();
|
|
|
|
int len = length();
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src_len == 0 || len == 0)
|
2018-01-18 21:37:17 +01:00
|
|
|
return -1; // won't find anything!
|
2014-09-21 06:43:42 +02:00
|
|
|
|
|
|
|
const CharType *src = c_str();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = p_from; i >= 0; i--) {
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found = true;
|
|
|
|
for (int j = 0; j < src_len; j++) {
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int read_pos = i + j;
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (read_pos >= len) {
|
2014-09-21 06:43:42 +02:00
|
|
|
|
|
|
|
ERR_PRINT("read_pos>=len");
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType srcc = _find_lower(src[read_pos]);
|
|
|
|
CharType dstc = _find_lower(p_str[j]);
|
2014-09-21 06:43:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (srcc != dstc) {
|
|
|
|
found = false;
|
2014-09-21 06:43:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::ends_with(const String &p_string) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int pos = find_last(p_string);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pos == -1)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2017-03-05 16:44:50 +01:00
|
|
|
return pos + p_string.length() == length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::begins_with(const String &p_string) const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_string.length() > length())
|
|
|
|
return false;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int l = p_string.length();
|
|
|
|
if (l == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *src = &p_string[0];
|
|
|
|
const CharType *str = &operator[](0);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-26 15:47:22 +01:00
|
|
|
int i = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (; i < l; i++) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src[i] != str[i])
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-26 15:47:22 +01:00
|
|
|
// only if i == l the p_string matches the beginning
|
|
|
|
return i == l;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::begins_with(const char *p_string) const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int l = length();
|
|
|
|
if (l == 0 || !p_string)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *str = &operator[](0);
|
|
|
|
int i = 0;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*p_string && i < l) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (*p_string != str[i])
|
|
|
|
return false;
|
|
|
|
i++;
|
|
|
|
p_string++;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-17 21:46:25 +01:00
|
|
|
return *p_string == 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-28 23:46:20 +02:00
|
|
|
bool String::is_enclosed_in(const String &p_string) const {
|
|
|
|
|
|
|
|
return begins_with(p_string) && ends_with(p_string);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::is_subsequence_of(const String &p_string) const {
|
2016-06-12 05:25:01 +02:00
|
|
|
|
|
|
|
return _base_is_subsequence_of(p_string, false);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::is_subsequence_ofi(const String &p_string) const {
|
2016-06-12 05:25:01 +02:00
|
|
|
|
|
|
|
return _base_is_subsequence_of(p_string, true);
|
|
|
|
}
|
|
|
|
|
2017-10-28 23:46:20 +02:00
|
|
|
bool String::is_quoted() const {
|
|
|
|
|
|
|
|
return is_enclosed_in("\"") || is_enclosed_in("'");
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::_base_is_subsequence_of(const String &p_string, bool case_insensitive) const {
|
2016-06-12 05:25:01 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = length();
|
2016-06-12 05:25:01 +02:00
|
|
|
if (len == 0) {
|
|
|
|
// Technically an empty string is subsequence of any string
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > p_string.length()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CharType *src = &operator[](0);
|
|
|
|
const CharType *tgt = &p_string[0];
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (; *src && *tgt; tgt++) {
|
2016-06-12 05:25:01 +02:00
|
|
|
bool match = false;
|
|
|
|
if (case_insensitive) {
|
|
|
|
CharType srcc = _find_lower(*src);
|
|
|
|
CharType tgtc = _find_lower(*tgt);
|
|
|
|
match = srcc == tgtc;
|
|
|
|
} else {
|
|
|
|
match = *src == *tgt;
|
|
|
|
}
|
|
|
|
if (match) {
|
|
|
|
src++;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!*src) {
|
2016-06-12 05:25:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-13 19:06:03 +02:00
|
|
|
Vector<String> String::bigrams() const {
|
|
|
|
int n_pairs = length() - 1;
|
|
|
|
Vector<String> b;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (n_pairs <= 0) {
|
2016-06-13 19:06:03 +02:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
b.resize(n_pairs);
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < n_pairs; i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
b.write[i] = substr(i, 2);
|
2016-06-13 19:06:03 +02:00
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similarity according to Sorensen-Dice coefficient
|
2017-03-05 16:44:50 +01:00
|
|
|
float String::similarity(const String &p_string) const {
|
|
|
|
if (operator==(p_string)) {
|
2016-06-13 19:06:03 +02:00
|
|
|
// Equal strings are totally similar
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
if (length() < 2 || p_string.length() < 2) {
|
|
|
|
// No way to calculate similarity without a single bigram
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> src_bigrams = bigrams();
|
|
|
|
Vector<String> tgt_bigrams = p_string.bigrams();
|
|
|
|
|
|
|
|
int src_size = src_bigrams.size();
|
|
|
|
int tgt_size = tgt_bigrams.size();
|
|
|
|
|
|
|
|
float sum = src_size + tgt_size;
|
|
|
|
float 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]) {
|
|
|
|
inter++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (2.0f * inter) / sum;
|
2016-06-13 19:06:03 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static bool _wildcard_match(const CharType *p_pattern, const CharType *p_string, bool p_case_sensitive) {
|
2014-02-10 02:10:30 +01:00
|
|
|
switch (*p_pattern) {
|
2017-03-05 16:44:50 +01:00
|
|
|
case '\0':
|
|
|
|
return !*p_string;
|
|
|
|
case '*':
|
|
|
|
return _wildcard_match(p_pattern + 1, p_string, p_case_sensitive) || (*p_string && _wildcard_match(p_pattern, p_string + 1, p_case_sensitive));
|
|
|
|
case '?':
|
|
|
|
return *p_string && (*p_string != '.') && _wildcard_match(p_pattern + 1, p_string + 1, p_case_sensitive);
|
|
|
|
default:
|
|
|
|
|
|
|
|
return (p_case_sensitive ? (*p_string == *p_pattern) : (_find_upper(*p_string) == _find_upper(*p_pattern))) && _wildcard_match(p_pattern + 1, p_string + 1, p_case_sensitive);
|
2016-03-09 00:00:52 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::match(const String &p_wildcard) const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!p_wildcard.length() || !length())
|
|
|
|
return false;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return _wildcard_match(p_wildcard.c_str(), c_str(), true);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool String::matchn(const String &p_wildcard) const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!p_wildcard.length() || !length())
|
|
|
|
return false;
|
2017-03-05 16:44:50 +01:00
|
|
|
return _wildcard_match(p_wildcard.c_str(), c_str(), false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::format(const Variant &values, String placeholder) const {
|
2016-11-01 15:14:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String new_string = String(this->ptr());
|
2016-11-01 15:14:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (values.get_type() == Variant::ARRAY) {
|
2016-11-01 15:14:48 +01:00
|
|
|
Array values_arr = values;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < values_arr.size(); i++) {
|
|
|
|
String i_as_str = String::num_int64(i);
|
2016-11-01 15:14:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (values_arr[i].get_type() == Variant::ARRAY) { //Array in Array structure [["name","RobotGuy"],[0,"godot"],["strength",9000.91]]
|
2016-11-01 15:14:48 +01:00
|
|
|
Array value_arr = values_arr[i];
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (value_arr.size() == 2) {
|
2016-11-01 15:14:48 +01:00
|
|
|
Variant v_key = value_arr[0];
|
2018-10-19 21:53:37 +02:00
|
|
|
String key = v_key;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (key.left(1) == "\"" && key.right(key.length() - 1) == "\"") {
|
|
|
|
key = key.substr(1, key.length() - 2);
|
2016-11-01 15:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Variant v_val = value_arr[1];
|
2018-10-19 21:53:37 +02:00
|
|
|
String val = v_val;
|
2016-11-01 15:14:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (val.left(1) == "\"" && val.right(val.length() - 1) == "\"") {
|
|
|
|
val = val.substr(1, val.length() - 2);
|
2016-11-01 15:14:48 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 12:14:48 +02:00
|
|
|
new_string = new_string.replace(placeholder.replace("_", key), val);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2016-11-01 15:14:48 +01:00
|
|
|
ERR_PRINT(String("STRING.format Inner Array size != 2 ").ascii().get_data());
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else { //Array structure ["RobotGuy","Logis","rookie"]
|
2016-11-01 15:14:48 +01:00
|
|
|
Variant v_val = values_arr[i];
|
2018-10-19 21:53:37 +02:00
|
|
|
String val = v_val;
|
2016-11-01 15:14:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (val.left(1) == "\"" && val.right(val.length() - 1) == "\"") {
|
|
|
|
val = val.substr(1, val.length() - 2);
|
2016-11-01 15:14:48 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 13:22:20 +02:00
|
|
|
if (placeholder.find("_") > -1) {
|
|
|
|
new_string = new_string.replace(placeholder.replace("_", i_as_str), val);
|
|
|
|
} else {
|
|
|
|
new_string = new_string.replace_first(placeholder, val);
|
|
|
|
}
|
2016-11-01 15:14:48 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (values.get_type() == Variant::DICTIONARY) {
|
2016-11-01 15:14:48 +01:00
|
|
|
Dictionary d = values;
|
|
|
|
List<Variant> keys;
|
|
|
|
d.get_key_list(&keys);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
|
2018-10-19 21:53:37 +02:00
|
|
|
String key = E->get();
|
|
|
|
String val = d[E->get()];
|
2016-11-01 15:14:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (key.left(1) == "\"" && key.right(key.length() - 1) == "\"") {
|
|
|
|
key = key.substr(1, key.length() - 2);
|
2016-11-01 15:14:48 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (val.left(1) == "\"" && val.right(val.length() - 1) == "\"") {
|
|
|
|
val = val.substr(1, val.length() - 2);
|
2016-11-01 15:14:48 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 12:14:48 +02:00
|
|
|
new_string = new_string.replace(placeholder.replace("_", key), val);
|
2016-11-01 15:14:48 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2016-11-01 15:14:48 +01:00
|
|
|
ERR_PRINT(String("Invalid type: use Array or Dictionary.").ascii().get_data());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_string;
|
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
String String::replace(const String &p_key, const String &p_with) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String new_string;
|
2017-03-05 16:44:50 +01:00
|
|
|
int search_from = 0;
|
|
|
|
int result = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while ((result = find(p_key, search_from)) >= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
new_string += substr(search_from, result - search_from);
|
|
|
|
new_string += p_with;
|
|
|
|
search_from = result + p_key.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
if (search_from == 0) {
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
new_string += substr(search_from, length() - search_from);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return new_string;
|
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
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')
|
|
|
|
k++;
|
|
|
|
search_from = result + k;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search_from == 0) {
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_string += substr(search_from, length() - search_from);
|
|
|
|
|
|
|
|
return new_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::replace_first(const String &p_key, const String &p_with) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-06-03 21:52:50 +02:00
|
|
|
int pos = find(p_key);
|
|
|
|
if (pos >= 0) {
|
|
|
|
return substr(0, pos) + p_with + substr(pos + p_key.length(), length());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-06-03 21:52:50 +02:00
|
|
|
return *this;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-12-11 23:38:07 +01:00
|
|
|
String String::replacen(const String &p_key, const String &p_with) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String new_string;
|
2017-03-05 16:44:50 +01:00
|
|
|
int search_from = 0;
|
|
|
|
int result = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while ((result = findn(p_key, search_from)) >= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
new_string += substr(search_from, result - search_from);
|
|
|
|
new_string += p_with;
|
|
|
|
search_from = result + p_key.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 23:38:07 +01:00
|
|
|
if (search_from == 0) {
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
new_string += substr(search_from, length() - search_from);
|
2014-02-10 02:10:30 +01:00
|
|
|
return new_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::left(int p_pos) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_pos <= 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_pos >= length())
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return substr(0, p_pos);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String String::right(int p_pos) const {
|
|
|
|
|
2019-02-20 16:36:30 +01:00
|
|
|
if (p_pos >= length())
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
|
|
|
|
2019-02-20 16:36:30 +01:00
|
|
|
if (p_pos <= 0)
|
|
|
|
return *this;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return substr(p_pos, (length() - p_pos));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CharType String::ord_at(int p_idx) const {
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, length(), 0);
|
|
|
|
return operator[](p_idx);
|
|
|
|
}
|
|
|
|
|
2017-10-11 10:27:54 +02:00
|
|
|
String String::dedent() const {
|
|
|
|
|
|
|
|
String new_string;
|
|
|
|
String indent;
|
|
|
|
bool has_indent = false;
|
|
|
|
bool has_text = false;
|
|
|
|
int line_start = 0;
|
|
|
|
int indent_stop = -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < length(); i++) {
|
|
|
|
|
|
|
|
CharType c = operator[](i);
|
|
|
|
if (c == '\n') {
|
|
|
|
if (has_text)
|
|
|
|
new_string += substr(indent_stop, i - indent_stop);
|
|
|
|
new_string += "\n";
|
|
|
|
has_text = false;
|
|
|
|
line_start = i + 1;
|
|
|
|
indent_stop = -1;
|
|
|
|
} else if (!has_text) {
|
|
|
|
if (c > 32) {
|
|
|
|
has_text = true;
|
|
|
|
if (!has_indent) {
|
|
|
|
has_indent = true;
|
|
|
|
indent = substr(line_start, i - line_start);
|
|
|
|
indent_stop = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (has_indent && indent_stop < 0) {
|
|
|
|
int j = i - line_start;
|
|
|
|
if (j >= indent.length() || c != indent[j])
|
|
|
|
indent_stop = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_text)
|
|
|
|
new_string += substr(indent_stop, length() - indent_stop);
|
|
|
|
|
|
|
|
return new_string;
|
|
|
|
}
|
|
|
|
|
2016-05-11 09:22:59 +02:00
|
|
|
String String::strip_edges(bool left, bool right) const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = length();
|
|
|
|
int beg = 0, end = len;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (left) {
|
|
|
|
for (int i = 0; i < len; i++) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (operator[](i) <= 32)
|
2016-05-11 09:22:59 +02:00
|
|
|
beg++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (right) {
|
|
|
|
for (int i = (int)(len - 1); i >= 0; i--) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (operator[](i) <= 32)
|
2016-05-11 09:22:59 +02:00
|
|
|
end--;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (beg == 0 && end == len)
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return substr(beg, end - beg);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String String::strip_escapes() const {
|
|
|
|
|
2019-05-31 15:27:53 +02:00
|
|
|
String new_string;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < length(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-05-31 15:27:53 +02:00
|
|
|
// Escape characters on first page of the ASCII table, before 32 (Space).
|
|
|
|
if (operator[](i) < 32)
|
|
|
|
continue;
|
|
|
|
new_string += operator[](i);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-05-31 15:27:53 +02:00
|
|
|
return new_string;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
String String::lstrip(const String &p_chars) const {
|
2018-04-13 16:40:27 +02:00
|
|
|
|
|
|
|
int len = length();
|
|
|
|
int beg;
|
|
|
|
|
|
|
|
for (beg = 0; beg < len; beg++) {
|
|
|
|
|
2019-01-07 18:02:55 +01:00
|
|
|
if (p_chars.find_char(get(beg)) == -1)
|
2018-04-13 16:40:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (beg == 0)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
return substr(beg, len - beg);
|
|
|
|
}
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
String String::rstrip(const String &p_chars) const {
|
2018-04-13 16:40:27 +02:00
|
|
|
|
|
|
|
int len = length();
|
|
|
|
int end;
|
|
|
|
|
|
|
|
for (end = len - 1; end >= 0; end--) {
|
|
|
|
|
2019-01-07 18:02:55 +01:00
|
|
|
if (p_chars.find_char(get(end)) == -1)
|
2018-04-13 16:40:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end == len - 1)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
return substr(0, end + 1);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String String::simplify_path() const {
|
|
|
|
|
|
|
|
String s = *this;
|
|
|
|
String drive;
|
|
|
|
if (s.begins_with("local://")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
drive = "local://";
|
|
|
|
s = s.substr(8, s.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
} else if (s.begins_with("res://")) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
drive = "res://";
|
|
|
|
s = s.substr(6, s.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
} else if (s.begins_with("user://")) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
drive = "user://";
|
2018-10-07 16:49:48 +02:00
|
|
|
s = s.substr(7, s.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
} else if (s.begins_with("/") || s.begins_with("\\")) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
drive = s.substr(0, 1);
|
|
|
|
s = s.substr(1, s.length() - 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
int p = s.find(":/");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p == -1)
|
|
|
|
p = s.find(":\\");
|
|
|
|
if (p != -1 && p < s.find("/")) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
drive = s.substr(0, p + 2);
|
|
|
|
s = s.substr(p + 2, s.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
s = s.replace("\\", "/");
|
|
|
|
while (true) { // in case of using 2 or more slash
|
|
|
|
String compare = s.replace("//", "/");
|
|
|
|
if (s == compare)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
s = compare;
|
2016-09-29 20:11:45 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<String> dirs = s.split("/", false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < dirs.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String d = dirs[i];
|
2017-03-05 16:44:50 +01:00
|
|
|
if (d == ".") {
|
2014-02-10 02:10:30 +01:00
|
|
|
dirs.remove(i);
|
|
|
|
i--;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (d == "..") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
dirs.remove(i);
|
|
|
|
i--;
|
|
|
|
} else {
|
|
|
|
dirs.remove(i);
|
2017-03-05 16:44:50 +01:00
|
|
|
dirs.remove(i - 1);
|
|
|
|
i -= 2;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
s = "";
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < dirs.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i > 0)
|
|
|
|
s += "/";
|
|
|
|
s += dirs[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return drive + s;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _humanize_digits(int p_num) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_num < 10)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 2;
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (p_num < 100)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 2;
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (p_num < 1024)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::humanize_size(size_t p_size) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint64_t _div = 1;
|
2019-06-08 21:57:55 +02:00
|
|
|
static const char *prefix[] = { " Bytes", " KB", " MB", " GB", " TB", " PB", " EB", "" };
|
2017-03-05 16:44:50 +01:00
|
|
|
int prefix_idx = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (p_size > (_div * 1024) && prefix[prefix_idx][0]) {
|
|
|
|
_div *= 1024;
|
2014-02-10 02:10:30 +01:00
|
|
|
prefix_idx++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int digits = prefix_idx > 0 ? _humanize_digits(p_size / _div) : 0;
|
|
|
|
double divisor = prefix_idx > 0 ? _div : 1;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2019-06-08 22:17:57 +02:00
|
|
|
return String::num(p_size / divisor).pad_decimals(digits) + prefix[prefix_idx];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
bool String::is_abs_path() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (length() > 1)
|
|
|
|
return (operator[](0) == '/' || operator[](0) == '\\' || find(":/") != -1 || find(":\\") != -1);
|
|
|
|
else if ((length()) == 1)
|
|
|
|
return (operator[](0) == '/' || operator[](0) == '\\');
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::is_valid_identifier() const {
|
|
|
|
|
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const wchar_t *str = &operator[](0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < len; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i == 0) {
|
|
|
|
if (str[0] >= '0' && str[0] <= '9')
|
2014-02-10 02:10:30 +01:00
|
|
|
return false; // no start with number plz
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool valid_char = (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '_';
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!valid_char)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//kind of poor should be rewritten properly
|
|
|
|
|
2016-09-29 20:28:05 +02:00
|
|
|
String String::word_wrap(int p_chars_per_line) const {
|
2015-08-26 04:00:11 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
|
|
|
int last_space = 0;
|
2015-08-26 04:00:11 +02:00
|
|
|
String ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
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";
|
2015-08-26 04:00:11 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret += substr(from, last_space - from) + "\n";
|
|
|
|
i = last_space; //rewind
|
2015-08-26 04:00:11 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
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;
|
2015-08-26 04:00:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (from < length()) {
|
|
|
|
ret += substr(from, length());
|
2015-08-26 04:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-12 21:12:34 +02:00
|
|
|
String String::http_escape() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharString temp = utf8();
|
|
|
|
String res;
|
2018-04-05 21:49:44 +02:00
|
|
|
for (int i = 0; i < temp.length(); ++i) {
|
|
|
|
char ord = temp[i];
|
2017-03-05 16:44:50 +01:00
|
|
|
if (ord == '.' || ord == '-' || ord == '_' || ord == '~' ||
|
|
|
|
(ord >= 'a' && ord <= 'z') ||
|
|
|
|
(ord >= 'A' && ord <= 'Z') ||
|
|
|
|
(ord >= '0' && ord <= '9')) {
|
|
|
|
res += ord;
|
|
|
|
} else {
|
|
|
|
char h_Val[3];
|
2015-11-20 00:19:44 +01:00
|
|
|
#if defined(__GNUC__) || defined(_MSC_VER)
|
2018-04-05 21:49:44 +02:00
|
|
|
snprintf(h_Val, 3, "%hhX", ord);
|
2015-11-20 00:19:44 +01:00
|
|
|
#else
|
2018-04-05 21:49:44 +02:00
|
|
|
sprintf(h_Val, "%hhX", ord);
|
2015-11-20 00:19:44 +01:00
|
|
|
#endif
|
2017-03-05 16:44:50 +01:00
|
|
|
res += "%";
|
|
|
|
res += h_Val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2015-11-18 12:33:29 +01:00
|
|
|
}
|
|
|
|
|
2018-04-12 21:12:34 +02:00
|
|
|
String String::http_unescape() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
String res;
|
|
|
|
for (int i = 0; i < length(); ++i) {
|
|
|
|
if (ord_at(i) == '%' && i + 2 < length()) {
|
|
|
|
CharType ord1 = ord_at(i + 1);
|
|
|
|
if ((ord1 >= '0' && ord1 <= '9') || (ord1 >= 'A' && ord1 <= 'Z')) {
|
|
|
|
CharType ord2 = ord_at(i + 2);
|
|
|
|
if ((ord2 >= '0' && ord2 <= '9') || (ord2 >= 'A' && ord2 <= 'Z')) {
|
2019-07-08 17:15:10 +02:00
|
|
|
char bytes[3] = { (char)ord1, (char)ord2, 0 };
|
2017-03-05 16:44:50 +01:00
|
|
|
res += (char)strtol(bytes, NULL, 16);
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res += ord_at(i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res += ord_at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return String::utf8(res.ascii());
|
2015-11-18 12:33:29 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String String::c_unescape() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String escaped = *this;
|
|
|
|
escaped = escaped.replace("\\a", "\a");
|
|
|
|
escaped = escaped.replace("\\b", "\b");
|
|
|
|
escaped = escaped.replace("\\f", "\f");
|
|
|
|
escaped = escaped.replace("\\n", "\n");
|
|
|
|
escaped = escaped.replace("\\r", "\r");
|
|
|
|
escaped = escaped.replace("\\t", "\t");
|
|
|
|
escaped = escaped.replace("\\v", "\v");
|
|
|
|
escaped = escaped.replace("\\'", "\'");
|
|
|
|
escaped = escaped.replace("\\\"", "\"");
|
|
|
|
escaped = escaped.replace("\\?", "\?");
|
|
|
|
escaped = escaped.replace("\\\\", "\\");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::c_escape() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String escaped = *this;
|
|
|
|
escaped = escaped.replace("\\", "\\\\");
|
|
|
|
escaped = escaped.replace("\a", "\\a");
|
|
|
|
escaped = escaped.replace("\b", "\\b");
|
|
|
|
escaped = escaped.replace("\f", "\\f");
|
|
|
|
escaped = escaped.replace("\n", "\\n");
|
|
|
|
escaped = escaped.replace("\r", "\\r");
|
|
|
|
escaped = escaped.replace("\t", "\\t");
|
|
|
|
escaped = escaped.replace("\v", "\\v");
|
|
|
|
escaped = escaped.replace("\'", "\\'");
|
|
|
|
escaped = escaped.replace("\?", "\\?");
|
|
|
|
escaped = escaped.replace("\"", "\\\"");
|
2017-01-16 18:03:38 +01:00
|
|
|
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::c_escape_multiline() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String escaped = *this;
|
|
|
|
escaped = escaped.replace("\\", "\\\\");
|
|
|
|
escaped = escaped.replace("\"", "\\\"");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
2016-01-10 19:01:06 +01:00
|
|
|
String String::json_escape() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String escaped = *this;
|
|
|
|
escaped = escaped.replace("\\", "\\\\");
|
|
|
|
escaped = escaped.replace("\b", "\\b");
|
|
|
|
escaped = escaped.replace("\f", "\\f");
|
|
|
|
escaped = escaped.replace("\n", "\\n");
|
|
|
|
escaped = escaped.replace("\r", "\\r");
|
|
|
|
escaped = escaped.replace("\t", "\\t");
|
|
|
|
escaped = escaped.replace("\v", "\\v");
|
|
|
|
escaped = escaped.replace("\"", "\\\"");
|
2016-01-10 19:01:06 +01:00
|
|
|
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String String::xml_escape(bool p_escape_quotes) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String str = *this;
|
|
|
|
str = str.replace("&", "&");
|
|
|
|
str = str.replace("<", "<");
|
|
|
|
str = str.replace(">", ">");
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_escape_quotes) {
|
2017-03-05 16:44:50 +01:00
|
|
|
str = str.replace("'", "'");
|
|
|
|
str = str.replace("\"", """);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
for (int i=1;i<32;i++) {
|
|
|
|
|
|
|
|
char chr[2]={i,0};
|
|
|
|
str=str.replace(chr,"&#"+String::num(i)+";");
|
|
|
|
}*/
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static _FORCE_INLINE_ int _xml_unescape(const CharType *p_src, int p_src_len, CharType *p_dst) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = 0;
|
|
|
|
while (p_src_len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (*p_src == '&') {
|
2014-06-11 15:41:03 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int eat = 0;
|
2014-06-11 15:41:03 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_src_len >= 4 && p_src[1] == '#') {
|
2014-06-11 15:41:03 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType c = 0;
|
2014-06-11 15:41:03 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 2; i < p_src_len; i++) {
|
2014-06-11 15:41:03 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
eat = i + 1;
|
|
|
|
CharType ct = p_src[i];
|
|
|
|
if (ct == ';') {
|
2014-06-11 15:41:03 +02:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (ct >= '0' && ct <= '9') {
|
|
|
|
ct = ct - '0';
|
|
|
|
} else if (ct >= 'a' && ct <= 'f') {
|
|
|
|
ct = (ct - 'a') + 10;
|
|
|
|
} else if (ct >= 'A' && ct <= 'F') {
|
|
|
|
ct = (ct - 'A') + 10;
|
2014-06-11 15:41:03 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
c <<= 4;
|
|
|
|
c |= ct;
|
2014-06-11 15:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_dst)
|
2017-03-05 16:44:50 +01:00
|
|
|
*p_dst = c;
|
2014-06-11 15:41:03 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (p_src_len >= 4 && p_src[1] == 'g' && p_src[2] == 't' && p_src[3] == ';') {
|
2014-06-11 15:41:03 +02:00
|
|
|
|
|
|
|
if (p_dst)
|
2017-03-05 16:44:50 +01:00
|
|
|
*p_dst = '>';
|
|
|
|
eat = 4;
|
|
|
|
} else if (p_src_len >= 4 && p_src[1] == 'l' && p_src[2] == 't' && p_src[3] == ';') {
|
2014-06-11 15:41:03 +02:00
|
|
|
|
|
|
|
if (p_dst)
|
2017-03-05 16:44:50 +01:00
|
|
|
*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] == ';') {
|
2014-06-11 15:41:03 +02:00
|
|
|
|
|
|
|
if (p_dst)
|
2017-03-05 16:44:50 +01:00
|
|
|
*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] == ';') {
|
2014-06-11 15:41:03 +02:00
|
|
|
|
|
|
|
if (p_dst)
|
2017-03-05 16:44:50 +01:00
|
|
|
*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] == ';') {
|
2014-06-11 15:41:03 +02:00
|
|
|
|
|
|
|
if (p_dst)
|
2017-03-05 16:44:50 +01:00
|
|
|
*p_dst = '\'';
|
|
|
|
eat = 6;
|
2014-06-11 15:41:03 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (p_dst)
|
2017-03-05 16:44:50 +01:00
|
|
|
*p_dst = *p_src;
|
|
|
|
eat = 1;
|
2014-06-11 15:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_dst)
|
|
|
|
p_dst++;
|
|
|
|
|
|
|
|
len++;
|
2017-03-05 16:44:50 +01:00
|
|
|
p_src += eat;
|
|
|
|
p_src_len -= eat;
|
2014-06-11 15:41:03 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (p_dst) {
|
2017-03-05 16:44:50 +01:00
|
|
|
*p_dst = *p_src;
|
2014-06-11 15:41:03 +02:00
|
|
|
p_dst++;
|
|
|
|
}
|
|
|
|
len++;
|
|
|
|
p_src++;
|
|
|
|
p_src_len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-06-11 15:41:03 +02:00
|
|
|
String String::xml_unescape() const {
|
|
|
|
|
|
|
|
String str;
|
|
|
|
int l = length();
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = _xml_unescape(c_str(), l, NULL);
|
|
|
|
if (len == 0)
|
2014-06-11 15:41:03 +02:00
|
|
|
return String();
|
2017-03-05 16:44:50 +01:00
|
|
|
str.resize(len + 1);
|
2018-12-16 01:44:18 +01:00
|
|
|
_xml_unescape(c_str(), l, str.ptrw());
|
2017-03-05 16:44:50 +01:00
|
|
|
str[len] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::pad_decimals(int p_digits) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String s = *this;
|
2014-02-10 02:10:30 +01:00
|
|
|
int c = s.find(".");
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c == -1) {
|
|
|
|
if (p_digits <= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return s;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
s += ".";
|
|
|
|
c = s.length() - 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_digits <= 0) {
|
|
|
|
return s.substr(0, c);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (s.length() - (c + 1) > p_digits) {
|
|
|
|
s = s.substr(0, c + p_digits + 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (s.length() - (c + 1) < p_digits) {
|
|
|
|
s += "0";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::pad_zeros(int p_digits) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String s = *this;
|
2014-02-10 02:10:30 +01:00
|
|
|
int end = s.find(".");
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (end == -1) {
|
|
|
|
end = s.length();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (end == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return s;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int begin = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (begin < end && (s[begin] < '0' || s[begin] > '9')) {
|
2014-02-10 02:10:30 +01:00
|
|
|
begin++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (begin >= end)
|
2014-02-10 02:10:30 +01:00
|
|
|
return s;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (end - begin < p_digits) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
s = s.insert(begin, "0");
|
2014-02-10 02:10:30 +01:00
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-04-13 16:40:27 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
bool String::is_valid_integer() const {
|
|
|
|
|
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
|
|
|
if (len != 1 && (operator[](0) == '+' || operator[](0) == '-'))
|
2014-02-10 02:10:30 +01:00
|
|
|
from++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = from; i < len; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (operator[](i) < '0' || operator[](i) > '9')
|
2014-02-10 02:10:30 +01:00
|
|
|
return false; // no start with number plz
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-20 14:58:00 +02:00
|
|
|
bool String::is_valid_hex_number(bool p_with_prefix) const {
|
|
|
|
|
|
|
|
int len = length();
|
|
|
|
|
2018-12-24 19:06:35 +01:00
|
|
|
if (len == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int from = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len != 1 && (operator[](0) == '+' || operator[](0) == '-'))
|
2016-10-20 14:58:00 +02:00
|
|
|
from++;
|
|
|
|
|
|
|
|
if (p_with_prefix) {
|
|
|
|
|
2018-04-18 22:20:39 +02:00
|
|
|
if (len < 3)
|
2016-10-20 14:58:00 +02:00
|
|
|
return false;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (operator[](from) != '0' || operator[](from + 1) != 'x') {
|
2016-10-20 14:58:00 +02:00
|
|
|
return false;
|
2018-04-18 22:20:39 +02:00
|
|
|
}
|
2016-10-20 14:58:00 +02:00
|
|
|
from += 2;
|
2018-04-18 22:20:39 +02:00
|
|
|
}
|
2016-10-20 14:58:00 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = from; i < len; i++) {
|
2016-10-20 14:58:00 +02:00
|
|
|
|
|
|
|
CharType c = operator[](i);
|
|
|
|
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
|
|
|
|
continue;
|
|
|
|
return false;
|
2018-04-18 22:20:39 +02:00
|
|
|
}
|
2016-10-20 14:58:00 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
bool String::is_valid_float() const {
|
|
|
|
|
|
|
|
int len = length();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int from = 0;
|
|
|
|
if (operator[](0) == '+' || operator[](0) == '-') {
|
2014-02-10 02:10:30 +01:00
|
|
|
from++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool exponent_found = false;
|
|
|
|
bool period_found = false;
|
|
|
|
bool sign_found = false;
|
|
|
|
bool exponent_values_found = false;
|
|
|
|
bool numbers_found = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = from; i < len; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (operator[](i) >= '0' && operator[](i) <= '9') {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (exponent_found)
|
2017-03-05 16:44:50 +01:00
|
|
|
exponent_values_found = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
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;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else
|
|
|
|
return false; // no start with number plz
|
|
|
|
}
|
|
|
|
|
|
|
|
return numbers_found;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::path_to_file(const String &p_path) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String src = this->replace("\\", "/").get_base_dir();
|
|
|
|
String dst = p_path.replace("\\", "/").get_base_dir();
|
2015-05-04 20:06:41 +02:00
|
|
|
String rel = src.path_to(dst);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (rel == dst) // failed
|
2015-05-04 20:06:41 +02:00
|
|
|
return p_path;
|
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
return rel + p_path.get_file();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::path_to(const String &p_path) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String src = this->replace("\\", "/");
|
|
|
|
String dst = p_path.replace("\\", "/");
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!src.ends_with("/"))
|
2017-03-05 16:44:50 +01:00
|
|
|
src += "/";
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!dst.ends_with("/"))
|
2017-03-05 16:44:50 +01:00
|
|
|
dst += "/";
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String base;
|
|
|
|
|
|
|
|
if (src.begins_with("res://") && dst.begins_with("res://")) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
base = "res:/";
|
|
|
|
src = src.replace("res://", "/");
|
|
|
|
dst = dst.replace("res://", "/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else if (src.begins_with("user://") && dst.begins_with("user://")) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
base = "user:/";
|
|
|
|
src = src.replace("user://", "/");
|
|
|
|
dst = dst.replace("user://", "/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else if (src.begins_with("/") && dst.begins_with("/")) {
|
|
|
|
|
|
|
|
//nothing
|
|
|
|
} else {
|
|
|
|
//dos style
|
2017-03-05 16:44:50 +01:00
|
|
|
String src_begin = src.get_slicec('/', 0);
|
|
|
|
String dst_begin = dst.get_slicec('/', 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src_begin != dst_begin)
|
2015-05-04 20:06:41 +02:00
|
|
|
return p_path; //impossible to do this
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
base = src_begin;
|
|
|
|
src = src.substr(src_begin.length(), src.length());
|
|
|
|
dst = dst.substr(dst_begin.length(), dst.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//remove leading and trailing slash and split
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<String> src_dirs = src.substr(1, src.length() - 2).split("/");
|
|
|
|
Vector<String> dst_dirs = dst.substr(1, dst.length() - 2).split("/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//find common parent
|
2017-03-05 16:44:50 +01:00
|
|
|
int common_parent = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
|
|
|
if (src_dirs.size() == common_parent)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (dst_dirs.size() == common_parent)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (src_dirs[common_parent] != dst_dirs[common_parent])
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
common_parent++;
|
|
|
|
}
|
|
|
|
|
|
|
|
common_parent--;
|
|
|
|
|
|
|
|
String dir;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = src_dirs.size() - 1; i > common_parent; i--) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dir += "../";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dir += dst_dirs[i] + "/";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (dir.length() == 0)
|
|
|
|
dir = "./";
|
2014-02-10 02:10:30 +01:00
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::is_valid_html_color() const {
|
|
|
|
|
|
|
|
return Color::html_is_valid(*this);
|
|
|
|
}
|
|
|
|
|
2019-04-09 00:18:03 +02:00
|
|
|
bool String::is_valid_filename() const {
|
|
|
|
|
|
|
|
String stripped = strip_edges();
|
|
|
|
if (*this != stripped) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stripped == String()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:08:25 +02:00
|
|
|
return !(find(":") != -1 || find("/") != -1 || find("\\") != -1 || find("?") != -1 || find("*") != -1 || find("\"") != -1 || find("|") != -1 || find("%") != -1 || find("<") != -1 || find(">") != -1);
|
2019-04-09 00:18:03 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
bool String::is_valid_ip_address() const {
|
|
|
|
|
2016-10-20 14:58:00 +02:00
|
|
|
if (find(":") >= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-20 14:58:00 +02:00
|
|
|
Vector<String> ip = split(":");
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < ip.size(); i++) {
|
2016-10-20 14:58:00 +02:00
|
|
|
|
|
|
|
String n = ip[i];
|
|
|
|
if (n.empty())
|
|
|
|
continue;
|
|
|
|
if (n.is_valid_hex_number(false)) {
|
|
|
|
int nint = n.hex_to_int(false);
|
|
|
|
if (nint < 0 || nint > 0xffff)
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if (!n.is_valid_ip_address())
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Vector<String> ip = split(".");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (ip.size() != 4)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < ip.size(); i++) {
|
2016-10-20 14:58:00 +02:00
|
|
|
|
|
|
|
String n = ip[i];
|
|
|
|
if (!n.is_valid_integer())
|
|
|
|
return false;
|
|
|
|
int val = n.to_int();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (val < 0 || val > 255)
|
2016-10-20 14:58:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::is_resource_file() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return begins_with("res://") && find("::") == -1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool String::is_rel_path() const {
|
|
|
|
|
|
|
|
return !is_abs_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::get_base_dir() const {
|
|
|
|
|
|
|
|
int basepos = find("://");
|
|
|
|
String rs;
|
|
|
|
String base;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (basepos != -1) {
|
|
|
|
int end = basepos + 3;
|
|
|
|
rs = substr(end, length());
|
|
|
|
base = substr(0, end);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
if (begins_with("/")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
rs = substr(1, length());
|
|
|
|
base = "/";
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
rs = *this;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int sep = MAX(rs.find_last("/"), rs.find_last("\\"));
|
|
|
|
if (sep == -1)
|
2014-02-10 02:10:30 +01:00
|
|
|
return base;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return base + rs.substr(0, sep);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String String::get_file() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int sep = MAX(find_last("/"), find_last("\\"));
|
|
|
|
if (sep == -1)
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return substr(sep + 1, length());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-14 04:51:09 +01:00
|
|
|
String String::get_extension() const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int pos = find_last(".");
|
2018-01-11 12:59:31 +01:00
|
|
|
if (pos < 0 || pos < MAX(find_last("/"), find_last("\\")))
|
|
|
|
return "";
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return substr(pos + 1, length());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::plus_file(const String &p_file) const {
|
2017-01-14 17:47:07 +01:00
|
|
|
if (empty())
|
|
|
|
return p_file;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (operator[](length() - 1) == '/' || (p_file.size() > 0 && p_file.operator[](0) == '/'))
|
|
|
|
return *this + p_file;
|
|
|
|
return *this + "/" + p_file;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-04-12 21:12:34 +02:00
|
|
|
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 {
|
|
|
|
char p[4] = { '%', 0, 0, 0 };
|
|
|
|
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
|
|
|
|
|
|
|
p[1] = hex[c >> 4];
|
|
|
|
p[2] = hex[c & 0xF];
|
|
|
|
encoded += p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
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')
|
|
|
|
c = (a - '0') << 4;
|
|
|
|
else if (a >= 'a' && a <= 'f')
|
|
|
|
c = (a - 'a' + 10) << 4;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint8_t d = 0;
|
|
|
|
|
|
|
|
if (b >= '0' && b <= '9')
|
|
|
|
d = (b - '0');
|
|
|
|
else if (b >= 'a' && b <= 'f')
|
|
|
|
d = (b - 'a' + 10);
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
c += d;
|
|
|
|
i += 2;
|
|
|
|
}
|
2018-07-25 03:11:03 +02:00
|
|
|
pe += c;
|
2018-04-12 21:12:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return String::utf8(pe.ptr());
|
|
|
|
}
|
|
|
|
|
2017-01-14 04:51:09 +01:00
|
|
|
String String::get_basename() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int pos = find_last(".");
|
2018-01-11 12:59:31 +01:00
|
|
|
if (pos < 0 || pos < MAX(find_last("/"), find_last("\\")))
|
2014-02-10 02:10:30 +01:00
|
|
|
return *this;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return substr(0, pos);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String itos(int64_t p_val) {
|
|
|
|
|
|
|
|
return String::num_int64(p_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
String rtos(double p_val) {
|
|
|
|
|
|
|
|
return String::num(p_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
String rtoss(double p_val) {
|
|
|
|
|
|
|
|
return String::num_scientific(p_val);
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:42:36 +01:00
|
|
|
// Right-pad with a character.
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::rpad(int min_length, const String &character) const {
|
2015-02-01 19:42:36 +01:00
|
|
|
String s = *this;
|
|
|
|
int padding = min_length - s.length();
|
|
|
|
if (padding > 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < padding; i++)
|
|
|
|
s = s + character;
|
2015-02-01 19:42:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
// Left-pad with a character.
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::lpad(int min_length, const String &character) const {
|
2015-02-01 19:42:36 +01:00
|
|
|
String s = *this;
|
|
|
|
int padding = min_length - s.length();
|
|
|
|
if (padding > 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < padding; i++)
|
|
|
|
s = character + s;
|
2015-02-01 19:42:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-01-10 21:44:20 +01:00
|
|
|
// sprintf is implemented in GDScript via:
|
|
|
|
// "fish %s pie" % "frog"
|
|
|
|
// "fish %s %d pie" % ["frog", 12]
|
2015-02-19 16:45:49 +01:00
|
|
|
// In case of an error, the string returned is the error description and "error" is true.
|
2017-03-05 16:44:50 +01:00
|
|
|
String String::sprintf(const Array &values, bool *error) const {
|
2015-01-10 21:44:20 +01:00
|
|
|
String formatted;
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType *self = (CharType *)c_str();
|
2015-01-10 21:44:20 +01:00
|
|
|
bool in_format = false;
|
|
|
|
int value_index = 0;
|
2017-09-01 22:33:39 +02:00
|
|
|
int min_chars = 0;
|
|
|
|
int min_decimals = 0;
|
|
|
|
bool in_decimals = false;
|
|
|
|
bool pad_with_zeroes = false;
|
|
|
|
bool left_justified = false;
|
|
|
|
bool show_sign = false;
|
2015-01-10 21:44:20 +01:00
|
|
|
|
2015-02-19 16:45:49 +01:00
|
|
|
*error = true;
|
2015-02-01 21:18:38 +01:00
|
|
|
|
2015-01-10 21:44:20 +01:00
|
|
|
for (; *self; self++) {
|
|
|
|
const CharType c = *self;
|
|
|
|
|
|
|
|
if (in_format) { // We have % - lets see what else we get.
|
|
|
|
switch (c) {
|
2015-02-01 19:42:36 +01:00
|
|
|
case '%': { // Replace %% with %
|
2015-01-10 21:44:20 +01:00
|
|
|
formatted += chr(c);
|
|
|
|
in_format = false;
|
|
|
|
break;
|
2015-02-01 19:42:36 +01:00
|
|
|
}
|
2015-01-10 21:44:20 +01:00
|
|
|
case 'd': // Integer (signed)
|
|
|
|
case 'o': // Octal
|
|
|
|
case 'x': // Hexadecimal (lowercase)
|
2015-02-01 19:42:36 +01:00
|
|
|
case 'X': { // Hexadecimal (uppercase)
|
2015-02-01 21:18:38 +01:00
|
|
|
if (value_index >= values.size()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "not enough arguments for format string";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
2015-02-01 19:42:36 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
if (!values[value_index].is_num()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "a number is required";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
int64_t value = values[value_index];
|
2017-09-01 22:33:39 +02:00
|
|
|
int base = 16;
|
2015-02-01 21:18:38 +01:00
|
|
|
bool capitalize = false;
|
|
|
|
switch (c) {
|
|
|
|
case 'd': base = 10; break;
|
|
|
|
case 'o': base = 8; break;
|
2017-09-01 22:33:39 +02:00
|
|
|
case 'x': break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case 'X':
|
|
|
|
base = 16;
|
|
|
|
capitalize = true;
|
|
|
|
break;
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
// Get basic number.
|
2017-02-08 02:40:35 +01:00
|
|
|
String str = String::num_int64(ABS(value), base, capitalize);
|
|
|
|
int number_len = str.length();
|
2015-02-01 19:42:36 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
// Padding.
|
|
|
|
String pad_char = pad_with_zeroes ? String("0") : String(" ");
|
|
|
|
if (left_justified) {
|
|
|
|
str = str.rpad(min_chars, pad_char);
|
2015-01-10 21:44:20 +01:00
|
|
|
} else {
|
2015-02-01 21:18:38 +01:00
|
|
|
str = str.lpad(min_chars, pad_char);
|
2015-01-10 21:44:20 +01:00
|
|
|
}
|
2015-02-01 21:18:38 +01:00
|
|
|
|
2017-02-08 02:40:35 +01:00
|
|
|
// Sign.
|
|
|
|
if (show_sign && value >= 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
str = str.insert(pad_with_zeroes ? 0 : str.length() - number_len, "+");
|
2017-02-08 02:40:35 +01:00
|
|
|
} else if (value < 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
str = str.insert(pad_with_zeroes ? 0 : str.length() - number_len, "-");
|
2017-02-08 02:40:35 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
formatted += str;
|
|
|
|
++value_index;
|
|
|
|
in_format = false;
|
|
|
|
|
2015-01-10 21:44:20 +01:00
|
|
|
break;
|
2015-02-01 19:42:36 +01:00
|
|
|
}
|
|
|
|
case 'f': { // Float
|
2015-02-01 21:18:38 +01:00
|
|
|
if (value_index >= values.size()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "not enough arguments for format string";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
2015-02-01 19:42:36 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
if (!values[value_index].is_num()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "a number is required";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
2015-02-01 19:42:36 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
double value = values[value_index];
|
|
|
|
String str = String::num(value, min_decimals);
|
2015-02-01 19:42:36 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
// Pad decimals out.
|
|
|
|
str = str.pad_decimals(min_decimals);
|
|
|
|
|
|
|
|
// Show sign
|
2018-11-09 09:27:06 +01:00
|
|
|
if (show_sign && str.left(1) != "-") {
|
2015-02-01 21:18:38 +01:00
|
|
|
str = str.insert(0, "+");
|
|
|
|
}
|
2015-01-10 21:44:20 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
// Padding
|
|
|
|
if (left_justified) {
|
|
|
|
str = str.rpad(min_chars);
|
2015-01-10 21:44:20 +01:00
|
|
|
} else {
|
2015-02-01 21:18:38 +01:00
|
|
|
str = str.lpad(min_chars);
|
2015-01-10 21:44:20 +01:00
|
|
|
}
|
2015-02-01 21:18:38 +01:00
|
|
|
|
|
|
|
formatted += str;
|
|
|
|
++value_index;
|
|
|
|
in_format = false;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2015-01-10 21:44:20 +01:00
|
|
|
break;
|
2015-02-01 19:42:36 +01:00
|
|
|
}
|
|
|
|
case 's': { // String
|
2015-02-01 21:18:38 +01:00
|
|
|
if (value_index >= values.size()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "not enough arguments for format string";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 19:42:36 +01:00
|
|
|
String str = values[value_index];
|
|
|
|
// Padding.
|
|
|
|
if (left_justified) {
|
|
|
|
str = str.rpad(min_chars);
|
|
|
|
} else {
|
|
|
|
str = str.lpad(min_chars);
|
|
|
|
}
|
2015-01-10 21:44:20 +01:00
|
|
|
|
2015-02-01 19:42:36 +01:00
|
|
|
formatted += str;
|
2015-01-10 21:44:20 +01:00
|
|
|
++value_index;
|
|
|
|
in_format = false;
|
|
|
|
break;
|
2015-02-01 19:42:36 +01:00
|
|
|
}
|
2015-02-01 21:18:38 +01:00
|
|
|
case 'c': {
|
|
|
|
if (value_index >= values.size()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "not enough arguments for format string";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to character.
|
|
|
|
String str;
|
|
|
|
if (values[value_index].is_num()) {
|
|
|
|
int value = values[value_index];
|
|
|
|
if (value < 0) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "unsigned byte integer is lower than maximum";
|
2015-02-01 21:18:38 +01:00
|
|
|
} else if (value > 255) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "unsigned byte integer is greater than maximum";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
str = chr(values[value_index]);
|
|
|
|
} else if (values[value_index].get_type() == Variant::STRING) {
|
|
|
|
str = values[value_index];
|
|
|
|
if (str.length() != 1) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "%c requires number or single-character string";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "%c requires number or single-character string";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Padding.
|
|
|
|
if (left_justified) {
|
|
|
|
str = str.rpad(min_chars);
|
|
|
|
} else {
|
|
|
|
str = str.lpad(min_chars);
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted += str;
|
|
|
|
++value_index;
|
|
|
|
in_format = false;
|
|
|
|
break;
|
|
|
|
}
|
2015-02-01 19:42:36 +01:00
|
|
|
case '-': { // Left justify
|
|
|
|
left_justified = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '+': { // Show + if positive.
|
|
|
|
show_sign = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9': {
|
2015-02-01 19:42:36 +01:00
|
|
|
int n = c - '0';
|
|
|
|
if (in_decimals) {
|
2015-02-01 21:18:38 +01:00
|
|
|
min_decimals *= 10;
|
|
|
|
min_decimals += n;
|
2015-02-01 19:42:36 +01:00
|
|
|
} else {
|
|
|
|
if (c == '0' && min_chars == 0) {
|
|
|
|
pad_with_zeroes = true;
|
|
|
|
} else {
|
|
|
|
min_chars *= 10;
|
|
|
|
min_chars += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-09-01 22:33:39 +02:00
|
|
|
case '.': { // Float separator.
|
2015-02-01 21:18:38 +01:00
|
|
|
if (in_decimals) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "too many decimal points in format";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
2015-02-01 19:42:36 +01:00
|
|
|
in_decimals = true;
|
2015-02-01 21:18:38 +01:00
|
|
|
min_decimals = 0; // We want to add the value manually.
|
2015-02-01 19:42:36 +01:00
|
|
|
break;
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-01 22:33:39 +02:00
|
|
|
case '*': { // Dynamic width, based on value.
|
2015-02-01 21:18:38 +01:00
|
|
|
if (value_index >= values.size()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "not enough arguments for format string";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!values[value_index].is_num()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "* wants number";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int size = values[value_index];
|
|
|
|
|
|
|
|
if (in_decimals) {
|
|
|
|
min_decimals = size;
|
|
|
|
} else {
|
|
|
|
min_chars = size;
|
|
|
|
}
|
2015-01-10 21:44:20 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
++value_index;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
default: {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "unsupported format character";
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
2015-01-10 21:44:20 +01:00
|
|
|
}
|
|
|
|
} else { // Not in format string.
|
|
|
|
switch (c) {
|
|
|
|
case '%':
|
|
|
|
in_format = true;
|
2015-02-01 19:42:36 +01:00
|
|
|
// Back to defaults:
|
|
|
|
min_chars = 0;
|
2015-02-01 21:18:38 +01:00
|
|
|
min_decimals = 6;
|
2015-02-01 19:42:36 +01:00
|
|
|
pad_with_zeroes = false;
|
|
|
|
left_justified = false;
|
|
|
|
show_sign = false;
|
|
|
|
in_decimals = false;
|
2015-01-10 21:44:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
formatted += chr(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-01 21:18:38 +01:00
|
|
|
if (in_format) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "incomplete format";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value_index != values.size()) {
|
2015-02-19 16:45:49 +01:00
|
|
|
return "not all arguments converted during string formatting";
|
2015-02-01 21:18:38 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 16:45:49 +01:00
|
|
|
*error = false;
|
2015-01-10 21:44:20 +01:00
|
|
|
return formatted;
|
|
|
|
}
|
2016-05-04 03:25:37 +02:00
|
|
|
|
2017-10-28 23:46:20 +02:00
|
|
|
String String::quote(String quotechar) const {
|
|
|
|
return quotechar + *this + quotechar;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::unquote() const {
|
|
|
|
if (!is_quoted()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return substr(1, length() - 2);
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:27:15 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
String TTR(const String &p_text) {
|
2016-05-04 03:25:37 +02:00
|
|
|
|
|
|
|
if (TranslationServer::get_singleton()) {
|
2016-05-17 23:27:15 +02:00
|
|
|
return TranslationServer::get_singleton()->tool_translate(p_text);
|
2016-05-04 03:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return p_text;
|
|
|
|
}
|
2016-05-17 23:27:15 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String RTR(const String &p_text) {
|
2016-05-17 23:27:15 +02:00
|
|
|
|
|
|
|
if (TranslationServer::get_singleton()) {
|
|
|
|
String rtr = TranslationServer::get_singleton()->tool_translate(p_text);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (rtr == String() || rtr == p_text) {
|
2016-05-17 23:27:15 +02:00
|
|
|
return TranslationServer::get_singleton()->translate(p_text);
|
|
|
|
} else {
|
|
|
|
return rtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_text;
|
|
|
|
}
|