mirror of
https://github.com/luanti-org/luanti.git
synced 2025-11-06 10:15:19 +01:00
Make string to v3f parsing consistent, replace core.setting_get_pos() by core.settings:get_pos() (#15438)
Co-authored-by: sfan5 <sfan5@live.de> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@@ -1068,14 +1068,41 @@ void safe_print_string(std::ostream &os, std::string_view str)
|
||||
os.setf(flags);
|
||||
}
|
||||
|
||||
|
||||
v3f str_to_v3f(std::string_view str)
|
||||
std::optional<v3f> str_to_v3f(std::string_view str)
|
||||
{
|
||||
v3f value;
|
||||
Strfnd f(str);
|
||||
f.next("(");
|
||||
value.X = stof(f.next(","));
|
||||
value.Y = stof(f.next(","));
|
||||
value.Z = stof(f.next(")"));
|
||||
return value;
|
||||
str = trim(str);
|
||||
|
||||
if (str.empty())
|
||||
return std::nullopt;
|
||||
|
||||
// Strip parentheses if they exist
|
||||
if (str.front() == '(' && str.back() == ')') {
|
||||
str.remove_prefix(1);
|
||||
str.remove_suffix(1);
|
||||
str = trim(str);
|
||||
}
|
||||
|
||||
std::istringstream iss((std::string(str)));
|
||||
|
||||
const auto expect_delimiter = [&]() {
|
||||
const auto c = iss.get();
|
||||
return c == ' ' || c == ',';
|
||||
};
|
||||
|
||||
v3f value;
|
||||
if (!(iss >> value.X))
|
||||
return std::nullopt;
|
||||
if (!expect_delimiter())
|
||||
return std::nullopt;
|
||||
if (!(iss >> value.Y))
|
||||
return std::nullopt;
|
||||
if (!expect_delimiter())
|
||||
return std::nullopt;
|
||||
if (!(iss >> value.Z))
|
||||
return std::nullopt;
|
||||
|
||||
if (!iss.eof())
|
||||
return std::nullopt;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user