mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-02 20:42:11 +02:00
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:
committed by
Linus Groh
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
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user