diff options
author | Aaron Franke <arnfranke@yahoo.com> | 2020-11-29 22:43:38 -0500 |
---|---|---|
committer | Aaron Franke <arnfranke@yahoo.com> | 2021-01-28 07:45:01 -0500 |
commit | e829b7aee48cfc988abea5a42bdbf02638a16513 (patch) | |
tree | 066731a9a3a000b97df58d33dd18841ef7f0b234 /core/string | |
parent | a3e3bf822761c477d3a297fe004496ffc6c7b10d (diff) |
Unify URI encoding/decoding and add to C#
http_escape and percent_encode have been unified into uri_encode, and http_unescape and percent_decode have been unified into uri_decode.
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/ustring.cpp | 63 | ||||
-rw-r--r-- | core/string/ustring.h | 7 |
2 files changed, 6 insertions, 64 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index a38395b1c7..0b6311dadf 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -3752,7 +3752,7 @@ bool String::is_valid_string() const { return valid; } -String String::http_escape() const { +String String::uri_encode() const { const CharString temp = utf8(); String res; for (int i = 0; i < temp.length(); ++i) { @@ -3776,7 +3776,7 @@ String String::http_escape() const { return res; } -String String::http_unescape() const { +String String::uri_decode() const { String res; for (int i = 0; i < length(); ++i) { if (unicode_at(i) == '%' && i + 2 < length()) { @@ -3791,6 +3791,8 @@ String String::http_unescape() const { } else { res += unicode_at(i); } + } else if (unicode_at(i) == '+') { + res += ' '; } else { res += unicode_at(i); } @@ -4342,63 +4344,6 @@ String String::plus_file(const String &p_file) const { return *this + "/" + p_file; } -String String::percent_encode() const { - CharString cs = utf8(); - String encoded; - for (int i = 0; i < cs.length(); i++) { - uint8_t c = cs[i]; - if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '~' || c == '.') { - char p[2] = { (char)c, 0 }; - encoded += p; - } else { - 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; - } - pe += c; - } - - return String::utf8(pe.ptr()); -} - String String::property_name_encode() const { // Escape and quote strings with extended ASCII or further Unicode characters // as well as '"', '=' or ' ' (32) diff --git a/core/string/ustring.h b/core/string/ustring.h index e3c747af26..821941036f 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -409,17 +409,14 @@ public: String xml_escape(bool p_escape_quotes = false) const; String xml_unescape() const; - String http_escape() const; - String http_unescape() const; + String uri_encode() const; + String uri_decode() const; String c_escape() const; String c_escape_multiline() const; String c_unescape() const; String json_escape() const; String word_wrap(int p_chars_per_line) const; - String percent_encode() const; - String percent_decode() const; - String property_name_encode() const; bool is_valid_identifier() const; |