diff options
author | Haoyu Qiu <timothyqiu32@gmail.com> | 2024-05-22 10:22:50 +0800 |
---|---|---|
committer | Haoyu Qiu <timothyqiu32@gmail.com> | 2024-09-27 19:42:30 +0800 |
commit | 6516ca6b11a6241d7908eb322343d44c10050d98 (patch) | |
tree | 4ab5d25c59eb44498f47b981714b88cdc5fe519c /core/string | |
parent | 506d6e427a4eecfc1e5e5ee1180019a876119701 (diff) | |
download | redot-engine-6516ca6b11a6241d7908eb322343d44c10050d98.tar.gz |
Parse fragment from URL
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/ustring.cpp | 25 | ||||
-rw-r--r-- | core/string/ustring.h | 2 |
2 files changed, 22 insertions, 5 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 391a203d5b..e6f7492a18 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -221,18 +221,35 @@ void CharString::copy_from(const char *p_cstr) { /* String */ /*************************************************************************/ -Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const { - // Splits the URL into scheme, host, port, path. Strip credentials when present. +Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const { + // Splits the URL into scheme, host, port, path, fragment. Strip credentials when present. String base = *this; r_scheme = ""; r_host = ""; r_port = 0; r_path = ""; + r_fragment = ""; + int pos = base.find("://"); // Scheme if (pos != -1) { - r_scheme = base.substr(0, pos + 3).to_lower(); - base = base.substr(pos + 3, base.length() - pos - 3); + bool is_scheme_valid = true; + for (int i = 0; i < pos; i++) { + if (!is_ascii_alphanumeric_char(base[i]) && base[i] != '+' && base[i] != '-' && base[i] != '.') { + is_scheme_valid = false; + break; + } + } + if (is_scheme_valid) { + r_scheme = base.substr(0, pos + 3).to_lower(); + base = base.substr(pos + 3, base.length() - pos - 3); + } + } + pos = base.find("#"); + // Fragment + if (pos != -1) { + r_fragment = base.substr(pos + 1); + base = base.substr(0, pos); } pos = base.find("/"); // Path diff --git a/core/string/ustring.h b/core/string/ustring.h index 5d4b209c25..aa62c9cb18 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -452,7 +452,7 @@ public: String c_escape_multiline() const; String c_unescape() const; String json_escape() const; - Error parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const; + Error parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const; String property_name_encode() const; |