From 54e14704679e9589822f28f64ece9e67613df3d8 Mon Sep 17 00:00:00 2001 From: hanaa12G Date: Sat, 14 Oct 2023 15:42:59 +0700 Subject: [PATCH] AK: Pass correct length to `StringUtils::convert_to_floating_point()` Fixed the issue in StringUtils::convert_to_floating_point() where the end pointer of the trimmed string was not being passed, causing the function to consistently return 'None' when given strings with trailing whitespaces. --- AK/StringUtils.cpp | 2 +- Tests/AK/TestStringUtils.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index 7bac50a8a0..ab087a4358 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -247,7 +247,7 @@ Optional convert_to_floating_point(StringView str, TrimWhitespace trim_whites : str; char const* start = string.characters_without_null_termination(); - return parse_floating_point_completely(start, start + str.length()); + return parse_floating_point_completely(start, start + string.length()); } template Optional convert_to_floating_point(StringView str, TrimWhitespace); diff --git a/Tests/AK/TestStringUtils.cpp b/Tests/AK/TestStringUtils.cpp index 90090acf10..d4b0765378 100644 --- a/Tests/AK/TestStringUtils.cpp +++ b/Tests/AK/TestStringUtils.cpp @@ -319,6 +319,13 @@ TEST_CASE(convert_to_uint_from_octal) EXPECT_EQ(actual.value(), 0177777u); } +TEST_CASE(convert_to_floating_point) +{ + auto number_string = " 123.45 "sv; + auto maybe_number = AK::StringUtils::convert_to_floating_point(number_string, TrimWhitespace::Yes); + EXPECT_APPROXIMATE(maybe_number.value(), 123.45f); +} + TEST_CASE(ends_with) { DeprecatedString test_string = "ABCDEF";