aboutsummaryrefslogtreecommitdiff
path: root/vendor/serde_json/src/lexical/exponent.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/serde_json/src/lexical/exponent.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/serde_json/src/lexical/exponent.rs')
-rw-r--r--vendor/serde_json/src/lexical/exponent.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/vendor/serde_json/src/lexical/exponent.rs b/vendor/serde_json/src/lexical/exponent.rs
new file mode 100644
index 0000000..6fc5197
--- /dev/null
+++ b/vendor/serde_json/src/lexical/exponent.rs
@@ -0,0 +1,50 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Utilities to calculate exponents.
+
+/// Convert usize into i32 without overflow.
+///
+/// This is needed to ensure when adjusting the exponent relative to
+/// the mantissa we do not overflow for comically-long exponents.
+#[inline]
+fn into_i32(value: usize) -> i32 {
+ if value > i32::max_value() as usize {
+ i32::max_value()
+ } else {
+ value as i32
+ }
+}
+
+// EXPONENT CALCULATION
+
+// Calculate the scientific notation exponent without overflow.
+//
+// For example, 0.1 would be -1, and 10 would be 1 in base 10.
+#[inline]
+pub(crate) fn scientific_exponent(
+ exponent: i32,
+ integer_digits: usize,
+ fraction_start: usize,
+) -> i32 {
+ if integer_digits == 0 {
+ let fraction_start = into_i32(fraction_start);
+ exponent.saturating_sub(fraction_start).saturating_sub(1)
+ } else {
+ let integer_shift = into_i32(integer_digits - 1);
+ exponent.saturating_add(integer_shift)
+ }
+}
+
+// Calculate the mantissa exponent without overflow.
+//
+// Remove the number of digits that contributed to the mantissa past
+// the dot, and add the number of truncated digits from the mantissa,
+// to calculate the scaling factor for the mantissa from a raw exponent.
+#[inline]
+pub(crate) fn mantissa_exponent(exponent: i32, fraction_digits: usize, truncated: usize) -> i32 {
+ if fraction_digits > truncated {
+ exponent.saturating_sub(into_i32(fraction_digits - truncated))
+ } else {
+ exponent.saturating_add(into_i32(truncated - fraction_digits))
+ }
+}