Skip to content

Commit

Permalink
string: fixup isNumber not accepting -1.0
Browse files Browse the repository at this point in the history
fixes #12
  • Loading branch information
vaxerski committed Aug 28, 2024
1 parent 0252fd1 commit aadf9a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/string/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit aadf9a2

Please sign in to comment.