mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-26 17:55:07 +02:00
LibCrypto: Define *BigInteger::to_base to convert big integers to String
This commit is contained in:
committed by
Linus Groh
parent
0ddc2e1f50
commit
3ad1f250e7
Notes:
sideshowbarker
2024-07-17 01:41:35 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/3ad1f250e7 Pull-request: https://github.com/SerenityOS/serenity/pull/17014 Reviewed-by: https://github.com/linusg ✅
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user