From ec37eadb394a2aef8ccc3326611ee0b2fb3ce8c7 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 17 Jan 2022 12:17:40 -0500 Subject: [PATCH] LibCrypto: Add Formatter Useful for seeing SignedBigInteger values in test failure messages. --- Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp | 7 +++++++ Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp index 59aaf1933d..0787f90eb1 100644 --- a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp +++ b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp @@ -308,3 +308,10 @@ bool SignedBigInteger::operator>=(const SignedBigInteger& other) const } } + +ErrorOr AK::Formatter::format(FormatBuilder& fmtbuilder, const Crypto::SignedBigInteger& value) +{ + if (value.is_negative()) + TRY(fmtbuilder.put_string("-")); + return Formatter::format(fmtbuilder, value.unsigned_value()); +} diff --git a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h index f85a4969d9..9bec718794 100644 --- a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h +++ b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h @@ -143,6 +143,11 @@ struct SignedDivisionResult { } +template<> +struct AK::Formatter : AK::Formatter { + ErrorOr format(FormatBuilder&, Crypto::SignedBigInteger const&); +}; + inline Crypto::SignedBigInteger operator""_sbigint(const char* string, size_t length) {