mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:47:34 +00:00
LibCrypto: Make constructing a BigInteger from string fallible
Previously, constructing a `UnsignedBigInteger::from_base()` could produce an incorrect result if the input string contained a valid Base36 digit that was out of range of the given base. The same method would also crash if the input string contained an invalid Base36 digit. An error is now returned in both these cases. Constructing a BigFraction from string is now also fallible, so that we can handle the case where we are given an input string with invalid digits.
This commit is contained in:
parent
0b0c7693e2
commit
48a3a02238
11 changed files with 68 additions and 57 deletions
|
@ -25,20 +25,18 @@ BigFraction::BigFraction(SignedBigInteger value)
|
|||
{
|
||||
}
|
||||
|
||||
BigFraction::BigFraction(StringView sv)
|
||||
ErrorOr<BigFraction> BigFraction::from_string(StringView sv)
|
||||
{
|
||||
// FIXME: This constructor is definitely fallible, errors should also be propagated
|
||||
// from both signed and unsigned version of from_base.
|
||||
auto maybe_dot_index = sv.find('.');
|
||||
|
||||
auto integer_part_view = sv.substring_view(0, maybe_dot_index.value_or(sv.length()));
|
||||
auto fraction_part_view = maybe_dot_index.has_value() ? sv.substring_view(1 + *maybe_dot_index) : "0"sv;
|
||||
|
||||
auto integer_part = SignedBigInteger::from_base(10, integer_part_view);
|
||||
auto fractional_part = SignedBigInteger::from_base(10, fraction_part_view);
|
||||
auto integer_part = TRY(SignedBigInteger::from_base(10, integer_part_view));
|
||||
auto fractional_part = TRY(SignedBigInteger::from_base(10, fraction_part_view));
|
||||
auto fraction_length = UnsignedBigInteger(static_cast<u64>(fraction_part_view.length()));
|
||||
|
||||
*this = BigFraction(move(integer_part)) + BigFraction(move(fractional_part), NumberTheory::Power("10"_bigint, move(fraction_length)));
|
||||
return BigFraction(move(integer_part)) + BigFraction(move(fractional_part), NumberTheory::Power("10"_bigint, move(fraction_length)));
|
||||
}
|
||||
|
||||
BigFraction BigFraction::operator+(BigFraction const& rhs) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue