hanickadot / compile-time-regular-expressions

Compile Time Regular Expression in C++
https://twitter.com/hankadusikova
Apache License 2.0
3.22k stars 177 forks source link

to_number suporting float numbers #298

Closed XRay3D closed 8 months ago

XRay3D commented 8 months ago

std::from_chars for int is base defaulted as 10, you can skip it if nothing else is specified. For floating point numbers, different arguments are used. Using:

value.to_number<double>(std::chars_format::general); // from "123.456"
value.to_number<double>(std::chars_format::hex); // from hex
value.to_number(); // from "123"
value.to_number(16); // from hex

https://github.com/hanickadot/compile-time-regular-expressions/blob/78ba3a3ddc6a977b0a46ad566876a6844eedb16a/single-header/ctre.hpp#L3636-L3638 Possible implementation

template <typename R = int> 
constexpr CTRE_FORCE_INLINE auto to_number(auto... args) const noexcept -> R {
    return _captures.template select<0>().template to_number<R>(args...);
}

https://github.com/hanickadot/compile-time-regular-expressions/blob/78ba3a3ddc6a977b0a46ad566876a6844eedb16a/single-header/ctre.hpp#L3406-L3411 Possible implementation

template <typename R = int> 
constexpr CTRE_FORCE_INLINE auto to_number(auto... args) const noexcept -> R {
    R result{};
    const auto view = to_view();
    std::from_chars(view.data(), view.data() + view.size(), result, args...);
    return result;
}
hanickadot commented 8 months ago

Good idea! Implemented in 029f1f13646cf65ec09780013418cb8f1c5d3a59.

XRay3D commented 8 months ago

Thank you.