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
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate;
use ;
// The ten Unicode subscript digits, in order, so that a digit's value is its position.
//
// They are contiguous in Unicode, but writing them out keeps the conversion from needing a fallible
// `char::from_u32`, and shows at a glance which characters are meant.
const SUBSCRIPT_DIGITS: = ;
/// Writes a number as a run of Unicode subscript digits.
///
/// The digits are the Subscripts block's `₀` through `₉`, so that 10 is written `₁₀`. There
/// is no sign and no leading zero: zero is written `₀`, and nothing else begins with it.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `n.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::chars::scripts::fmt_subscript_digits;
/// use std::fmt::{Display, Formatter, Result};
///
/// struct Indexed(u64);
///
/// impl Display for Indexed {
/// fn fmt(&self, f: &mut Formatter) -> Result {
/// f.write_str("x")?;
/// fmt_subscript_digits(self.0, f)
/// }
/// }
///
/// assert_eq!(Indexed(0).to_string(), "x₀");
/// assert_eq!(Indexed(10).to_string(), "x₁₀");
/// ```
/// Reads a number written as a run of Unicode subscript digits.
///
/// This accepts exactly what [`fmt_subscript_digits`] writes, and nothing else: the empty string, a
/// string holding anything but a subscript digit, a string with a leading zero, and a number too
/// large for a [`u64`] are all rejected. Accepting only the one spelling of a number is what makes
/// the two functions inverse to each other.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `s.len()`.
///
/// # Examples
/// ```
/// use malachite_base::chars::scripts::parse_subscript_digits;
///
/// assert_eq!(parse_subscript_digits("₀"), Some(0));
/// assert_eq!(parse_subscript_digits("₁₀"), Some(10));
/// assert_eq!(parse_subscript_digits(""), None);
/// assert_eq!(parse_subscript_digits("10"), None);
/// assert_eq!(parse_subscript_digits("₀₁"), None);
/// ```