1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-06 17:41:04 +00: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:
AFCMS 2024-12-04 18:19:46 +01:00 committed by GitHub
parent 18caf3a18d
commit e545e96d2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 162 additions and 45 deletions

View file

@ -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;
}

View file

@ -20,6 +20,7 @@
#include <cctype>
#include <cwctype>
#include <unordered_map>
#include <optional>
class Translations;
@ -789,9 +790,9 @@ std::string sanitize_untrusted(std::string_view str, bool keep_escapes = true);
void safe_print_string(std::ostream &os, std::string_view str);
/**
* Parses a string of form `(1, 2, 3)` to a v3f
* Parses a string of form `(1, 2, 3)` or `1, 2, 4` to a v3f
*
* @param str string
* @return float vector
*/
v3f str_to_v3f(std::string_view str);
std::optional<v3f> str_to_v3f(std::string_view str);