Skip to content

Commit 8af1ae2

Browse files
committed
Tons of various cleanups and clippy fixes
1 parent b1f82ee commit 8af1ae2

File tree

8 files changed

+97
-83
lines changed

8 files changed

+97
-83
lines changed

src/binary.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::common::AdjustedMantissa;
22
use crate::float::Float;
33

44
#[inline]
5-
pub fn compute_float_from_exp_mantissa<F: Float>(q: i64, mut w: u64) -> AdjustedMantissa {
5+
pub fn compute_float<F: Float>(q: i64, mut w: u64) -> AdjustedMantissa {
66
let am_zero = AdjustedMantissa::zero_pow2(0);
77
let am_inf = AdjustedMantissa::zero_pow2(F::INFINITE_POWER);
88
let am_error = AdjustedMantissa::zero_pow2(-1);
@@ -15,7 +15,7 @@ pub fn compute_float_from_exp_mantissa<F: Float>(q: i64, mut w: u64) -> Adjusted
1515
let lz = w.leading_zeros();
1616
w <<= lz;
1717
let (lo, hi) = compute_product_approx(q, w, F::MANTISSA_EXPLICIT_BITS + 3);
18-
if lo == 0xFFFFFFFFFFFFFFFF {
18+
if lo == 0xFFFF_FFFF_FFFF_FFFF {
1919
let inside_safe_exponent = (q >= -27) && (q <= 55);
2020
if !inside_safe_exponent {
2121
return am_error;
@@ -31,25 +31,24 @@ pub fn compute_float_from_exp_mantissa<F: Float>(q: i64, mut w: u64) -> Adjusted
3131
mantissa >>= -power2 + 1;
3232
mantissa += mantissa & 1;
3333
mantissa >>= 1;
34-
power2 = (mantissa >= (1u64 << F::MANTISSA_EXPLICIT_BITS)) as i32;
34+
power2 = (mantissa >= (1_u64 << F::MANTISSA_EXPLICIT_BITS)) as i32;
3535
return AdjustedMantissa { mantissa, power2 };
3636
}
3737
if lo <= 1
3838
&& q >= F::MIN_EXPONENT_ROUND_TO_EVEN as i64
3939
&& q <= F::MAX_EXPONENT_ROUND_TO_EVEN as i64
4040
&& mantissa & 3 == 1
41+
&& (mantissa << (upperbit + 64 - F::MANTISSA_EXPLICIT_BITS as i32 - 3)) == hi
4142
{
42-
if (mantissa << (upperbit + 64 - F::MANTISSA_EXPLICIT_BITS as i32 - 3)) == hi {
43-
mantissa &= !1u64;
44-
}
43+
mantissa &= !1_u64;
4544
}
4645
mantissa += mantissa & 1;
4746
mantissa >>= 1;
48-
if mantissa >= (2u64 << F::MANTISSA_EXPLICIT_BITS) {
49-
mantissa = 1u64 << F::MANTISSA_EXPLICIT_BITS;
47+
if mantissa >= (2_u64 << F::MANTISSA_EXPLICIT_BITS) {
48+
mantissa = 1_u64 << F::MANTISSA_EXPLICIT_BITS;
5049
power2 += 1;
5150
}
52-
mantissa &= !(1u64 << F::MANTISSA_EXPLICIT_BITS);
51+
mantissa &= !(1_u64 << F::MANTISSA_EXPLICIT_BITS);
5352
if power2 >= F::INFINITE_POWER {
5453
return am_inf;
5554
}
@@ -58,7 +57,7 @@ pub fn compute_float_from_exp_mantissa<F: Float>(q: i64, mut w: u64) -> Adjusted
5857

5958
#[inline]
6059
fn power(q: i32) -> i32 {
61-
(q.wrapping_mul(152170 + 65536) >> 16) + 63
60+
(q.wrapping_mul(152_170 + 65536) >> 16) + 63
6261
}
6362

6463
#[inline]
@@ -77,9 +76,9 @@ fn compute_product_approx(q: i64, w: u64, precision: usize) -> (u64, u64) {
7776
debug_assert!(precision <= 64);
7877

7978
let mask = if precision < 64 {
80-
0xFFFFFFFFFFFFFFFFu64 >> precision
79+
0xFFFF_FFFF_FFFF_FFFF_u64 >> precision
8180
} else {
82-
0xFFFFFFFFFFFFFFFFu64
81+
0xFFFF_FFFF_FFFF_FFFF_u64
8382
};
8483
let index = (q - SMALLEST_POWER_OF_FIVE as i64) as usize;
8584
let (lo5, hi5) = unsafe { *POWER_OF_FIVE_128.get_unchecked(index) };
@@ -98,6 +97,7 @@ const SMALLEST_POWER_OF_FIVE: i32 = -342;
9897
const LARGEST_POWER_OF_FIVE: i32 = 308;
9998
const N_POWERS_OF_FIVE: usize = (LARGEST_POWER_OF_FIVE - SMALLEST_POWER_OF_FIVE + 1) as usize;
10099

100+
#[allow(clippy::unreadable_literal)]
101101
const POWER_OF_FIVE_128: [(u64, u64); N_POWERS_OF_FIVE] = [
102102
(0xeef453d6923bd65a, 0x113faa2906a13b3f),
103103
(0x9558b4661b6565f8, 0x4ac7ca59a424c507),

src/common.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl<'a> AsciiStr<'a> {
1414
Self {
1515
ptr: s.as_ptr(),
1616
end: unsafe { s.as_ptr().add(s.len()) },
17-
_marker: Default::default(),
17+
_marker: PhantomData::default(),
1818
}
1919
}
2020

@@ -90,7 +90,7 @@ impl<'a> AsciiStr<'a> {
9090
#[inline]
9191
pub fn read_u64(&self) -> u64 {
9292
debug_assert!(self.check_len(8));
93-
let mut value = 0u64;
93+
let mut value = 0_u64;
9494
let src = self.ptr;
9595
let dst = &mut value as *mut _ as *mut u8;
9696
unsafe { ptr::copy_nonoverlapping(src, dst, 8) };
@@ -159,7 +159,7 @@ pub trait ByteSlice: AsRef<[u8]> + AsMut<[u8]> {
159159
#[inline]
160160
fn read_u64(&self) -> u64 {
161161
debug_assert!(self.as_ref().len() >= 8);
162-
let mut value = 0u64;
162+
let mut value = 0_u64;
163163
let src = self.as_ref().as_ptr();
164164
let dst = &mut value as *mut _ as *mut u8;
165165
unsafe { ptr::copy_nonoverlapping(src, dst, 8) };
@@ -179,9 +179,9 @@ impl ByteSlice for [u8] {}
179179

180180
#[inline]
181181
pub fn is_8digits_le(v: u64) -> bool {
182-
let a = v.wrapping_add(0x4646464646464646);
183-
let b = v.wrapping_sub(0x3030303030303030);
184-
(a | b) & 0x8080808080808080 == 0
182+
let a = v.wrapping_add(0x4646_4646_4646_4646);
183+
let b = v.wrapping_sub(0x3030_3030_3030_3030);
184+
(a | b) & 0x8080_8080_8080_8080 == 0
185185
}
186186

187187
#[inline]

src/decimal.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct Decimal {
1111

1212
impl Default for Decimal {
1313
fn default() -> Self {
14-
Decimal {
14+
Self {
1515
num_digits: 0,
1616
decimal_point: 0,
1717
negative: false,
@@ -49,9 +49,9 @@ impl Decimal {
4949
return u64::MAX;
5050
}
5151
let dp = self.decimal_point as usize;
52-
let mut n = 0u64;
52+
let mut n = 0_u64;
5353
for i in 0..dp {
54-
n = 10 * n;
54+
n *= 10;
5555
if i < self.num_digits {
5656
n += self.digits[i] as u64;
5757
}
@@ -77,7 +77,7 @@ impl Decimal {
7777
let num_new_digits = number_of_digits_decimal_left_shift(self, shift);
7878
let mut read_index = self.num_digits;
7979
let mut write_index = self.num_digits + num_new_digits;
80-
let mut n = 0u64;
80+
let mut n = 0_u64;
8181
while read_index != 0 {
8282
read_index -= 1;
8383
write_index -= 1;
@@ -114,7 +114,7 @@ impl Decimal {
114114
pub fn right_shift(&mut self, shift: usize) {
115115
let mut read_index = 0;
116116
let mut write_index = 0;
117-
let mut n = 0u64;
117+
let mut n = 0_u64;
118118
while (n >> shift) == 0 {
119119
if read_index < self.num_digits {
120120
n = (10 * n) + self.digits[read_index] as u64;
@@ -123,7 +123,7 @@ impl Decimal {
123123
return;
124124
} else {
125125
while (n >> shift) == 0 {
126-
n = 10 * n;
126+
n *= 10;
127127
read_index += 1;
128128
}
129129
break;
@@ -137,7 +137,7 @@ impl Decimal {
137137
self.truncated = false;
138138
return;
139139
}
140-
let mask = (1u64 << shift) - 1;
140+
let mask = (1_u64 << shift) - 1;
141141
while read_index < self.num_digits {
142142
let new_digit = (n >> shift) as u8;
143143
n = (10 * (n & mask)) + self.digits[read_index] as u64;
@@ -183,7 +183,7 @@ pub fn parse_decimal(mut s: &[u8]) -> Decimal {
183183
if !is_8digits_le(v) {
184184
break;
185185
}
186-
d.digits[d.num_digits..].write_u64(v - 0x3030303030303030);
186+
d.digits[d.num_digits..].write_u64(v - 0x3030_3030_3030_3030);
187187
d.num_digits += 8;
188188
s = s.advance(8);
189189
}
@@ -200,7 +200,7 @@ pub fn parse_decimal(mut s: &[u8]) -> Decimal {
200200
} else if s.check_first(b'+') {
201201
s = s.advance(1);
202202
}
203-
let mut exp_num = 0i32;
203+
let mut exp_num = 0_i32;
204204
parse_digits(&mut s, |digit| {
205205
if exp_num < 0x10000 {
206206
exp_num = 10 * exp_num + digit as i32;
@@ -283,12 +283,12 @@ fn number_of_digits_decimal_left_shift(d: &Decimal, mut shift: usize) -> usize {
283283
let pow5_a = (0x7FF & x_a) as usize;
284284
let pow5_b = (0x7FF & x_b) as usize;
285285
let pow5 = &TABLE_POW5[pow5_a..];
286-
for i in 0..(pow5_b - pow5_a) {
286+
for (i, &p5) in pow5.iter().enumerate().take(pow5_b - pow5_a) {
287287
if i >= d.num_digits {
288288
return num_new_digits - 1;
289-
} else if d.digits[i] == pow5[i] {
289+
} else if d.digits[i] == p5 {
290290
continue;
291-
} else if d.digits[i] < pow5[i] {
291+
} else if d.digits[i] < p5 {
292292
return num_new_digits - 1;
293293
} else {
294294
return num_new_digits;

src/float.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub trait Float:
3737
const SMALLEST_POWER_OF_TEN: i32;
3838
const LARGEST_POWER_OF_TEN: i32;
3939

40-
const MAX_MANTISSA_FAST_PATH: u64 = 2u64 << Self::MANTISSA_EXPLICIT_BITS;
40+
const MAX_MANTISSA_FAST_PATH: u64 = 2_u64 << Self::MANTISSA_EXPLICIT_BITS;
4141

4242
fn from_u64(v: u64) -> Self;
4343
fn pow10_fast_path(exponent: usize) -> Self;
@@ -46,10 +46,10 @@ pub trait Float:
4646
impl private::Sealed for f32 {}
4747

4848
impl Float for f32 {
49-
const INFINITY: Self = f32::INFINITY;
50-
const NEG_INFINITY: Self = f32::NEG_INFINITY;
51-
const NAN: Self = f32::NAN;
52-
const NEG_NAN: Self = -f32::NAN;
49+
const INFINITY: Self = Self::INFINITY;
50+
const NEG_INFINITY: Self = Self::NEG_INFINITY;
51+
const NAN: Self = Self::NAN;
52+
const NEG_NAN: Self = -Self::NAN;
5353

5454
const MANTISSA_EXPLICIT_BITS: usize = 23;
5555
const MIN_EXPONENT_ROUND_TO_EVEN: i32 = -17;
@@ -68,6 +68,7 @@ impl Float for f32 {
6868
}
6969

7070
#[inline]
71+
#[allow(clippy::use_self)]
7172
fn pow10_fast_path(exponent: usize) -> Self {
7273
const TABLE: [f32; 11] = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10];
7374
unsafe { *TABLE.get_unchecked(exponent) }
@@ -77,10 +78,10 @@ impl Float for f32 {
7778
impl private::Sealed for f64 {}
7879

7980
impl Float for f64 {
80-
const INFINITY: Self = f64::INFINITY;
81-
const NEG_INFINITY: Self = f64::NEG_INFINITY;
82-
const NAN: Self = f64::NAN;
83-
const NEG_NAN: Self = -f64::NAN;
81+
const INFINITY: Self = Self::INFINITY;
82+
const NEG_INFINITY: Self = Self::NEG_INFINITY;
83+
const NAN: Self = Self::NAN;
84+
const NEG_NAN: Self = -Self::NAN;
8485

8586
const MANTISSA_EXPLICIT_BITS: usize = 52;
8687
const MIN_EXPONENT_ROUND_TO_EVEN: i32 = -4;
@@ -100,6 +101,7 @@ impl Float for f64 {
100101

101102
#[inline]
102103
fn pow10_fast_path(exponent: usize) -> Self {
104+
#[allow(clippy::use_self)]
103105
const TABLE: [f64; 23] = [
104106
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
105107
1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22,

src/lib.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ pub type Result<T> = std::result::Result<T, Error>;
4141
pub trait FastFloat: float::Float {
4242
/// Parse a float number from string (full).
4343
///
44-
/// This method parses the entire string, returning an error either if the string doesn't
45-
/// start with a valid float number, or if any characters are left remaining unparsed.
46-
/// Scientific notation is enabled.
44+
/// This method parses the float number from the entire string.
45+
///
46+
/// # Errors
47+
///
48+
/// Will return an error either if the string doesn't start with a valid float number,
49+
/// or if any characterse are left remaining unparsed.
4750
#[inline]
4851
fn parse_float<S: AsRef<[u8]>>(s: S) -> Result<Self> {
4952
let s = s.as_ref();
@@ -55,9 +58,14 @@ pub trait FastFloat: float::Float {
5558

5659
/// Parse a float number from string (partial).
5760
///
58-
/// This method parses the string greedily while it can and in case of success returns
59-
/// the parsed number along with the number of characters consumed. Returns an error if
60-
/// the string doesn't start with a valid float number. Scientific notation is enabled.
61+
/// This method parses as many digits possible and returns the resulting number along
62+
/// with the number of digits processed (in case of success, this number is always
63+
/// positive).
64+
///
65+
/// # Errors
66+
///
67+
/// Will return an error either if the string doesn't start with a valid float number
68+
/// – that is, if no zero digits were processed.
6169
#[inline]
6270
fn parse_float_partial<S: AsRef<[u8]>>(s: S) -> Result<(Self, usize)> {
6371
parse::parse_float(s.as_ref()).ok_or(Error)
@@ -69,19 +77,27 @@ impl FastFloat for f64 {}
6977

7078
/// Parse a float number from string (full).
7179
///
72-
/// This method parses the entire string, returning an error either if the string doesn't
73-
/// start with a valid float number, or if any characters are left remaining unparsed.
74-
/// Scientific notation is enabled.
80+
/// This function parses the float number from the entire string.
81+
///
82+
/// # Errors
83+
///
84+
/// Will return an error either if the string doesn't start with a valid float number,
85+
/// or if any characterse are left remaining unparsed.
7586
#[inline]
7687
pub fn parse<T: FastFloat, S: AsRef<[u8]>>(s: S) -> Result<T> {
7788
T::parse_float(s)
7889
}
7990

8091
/// Parse a float number from string (partial).
8192
///
82-
/// This method parses the string greedily while it can and in case of success returns
83-
/// the parsed number along with the number of characters consumed. Returns an error if
84-
/// the string doesn't start with a valid float number. Scientific notation is enabled.
93+
/// This function parses as many digits possible and returns the resulting number along
94+
/// with the number of digits processed (in case of success, this number is always
95+
/// positive).
96+
///
97+
/// # Errors
98+
///
99+
/// Will return an error either if the string doesn't start with a valid float number
100+
/// – that is, if no zero digits were processed.
85101
#[inline]
86102
pub fn parse_partial<T: FastFloat, S: AsRef<[u8]>>(s: S) -> Result<(T, usize)> {
87103
T::parse_float_partial(s)

0 commit comments

Comments
 (0)