LibCrypto: Add user-defined literals to convert numbers to a BigInt

It is much more convenient to define constants with:

    1000_bigint

Than with:

    Crypto::UnsignedBigInteger { 1000 }
This commit is contained in:
Timothy Flynn 2024-11-17 13:28:41 -05:00 committed by Tim Flynn
parent e236f1d2ae
commit b94307583b
Notes: github-actions[bot] 2024-11-21 00:07:05 +00:00

View file

@ -172,8 +172,26 @@ struct AK::Formatter<Crypto::UnsignedBigInteger> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder&, Crypto::UnsignedBigInteger const&); ErrorOr<void> format(FormatBuilder&, Crypto::UnsignedBigInteger const&);
}; };
inline Crypto::UnsignedBigInteger inline Crypto::UnsignedBigInteger operator""_bigint(char const* string, size_t length)
operator""_bigint(char const* string, size_t length)
{ {
return MUST(Crypto::UnsignedBigInteger::from_base(10, { string, length })); return MUST(Crypto::UnsignedBigInteger::from_base(10, { string, length }));
} }
inline Crypto::UnsignedBigInteger operator""_bigint(unsigned long long value)
{
auto result = Crypto::UnsignedBigInteger { static_cast<u64>(value) };
VERIFY(!result.is_invalid());
return result;
}
inline Crypto::UnsignedBigInteger operator""_bigint(long double value)
{
VERIFY(value >= 0);
VERIFY(value < static_cast<long double>(NumericLimits<double>::max()));
auto result = Crypto::UnsignedBigInteger { static_cast<double>(value) };
VERIFY(!result.is_invalid());
return result;
}