| { | ||
| "git": { | ||
| "sha1": "e16ba527854ca0edc36695e4e65fdcbc4dccd188" | ||
| "sha1": "8b080b3753b91c58113c47c99108185d0614499c" | ||
| }, | ||
| "path_in_vcs": "utils/writeable" | ||
| } |
+1
-1
@@ -639,3 +639,3 @@ # This file is automatically @generated by Cargo. | ||
| name = "writeable" | ||
| version = "0.5.3" | ||
| version = "0.5.4" | ||
| dependencies = [ | ||
@@ -642,0 +642,0 @@ "criterion", |
+2
-2
@@ -14,5 +14,5 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| edition = "2021" | ||
| rust-version = "1.66" | ||
| rust-version = "1.67" | ||
| name = "writeable" | ||
| version = "0.5.3" | ||
| version = "0.5.4" | ||
| authors = ["The ICU4X Project Developers"] | ||
@@ -19,0 +19,0 @@ include = [ |
+11
-53
@@ -10,8 +10,9 @@ // This file is part of ICU4X. For terms of use, please see the file | ||
| macro_rules! impl_write_num { | ||
| ($u:ty, $i:ty, $test:ident, $max_ilog_10:expr) => { | ||
| ($u:ty, $i:ty, $test:ident) => { | ||
| impl $crate::Writeable for $u { | ||
| fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result { | ||
| let mut buf = [b'0'; $max_ilog_10 + 1]; | ||
| const MAX_LEN: usize = <$u>::MAX.ilog10() as usize + 1; | ||
| let mut buf = [b'0'; MAX_LEN]; | ||
| let mut n = *self; | ||
| let mut i = $max_ilog_10 + 1; | ||
| let mut i = MAX_LEN; | ||
| #[allow(clippy::indexing_slicing)] // n < 10^i | ||
@@ -23,3 +24,3 @@ while n != 0 { | ||
| } | ||
| if i == buf.len() { | ||
| if i == MAX_LEN { | ||
| debug_assert_eq!(*self, 0); | ||
@@ -34,3 +35,2 @@ i -= 1; | ||
| fn writeable_length_hint(&self) -> $crate::LengthHint { | ||
| #[allow(unstable_name_collisions)] // that's the idea | ||
| LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1) | ||
@@ -40,33 +40,2 @@ } | ||
| impl ILog10Ext for $u { | ||
| fn checked_ilog10(self) -> Option<u32> { | ||
| if self == 0 { | ||
| return None; | ||
| } | ||
| let b = (<$u>::BITS - 1) - self.leading_zeros(); | ||
| // self ∈ [2ᵇ, 2ᵇ⁺¹-1] => ⌊log₁₀(self)⌋ ∈ [⌊log₁₀(2ᵇ)⌋, ⌊log₁₀(2ᵇ⁺¹-1)⌋] | ||
| // <=> ⌊log₁₀(self)⌋ ∈ [⌊log₁₀(2ᵇ)⌋, ⌊log₁₀(2ᵇ⁺¹)⌋] | ||
| // <=> ⌊log₁₀(self)⌋ ∈ [⌊b log₁₀(2)⌋, ⌊(b+1) log₁₀(2)⌋] | ||
| // The second line holds because there is no integer in | ||
| // [log₁₀(2ᶜ-1), log₁₀(2ᶜ)], if there were, there'd be some 10ⁿ in | ||
| // [2ᶜ-1, 2ᶜ], but it can't be 2ᶜ-1 due to parity nor 2ᶜ due to prime | ||
| // factors. | ||
| const M: u32 = (core::f64::consts::LOG10_2 * (1 << 26) as f64) as u32; | ||
| let low = (b * M) >> 26; | ||
| let high = ((b + 1) * M) >> 26; | ||
| // If the bounds aren't tight (e.g. 87 ∈ [64, 127] ⟹ ⌊log₁₀(87)⌋ ∈ [1,2]), | ||
| // compare to 10ʰ (100). This shouldn't happen too often as there are more | ||
| // powers of 2 than 10 (it happens for 14% of u32s). | ||
| Some(if high == low { | ||
| low | ||
| } else if self < (10 as $u).pow(high) { | ||
| low | ||
| } else { | ||
| high | ||
| }) | ||
| } | ||
| } | ||
| impl $crate::Writeable for $i { | ||
@@ -121,20 +90,9 @@ fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result { | ||
| /// `checked_ilog10` is added as a method on integer types in 1.67. | ||
| /// This extension trait provides it for older compilers. | ||
| trait ILog10Ext: Sized { | ||
| fn checked_ilog10(self) -> Option<u32>; | ||
| } | ||
| impl_write_num!(u8, i8, test_u8); | ||
| impl_write_num!(u16, i16, test_u16); | ||
| impl_write_num!(u32, i32, test_u32); | ||
| impl_write_num!(u64, i64, test_u64); | ||
| impl_write_num!(u128, i128, test_u128); | ||
| impl_write_num!(usize, isize, test_usize); | ||
| impl_write_num!(u8, i8, test_u8, 2); | ||
| impl_write_num!(u16, i16, test_u16, 4); | ||
| impl_write_num!(u32, i32, test_u32, 9); | ||
| impl_write_num!(u64, i64, test_u64, 19); | ||
| impl_write_num!(u128, i128, test_u128, 38); | ||
| impl_write_num!( | ||
| usize, | ||
| isize, | ||
| test_usize, | ||
| if usize::MAX as u64 == u64::MAX { 19 } else { 9 } | ||
| ); | ||
| impl Writeable for str { | ||
@@ -141,0 +99,0 @@ #[inline] |
Sorry, the diff of this file is not supported yet