1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::{
ibig::IBig,
radix::{self, Digit},
sign::Sign::*,
ubig::UBig,
};
use core::{
fmt::{self, Display, Formatter},
str::FromStr,
};
mod non_power_two;
mod power_two;
impl FromStr for UBig {
type Err = ParseError;
fn from_str(s: &str) -> Result<UBig, ParseError> {
UBig::from_str_radix(s, 10)
}
}
impl FromStr for IBig {
type Err = ParseError;
fn from_str(s: &str) -> Result<IBig, ParseError> {
IBig::from_str_radix(s, 10)
}
}
impl UBig {
pub fn from_str_radix(src: &str, radix: u32) -> Result<UBig, ParseError> {
radix::check_radix_valid(radix);
let src = src.strip_prefix("+").unwrap_or(src);
UBig::from_str_radix_no_sign(src, radix)
}
pub fn from_str_with_radix_prefix(src: &str) -> Result<UBig, ParseError> {
let src = src.strip_prefix("+").unwrap_or(src);
UBig::from_str_with_radix_prefix_no_sign(src)
}
fn from_str_with_radix_prefix_no_sign(src: &str) -> Result<UBig, ParseError> {
if let Some(bin) = src.strip_prefix("0b") {
UBig::from_str_radix_no_sign(bin, 2)
} else if let Some(oct) = src.strip_prefix("0o") {
UBig::from_str_radix_no_sign(oct, 8)
} else if let Some(hex) = src.strip_prefix("0x") {
UBig::from_str_radix_no_sign(hex, 16)
} else {
UBig::from_str_radix_no_sign(src, 10)
}
}
fn from_str_radix_no_sign(mut src: &str, radix: Digit) -> Result<UBig, ParseError> {
debug_assert!(radix::is_radix_valid(radix));
if src.is_empty() {
return Err(ParseError::NoDigits);
}
while let Some(src2) = src.strip_prefix("0") {
src = src2;
}
if radix.is_power_of_two() {
power_two::parse(src, radix)
} else {
non_power_two::parse(src, radix)
}
}
}
impl IBig {
pub fn from_str_radix(mut src: &str, radix: u32) -> Result<IBig, ParseError> {
radix::check_radix_valid(radix);
let sign;
match src.strip_prefix("-") {
Some(s) => {
sign = Negative;
src = s;
}
None => {
sign = Positive;
src = src.strip_prefix("+").unwrap_or(src);
}
}
let mag = UBig::from_str_radix_no_sign(src, radix)?;
Ok(IBig::from_sign_magnitude(sign, mag))
}
pub fn from_str_with_radix_prefix(mut src: &str) -> Result<IBig, ParseError> {
let sign;
match src.strip_prefix("-") {
Some(s) => {
sign = Negative;
src = s;
}
None => {
sign = Positive;
src = src.strip_prefix("+").unwrap_or(src);
}
}
let mag = UBig::from_str_with_radix_prefix_no_sign(src)?;
Ok(IBig::from_sign_magnitude(sign, mag))
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ParseError {
NoDigits,
InvalidDigit,
}
impl Display for ParseError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
ParseError::NoDigits => f.write_str("no digits"),
ParseError::InvalidDigit => f.write_str("invalid digit"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseError {}