string: fixup isNumber not accepting -1.0

fixes #12
This commit is contained in:
Vaxry 2024-08-28 18:53:00 +02:00
parent 0252fd13e7
commit aadf9a27dd
2 changed files with 8 additions and 1 deletions

View file

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

View file

@ -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");