1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 11:18:13 +00:00

LibCrypto: Replace from_base{2,8,10,16}() & to_base10 with from_base(N)

This allows us to support parsing and serializing BigIntegers to and
from any base N (such that 2 <= N <= 36).
This commit is contained in:
Idan Horowitz 2021-06-29 17:51:52 +03:00 committed by Linus Groh
parent a768131720
commit 005d75656e
14 changed files with 56 additions and 132 deletions

View file

@ -27,57 +27,30 @@ size_t SignedBigInteger::export_data(Bytes data, bool remove_leading_zeros) cons
return m_unsigned_data.export_data(bytes_view, remove_leading_zeros) + 1;
}
static bool parse_sign(StringView& str)
SignedBigInteger SignedBigInteger::from_base(u16 N, StringView str)
{
bool sign = false;
auto sign = false;
if (str.length() > 1) {
auto maybe_sign = str[0];
if (maybe_sign == '-') {
str = str.substring_view(1, str.length() - 1);
str = str.substring_view(1);
sign = true;
}
if (maybe_sign == '+')
str = str.substring_view(1, str.length() - 1);
str = str.substring_view(1);
}
return sign;
}
SignedBigInteger SignedBigInteger::from_base10(StringView str)
{
auto sign = parse_sign(str);
auto unsigned_data = UnsignedBigInteger::from_base10(str);
auto unsigned_data = UnsignedBigInteger::from_base(N, str);
return { move(unsigned_data), sign };
}
SignedBigInteger SignedBigInteger::from_base2(StringView str)
{
auto sign = parse_sign(str);
auto unsigned_data = UnsignedBigInteger::from_base2(str);
return { move(unsigned_data), sign };
}
SignedBigInteger SignedBigInteger::from_base8(StringView str)
{
auto sign = parse_sign(str);
auto unsigned_data = UnsignedBigInteger::from_base8(str);
return { move(unsigned_data), sign };
}
SignedBigInteger SignedBigInteger::from_base16(StringView str)
{
auto sign = parse_sign(str);
auto unsigned_data = UnsignedBigInteger::from_base16(str);
return { move(unsigned_data), sign };
}
String SignedBigInteger::to_base10() const
String SignedBigInteger::to_base(u16 N) const
{
StringBuilder builder;
if (m_sign)
builder.append('-');
builder.append(m_unsigned_data.to_base10());
builder.append(m_unsigned_data.to_base(N));
return builder.to_string();
}