oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/numbering.rs
14.7 KiB, 5 runs
created by r1870400018:63484, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // U3 owns this file. `numbering(pattern, ..nums)`: the one implementation of Typst numbering patterns |
| 2 | // (`"1.a)"`, `"I"`, `"*"`); heading, figure, list, footnote and page numbering (U5, U8) all call `apply`. |
| 3 | // The twenty-three numeral systems and their tables were read off the `typst` 0.15.1 oracle: bijective |
| 4 | // alphabets for Latin, kana and Hangul, greedy additive tables for Roman, Greek and Hebrew, positional |
| 5 | // digit sets, circled forms, and Chinese with ten-thousand grouping. |
| 6 | |
| 7 | use crate::diag::DiagnosticKind; |
| 8 | use crate::eval::args::Args; |
| 9 | use crate::eval::func::{ |
| 10 | Func, |
| 11 | NativeFunc, |
| 12 | }; |
| 13 | use crate::eval::lib::foundations::{ |
| 14 | mismatch, |
| 15 | need, |
| 16 | }; |
| 17 | use crate::eval::scope::Scope; |
| 18 | use crate::eval::value::Value; |
| 19 | use crate::eval::Engine; |
| 20 | use crate::syntax::Span; |
| 21 | |
| 22 | use oxedyne_fe2o3_core::prelude::*; |
| 23 | |
| 24 | native_fns! { |
| 25 | pub enum NumberingFn { |
| 26 | Numbering => "numbering", |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | pub fn define(scope: &mut Scope) { |
| 31 | scope.define("numbering", Value::Func(Func::Native(NativeFunc::Numbering(NumberingFn::Numbering)))); |
| 32 | } |
| 33 | |
| 34 | pub fn call(_f: NumberingFn, engine: &mut Engine, mut args: Args) -> Outcome<Value> { |
| 35 | let span = args.span; |
| 36 | let numbering = res!(need(engine, &mut args, "numbering")); |
| 37 | let rest = res!(args.all::<Value>()); |
| 38 | let mut nums = Vec::with_capacity(rest.len()); |
| 39 | for v in rest { |
| 40 | match v { |
| 41 | Value::Int(i) if i >= 0 => nums.push(i as u64), |
| 42 | Value::Int(_) => return Err(engine.error(DiagnosticKind::Type, span, "number must be at least zero")), |
| 43 | other => return Err(mismatch(engine, span, "integer", &other)), |
| 44 | } |
| 45 | } |
| 46 | res!(crate::eval::lib::foundations::finish(engine, args)); |
| 47 | apply_at(engine, span, &numbering, &nums) |
| 48 | } |
| 49 | |
| 50 | /// Formats `nums` with a pattern string (`"1.1"`) or calls a numbering function. |
| 51 | pub fn apply(engine: &mut Engine, numbering: &Value, nums: &[u64]) -> Outcome<Value> { |
| 52 | apply_at(engine, Span::detached(), numbering, nums) |
| 53 | } |
| 54 | |
| 55 | /// As [`apply`], a pattern without its first prefix and its suffix: a reference to a heading numbered |
| 56 | /// `"1."` reads "1". A numbering function is called as it is. |
| 57 | pub fn apply_trimmed(engine: &mut Engine, numbering: &Value, nums: &[u64]) -> Outcome<Value> { |
| 58 | apply_with(engine, Span::detached(), numbering, nums, true) |
| 59 | } |
| 60 | |
| 61 | /// One number at nesting depth `k` (from zero), as an enumeration item shows it: the pattern's first |
| 62 | /// prefix, the `k`th counting symbol (the last one repeating past the end) and the suffix. A |
| 63 | /// numbering function is called with the number alone. |
| 64 | pub fn apply_kth(engine: &mut Engine, numbering: &Value, k: usize, n: u64) -> Outcome<Value> { |
| 65 | let span = Span::detached(); |
| 66 | let numbering = &numbering.clone().symbol_as_str(); |
| 67 | match numbering { |
| 68 | Value::Str(s) => { |
| 69 | let pat = match Pattern::parse(s) { |
| 70 | Some(p) => p, |
| 71 | None => return Err(engine.error(DiagnosticKind::Type, span, "invalid numbering pattern")), |
| 72 | }; |
| 73 | let kind = pat.kind_at(k); |
| 74 | if n == 0 { |
| 75 | if let Some(name) = kind.zeroless_name() { |
| 76 | engine.warn(DiagnosticKind::Lint, span, fmt!("the numeral system `{}` cannot represent zero", name)); |
| 77 | } |
| 78 | } |
| 79 | Ok(Value::str(pat.apply_kth(k, n))) |
| 80 | } |
| 81 | _ => apply_at(engine, span, numbering, &[n]), |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn apply_at(engine: &mut Engine, span: Span, numbering: &Value, nums: &[u64]) -> Outcome<Value> { |
| 86 | apply_with(engine, span, numbering, nums, false) |
| 87 | } |
| 88 | |
| 89 | fn apply_with(engine: &mut Engine, span: Span, numbering: &Value, nums: &[u64], trimmed: bool) -> Outcome<Value> { |
| 90 | let numbering = &numbering.clone().symbol_as_str(); |
| 91 | match numbering { |
| 92 | Value::Str(s) => { |
| 93 | let mut pat = match Pattern::parse(s) { |
| 94 | Some(p) => p, |
| 95 | None => return Err(engine.error(DiagnosticKind::Type, span, "invalid numbering pattern")), |
| 96 | }; |
| 97 | pat.trimmed = trimmed; |
| 98 | for (i, n) in nums.iter().enumerate() { |
| 99 | if *n == 0 { |
| 100 | let kind = pat.kind_at(i); |
| 101 | if let Some(name) = kind.zeroless_name() { |
| 102 | engine.warn(DiagnosticKind::Lint, span, fmt!("the numeral system `{}` cannot represent zero", name)); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | Ok(Value::str(pat.apply(nums))) |
| 107 | } |
| 108 | Value::Func(f) => { |
| 109 | let mut a = Args::new(span); |
| 110 | for n in nums { |
| 111 | a.push(span, Value::Int(*n as i64)); |
| 112 | } |
| 113 | engine.call_func(f, a) |
| 114 | } |
| 115 | other => Err(mismatch(engine, span, "string or function", other)), |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// A parsed pattern: each counting symbol with the text before it, and the text after the last one. |
| 120 | #[derive(Clone, Debug)] |
| 121 | pub struct Pattern { |
| 122 | pub pieces: Vec<(String, Kind)>, |
| 123 | pub suffix: String, |
| 124 | pub trimmed: bool, // drop the first prefix and the suffix, as heading references do |
| 125 | } |
| 126 | |
| 127 | impl Pattern { |
| 128 | pub fn parse(s: &str) -> Option<Self> { |
| 129 | let mut pieces = Vec::new(); |
| 130 | let mut handled = 0; |
| 131 | for (i, c) in s.char_indices() { |
| 132 | if let Some(k) = Kind::from_char(c) { |
| 133 | pieces.push((s[handled..i].to_string(), k)); |
| 134 | handled = i + c.len_utf8(); |
| 135 | } |
| 136 | } |
| 137 | if pieces.is_empty() { |
| 138 | return None; |
| 139 | } |
| 140 | Some(Self { pieces, suffix: s[handled..].to_string(), trimmed: false }) |
| 141 | } |
| 142 | |
| 143 | pub fn kind_at(&self, i: usize) -> Kind { |
| 144 | match self.pieces.get(i).or(self.pieces.last()) { |
| 145 | Some((_, k)) => *k, |
| 146 | None => Kind::Arabic, |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// The `k`th counting symbol alone, between the first prefix and the suffix. |
| 151 | pub fn apply_kth(&self, k: usize, n: u64) -> String { |
| 152 | let mut out = String::new(); |
| 153 | if let Some((prefix, _)) = self.pieces.first() { |
| 154 | out.push_str(prefix); |
| 155 | } |
| 156 | out.push_str(&self.kind_at(k).apply(n)); |
| 157 | out.push_str(&self.suffix); |
| 158 | out |
| 159 | } |
| 160 | |
| 161 | pub fn apply(&self, nums: &[u64]) -> String { |
| 162 | let mut out = String::new(); |
| 163 | let mut it = nums.iter(); |
| 164 | for (i, ((prefix, kind), n)) in self.pieces.iter().zip(&mut it).enumerate() { |
| 165 | if i > 0 || !self.trimmed { |
| 166 | out.push_str(prefix); |
| 167 | } |
| 168 | out.push_str(&kind.apply(*n)); |
| 169 | } |
| 170 | if let Some((prefix, kind)) = self.pieces.last() { |
| 171 | for n in it { |
| 172 | if prefix.is_empty() { |
| 173 | out.push_str(&self.suffix); |
| 174 | } else { |
| 175 | out.push_str(prefix); |
| 176 | } |
| 177 | out.push_str(&kind.apply(*n)); |
| 178 | } |
| 179 | } |
| 180 | if !self.trimmed { |
| 181 | out.push_str(&self.suffix); |
| 182 | } |
| 183 | out |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 188 | pub enum Kind { |
| 189 | Arabic, |
| 190 | LowerLatin, |
| 191 | UpperLatin, |
| 192 | LowerRoman, |
| 193 | UpperRoman, |
| 194 | LowerGreek, |
| 195 | UpperGreek, |
| 196 | Symbol, |
| 197 | Hebrew, |
| 198 | LowerChinese, |
| 199 | UpperChinese, |
| 200 | HiraganaAiueo, |
| 201 | HiraganaIroha, |
| 202 | KatakanaAiueo, |
| 203 | KatakanaIroha, |
| 204 | KoreanJamo, |
| 205 | KoreanSyllable, |
| 206 | EasternArabic, |
| 207 | Persian, |
| 208 | Devanagari, |
| 209 | Bengali, |
| 210 | Circled, |
| 211 | DoubleCircled, |
| 212 | } |
| 213 | |
| 214 | const HIRAGANA_AIUEO: &str = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん"; |
| 215 | const HIRAGANA_IROHA: &str = "いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす"; |
| 216 | const KATAKANA_AIUEO: &str = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン"; |
| 217 | const KATAKANA_IROHA: &str = "イロハニホヘトチリヌルヲワカヨタレソツネナラムウヰノオクヤマケフコエテアサキユメミシヱヒモセス"; |
| 218 | const KOREAN_JAMO: &str = "ㄱㄴㄷㄹㅁㅂㅅㅇㅈㅊㅋㅌㅍㅎ"; |
| 219 | const KOREAN_SYLLABLE: &str = "가나다라마바사아자차카타파하"; |
| 220 | const SYMBOLS: [&str; 6] = ["*", "†", "‡", "§", "¶", "‖"]; |
| 221 | |
| 222 | const ROMAN: [(&str, u64); 20] = [ |
| 223 | ("M̅", 1_000_000), ("D̅", 500_000), ("C̅", 100_000), ("L̅", 50_000), ("X̅", 10_000), ("V̅", 5_000), |
| 224 | ("I̅V̅", 4_000), ("M", 1000), ("CM", 900), ("D", 500), ("CD", 400), ("C", 100), ("XC", 90), |
| 225 | ("L", 50), ("XL", 40), ("X", 10), ("IX", 9), ("V", 5), ("IV", 4), ("I", 1), |
| 226 | ]; |
| 227 | |
| 228 | const GREEK_LOWER: [(&str, u64); 36] = [ |
| 229 | ("͵θ", 9000), ("͵η", 8000), ("͵ζ", 7000), ("͵ϛ", 6000), ("͵ε", 5000), ("͵δ", 4000), ("͵γ", 3000), |
| 230 | ("͵β", 2000), ("͵α", 1000), ("ϡ", 900), ("ω", 800), ("ψ", 700), ("χ", 600), ("φ", 500), ("υ", 400), |
| 231 | ("τ", 300), ("σ", 200), ("ρ", 100), ("ϟ", 90), ("π", 80), ("ο", 70), ("ξ", 60), ("ν", 50), ("μ", 40), |
| 232 | ("λ", 30), ("κ", 20), ("ι", 10), ("θ", 9), ("η", 8), ("ζ", 7), ("στ", 6), ("ε", 5), ("δ", 4), ("γ", 3), |
| 233 | ("β", 2), ("α", 1), |
| 234 | ]; |
| 235 | |
| 236 | const GREEK_UPPER: [(&str, u64); 36] = [ |
| 237 | ("͵Θ", 9000), ("͵Η", 8000), ("͵Ζ", 7000), ("͵Ϛ", 6000), ("͵Ε", 5000), ("͵Δ", 4000), ("͵Γ", 3000), |
| 238 | ("͵Β", 2000), ("͵Α", 1000), ("Ϡ", 900), ("Ω", 800), ("Ψ", 700), ("Χ", 600), ("Φ", 500), ("Υ", 400), |
| 239 | ("Τ", 300), ("Σ", 200), ("Ρ", 100), ("Ϟ", 90), ("Π", 80), ("Ο", 70), ("Ξ", 60), ("Ν", 50), ("Μ", 40), |
| 240 | ("Λ", 30), ("Κ", 20), ("Ι", 10), ("Θ", 9), ("Η", 8), ("Ζ", 7), ("ΣΤ", 6), ("Ε", 5), ("Δ", 4), ("Γ", 3), |
| 241 | ("Β", 2), ("Α", 1), |
| 242 | ]; |
| 243 | |
| 244 | const HEBREW: [(&str, u64); 22] = [ |
| 245 | ("ת", 400), ("ש", 300), ("ר", 200), ("ק", 100), ("צ", 90), ("פ", 80), ("ע", 70), ("ס", 60), ("נ", 50), |
| 246 | ("מ", 40), ("ל", 30), ("כ", 20), ("י", 10), ("ט", 9), ("ח", 8), ("ז", 7), ("ו", 6), ("ה", 5), ("ד", 4), |
| 247 | ("ג", 3), ("ב", 2), ("א", 1), |
| 248 | ]; |
| 249 | |
| 250 | const CHINESE_LOWER_DIGITS: [&str; 10] = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]; |
| 251 | const CHINESE_UPPER_DIGITS: [&str; 10] = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; |
| 252 | const CHINESE_LOWER_UNITS: [&str; 3] = ["十", "百", "千"]; |
| 253 | const CHINESE_UPPER_UNITS: [&str; 3] = ["拾", "佰", "仟"]; |
| 254 | const CHINESE_GROUPS: [&str; 5] = ["", "万", "亿", "兆", "京"]; |
| 255 | |
| 256 | impl Kind { |
| 257 | pub fn from_char(c: char) -> Option<Kind> { |
| 258 | let k = match c { |
| 259 | '1' => Kind::Arabic, |
| 260 | 'a' => Kind::LowerLatin, |
| 261 | 'A' => Kind::UpperLatin, |
| 262 | 'i' => Kind::LowerRoman, |
| 263 | 'I' => Kind::UpperRoman, |
| 264 | 'α' => Kind::LowerGreek, |
| 265 | 'Α' => Kind::UpperGreek, |
| 266 | '*' => Kind::Symbol, |
| 267 | 'א' => Kind::Hebrew, |
| 268 | '一' => Kind::LowerChinese, |
| 269 | '壹' => Kind::UpperChinese, |
| 270 | 'あ' => Kind::HiraganaAiueo, |
| 271 | 'い' => Kind::HiraganaIroha, |
| 272 | 'ア' => Kind::KatakanaAiueo, |
| 273 | 'イ' => Kind::KatakanaIroha, |
| 274 | 'ㄱ' => Kind::KoreanJamo, |
| 275 | '가' => Kind::KoreanSyllable, |
| 276 | '١' => Kind::EasternArabic, |
| 277 | '۱' => Kind::Persian, |
| 278 | '१' => Kind::Devanagari, |
| 279 | '১' => Kind::Bengali, |
| 280 | '①' => Kind::Circled, |
| 281 | '⓵' => Kind::DoubleCircled, |
| 282 | _ => return None, |
| 283 | }; |
| 284 | Some(k) |
| 285 | } |
| 286 | |
| 287 | /// The system's name in Typst's "cannot represent zero" warning, for the systems that warn. |
| 288 | fn zeroless_name(self) -> Option<&'static str> { |
| 289 | match self { |
| 290 | Kind::LowerLatin => Some("latin"), |
| 291 | Kind::UpperLatin => Some("Latin"), |
| 292 | _ => None, |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | pub fn apply(self, n: u64) -> String { |
| 297 | match self { |
| 298 | Kind::Arabic => n.to_string(), |
| 299 | Kind::LowerLatin => bijective(n, "abcdefghijklmnopqrstuvwxyz"), |
| 300 | Kind::UpperLatin => bijective(n, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |
| 301 | Kind::LowerRoman => if n == 0 { "n".to_string() } else { additive(n, &ROMAN).to_lowercase() }, |
| 302 | Kind::UpperRoman => if n == 0 { "N".to_string() } else { additive(n, &ROMAN) }, |
| 303 | Kind::LowerGreek => if n == 0 { "𐆊".to_string() } else { additive(n, &GREEK_LOWER) }, |
| 304 | Kind::UpperGreek => if n == 0 { "𐆊".to_string() } else { additive(n, &GREEK_UPPER) }, |
| 305 | Kind::Symbol => if n == 0 { |
| 306 | "0".to_string() |
| 307 | } else { |
| 308 | SYMBOLS[((n - 1) % 6) as usize].repeat(((n - 1) / 6 + 1) as usize) |
| 309 | }, |
| 310 | Kind::Hebrew => hebrew(n), |
| 311 | Kind::LowerChinese => chinese(n, &CHINESE_LOWER_DIGITS, &CHINESE_LOWER_UNITS), |
| 312 | Kind::UpperChinese => chinese(n, &CHINESE_UPPER_DIGITS, &CHINESE_UPPER_UNITS), |
| 313 | Kind::HiraganaAiueo => bijective(n, HIRAGANA_AIUEO), |
| 314 | Kind::HiraganaIroha => bijective(n, HIRAGANA_IROHA), |
| 315 | Kind::KatakanaAiueo => bijective(n, KATAKANA_AIUEO), |
| 316 | Kind::KatakanaIroha => bijective(n, KATAKANA_IROHA), |
| 317 | Kind::KoreanJamo => bijective(n, KOREAN_JAMO), |
| 318 | Kind::KoreanSyllable => bijective(n, KOREAN_SYLLABLE), |
| 319 | Kind::EasternArabic => digits(n, '٠'), |
| 320 | Kind::Persian => digits(n, '۰'), |
| 321 | Kind::Devanagari => digits(n, '०'), |
| 322 | Kind::Bengali => digits(n, '০'), |
| 323 | Kind::Circled => circled(n), |
| 324 | Kind::DoubleCircled => match n { |
| 325 | 1..=10 => char::from_u32(0x24F5 + n as u32 - 1).map(|c| c.to_string()).unwrap_or_default(), |
| 326 | _ => n.to_string(), |
| 327 | }, |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // Bijective base-k over an alphabet: 1 is the first letter, k + 1 the first letter twice. Zero, which |
| 333 | // such a system cannot write, falls back to "0". |
| 334 | fn bijective(n: u64, alphabet: &str) -> String { |
| 335 | if n == 0 { |
| 336 | return "0".to_string(); |
| 337 | } |
| 338 | let letters: Vec<char> = alphabet.chars().collect(); |
| 339 | let k = letters.len() as u64; |
| 340 | let mut n = n; |
| 341 | let mut out = Vec::new(); |
| 342 | while n > 0 { |
| 343 | n -= 1; |
| 344 | out.push(letters[(n % k) as usize]); |
| 345 | n /= k; |
| 346 | } |
| 347 | out.iter().rev().collect() |
| 348 | } |
| 349 | |
| 350 | fn additive(n: u64, table: &[(&str, u64)]) -> String { |
| 351 | let mut n = n; |
| 352 | let mut out = String::new(); |
| 353 | for (sym, v) in table { |
| 354 | while n >= *v { |
| 355 | out.push_str(sym); |
| 356 | n -= v; |
| 357 | } |
| 358 | } |
| 359 | out |
| 360 | } |
| 361 | |
| 362 | fn hebrew(n: u64) -> String { |
| 363 | if n == 0 { |
| 364 | return "0".to_string(); |
| 365 | } |
| 366 | // 15 and 16 are written 9 + 6 and 9 + 7, avoiding spellings of the divine name. |
| 367 | let mut n = n; |
| 368 | let mut out = String::new(); |
| 369 | for (sym, v) in &HEBREW { |
| 370 | while n >= *v { |
| 371 | match n { |
| 372 | 15 => { out.push_str("טו"); return out; } |
| 373 | 16 => { out.push_str("טז"); return out; } |
| 374 | _ => { out.push_str(sym); n -= v; } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | out |
| 379 | } |
| 380 | |
| 381 | fn digits(n: u64, zero: char) -> String { |
| 382 | n.to_string().chars().map(|d| { |
| 383 | let k = d.to_digit(10).unwrap_or(0); |
| 384 | char::from_u32(zero as u32 + k).unwrap_or(d) |
| 385 | }).collect() |
| 386 | } |
| 387 | |
| 388 | fn circled(n: u64) -> String { |
| 389 | let c = match n { |
| 390 | 0 => Some('⓪'), |
| 391 | 1..=20 => char::from_u32(0x2460 + n as u32 - 1), |
| 392 | 21..=35 => char::from_u32(0x3251 + n as u32 - 21), |
| 393 | 36..=50 => char::from_u32(0x32B1 + n as u32 - 36), |
| 394 | _ => None, |
| 395 | }; |
| 396 | match c { |
| 397 | Some(c) => c.to_string(), |
| 398 | None => n.to_string(), |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // Chinese numerals with ten-thousand grouping (万, 亿, 兆, 京): a zero inside or between groups is |
| 403 | // written once as 零, and a leading 一十 is shortened to 十. |
| 404 | fn chinese(n: u64, dig: &[&str; 10], units: &[&str; 3]) -> String { |
| 405 | if n == 0 { |
| 406 | return dig[0].to_string(); |
| 407 | } |
| 408 | let mut groups = Vec::new(); |
| 409 | let mut m = n; |
| 410 | while m > 0 { |
| 411 | groups.push((m % 10_000) as u32); |
| 412 | m /= 10_000; |
| 413 | } |
| 414 | let mut out = String::new(); |
| 415 | let mut zero_pending = false; |
| 416 | let top = groups.len() - 1; |
| 417 | for gi in (0..groups.len()).rev() { |
| 418 | let g = groups[gi]; |
| 419 | if g == 0 { |
| 420 | zero_pending = !out.is_empty(); |
| 421 | continue; |
| 422 | } |
| 423 | if !out.is_empty() && (zero_pending || g < 1000) { |
| 424 | out.push_str(dig[0]); |
| 425 | } |
| 426 | zero_pending = false; |
| 427 | out.push_str(&chinese_group(g, gi == top, dig, units)); |
| 428 | out.push_str(CHINESE_GROUPS.get(gi).copied().unwrap_or("")); |
| 429 | } |
| 430 | out |
| 431 | } |
| 432 | |
| 433 | fn chinese_group(g: u32, leading: bool, dig: &[&str; 10], units: &[&str; 3]) -> String { |
| 434 | if leading && (10..20).contains(&g) { |
| 435 | let mut s = units[0].to_string(); |
| 436 | if g > 10 { |
| 437 | s.push_str(dig[(g - 10) as usize]); |
| 438 | } |
| 439 | return s; |
| 440 | } |
| 441 | let ds = [g / 1000, g / 100 % 10, g / 10 % 10, g % 10]; |
| 442 | let mut out = String::new(); |
| 443 | let mut zero = false; |
| 444 | for (i, d) in ds.iter().enumerate() { |
| 445 | if *d == 0 { |
| 446 | zero = !out.is_empty(); |
| 447 | continue; |
| 448 | } |
| 449 | if zero { |
| 450 | out.push_str(dig[0]); |
| 451 | zero = false; |
| 452 | } |
| 453 | out.push_str(dig[*d as usize]); |
| 454 | if i < 3 { |
| 455 | out.push_str(units[2 - i]); |
| 456 | } |
| 457 | } |
| 458 | out |
| 459 | } |