LibCrypto: Add {Signed,Unsigned}BigInteger::from_base{2, 8, 16} helpers
These can be used to create BigInteger instances from non-decimal number strings.
This commit is contained in:
parent
e4e6e03364
commit
2ad2e055e2
Notes:
sideshowbarker
2024-07-18 12:16:53 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/2ad2e055e28 Pull-request: https://github.com/SerenityOS/serenity/pull/8045 Reviewed-by: https://github.com/linusg
4 changed files with 69 additions and 2 deletions
|
@ -27,7 +27,7 @@ size_t SignedBigInteger::export_data(Bytes data, bool remove_leading_zeros) cons
|
|||
return m_unsigned_data.export_data(bytes_view, remove_leading_zeros) + 1;
|
||||
}
|
||||
|
||||
SignedBigInteger SignedBigInteger::from_base10(StringView str)
|
||||
static bool parse_sign(StringView& str)
|
||||
{
|
||||
bool sign = false;
|
||||
if (str.length() > 1) {
|
||||
|
@ -39,10 +39,37 @@ SignedBigInteger SignedBigInteger::from_base10(StringView str)
|
|||
if (maybe_sign == '+')
|
||||
str = str.substring_view(1, str.length() - 1);
|
||||
}
|
||||
return sign;
|
||||
}
|
||||
|
||||
SignedBigInteger SignedBigInteger::from_base10(StringView str)
|
||||
{
|
||||
auto sign = parse_sign(str);
|
||||
auto unsigned_data = UnsignedBigInteger::from_base10(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
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
|
|
@ -62,8 +62,11 @@ public:
|
|||
|
||||
size_t export_data(Bytes, bool remove_leading_zeros = false) const;
|
||||
|
||||
static SignedBigInteger from_base2(StringView str);
|
||||
static SignedBigInteger from_base8(StringView str);
|
||||
static SignedBigInteger from_base10(StringView str);
|
||||
String to_base10() const;
|
||||
static SignedBigInteger from_base16(StringView str);
|
||||
|
||||
u64 to_u64() const;
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include "UnsignedBigInteger.h"
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/StringHash.h>
|
||||
#include <LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h>
|
||||
|
@ -71,7 +72,40 @@ UnsignedBigInteger UnsignedBigInteger::from_base10(const String& str)
|
|||
UnsignedBigInteger ten { 10 };
|
||||
|
||||
for (auto& c : str) {
|
||||
result = result.multiplied_by(ten).plus(c - '0');
|
||||
result = result.multiplied_by(ten).plus(parse_ascii_digit(c));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
UnsignedBigInteger UnsignedBigInteger::from_base2(const String& str)
|
||||
{
|
||||
UnsignedBigInteger result;
|
||||
UnsignedBigInteger two { 2 };
|
||||
|
||||
for (auto& c : str) {
|
||||
result = result.multiplied_by(two).plus(parse_ascii_digit(c));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
UnsignedBigInteger UnsignedBigInteger::from_base8(const String& str)
|
||||
{
|
||||
UnsignedBigInteger result;
|
||||
UnsignedBigInteger eight { 8 };
|
||||
|
||||
for (auto& c : str) {
|
||||
result = result.multiplied_by(eight).plus(parse_ascii_digit(c));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
UnsignedBigInteger UnsignedBigInteger::from_base16(const String& str)
|
||||
{
|
||||
UnsignedBigInteger result;
|
||||
UnsignedBigInteger sixteen { 16 };
|
||||
|
||||
for (auto& c : str) {
|
||||
result = result.multiplied_by(sixteen).plus(parse_ascii_hex_digit(c));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -53,8 +53,11 @@ public:
|
|||
|
||||
size_t export_data(Bytes, bool remove_leading_zeros = false) const;
|
||||
|
||||
static UnsignedBigInteger from_base2(const String& str);
|
||||
static UnsignedBigInteger from_base8(const String& str);
|
||||
static UnsignedBigInteger from_base10(const String& str);
|
||||
String to_base10() const;
|
||||
static UnsignedBigInteger from_base16(const String& str);
|
||||
|
||||
u64 to_u64() const;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue