Skip to content
← Back

src/ir/core_numeric_format.ghul

1
namespace IR is
2
/// Which numeric format strings the core library's `to_string(format)`
3
/// writes on a target whose core library is compiled from source: the
4
/// standard formats `D`, `E`, `F`, `G`, `N`, `R` and `X`, either case,
5
/// with an optional precision, `D` and `X` only for an integer; and
6
/// custom patterns of `0` and `#` placeholders with at most one `.` and
7
/// `,` among the placeholders before it.
8
class CORE_NUMERIC_FORMAT is
9
/// Whether the core library writes a value under `format`.
10
///
11
/// - format: the format string written after the `:` in an interpolation
12
/// - integer: whether the value is of an integer type rather than a floating one
13
is_covered(format: string, integer: bool) -> bool static is
14
if format.length == 0 then
15
return true
16
fi
17
18
let letter = char.to_upper_invariant(format[0])
19
20
if "DEFGNRX".index_of(letter) >= 0 then
21
for i in 1..format.length do
22
if !char.is_ascii_digit(format[i]) then
23
return _is_custom(format)
24
fi
25
od
26
27
return integer \/ (letter != 'D' /\ letter != 'X')
28
fi
29
30
return _is_custom(format)
31
si
32
33
_is_custom(format: string) -> bool static is
34
let dot = format.index_of('.')
35
36
for i in 0..format.length do
37
let c = format[i]
38
39
if c == '0' \/ c == '#' then
40
continue
41
fi
42
43
if c == '.' /\ i == dot then
44
continue
45
fi
46
47
if c == ',' /\ (dot < 0 \/ i < dot) then
48
continue
49
fi
50
51
return false
52
od
53
54
return true
55
si
56
si
57
si