Merge pull request #86936 from akx/env-utf-8
Attempt parsing environment variables as UTF-8
This commit is contained in:
commit
b0d07b1bc2
|
@ -705,10 +705,15 @@ bool OS_Unix::has_environment(const String &p_var) const {
|
|||
}
|
||||
|
||||
String OS_Unix::get_environment(const String &p_var) const {
|
||||
if (getenv(p_var.utf8().get_data())) {
|
||||
return getenv(p_var.utf8().get_data());
|
||||
}
|
||||
const char *val = getenv(p_var.utf8().get_data());
|
||||
if (val == nullptr) { // Not set; return empty string
|
||||
return "";
|
||||
}
|
||||
String s;
|
||||
if (s.parse_utf8(val) == OK) {
|
||||
return s;
|
||||
}
|
||||
return String(val); // Not valid UTF-8, so return as-is
|
||||
}
|
||||
|
||||
void OS_Unix::set_environment(const String &p_var, const String &p_value) const {
|
||||
|
|
|
@ -47,11 +47,33 @@ TEST_CASE("[OS] Environment variables") {
|
|||
OS::get_singleton()->has_environment("HOME"),
|
||||
"The HOME environment variable should be present.");
|
||||
#endif
|
||||
}
|
||||
|
||||
OS::get_singleton()->set_environment("HELLO", "world");
|
||||
TEST_CASE("[OS] UTF-8 environment variables") {
|
||||
String value = String::utf8("hell\xc3\xb6"); // "hellö", UTF-8 encoded
|
||||
|
||||
OS::get_singleton()->set_environment("HELLO", value);
|
||||
String val = OS::get_singleton()->get_environment("HELLO");
|
||||
CHECK_MESSAGE(
|
||||
OS::get_singleton()->get_environment("HELLO") == "world",
|
||||
val == value,
|
||||
"The previously-set HELLO environment variable should return the expected value.");
|
||||
CHECK_MESSAGE(
|
||||
val.length() == 5,
|
||||
"The previously-set HELLO environment variable was decoded as UTF-8 and should have a length of 5.");
|
||||
OS::get_singleton()->unset_environment("HELLO");
|
||||
}
|
||||
|
||||
TEST_CASE("[OS] Non-UTF-8 environment variables") {
|
||||
String value = String("\xff t\xf6rkylempij\xe4vongahdus"); // hex FF and a Finnish pangram, latin-1
|
||||
OS::get_singleton()->set_environment("HELLO", value);
|
||||
String val = OS::get_singleton()->get_environment("HELLO");
|
||||
CHECK_MESSAGE(
|
||||
val == value,
|
||||
"The previously-set HELLO environment variable should return the expected value.");
|
||||
CHECK_MESSAGE(
|
||||
val.length() == 23,
|
||||
"The previously-set HELLO environment variable was not decoded from Latin-1.");
|
||||
OS::get_singleton()->unset_environment("HELLO");
|
||||
}
|
||||
|
||||
TEST_CASE("[OS] Command line arguments") {
|
||||
|
|
Loading…
Reference in New Issue