mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
In the benchmark added here, fmt's dragonbox is ~3x faster than our own Ryu implementation (1197ms for dragonbox vs. 3435ms for Ryu). Daniel Lemire recently published an article about these algorithms: https://lemire.me/blog/2026/02/01/converting-floats-to-strings-quickly/ In this article, fmt's dragonbox implementation is actually one of the slower ones (with the caveat that some comments note that the article is a bit out-of-date). I've gone with fmt here because: 1. It has a readily available recent version on vcpkg. 2. It provides the methods we need to actually convert a floating point to decimal exponential form. 3. There is an ongoing effort to replace dragonbox with a new algorithm, zmij, which promises to be faster. 4. It is one of the only users of AK/UFixedBigInt, so we can potentially remove that as well soon. 5. Bringing in fmt opens the door to replacing a bunch of AK::format facilities with fmt as well.
52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
/*
|
|
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
|
* Copyright (c) 2025-2026, Tim Flynn <trflynn89@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Concepts.h>
|
|
#include <AK/Optional.h>
|
|
#include <AK/StringUtils.h>
|
|
|
|
namespace AK {
|
|
|
|
template<typename T>
|
|
struct ParseFirstNumberResult {
|
|
T value { 0 };
|
|
size_t characters_parsed { 0 };
|
|
};
|
|
|
|
template<Arithmetic T>
|
|
Optional<ParseFirstNumberResult<T>> parse_first_number(StringView, TrimWhitespace = TrimWhitespace::Yes, int base = 10);
|
|
|
|
template<Arithmetic T>
|
|
Optional<ParseFirstNumberResult<T>> parse_first_number(Utf16View const&, TrimWhitespace = TrimWhitespace::Yes, int base = 10);
|
|
|
|
template<Arithmetic T>
|
|
Optional<T> parse_number(StringView, TrimWhitespace = TrimWhitespace::Yes, int base = 10);
|
|
|
|
template<Arithmetic T>
|
|
Optional<T> parse_number(Utf16View const&, TrimWhitespace = TrimWhitespace::Yes, int base = 10);
|
|
|
|
template<Integral T>
|
|
Optional<T> parse_hexadecimal_number(StringView, TrimWhitespace = TrimWhitespace::Yes);
|
|
|
|
template<Integral T>
|
|
Optional<T> parse_hexadecimal_number(Utf16View const&, TrimWhitespace = TrimWhitespace::Yes);
|
|
|
|
struct DecimalExponentialForm {
|
|
constexpr bool operator==(DecimalExponentialForm const& other) const = default;
|
|
|
|
bool sign { false };
|
|
u64 fraction { 0 };
|
|
i32 exponent { 0 };
|
|
};
|
|
|
|
template<FloatingPoint T>
|
|
DecimalExponentialForm convert_to_decimal_exponential_form(T value);
|
|
|
|
}
|