difftreelog
feat add intrinsics for numeric parsing
in: master
2 files changed
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth128 ("asciiUpper", builtin_ascii_upper::INST),128 ("asciiUpper", builtin_ascii_upper::INST),129 ("asciiLower", builtin_ascii_lower::INST),129 ("asciiLower", builtin_ascii_lower::INST),130 ("findSubstr", builtin_find_substr::INST),130 ("findSubstr", builtin_find_substr::INST),131 ("parseInt", builtin_parse_int::INST),132 ("parseOctal", builtin_parse_octal::INST),133 ("parseHex", builtin_parse_hex::INST),131 // Misc134 // Misc132 ("length", builtin_length::INST),135 ("length", builtin_length::INST),133 ("startsWith", builtin_starts_with::INST),136 ("startsWith", builtin_starts_with::INST),312 out.build()315 out.build()313 }316 }314 #[cfg(feature = "legacy-this-file")]317 #[cfg(feature = "legacy-this-file")]315 fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {318 fn initialize(&self, s: State, source: Source) -> Context {316 let mut builder = ObjValueBuilder::new();319 let mut builder = ObjValueBuilder::new();317 builder.with_super(self.stdlib_obj.clone());320 builder.with_super(self.stdlib_obj.clone());318 builder321 buildercrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth1use jrsonnet_evaluator::{1use jrsonnet_evaluator::{2 error::{ErrorKind::*, Result},2 error::{ErrorKind::*, Result},3 function::builtin,3 function::builtin,4 throw,4 typed::{Either2, VecVal, M1},5 typed::{Either2, VecVal, M1},5 val::ArrValue,6 val::ArrValue,6 Either, IStr, Val,7 Either, IStr, Val,74 Ok(out.into())75 Ok(out.into())75}76}7778#[builtin]79pub fn builtin_parse_int(raw: IStr) -> Result<f64> {80 let mut chars = raw.chars();81 if let Some(first_char) = chars.next() {82 if first_char == '-' {83 let remaining = chars.as_str();84 if remaining.is_empty() {85 throw!("Not an integer: \"{}\"", raw);86 }87 parse_nat::<10>(remaining).map(|value| -value)88 } else {89 parse_nat::<10>(raw.as_str())90 }91 } else {92 throw!("Not an integer: \"{}\"", raw);93 }94}9596#[builtin]97pub fn builtin_parse_octal(raw: IStr) -> Result<f64> {98 if raw.is_empty() {99 throw!("Not an octal number: \"\"");100 }101102 parse_nat::<8>(raw.as_str())103}104105#[builtin]106pub fn builtin_parse_hex(raw: IStr) -> Result<f64> {107 if raw.is_empty() {108 throw!("Not hexadecimal: \"\"");109 }110111 parse_nat::<16>(raw.as_str())112}113114fn parse_nat<const BASE: u32>(raw: &str) -> Result<f64> {115 debug_assert!(116 1 <= BASE && BASE <= 16,117 "integer base should be between 1 and 16"118 );119120 const ZERO_CODE: u32 = '0' as u32;121 const UPPER_A_CODE: u32 = 'A' as u32;122 const LOWER_A_CODE: u32 = 'a' as u32;123124 #[inline]125 fn checked_sub_if(condition: bool, lhs: u32, rhs: u32) -> Option<u32> {126 if condition {127 lhs.checked_sub(rhs)128 } else {129 None130 }131 }132133 let base = BASE as f64;134135 raw.chars().try_fold(0f64, |aggregate, digit| {136 let digit = digit as u32;137 let digit = if let Some(digit) = checked_sub_if(BASE > 10, digit, LOWER_A_CODE) {138 digit + 10139 } else if let Some(digit) = checked_sub_if(BASE > 10, digit, UPPER_A_CODE) {140 digit + 10141 } else {142 digit.checked_sub(ZERO_CODE).unwrap_or(BASE)143 };144145 if digit < BASE {146 Ok(base * aggregate + digit as f64)147 } else {148 throw!("{raw} is not a base {BASE} integer",);149 }150 })151}152153#[cfg(test)]154mod tests {155 use super::*;156157 #[test]158 fn parse_nat_base_10() {159 assert_eq!(parse_nat::<10>("0").unwrap(), 0.);160 assert_eq!(parse_nat::<10>("3").unwrap(), 3.);161 assert_eq!(parse_nat::<10>("27").unwrap(), 10. * 2. + 7.);162 assert_eq!(parse_nat::<10>("123").unwrap(), 10. * (10. * 1. + 2.) + 3.);163 }164}76165