mln-rs-expression 0.0.7

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Ports include/mln/style/expression/number_format.hpp and
// src/mln/style/expression/number_format.cpp.

use crate::expression::{
    EvaluationContext, EvaluationResult, Expression, ExpressionBase, Kind, downcast,
};
use crate::parsing_context::{ParseResult, ParsingContext};
use crate::type_::{self, Type};
use crate::value::{Value, to_string};
use maplibre_native_base::{Value as MbglValue, ValueObject};
use mln_conversion::Convertible;

const LOCALE_KEY: &str = "locale";
const CURRENCY_KEY: &str = "currency";
const MIN_FRACTION_DIGITS_KEY: &str = "min-fraction-digits";
const MAX_FRACTION_DIGITS_KEY: &str = "max-fraction-digits";

/// `platform::formatNumber` (platform/default/src/mln/i18n/number_format.cpp,
/// ICU `NumberFormatter`).
// TODO(port): ICU number formatting. This implements ICU's behaviour for the
// root/en locales only: the shortest round-trip decimal of the double, rounded
// half-even to maxFractionDigits and padded to minFractionDigits, ',' grouping
// and '.' decimal separator; with a currency, ICU's currency precision and an
// en-US symbol for a few common currencies. The locale is ignored (see
// DEVIATIONS.md).
fn format_number(
    number: f64,
    _locale_id: &str,
    currency: &str,
    min_fraction_digits: u8,
    max_fraction_digits: u8,
) -> String {
    if number.is_nan() {
        return "NaN".to_owned();
    }
    if number.is_infinite() {
        return if number > 0.0 { "∞" } else { "-∞" }.to_owned();
    }

    let (symbol, min_fraction, max_fraction) = if currency.is_empty() {
        (None, min_fraction_digits as usize, max_fraction_digits as usize)
    } else {
        let digits = match currency {
            "JPY" | "KRW" | "VND" | "CLP" | "ISK" | "UGX" | "PYG" => 0,
            _ => 2,
        };
        let symbol = match currency {
            "USD" => "$".to_owned(),
            "EUR" => "€".to_owned(),
            "JPY" => "¥".to_owned(),
            "GBP" => "£".to_owned(),
            "CNY" => "CN¥".to_owned(),
            "INR" => "₹".to_owned(),
            "KRW" => "₩".to_owned(),
            "CAD" => "CA$".to_owned(),
            "AUD" => "A$".to_owned(),
            other => format!("{other}\u{a0}"),
        };
        (Some(symbol), digits, digits)
    };
    let max_fraction = max_fraction.max(min_fraction);

    // Shortest round-trip digits of |number| (`digits` is 0.d0d1d2... *
    // 10^point, so the decimal point sits after index `point`).
    let (mut digits, mut point) = shortest_digits(number.abs());
    if digits.iter().all(|d| *d == 0) {
        digits = vec![0];
        point = 1;
    }

    // Round half-even at max_fraction fraction digits.
    let keep = point + max_fraction as i64;
    if keep < digits.len() as i64 {
        let (head_len, round_up) = if keep < 0 {
            (0usize, false)
        } else {
            let keep = keep as usize;
            let first = digits[keep];
            let rest_nonzero = digits[keep + 1..].iter().any(|d| *d != 0);
            let last_kept_odd = keep > 0 && digits[keep - 1] % 2 == 1;
            (keep, first > 5 || (first == 5 && (rest_nonzero || last_kept_odd)))
        };
        digits.truncate(head_len);
        if round_up {
            let mut i = digits.len();
            loop {
                if i == 0 {
                    digits.insert(0, 1);
                    point += 1;
                    break;
                }
                i -= 1;
                if digits[i] == 9 {
                    digits[i] = 0;
                } else {
                    digits[i] += 1;
                    break;
                }
            }
        }
    }

    // Integer and fraction digit strings.
    let digit_at = |i: i64| -> u8 {
        if i >= 0 && (i as usize) < digits.len() { digits[i as usize] } else { 0 }
    };
    let mut int_part = String::new();
    if point <= 0 {
        int_part.push('0');
    } else {
        for i in 0..point {
            int_part.push((b'0' + digit_at(i)) as char);
        }
    }
    let mut frac_part = String::new();
    for i in 0..max_fraction as i64 {
        frac_part.push((b'0' + digit_at(point + i)) as char);
    }
    while frac_part.len() > min_fraction && frac_part.ends_with('0') {
        frac_part.pop();
    }

    let mut grouped = String::new();
    let int_len = int_part.len();
    for (i, c) in int_part.chars().enumerate() {
        if i > 0 && (int_len - i) % 3 == 0 {
            grouped.push(',');
        }
        grouped.push(c);
    }
    // ICU keeps the sign of negative numbers that round to zero, and of -0.
    let mut result = String::new();
    if number.is_sign_negative() {
        result.push('-');
    }
    if let Some(symbol) = symbol {
        result.push_str(&symbol);
    }
    result.push_str(&grouped);
    if !frac_part.is_empty() {
        result.push('.');
        result.push_str(&frac_part);
    }
    result
}


/// Digits and decimal point position of a `{:e}` formatting.
fn sci_digits(sci: &str) -> (Vec<u8>, i64) {
    let (mantissa, exponent) = sci.split_once('e').unwrap();
    let exponent: i64 = exponent.parse().unwrap();
    let digits = mantissa.bytes().filter(|b| b.is_ascii_digit()).map(|b| b - b'0').collect();
    (digits, exponent + 1)
}

/// The shortest round-trip decimal digits of `x`, as double-conversion's
/// SHORTEST mode (which ICU's DecimalQuantity uses) produces them: when two
/// shortest candidates are exactly equally close, the even one (Rust's
/// formatter may pick the other).
fn shortest_digits(x: f64) -> (Vec<u8>, i64) {
    let (digits, point) = sci_digits(&format!("{x:e}"));
    let (mut exact, exact_point) = sci_digits(&format!("{x:.1100e}"));
    while exact.len() > 1 && exact.last() == Some(&0) {
        exact.pop();
    }
    let l = digits.len();
    if exact_point == point && exact.len() == l + 1 && exact[l] == 5 && l <= 17 {
        let to_int = |d: &[u8]| d.iter().fold(0u64, |a, d| a * 10 + *d as u64);
        let low = to_int(&exact[..l]);
        let shortest = to_int(&digits);
        if shortest == low || shortest == low + 1 {
            let even = if low % 2 == 0 { low } else { low + 1 };
            let mut out: Vec<u8> = even.to_string().bytes().map(|b| b - b'0').collect();
            let mut p = point;
            if out.len() > l {
                p += 1;
            }
            while out.len() < l {
                out.insert(0, 0);
            }
            while out.len() > 1 && out.last() == Some(&0) {
                out.pop();
            }
            return (out, p);
        }
    }
    (digits, point)
}

pub struct NumberFormat {
    base: ExpressionBase,
    number: Box<dyn Expression>,
    locale: Option<Box<dyn Expression>>,
    currency: Option<Box<dyn Expression>>,
    min_fraction_digits: Option<Box<dyn Expression>>,
    max_fraction_digits: Option<Box<dyn Expression>>,
}

fn opt_deps(e: &Option<Box<dyn Expression>>) -> crate::expression::Dependency {
    e.as_ref().map_or(Default::default(), |e| e.dependencies())
}

fn opt_equal(l: &Option<Box<dyn Expression>>, r: &Option<Box<dyn Expression>>) -> bool {
    match (l, r) {
        (Some(l), Some(r)) => l.equals(&**r),
        (None, None) => true,
        _ => false,
    }
}

/// `static_cast<uint8_t>(double)`: through int32 on x86-64.
fn static_cast_u8(d: f64) -> u8 {
    (if d > -2147483649.0 && d < 2147483648.0 { d as i32 } else { i32::MIN }) as u8
}

impl NumberFormat {
    pub fn new(
        number: Box<dyn Expression>,
        locale: Option<Box<dyn Expression>>,
        currency: Option<Box<dyn Expression>>,
        min_fraction_digits: Option<Box<dyn Expression>>,
        max_fraction_digits: Option<Box<dyn Expression>>,
    ) -> Self {
        let deps = number.dependencies()
            | opt_deps(&locale)
            | opt_deps(&currency)
            | opt_deps(&min_fraction_digits)
            | opt_deps(&max_fraction_digits);
        NumberFormat {
            base: ExpressionBase::new(Kind::NumberFormat, Type::String, deps),
            number,
            locale,
            currency,
            min_fraction_digits,
            max_fraction_digits,
        }
    }

    pub fn parse(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
        let length = value.array_length();

        if length != 3 {
            ctx.error(format!("Expected two arguments, but found {length} instead."));
            return None;
        }

        let Some(number_result) = ctx.parse(&value.array_member(1), 1, Some(Type::Number), None) else {
            ctx.error("Failed to parse the number.".to_owned());
            return None;
        };

        let t = number_result.get_type();
        if *t != Type::Number {
            ctx.error(format!(
                "Expected argument of type number, but found {} instead.",
                type_::to_string(t)
            ));
            return None;
        }

        let options = value.array_member(2);
        if !options.is_object() {
            ctx.error("Number-format options argument must be an object.".to_owned());
            return None;
        }

        let mut parse_option = |key: &str, t: Type, message: &str| -> Result<Option<Box<dyn Expression>>, ()> {
            if let Some(option) = options.object_member(key) {
                match ctx.parse(&option, 1, Some(t), None) {
                    Some(r) => Ok(Some(r)),
                    None => {
                        ctx.error(message.to_owned());
                        Err(())
                    }
                }
            } else {
                Ok(None)
            }
        };

        let locale_result = parse_option(LOCALE_KEY, Type::String, "Number-format locale parsing failed.").ok()?;
        let currency_result =
            parse_option(CURRENCY_KEY, Type::String, "Number-format currency parsing failed.").ok()?;
        let min_fraction_digits_result = parse_option(
            MIN_FRACTION_DIGITS_KEY,
            Type::Number,
            "Number-format min-fraction-digits parsing failed.",
        )
        .ok()?;
        let max_fraction_digits_result = parse_option(
            MAX_FRACTION_DIGITS_KEY,
            Type::Number,
            "Number-format max-fraction-digits parsing failed.",
        )
        .ok()?;

        Some(Box::new(NumberFormat::new(
            number_result,
            locale_result,
            currency_result,
            min_fraction_digits_result,
            max_fraction_digits_result,
        )))
    }
}

impl Expression for NumberFormat {
    fn base(&self) -> &ExpressionBase {
        &self.base
    }

    fn evaluate(&self, params: &EvaluationContext<'_>) -> EvaluationResult {
        let number_result = self.number.evaluate(params)?;
        let evaluated_number = number_result.as_number().expect("numeric input");

        let mut evaluated_locale = String::new();
        if let Some(locale) = &self.locale {
            evaluated_locale = to_string(&locale.evaluate(params)?);
        }

        let mut evaluated_currency = String::new();
        if let Some(currency) = &self.currency {
            evaluated_currency = to_string(&currency.evaluate(params)?);
        }

        let mut evaluated_min_fraction_digits: u8 = 0;
        if let Some(min) = &self.min_fraction_digits {
            evaluated_min_fraction_digits = static_cast_u8(min.evaluate(params)?.as_number().expect("number"));
        }

        let mut evaluated_max_fraction_digits: u8 = 3;
        if let Some(max) = &self.max_fraction_digits {
            evaluated_max_fraction_digits = static_cast_u8(max.evaluate(params)?.as_number().expect("number"));
        }

        Ok(Value::String(format_number(
            evaluated_number,
            &evaluated_locale,
            &evaluated_currency,
            evaluated_min_fraction_digits,
            evaluated_max_fraction_digits,
        )))
    }

    fn each_child<'a>(&'a self, visit: &mut dyn FnMut(&'a dyn Expression)) {
        visit(&*self.number);
        for e in [&self.locale, &self.currency, &self.min_fraction_digits, &self.max_fraction_digits]
            .into_iter()
            .flatten()
        {
            visit(&**e);
        }
    }

    fn equals(&self, e: &dyn Expression) -> bool {
        if e.get_kind() == Kind::NumberFormat {
            let rhs = downcast::<NumberFormat>(e).expect("NumberFormat");
            return opt_equal(&self.locale, &rhs.locale)
                && opt_equal(&self.currency, &rhs.currency)
                && opt_equal(&self.min_fraction_digits, &rhs.min_fraction_digits)
                && opt_equal(&self.max_fraction_digits, &rhs.max_fraction_digits)
                && self.number.equals(&*rhs.number);
        }
        false
    }

    fn possible_outputs(&self) -> Vec<Option<Value>> {
        vec![None]
    }

    fn serialize(&self) -> MbglValue {
        let mut options = ValueObject::new();
        if let Some(locale) = &self.locale {
            options.insert(LOCALE_KEY.to_owned(), locale.serialize());
        }
        if let Some(currency) = &self.currency {
            options.insert(CURRENCY_KEY.to_owned(), currency.serialize());
        }
        if let Some(min) = &self.min_fraction_digits {
            options.insert(MIN_FRACTION_DIGITS_KEY.to_owned(), min.serialize());
        }
        if let Some(max) = &self.max_fraction_digits {
            options.insert(MAX_FRACTION_DIGITS_KEY.to_owned(), max.serialize());
        }
        MbglValue::Array(vec![
            MbglValue::String(self.get_operator()),
            self.number.serialize(),
            MbglValue::Object(options),
        ])
    }

    fn get_operator(&self) -> String {
        "number-format".to_owned()
    }
}

#[cfg(test)]
mod tests {
    use super::format_number;

    #[test]
    fn icu_like() {
        assert_eq!(format_number(123456.789, "", "", 0, 3), "123,456.789");
        assert_eq!(format_number(123456.789, "en-US", "JPY", 0, 3), "¥123,457");
        assert_eq!(format_number(123456.789, "en-US", "EUR", 0, 3), "€123,456.79");
        assert_eq!(format_number(987654321.234567, "en-US", "", 15, 20), "987,654,321.234567000000000");
        assert_eq!(format_number(987654321.234567, "en-US", "", 2, 4), "987,654,321.2346");
        assert_eq!(format_number(0.5, "", "", 0, 0), "0");
        assert_eq!(format_number(1.5, "", "", 0, 0), "2");
        assert_eq!(format_number(0.0005, "", "", 0, 3), "0");
    }
}