From aadf9a27dddd2272ca354ba5a22a0c2d1f919039 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Wed, 28 Aug 2024 18:53:00 +0200 Subject: [PATCH] string: fixup isNumber not accepting -1.0 fixes #12 --- src/string/String.cpp | 6 +++++- tests/string.cpp | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/string/String.cpp b/src/string/String.cpp index 17d9b33..2ff2dac 100644 --- a/src/string/String.cpp +++ b/src/string/String.cpp @@ -26,6 +26,8 @@ bool Hyprutils::String::isNumber(const std::string& str, bool allowfloat) { if (str.empty()) return false; + bool decimalParsed = false; + for (size_t i = 0; i < str.length(); ++i) { const char& c = str.at(i); @@ -44,9 +46,11 @@ bool Hyprutils::String::isNumber(const std::string& str, bool allowfloat) { if (i == 0) return false; - if (str.at(0) == '-') + if (decimalParsed) return false; + decimalParsed = true; + continue; } } diff --git a/tests/string.cpp b/tests/string.cpp index 67060ed..51cb3ad 100644 --- a/tests/string.cpp +++ b/tests/string.cpp @@ -30,6 +30,9 @@ int main(int argc, char** argv, char** envp) { EXPECT(isNumber("vvss", true), false); EXPECT(isNumber("0.9999s", true), false); EXPECT(isNumber("s0.9999", true), false); + EXPECT(isNumber("-1.0", true), true); + EXPECT(isNumber("-1..0", true), false); + EXPECT(isNumber("-10.0000000001", true), true); CVarList list("hello world!", 0, 's', true); EXPECT(list[0], "hello");