Skip to main content

ravenrs_sheet/numfmt/
builtin.rs

1//! Built-in number formats (ECMA-376 ยง18.8.30, en-US).
2
3const BUILTINS: [(u32, &str); 36] = [
4    (0, "General"),
5    (1, "0"),
6    (2, "0.00"),
7    (3, "#,##0"),
8    (4, "#,##0.00"),
9    (5, "\"$\"#,##0_);\\(\"$\"#,##0\\)"),
10    (6, "\"$\"#,##0_);[Red]\\(\"$\"#,##0\\)"),
11    (7, "\"$\"#,##0.00_);\\(\"$\"#,##0.00\\)"),
12    (8, "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)"),
13    (9, "0%"),
14    (10, "0.00%"),
15    (11, "0.00E+00"),
16    (12, "# ?/?"),
17    (13, "# ??/??"),
18    (14, "m/d/yyyy"),
19    (15, "d-mmm-yy"),
20    (16, "d-mmm"),
21    (17, "mmm-yy"),
22    (18, "h:mm AM/PM"),
23    (19, "h:mm:ss AM/PM"),
24    (20, "h:mm"),
25    (21, "h:mm:ss"),
26    (22, "m/d/yyyy h:mm"),
27    (37, "#,##0 ;(#,##0)"),
28    (38, "#,##0 ;[Red](#,##0)"),
29    (39, "#,##0.00;(#,##0.00)"),
30    (40, "#,##0.00;[Red](#,##0.00)"),
31    (41, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)"),
32    (42, "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)"),
33    (43, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)"),
34    (44, "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)"),
35    (45, "mm:ss"),
36    (46, "[h]:mm:ss"),
37    (47, "mmss.0"),
38    (48, "##0.0E+0"),
39    (49, "@"),
40];
41
42/// Built-in format code for an id (0..=49, en-US), e.g. 14 => "m/d/yyyy". Locale-specific ids
43/// (23โ€“36) and unknown ids return `None`.
44pub fn builtin_format(id: u32) -> Option<&'static str> {
45    BUILTINS.iter().find(|(i, _)| *i == id).map(|(_, c)| *c)
46}
47
48/// Id of a built-in format code. Matching ignores case and backslash escapes.
49pub fn builtin_id(code: &str) -> Option<u32> {
50    if let Some((id, _)) = BUILTINS.iter().find(|(_, c)| *c == code) {
51        return Some(*id);
52    }
53    let norm = |s: &str| s.chars().filter(|&c| c != '\\').flat_map(char::to_lowercase).collect::<String>();
54    let n = norm(code);
55    BUILTINS.iter().find(|(_, c)| norm(c) == n).map(|(id, _)| *id)
56}