mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
Merge branch 'main' into release-0.32
This commit is contained in:
commit
f3131b058c
4 changed files with 44 additions and 19 deletions
|
|
@ -1,3 +1,9 @@
|
|||
debug.log("file extension")
|
||||
asserts.equals(nil, file.ext("config:test"))
|
||||
asserts.equals("cfg", file.ext("config:test.cfg"))
|
||||
asserts.equals("gz", file.ext("config:test.cfg.gz"))
|
||||
asserts.equals("config:test.cfg", file.remove_ext("config:test.cfg.gz"))
|
||||
|
||||
debug.log("check initial state")
|
||||
assert(file.exists("config:"))
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ function file.stem(path)
|
|||
end
|
||||
|
||||
function file.ext(path)
|
||||
return path:match("%.([^:/\\]+)$")
|
||||
return path:match("[^:/\\.]%.([^.:/\\]+)$")
|
||||
end
|
||||
|
||||
function file.prefix(path)
|
||||
|
|
@ -24,7 +24,11 @@ function file.parent(path)
|
|||
end
|
||||
|
||||
function file.remove_ext(path)
|
||||
return string.format("%s/%s", file.parent(path), file.stem(path))
|
||||
local ext = file.ext(path)
|
||||
if not ext then
|
||||
return path
|
||||
end
|
||||
return path:sub(1, -#ext - 2)
|
||||
end
|
||||
|
||||
function file.path(path)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
#include "stringutil.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <locale>
|
||||
#include "stringutil.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
|
|
@ -509,17 +511,22 @@ int util::replaceAll(
|
|||
return replace_all(str, from, to);
|
||||
}
|
||||
|
||||
// replace it with std::from_chars in the far far future
|
||||
double util::parse_double(const std::string& str) {
|
||||
std::istringstream ss(str);
|
||||
ss.imbue(std::locale("C"));
|
||||
double d;
|
||||
ss >> d;
|
||||
if (ss.fail()) {
|
||||
throw std::runtime_error("invalid number format");
|
||||
}
|
||||
return d;
|
||||
}
|
||||
double util::parse_double(const std::string& str) {
|
||||
const char* begin = str.data();
|
||||
const char* end = begin + str.size();
|
||||
while (begin != end && std::isspace(static_cast<unsigned char>(*begin))) {
|
||||
begin++;
|
||||
}
|
||||
if (begin != end && *begin == '+') {
|
||||
begin++;
|
||||
}
|
||||
double d = 0.0;
|
||||
const auto result = std::from_chars(begin, end, d);
|
||||
if (result.ec != std::errc()) {
|
||||
throw std::runtime_error("invalid number format");
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
double util::parse_double(const std::string& str, size_t offset, size_t len) {
|
||||
return parse_double(str.substr(offset, len));
|
||||
|
|
|
|||
|
|
@ -68,6 +68,14 @@ TEST(stringutil, base64_urlsafe) {
|
|||
}
|
||||
}
|
||||
|
||||
TEST(stringutil, parse_double) {
|
||||
EXPECT_DOUBLE_EQ(util::parse_double("53.125"), 53.125);
|
||||
EXPECT_DOUBLE_EQ(util::parse_double("-0.25"), -0.25);
|
||||
EXPECT_DOUBLE_EQ(util::parse_double(" +12.5"), 12.5);
|
||||
EXPECT_DOUBLE_EQ(util::parse_double("xx1.75yy", 2, 4), 1.75);
|
||||
EXPECT_THROW(util::parse_double("not-a-number"), std::runtime_error);
|
||||
}
|
||||
|
||||
class StringParser : BasicParser<char> {
|
||||
public:
|
||||
StringParser(std::string_view source) : BasicParser("<string>", source) {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue