summaryrefslogtreecommitdiff
path: root/core/ustring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r--core/ustring.cpp165
1 files changed, 88 insertions, 77 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp
index 78feddb229..75e3b6f22e 100644
--- a/core/ustring.cpp
+++ b/core/ustring.cpp
@@ -31,6 +31,7 @@
#include "ustring.h"
#include "core/color.h"
+#include "core/math/crypto_core.h"
#include "core/math/math_funcs.h"
#include "core/os/memory.h"
#include "core/print_string.h"
@@ -38,9 +39,6 @@
#include "core/ucaps.h"
#include "core/variant.h"
-#include "thirdparty/misc/md5.h"
-#include "thirdparty/misc/sha256.h"
-
#include <wchar.h>
#ifndef NO_USE_STDLIB
@@ -481,8 +479,6 @@ signed char String::nocasecmp_to(const String &p_str) const {
this_str++;
that_str++;
}
-
- return 0; //should never reach anyway
}
signed char String::casecmp_to(const String &p_str) const {
@@ -513,8 +509,6 @@ signed char String::casecmp_to(const String &p_str) const {
this_str++;
that_str++;
}
-
- return 0; //should never reach anyway
}
signed char String::naturalnocasecmp_to(const String &p_str) const {
@@ -731,8 +725,6 @@ String String::get_slicec(CharType p_splitter, int p_slice) const {
i++;
}
-
- return String(); //no find!
}
Vector<String> String::split_spaces() const {
@@ -786,7 +778,7 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p
if (p_allow_empty || (end > from)) {
if (p_maxsplit <= 0)
ret.push_back(substr(from, end - from));
- else if (p_maxsplit > 0) {
+ else {
// Put rest of the string and leave cycle.
if (p_maxsplit == ret.size()) {
@@ -1725,6 +1717,45 @@ int64_t String::hex_to_int64(bool p_with_prefix) const {
return hex * sign;
}
+int64_t String::bin_to_int64(bool p_with_prefix) const {
+
+ if (p_with_prefix && length() < 3)
+ 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;
+}
+
int String::to_int() const {
if (length() == 0)
@@ -2221,54 +2252,63 @@ uint64_t String::hash64() const {
String String::md5_text() const {
CharString cs = utf8();
- MD5_CTX ctx;
- MD5Init(&ctx);
- MD5Update(&ctx, (unsigned char *)cs.ptr(), cs.length());
- MD5Final(&ctx);
- return String::md5(ctx.digest);
+ unsigned char hash[16];
+ CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
+ return String::hex_encode_buffer(hash, 16);
+}
+
+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);
}
String String::sha256_text() const {
CharString cs = utf8();
unsigned char hash[32];
- sha256_context ctx;
- sha256_init(&ctx);
- sha256_hash(&ctx, (unsigned char *)cs.ptr(), cs.length());
- sha256_done(&ctx, hash);
+ CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
return String::hex_encode_buffer(hash, 32);
}
Vector<uint8_t> String::md5_buffer() const {
CharString cs = utf8();
- MD5_CTX ctx;
- MD5Init(&ctx);
- MD5Update(&ctx, (unsigned char *)cs.ptr(), cs.length());
- MD5Final(&ctx);
+ unsigned char hash[16];
+ CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
Vector<uint8_t> ret;
ret.resize(16);
for (int i = 0; i < 16; i++) {
- ret.write[i] = ctx.digest[i];
- };
-
+ ret.write[i] = hash[i];
+ }
return ret;
};
+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;
+}
+
Vector<uint8_t> String::sha256_buffer() const {
CharString cs = utf8();
unsigned char hash[32];
- sha256_context ctx;
- sha256_init(&ctx);
- sha256_hash(&ctx, (unsigned char *)cs.ptr(), cs.length());
- sha256_done(&ctx, hash);
+ CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
Vector<uint8_t> ret;
ret.resize(32);
for (int i = 0; i < 32; i++) {
ret.write[i] = hash[i];
}
-
return ret;
}
@@ -2292,6 +2332,9 @@ String String::insert(int p_at_pos, const String &p_string) const {
}
String String::substr(int p_from, int p_chars) const {
+ if (p_chars == -1)
+ p_chars = length() - p_from;
+
if (empty() || p_from < 0 || p_from >= length() || p_chars <= 0)
return "";
@@ -2917,26 +2960,12 @@ String String::replace(const char *p_key, const char *p_with) const {
String String::replace_first(const String &p_key, const String &p_with) const {
- String new_string;
- int search_from = 0;
- int result = 0;
-
- while ((result = find(p_key, search_from)) >= 0) {
-
- new_string += substr(search_from, result - search_from);
- new_string += p_with;
- search_from = result + p_key.length();
- break;
- }
-
- if (search_from == 0) {
-
- return *this;
+ int pos = find(p_key);
+ if (pos >= 0) {
+ return substr(0, pos) + p_with + substr(pos + p_key.length(), length());
}
- new_string += substr(search_from, length() - search_from);
-
- return new_string;
+ return *this;
}
String String::replacen(const String &p_key, const String &p_with) const {
@@ -3063,29 +3092,16 @@ String String::strip_edges(bool left, bool right) const {
String String::strip_escapes() const {
- int len = length();
- int beg = 0, end = len;
-
+ String new_string;
for (int i = 0; i < length(); i++) {
- if (operator[](i) <= 31)
- beg++;
- else
- break;
- }
-
- for (int i = (int)(length() - 1); i >= 0; i--) {
-
- if (operator[](i) <= 31)
- end--;
- else
- break;
+ // Escape characters on first page of the ASCII table, before 32 (Space).
+ if (operator[](i) < 32)
+ continue;
+ new_string += operator[](i);
}
- if (beg == 0 && end == len)
- return *this;
-
- return substr(beg, end - beg);
+ return new_string;
}
String String::lstrip(const String &p_chars) const {
@@ -3209,7 +3225,7 @@ static int _humanize_digits(int p_num) {
String String::humanize_size(size_t p_size) {
uint64_t _div = 1;
- static const char *prefix[] = { " Bytes", " KB", " MB", " GB", "TB", " PB", "HB", "" };
+ static const char *prefix[] = { " Bytes", " KB", " MB", " GB", " TB", " PB", " EB", "" };
int prefix_idx = 0;
while (p_size > (_div * 1024) && prefix[prefix_idx][0]) {
@@ -3220,7 +3236,7 @@ String String::humanize_size(size_t p_size) {
int digits = prefix_idx > 0 ? _humanize_digits(p_size / _div) : 0;
double divisor = prefix_idx > 0 ? _div : 1;
- return String::num(p_size / divisor, digits) + prefix[prefix_idx];
+ return String::num(p_size / divisor).pad_decimals(digits) + prefix[prefix_idx];
}
bool String::is_abs_path() const {
@@ -3322,7 +3338,7 @@ String String::http_unescape() const {
if ((ord1 >= '0' && ord1 <= '9') || (ord1 >= 'A' && ord1 <= 'Z')) {
CharType ord2 = ord_at(i + 2);
if ((ord2 >= '0' && ord2 <= '9') || (ord2 >= 'A' && ord2 <= 'Z')) {
- char bytes[2] = { (char)ord1, (char)ord2 };
+ char bytes[3] = { (char)ord1, (char)ord2, 0 };
res += (char)strtol(bytes, NULL, 16);
i += 2;
}
@@ -3784,11 +3800,7 @@ bool String::is_valid_filename() const {
return false;
}
- if (find(":") != -1 || find("/") != -1 || find("\\") != -1 || find("?") != -1 || find("*") != -1 || find("\"") != -1 || find("|") != -1 || find("%") != -1 || find("<") != -1 || find(">") != -1) {
- return false;
- } else {
- return true;
- }
+ return !(find(":") != -1 || find("/") != -1 || find("\\") != -1 || find("?") != -1 || find("*") != -1 || find("\"") != -1 || find("|") != -1 || find("%") != -1 || find("<") != -1 || find(">") != -1);
}
bool String::is_valid_ip_address() const {
@@ -3926,7 +3938,6 @@ String String::percent_decode() const {
uint8_t a = LOWERCASE(cs[i + 1]);
uint8_t b = LOWERCASE(cs[i + 2]);
- c = 0;
if (a >= '0' && a <= '9')
c = (a - '0') << 4;
else if (a >= 'a' && a <= 'f')