git.delta.rocks / jrsonnet / refs/commits / 4de6d4c2f204

difftreelog

chore simplify `builtin_parse_int`

Petr Portnov2022-11-23parent: #b4d7701.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
50 Ok(str.to_ascii_lowercase())50 Ok(str.to_ascii_lowercase())
51}51}
52
53pub fn repeat(what: Either![IStr, ArrValue], count: i32) {
54 joi
55}
5652
57#[builtin]53#[builtin]
58pub fn builtin_find_substr(pat: IStr, str: IStr) -> Result<ArrValue> {54pub fn builtin_find_substr(pat: IStr, str: IStr) -> Result<ArrValue> {
8177
82#[builtin]78#[builtin]
83pub fn builtin_parse_int(raw: IStr) -> Result<f64> {79pub fn builtin_parse_int(raw: IStr) -> Result<f64> {
84 let mut chars = raw.chars();
85 if let Some(first_char) = chars.next() {80 if let Some(raw) = raw.strip_prefix('-') {
86 if first_char == '-' {
87 let remaining = chars.as_str();
88 if remaining.is_empty() {81 if raw.is_empty() {
89 throw!("Integer only consists of a minus");82 throw!("integer only consists of a minus")
90 }83 }
84
91 parse_nat::<10>(remaining).map(|value| -value)85 parse_nat::<10>(raw).map(|value| -value)
92 } else {
93 parse_nat::<10>(raw.as_str())
94 }
95 } else {86 } else {
87 if raw.is_empty() {
96 throw!("Empty decimal integer",);88 throw!("empty integer")
89 }
90
91 parse_nat::<10>(raw.as_str())
97 }92 }
98}93}
9994