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.
This commit is contained in:
parent
a3e3bf8227
commit
e829b7aee4
15 changed files with 81 additions and 142 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue