Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating bigint with FromStr trait for issue 81 #94

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions num/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// If not, see <https://opensource.org/licenses/MIT>.

use crate::error::ParseLengthError;
use core::convert::TryInto;

macro_rules! construct_bigint {
($name:ident, $n_words:expr) => {
Expand Down Expand Up @@ -830,6 +831,20 @@ macro_rules! construct_bigint {
}
}

impl ::core::str::FromStr for $name {
type Err = ::core::num::ParseIntError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand exactly what the code below does, so test case will be appreciated.

My understanding is that for FromStr implementation we support only decimals (like standard library) and should have from_str_radix for other bases (which may be another PR, not necessary doing it here). Second, what you need is not a map over split iterator but a map over chunks iterator...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i'm seeing

let splitted = s.split("0x");
let inner_big_int: Vec<u64> = splitted
.map(|x| format!("0x{}", x))
.map(|x| x.parse::<u64>().unwrap())
.collect();
let inner_big_int_array = inner_big_int.try_into().unwrap();
Ok(Self(inner_big_int_array))
}
}

impl ::core::fmt::Display for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Debug::fmt(self, f)
Expand Down