1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:37:36 +00:00

LibCrypto: Define *BigInteger::to_base to convert big integers to String

This commit is contained in:
Timothy Flynn 2023-01-13 11:40:04 -05:00 committed by Linus Groh
parent 0ddc2e1f50
commit 3ad1f250e7
5 changed files with 28 additions and 12 deletions

View file

@ -146,11 +146,11 @@ UnsignedBigInteger UnsignedBigInteger::from_base(u16 N, StringView str)
return result;
}
DeprecatedString UnsignedBigInteger::to_base_deprecated(u16 N) const
ErrorOr<String> UnsignedBigInteger::to_base(u16 N) const
{
VERIFY(N <= 36);
if (*this == UnsignedBigInteger { 0 })
return "0";
return String::from_utf8("0"sv);
StringBuilder builder;
UnsignedBigInteger temp(*this);
@ -160,11 +160,16 @@ DeprecatedString UnsignedBigInteger::to_base_deprecated(u16 N) const
while (temp != UnsignedBigInteger { 0 }) {
UnsignedBigIntegerAlgorithms::divide_u16_without_allocation(temp, N, quotient, remainder);
VERIFY(remainder.words()[0] < N);
builder.append(to_ascii_base36_digit(remainder.words()[0]));
TRY(builder.try_append(to_ascii_base36_digit(remainder.words()[0])));
temp.set_to(quotient);
}
return builder.to_deprecated_string().reverse();
return TRY(builder.to_string()).reverse();
}
DeprecatedString UnsignedBigInteger::to_base_deprecated(u16 N) const
{
return MUST(to_base(N)).to_deprecated_string();
}
u64 UnsignedBigInteger::to_u64() const