1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:34:57 +00:00

AK: Add to_{double, float} convenience functions to all string types

These are guarded with #ifndef KERNEL, since doubles (and floats) are
not allowed in KERNEL mode.
In StringUtils there is convert_to_floating_point which does have a
template parameter incase you have a templated type.
This commit is contained in:
davidot 2022-10-11 00:48:45 +02:00 committed by Linus Groh
parent 2334cd85a2
commit 6fd8e96d53
8 changed files with 68 additions and 0 deletions

View file

@ -15,6 +15,7 @@
#include <AK/Vector.h>
#ifndef KERNEL
# include <AK/FloatingPointStringConversions.h>
# include <AK/String.h>
#endif
@ -232,6 +233,23 @@ template Optional<u16> convert_to_uint_from_octal(StringView str, TrimWhitespace
template Optional<u32> convert_to_uint_from_octal(StringView str, TrimWhitespace);
template Optional<u64> convert_to_uint_from_octal(StringView str, TrimWhitespace);
#ifndef KERNEL
template<typename T>
Optional<T> convert_to_floating_point(StringView str, TrimWhitespace trim_whitespace)
{
static_assert(IsSame<T, double> || IsSame<T, float>);
auto string = trim_whitespace == TrimWhitespace::Yes
? str.trim_whitespace()
: str;
char const* start = string.characters_without_null_termination();
return parse_floating_point_completely<T>(start, start + str.length());
}
template Optional<double> convert_to_floating_point(StringView str, TrimWhitespace);
template Optional<float> convert_to_floating_point(StringView str, TrimWhitespace);
#endif
bool equals_ignoring_case(StringView a, StringView b)
{
if (a.length() != b.length())