From 4f199ee1c6cf3bda338e1c3e654a48d8da7389a0 Mon Sep 17 00:00:00 2001 From: PS Date: Thu, 18 Mar 2021 00:26:47 -0700 Subject: [PATCH] Fixed a problem with r64 parsing --- src/gs_libs/gs_types.cpp | 10 +++++++--- src/tests/sanity_tests.cpp | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/gs_libs/gs_types.cpp b/src/gs_libs/gs_types.cpp index cbd0678..3f444fa 100644 --- a/src/gs_libs/gs_types.cpp +++ b/src/gs_libs/gs_types.cpp @@ -1885,12 +1885,16 @@ ValidateAndParseFloat(gs_const_string String) if (StringIsValid) { - u64 DecimalIndex = FindFirst(String, '.'); + s64 DecimalIndex = FindFirst(String, '.'); u64 TempParsedLength = 0; u64 PlacesAfterPoint = 0; gs_const_string IntegerString = GetStringBefore(String, DecimalIndex); - gs_const_string DecimalString = GetStringAfter(String, DecimalIndex + 1); + gs_const_string DecimalString = {}; + if (DecimalIndex >= 0) + { + DecimalString = GetStringAfter(String, DecimalIndex + 1); + } r32 Polarity = 1; if (IntegerString.Str[0] == '-') @@ -1910,7 +1914,7 @@ ValidateAndParseFloat(gs_const_string String) } Result.ParsedLength = TempParsedLength + PlacesAfterPoint; - if (DecimalIndex < String.Length) { Result.ParsedLength += 1; } + if (DecimalIndex < (s64)String.Length) { Result.ParsedLength += 1; } Result.Success = true; } diff --git a/src/tests/sanity_tests.cpp b/src/tests/sanity_tests.cpp index 6130f5d..3d37c1a 100644 --- a/src/tests/sanity_tests.cpp +++ b/src/tests/sanity_tests.cpp @@ -76,14 +76,16 @@ int main (int ArgCount, char** Args) TestResult(ParseUInt(ConstString("532")) == 532); TestResult(ParseInt(ConstString("-1234567890")) == -1234567890); TestResult(ParseFloat(ConstString("-12345.6789")) == -12345.6789); + TestResult(ParseFloat(ConstString("-1")) == -1); + TestResult(ParseFloat(ConstString("-.035")) == -.035); TestString.Length = 0; U64ToASCII(&TestString, 53298, 10); TestResult(StringTest(TestString.ConstString, ConstString("53298"))); TestString.Length = 0; - R64ToASCII(&TestString, 145732.321, 2); - TestResult(StringTest(TestString.ConstString, ConstString("145732.32"))); + R64ToASCII(&TestString, -145732.321, 2); + TestResult(StringTest(TestString.ConstString, ConstString("-145732.32"))); } Test("gs_path.h")