num2words2-core 0.1.1

Number-to-words conversion for 120+ languages (Rust port of num2words). 42 -> forty-two.
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//! Port of `lang_SV.py` (`Num2Word_SV`), via `lang_EUR` → `Num2Word_Base`.
//!
//! Engine-style language: SV supplies `cards` + `merge` and lets the base
//! `to_cardinal` drive `splitnum`/`clean`. It overrides `to_ordinal`,
//! `to_ordinal_num` and `to_year` outright.
//!
//! SV redefines `set_high_numwords` with a body byte-identical to
//! `Num2Word_EUR`'s; the only real change is the suffix pair
//! `GIGA_SUFFIX = "iljarder"` / `MEGA_SUFFIX = "iljoner"`, which yields the
//! Swedish long scale: 10^6 "miljoner", 10^9 "miljarder", 10^12 "biljoner",
//! 10^15 "biljarder", … up to 10^600 "centiljoner" / 10^603 "centiljarder".
//! MAXVAL is therefore 10^606 — far past u64/i128, so everything stays BigInt.
//!
//! # Faithfully reproduced Python bugs
//!
//! Per the porting contract these are preserved verbatim, not fixed:
//!
//! 1. **`ords["tjugo"]` is unreachable.** `to_ordinal` looks the last word up
//!    by its last *4* chars, then its last *3*. "tjugo" is 5 chars, so neither
//!    probe can ever hit it and the entry is dead code. Every multiple of 20
//!    falls through to the generic "de" suffix: `to_ordinal(20)` → "tjugode",
//!    not "tjugonde"; `to_ordinal(120)` → "etthundratjugode". Confirmed
//!    against the frozen corpus.
//!
//! 2. **Plural mega/giga words get ordinalised as-is.** The cards store the
//!    *plural* "miljoner"/"miljarder", and `merge` only singularises them on
//!    the `lnum == 1` path ("en miljon"). So `to_ordinal(10**7)` →
//!    "tio miljonerde" and `to_ordinal(10**10)` → "tio miljarderde", while
//!    `to_ordinal(10**6)` → "en miljonde". Corpus agrees.
//!
//! 3. **`merge` returns `lnum + rnum` where a product is meant.** The
//!    `rnum >= 1000000` branches precede the `rnum > lnum` multiply branch, so
//!    merging ("ett",1) with ("miljoner",10^6) yields num 1_000_001, not
//!    10^6. The bogus totals never reach the output text (only the string is
//!    used at the top level, and the subsequent comparisons happen to land in
//!    the same branches), so cardinals are unaffected — but the numeric field
//!    of the tree is genuinely wrong and is reproduced as such.
//!
//! 4. **`to_ordinal_num` skips `verify_ordinal`.** Negatives are accepted and
//!    formatted off the decimal string: `to_ordinal_num(-1)` → "-1:a",
//!    `to_ordinal_num(-42)` → "-42:a". Its last two branches are also
//!    identical (both append ":e"), so the final `else` is dead — kept here
//!    for structural fidelity.
//!
//! 5. **SV's `to_currency` double-spaces the minus on the int path.** It
//!    builds `"%s %s %s" % (minus_str, money, unit)` with `minus_str =
//!    self.negword` — the *un-stripped* "minus " — where every other path
//!    uses `self.negword.strip()`. `.strip()` only removes the leading and
//!    trailing space, so the interior double space survives:
//!    `to_currency(-10, "EUR")` → "minus  tio euros". The float path
//!    delegates to `Num2Word_Base` and correctly yields a single space
//!    ("minus tolv euros, trettiofyra cents"), so the same call spaces
//!    differently depending on whether the caller passed `-10` or `-10.0`.
//!
//! 6. **SV's int path silently ignores `adjective=`.** `Num2Word_Base`'s int
//!    path applies `prefix_currency`, but SV's override never consults
//!    `CURRENCY_ADJECTIVES`. So `to_currency(2, "USD", adjective=True)` is
//!    "två dollars" while `to_currency(2.0, "USD", adjective=True)` is
//!    "två US dollars, noll cents". `cents=` and `separator=` are likewise
//!    dead on the int path. `CURRENCY_ADJECTIVES` is still wired up below
//!    because the float path (via the base) does honour it.
//!
//! # Where SV's currency table actually comes from
//!
//! `Num2Word_SV` defines no `CURRENCY_FORMS` of its own and inherits
//! `Num2Word_EUR`'s. That dict is a *class* attribute, and
//! `Num2Word_EN.__init__` does `self.CURRENCY_FORMS["CHF"] = ...` — a
//! subscript assignment, which mutates the inherited dict **in place** rather
//! than shadowing it with an instance copy. `num2words2/__init__.py`
//! instantiates every converter at import time, so importing the package
//! rewrites `Num2Word_EUR.CURRENCY_FORMS` for all of its subclasses, SV
//! included.
//!
//! That is why the table below is not what `lang_EUR.py` reads:
//!
//! * EUR is `("euro", "euros")`, not lang_EUR's `("euro", "euro")`.
//! * GBP is `("pound", "pounds")`, not `("pound sterling", "pounds sterling")`.
//! * SAR is `("riyal", "riyals")`, not `("saudi riyal", "saudi riyals")`.
//! * 17 codes absent from lang_EUR (CHF, CNY, KWD, BHD, NGN, …) are present.
//!
//! The 39 entries here are transcribed from the live post-import state, which
//! is what the frozen corpus was generated against. `Num2Word_EN` is the only
//! class that pollutes the EUR dict, and it is instantiated unconditionally,
//! so the state is deterministic — but see the port notes: this is global
//! mutable state and a Python-side reordering would silently invalidate it.
//!
//! `CURRENCY_PRECISION` is *not* affected: EN sets it with `self.X = {...}`
//! (a plain assignment), which binds an instance attribute and leaves
//! `Num2Word_Base.CURRENCY_PRECISION` an empty dict. SV therefore resolves
//! every code to the default divisor of 100 — including the ones that are
//! 3-decimal (KWD/BHD/OMR/…) or 0-decimal (JPY/KRW) elsewhere. Hence
//! `to_currency(12.34, "KWD")` → "tolv dinars, trettiofyra fils" (cents, not
//! mils) and `to_currency(12.34, "JPY")` → "tolv yen, trettiofyra sen"
//! rather than rounding to a whole yen. The corpus confirms both. So
//! `currency_precision` is deliberately left at the trait default.

use crate::base::{set_low_numwords, set_mid_numwords, Cards, Lang, N2WError, Result};
use crate::currency::{CurrencyForms, CurrencyValue};
use crate::floatpath::{default_to_cardinal_float, FloatValue};
use crate::strnum::python_decimal_str;
use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use num_traits::{FromPrimitive, One, Signed, Zero};
use std::collections::HashMap;

/// Port of `Num2Word_EUR.gen_high_numwords`.
///
/// Reimplemented locally rather than imported from `lang_en` so this file
/// stays self-contained (the registry wiring is generated mechanically).
fn gen_high_numwords(units: &[&str], tens: &[&str], lows: &[&str]) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    // Python: [u + t for t in tens for u in units] — `tens` is the outer loop.
    for t in tens {
        for u in units {
            out.push(format!("{}{}", u, t));
        }
    }
    out.reverse();

    const REPLACEMENTS: &[(&str, &str)] = &[
        ("novemn", "noven"),
        ("novemo", "novo"),
        ("octoo", "octo"),
        ("quintd", "quind"),
        ("quintn", "quin"),
        ("quintq", "quinq"),
        ("quints", "quins"),
        ("quintt", "quint"),
        ("quintv", "quinv"),
        ("septenn", "septen"),
        ("septent", "sept"),
        ("sexn", "sen"),
        ("sexs", "ses"),
        ("tresd", "tred"),
        ("tresn", "tren"),
        ("tress", "tres"),
        ("tresv", "trev"),
        ("unno", "uno"),
    ];
    for (k, v) in REPLACEMENTS {
        out = out.iter().map(|o| o.replace(k, v)).collect();
    }
    out.extend(lows.iter().map(|s| s.to_string()));
    out
}

/// Python's `s[-n:]` — by character, and clamping instead of panicking when
/// the string is shorter than `n`. Swedish words carry å/ä/ö, so byte
/// slicing would corrupt them (and can panic mid-codepoint).
fn last_n_chars(s: &str, n: usize) -> String {
    let count = s.chars().count();
    s.chars().skip(count.saturating_sub(n)).collect()
}

/// Python's `s[:-n]` — by character. `n` larger than the string yields "",
/// matching Python (`"ett"[:-4] == ""`), which the `ords` lookup relies on:
/// "ett" is found via its 4-char probe, then sliced by 4 down to "".
fn drop_last_n_chars(s: &str, n: usize) -> String {
    let count = s.chars().count();
    s.chars().take(count.saturating_sub(n)).collect()
}

pub struct LangSv {
    cards: Cards,
    maxval: BigInt,
    ords: HashMap<&'static str, &'static str>,
    exclude_title: Vec<String>,
    /// `CURRENCY_FORMS` as it exists *after* package import — see the module
    /// docs. Built once here; constructing it per call is what made an
    /// earlier revision of this port 10x slower than the Python.
    currency_forms: HashMap<&'static str, CurrencyForms>,
    /// `CURRENCY_ADJECTIVES`, inherited unmodified from `Num2Word_EUR`.
    currency_adjectives: HashMap<&'static str, &'static str>,
}

impl Default for LangSv {
    fn default() -> Self {
        Self::new()
    }
}

impl LangSv {
    pub fn new() -> Self {
        let lows = ["non", "oct", "sept", "sext", "quint", "quadr", "tr", "b", "m"];
        let units = [
            "", "un", "duo", "tres", "quattuor", "quint", "sex", "septen", "octo", "novem",
        ];
        let tens = [
            "dec",
            "vigint",
            "trigint",
            "quadragint",
            "quinquagint",
            "sexagint",
            "septuagint",
            "octogint",
            "nonagint",
        ];
        let mut high = vec!["cent".to_string()];
        high.extend(gen_high_numwords(&units, &tens, &lows));

        let mut cards = Cards::new();

        // SV's set_high_numwords: cap = 3 + 6*len(high); zip(high, range(cap, 3, -6)).
        // Long scale, step -6: each word supplies BOTH a giga (10^n) and a
        // mega (10^(n-3)) card. len(high) == 100, so cap == 603 and the
        // range yields exactly 100 values (603, 597, …, 9) — zip consumes
        // every high word.
        let cap = 3 + 6 * high.len() as i64;
        let mut n = cap;
        for word in high.iter() {
            if n <= 3 {
                break;
            }
            // Both suffixes are non-empty for SV, so both branches always fire.
            cards.insert(
                BigInt::from(10u8).pow(n as u32),
                format!("{}{}", word, "iljarder"),
            );
            cards.insert(
                BigInt::from(10u8).pow((n - 3) as u32),
                format!("{}{}", word, "iljoner"),
            );
            n -= 6;
        }

        set_mid_numwords(
            &mut cards,
            &[
                (1000, "tusen"),
                (100, "hundra"),
                (90, "nittio"),
                (80, "åttio"),
                (70, "sjuttio"),
                (60, "sextio"),
                (50, "femtio"),
                (40, "fyrtio"),
                (30, "trettio"),
            ],
        );
        set_low_numwords(
            &mut cards,
            &[
                "tjugo", "nitton", "arton", "sjutton", "sexton", "femton", "fjorton", "tretton",
                "tolv", "elva", "tio", "nio", "åtta", "sju", "sex", "fem", "fyra", "tre", "två",
                "ett", "noll",
            ],
        );

        // MAXVAL = 1000 * highest card = 1000 * 10^603 = 10^606.
        let maxval = cards.highest().cloned().unwrap_or_else(BigInt::zero) * BigInt::from(1000);

        // Note "tjugo": dead entry, see the module docs. Kept because the
        // Python dict ships it and its absence would be a behaviour change if
        // the lookup were ever widened.
        let ords: HashMap<&str, &str> = [
            ("noll", "nollte"),
            ("ett", "första"),
            ("två", "andra"),
            ("tre", "tredje"),
            ("fyra", "fjärde"),
            ("fem", "femte"),
            ("sex", "sjätte"),
            ("sju", "sjunde"),
            ("åtta", "åttonde"),
            ("nio", "nionde"),
            ("tio", "tionde"),
            ("elva", "elfte"),
            ("tolv", "tolfte"),
            ("tjugo", "tjugonde"),
        ]
        .into_iter()
        .collect();

        // Arity is load-bearing: `pluralize` indexes these, and PLN/RON carry
        // a third form that must not be dropped even though EUR's `pluralize`
        // never reaches index 2.
        let currency_forms: HashMap<&'static str, CurrencyForms> = [
            ("AED", CurrencyForms::new(&["dirham", "dirhams"], &["fils", "fils"])),
            ("AUD", CurrencyForms::new(&["dollar", "dollars"], &["cent", "cents"])),
            ("BHD", CurrencyForms::new(&["dinar", "dinars"], &["fils", "fils"])),
            ("BRL", CurrencyForms::new(&["real", "reais"], &["cent", "cents"])),
            ("BYN", CurrencyForms::new(&["rouble", "roubles"], &["kopek", "kopeks"])),
            ("CAD", CurrencyForms::new(&["dollar", "dollars"], &["cent", "cents"])),
            ("CHF", CurrencyForms::new(&["franc", "francs"], &["rappen", "rappen"])),
            ("CNY", CurrencyForms::new(&["yuan", "yuan"], &["fen", "fen"])),
            ("EEK", CurrencyForms::new(&["kroon", "kroons"], &["sent", "senti"])),
            ("EUR", CurrencyForms::new(&["euro", "euros"], &["cent", "cents"])),
            ("GBP", CurrencyForms::new(&["pound", "pounds"], &["penny", "pence"])),
            ("HKD", CurrencyForms::new(&["dollar", "dollars"], &["cent", "cents"])),
            ("HUF", CurrencyForms::new(&["forint", "forint"], &["fillér", "fillér"])),
            ("INR", CurrencyForms::new(&["rupee", "rupees"], &["paisa", "paise"])),
            ("IQD", CurrencyForms::new(&["dinar", "dinars"], &["fils", "fils"])),
            ("ISK", CurrencyForms::new(&["króna", "krónur"], &["aur", "aurar"])),
            ("JOD", CurrencyForms::new(&["dinar", "dinars"], &["fils", "fils"])),
            ("JPY", CurrencyForms::new(&["yen", "yen"], &["sen", "sen"])),
            ("KRW", CurrencyForms::new(&["won", "won"], &["jeon", "jeon"])),
            ("KWD", CurrencyForms::new(&["dinar", "dinars"], &["fils", "fils"])),
            ("LTL", CurrencyForms::new(&["litas", "litas"], &["cent", "cents"])),
            ("LVL", CurrencyForms::new(&["lat", "lats"], &["santim", "santims"])),
            ("LYD", CurrencyForms::new(&["dinar", "dinars"], &["dirham", "dirhams"])),
            ("MXN", CurrencyForms::new(&["peso", "pesos"], &["cent", "cents"])),
            ("NGN", CurrencyForms::new(&["naira", "naira"], &["kobo", "kobo"])),
            ("NOK", CurrencyForms::new(&["krone", "kroner"], &["øre", "øre"])),
            ("NZD", CurrencyForms::new(&["dollar", "dollars"], &["cent", "cents"])),
            ("OMR", CurrencyForms::new(&["rial", "rials"], &["baisa", "baisa"])),
            ("PLN", CurrencyForms::new(&["zloty", "zlotys", "zlotu"], &["grosz", "groszy"])),
            ("QAR", CurrencyForms::new(&["riyal", "riyals"], &["dirham", "dirhams"])),
            ("RON", CurrencyForms::new(&["leu", "lei", "de lei"], &["ban", "bani", "de bani"])),
            ("RUB", CurrencyForms::new(&["rouble", "roubles"], &["kopek", "kopeks"])),
            ("SAR", CurrencyForms::new(&["riyal", "riyals"], &["halalah", "halalas"])),
            ("SEK", CurrencyForms::new(&["krona", "kronor"], &["öre", "öre"])),
            ("SGD", CurrencyForms::new(&["dollar", "dollars"], &["cent", "cents"])),
            ("TND", CurrencyForms::new(&["dinar", "dinars"], &["millime", "millimes"])),
            ("USD", CurrencyForms::new(&["dollar", "dollars"], &["cent", "cents"])),
            ("UZS", CurrencyForms::new(&["sum", "sums"], &["tiyin", "tiyins"])),
            ("ZAR", CurrencyForms::new(&["rand", "rand"], &["cent", "cents"])),
        ]
        .into_iter()
        .collect();

        let currency_adjectives: HashMap<&'static str, &'static str> = [
            ("AUD", "Australian"),
            ("BYN", "Belarusian"),
            ("CAD", "Canadian"),
            ("EEK", "Estonian"),
            ("HUF", "Hungarian"),
            ("INR", "Indian"),
            ("ISK", "íslenskar"),
            ("JPY", "Japanese"),
            ("KRW", "Korean"),
            ("MXN", "Mexican"),
            ("NOK", "Norwegian"),
            ("RON", "Romanian"),
            ("RUB", "Russian"),
            ("SAR", "Saudi"),
            ("USD", "US"),
            ("UZS", "Uzbekistan"),
        ]
        .into_iter()
        .collect();

        LangSv {
            cards,
            maxval,
            ords,
            exclude_title: vec!["och".into(), "komma".into(), "minus".into()],
            currency_forms,
            currency_adjectives,
        }
    }

    /// Port of `Num2Word_Base.verify_ordinal`. The float check is vacuous for
    /// BigInt input; only the negative check can fire.
    fn verify_ordinal(&self, value: &BigInt) -> Result<()> {
        if value.sign() == num_bigint::Sign::Minus {
            return Err(N2WError::Type(format!(
                "Cannot treat negative num {} as ordinal.",
                value
            )));
        }
        Ok(())
    }

    /// `verify_ordinal` for a float/Decimal: the float check fires first,
    /// then the negative one — both TypeError with Base's default wording.
    /// `-0.0` passes both. Returns the whole value on success.
    fn verify_ordinal_num(&self, value: &FloatValue) -> Result<BigInt> {
        let whole = match value.as_whole_int() {
            Some(i) => i,
            None => {
                return Err(N2WError::Type(format!(
                    "Cannot treat float {} as ordinal.",
                    sv_py_num_str(value)
                )))
            }
        };
        if whole.is_negative() {
            return Err(N2WError::Type(format!(
                "Cannot treat negative num {} as ordinal.",
                sv_py_num_str(value)
            )));
        }
        Ok(whole)
    }

    /// Python's `self.to_cardinal(x)` for an f64 — Base semantics: whole →
    /// integer path, fractional → the base float grammar ("komma" + digits).
    fn cardinal_num_f64(&self, x: f64) -> Result<String> {
        if x.fract() == 0.0 {
            if let Some(i) = BigInt::from_f64(x) {
                return self.to_cardinal(&i);
            }
        }
        default_to_cardinal_float(
            self,
            &FloatValue::Float {
                value: x,
                precision: sv_float_repr_precision(x),
            },
            None,
        )
    }

    /// Python's `self.to_cardinal(x)` for a Decimal.
    fn cardinal_num_dec(&self, x: &BigDecimal) -> Result<String> {
        if x.is_integer() {
            return self.to_cardinal(&x.with_scale(0).as_bigint_and_exponent().0);
        }
        let precision = x.as_bigint_and_exponent().1.max(0) as u32;
        default_to_cardinal_float(
            self,
            &FloatValue::Decimal {
                value: x.clone(),
                precision,
            },
            None,
        )
    }
}

/// `abs(Decimal(repr(f)).as_tuple().exponent)` for an f64. Mirrors the
/// private helper in `floatpath`.
fn sv_float_repr_precision(f: f64) -> u32 {
    let s = format!("{}", f);
    match s.split_once('.') {
        Some((_, frac)) if !frac.contains('e') => frac.len() as u32,
        _ => 0,
    }
}

/// Best-effort `str(number)` for the TypeError messages (the corpus compares
/// exception types only, so the float arm's repr need not be byte-exact).
fn sv_py_num_str(value: &FloatValue) -> String {
    match value {
        FloatValue::Float { value, .. } => format!("{}", value),
        FloatValue::Decimal { value, .. } => python_decimal_str(value),
    }
}

impl Lang for LangSv {
    /// This language's own `to_currency(currency=...)` default,
    /// read from the live Python signature. Only 44 of 156 use EUR.
    fn default_currency(&self) -> &str {
        "EUR"
    }

    /// This language's own `to_currency(separator=...)` default,
    /// read from the live Python signature. Base's is ",", but only
    /// 36 of 149 languages actually use it — most default to " " or a
    /// conjunction, so inheriting Base's comma silently corrupts them.
    fn default_separator(&self) -> &str {
        ","
    }

    fn cards(&self) -> &Cards {
        &self.cards
    }
    fn maxval(&self) -> &BigInt {
        &self.maxval
    }
    fn negword(&self) -> &str {
        "minus "
    }
    fn pointword(&self) -> &str {
        "komma"
    }
    fn exclude_title(&self) -> &[String] {
        &self.exclude_title
    }

    fn merge(&self, l: (&str, &BigInt), r: (&str, &BigInt)) -> (String, BigInt) {
        let (ltext, lnum) = l;
        let (rtext, rnum) = r;
        let hundred = BigInt::from(100);
        let million = BigInt::from(1_000_000);

        if lnum.is_one() && rnum < &hundred {
            (rtext.to_string(), rnum.clone())
        } else if &hundred > lnum && lnum > rnum {
            (format!("{}{}", ltext, rtext), lnum + rnum)
        } else if lnum >= &hundred && &hundred > rnum {
            (format!("{}{}", ltext, rtext), lnum + rnum)
        } else if rnum >= &million && lnum.is_one() {
            // Singularise the plural card word: "miljoner" -> "miljon",
            // "miljarder" -> "miljard". Python: "%s %s" % ("en", rtext[:-2]).
            // Note the `lnum + rnum` — a product is clearly intended; see the
            // module docs, bug 3.
            (format!("en {}", drop_last_n_chars(rtext, 2)), lnum + rnum)
        } else if rnum >= &million && lnum > &BigInt::one() {
            (format!("{} {}", ltext, rtext), lnum + rnum)
        } else if rnum > lnum {
            // Swedish drops "ett" before "hundra" in the hundred-thousand
            // compound: 100000 is "hundratusen", not "etthundratusen".
            if lnum == &hundred && rnum == &BigInt::from(1000) {
                return ("hundratusen".to_string(), BigInt::from(100_000));
            }
            // No triple consonants: "ett" + "tusen" = "etttusen" -> "ettusen";
            // likewise 21000 -> "tjugoettusen". Python's str.replace is
            // replace-all, and so is Rust's.
            (
                format!("{}{}", ltext, rtext).replace("ttt", "tt"),
                lnum * rnum,
            )
        } else {
            (format!("{} {}", ltext, rtext), lnum + rnum)
        }
    }

    fn to_ordinal(&self, value: &BigInt) -> Result<String> {
        self.verify_ordinal(value)?;
        let cardinal = self.to_cardinal(value)?;
        let mut outwords: Vec<String> = cardinal.split(' ').map(|s| s.to_string()).collect();
        // to_cardinal never returns "", so split(' ') always yields >= 1 item.
        let lastword = outwords.last().unwrap().clone();

        // Probe ords by the last 4 chars, then the last 3. Python swallows the
        // KeyErrors; the final fallback is the generic "de" suffix with
        // ending_length left at its initial 0.
        let mut ending_length: usize = 0;
        let lastword_ending: String = match self.ords.get(last_n_chars(&lastword, 4).as_str()) {
            Some(e) => {
                ending_length = 4;
                (*e).to_string()
            }
            None => match self.ords.get(last_n_chars(&lastword, 3).as_str()) {
                Some(e) => {
                    ending_length = 3;
                    (*e).to_string()
                }
                None => "de".to_string(),
            },
        };

        // Python compares the *value* to "de", so an ords entry that happened
        // to equal "de" would also take the no-truncation path. None does.
        let lastword_first_part = if lastword_ending == "de" {
            self.title(&lastword)
        } else {
            drop_last_n_chars(&self.title(&lastword), ending_length)
        };

        let n = outwords.len();
        outwords[n - 1] = format!("{}{}", lastword_first_part, lastword_ending);
        Ok(outwords.join(" "))
    }

    fn to_ordinal_num(&self, value: &BigInt) -> Result<String> {
        // No verify_ordinal call here — negatives pass straight through.
        let s = value.to_string();
        let one = BigInt::one();
        let two = BigInt::from(2);
        let eleven = BigInt::from(11);
        let twelve = BigInt::from(12);

        if value == &one || value == &two {
            Ok(format!("{}:a", s))
        } else if (s.ends_with('1') || s.ends_with('2')) && value != &eleven && value != &twelve {
            Ok(format!("{}:a", s))
        } else if s.ends_with(|c: char| matches!(c, '3'..='9' | '0')) {
            Ok(format!("{}:e", s))
        } else {
            // Unreachable in Python too: identical to the branch above.
            Ok(format!("{}:e", s))
        }
    }

    fn to_year(&self, value: &BigInt) -> Result<String> {
        let thousand = BigInt::from(1000);
        let two_thousand = BigInt::from(2000);

        if value.sign() == num_bigint::Sign::Minus {
            return Ok(format!("{} f.Kr.", self.to_cardinal(&(-value))?));
        }
        if value < &thousand {
            return self.to_cardinal(value);
        }
        if value < &two_thousand {
            // 1000..1999 read as "<century>hundra<remainder>": 1492 ->
            // "fjortonhundranittiotvå". Both operands are non-negative here,
            // so Python's floor division matches a plain BigInt divide.
            let hundred = BigInt::from(100);
            let century = value / &hundred;
            let remainder = value % &hundred;
            if remainder.is_zero() {
                return Ok(format!("{}hundra", self.to_cardinal(&century)?));
            }
            return Ok(format!(
                "{}hundra{}",
                self.to_cardinal(&century)?,
                self.to_cardinal(&remainder)?
            ));
        }
        // 2000+ read as a plain cardinal: "tvåtusentjugofyra".
        self.to_cardinal(value)
    }

    /// `to_ordinal(float/Decimal)`: `verify_ordinal` raises TypeError for
    /// any fractional or negative value; a whole non-negative one flows
    /// through `to_cardinal` + the `ords` suffix table — identical to the
    /// integer ordinal ("5.0" → "femte", `1e+16` → "tio biljarderde").
    fn ordinal_float_entry(&self, value: &FloatValue) -> Result<String> {
        let whole = self.verify_ordinal_num(value)?;
        self.to_ordinal(&whole)
    }

    /// `to_ordinal_num(float/Decimal)` — no verify_ordinal at all:
    ///
    /// ```python
    /// if value == 1 or value == 2:                       str(value) + ":a"
    /// elif str(value).endswith(("1","2")) and value not in (11,12):  ":a"
    /// else:                                              str(value) + ":e"
    /// ```
    ///
    /// Numeric equality plus a *lexical* suffix test on `str(value)` — so
    /// "1.0"/"2.0" take ":a" (== 1/2), "42.0" takes ":e" (ends "0"), and
    /// "1E+2" takes ":a" (ends "2").
    fn ordinal_num_float_entry(&self, value: &FloatValue, repr_str: &str) -> Result<String> {
        let one = BigInt::one();
        let two = BigInt::from(2);
        let eleven = BigInt::from(11);
        let twelve = BigInt::from(12);
        let whole = value.as_whole_int();
        let eq = |n: &BigInt| whole.as_ref() == Some(n);

        if eq(&one) || eq(&two) {
            Ok(format!("{}:a", repr_str))
        } else if (repr_str.ends_with('1') || repr_str.ends_with('2'))
            && !eq(&eleven)
            && !eq(&twelve)
        {
            Ok(format!("{}:a", repr_str))
        } else {
            Ok(format!("{}:e", repr_str))
        }
    }

    /// `to_year(float/Decimal)` — `Num2Word_SV.to_year`'s arithmetic on the
    /// raw value: BC years render the *absolute* cardinal + " f.Kr."
    /// (`-1.5` → "ett komma fem f.Kr."), 1000..1999 splits into
    /// "<century>hundra<remainder>" ("1234.0" → "tolvhundratrettiofyra"),
    /// everything else is a plain cardinal.
    fn year_float_entry(&self, value: &FloatValue) -> Result<String> {
        match value {
            FloatValue::Float { value: v, precision } => {
                let v = *v;
                if v < 0.0 {
                    // to_cardinal(-val) + " f.Kr."
                    let abs = FloatValue::Float {
                        value: -v,
                        precision: *precision,
                    };
                    return Ok(format!("{} f.Kr.", self.cardinal_float_entry(&abs, None)?));
                }
                if v < 1000.0 {
                    return self.cardinal_float_entry(value, None);
                }
                if v < 2000.0 {
                    let century = (v / 100.0).floor();
                    let remainder = v.rem_euclid(100.0);
                    if remainder == 0.0 {
                        return Ok(format!("{}hundra", self.cardinal_num_f64(century)?));
                    }
                    return Ok(format!(
                        "{}hundra{}",
                        self.cardinal_num_f64(century)?,
                        self.cardinal_num_f64(remainder)?
                    ));
                }
                self.cardinal_float_entry(value, None)
            }
            FloatValue::Decimal { value: d, precision } => {
                if d.is_negative() {
                    let abs = FloatValue::Decimal {
                        value: -d.clone(),
                        precision: *precision,
                    };
                    return Ok(format!("{} f.Kr.", self.cardinal_float_entry(&abs, None)?));
                }
                if d < &BigDecimal::from(1000) {
                    return self.cardinal_float_entry(value, None);
                }
                if d < &BigDecimal::from(2000) {
                    let hundred = BigDecimal::from(100);
                    let century =
                        (d / &hundred).with_scale_round(0, bigdecimal::RoundingMode::Down);
                    let remainder = d - &century * &hundred;
                    if remainder.is_zero() {
                        return Ok(format!("{}hundra", self.cardinal_num_dec(&century)?));
                    }
                    return Ok(format!(
                        "{}hundra{}",
                        self.cardinal_num_dec(&century)?,
                        self.cardinal_num_dec(&remainder)?
                    ));
                }
                self.cardinal_float_entry(value, None)
            }
        }
    }

    // ---- currency ----------------------------------------------------
    //
    // `currency_precision` is intentionally NOT overridden: SV resolves
    // `CURRENCY_PRECISION` to `Num2Word_Base`'s empty dict, so every code
    // takes the default divisor of 100. See the module docs.
    //
    // `money_verbose` / `cents_verbose` / `cents_terse` are likewise left
    // alone — SV inherits the base versions, which the trait defaults already
    // mirror (`to_cardinal`, `to_cardinal`, and zero-padded digits at the
    // currency's precision).
    //
    // `to_cheque` is not overridden either: SV inherits
    // `Num2Word_Base.to_cheque` verbatim, which `default_to_cheque` ports.
    // With divisor 100 it renders "<words> AND <NN>/100 <PLURAL UNIT>"
    // upper-cased — e.g. 1234.56 EUR → "ETTUSEN TVÅHUNDRATRETTIOFYRA AND
    // 56/100 EUROS". All nine cheque corpus rows land on that default.

    fn lang_name(&self) -> &str {
        "Num2Word_SV"
    }

    fn currency_forms(&self, code: &str) -> Option<&CurrencyForms> {
        self.currency_forms.get(code)
    }

    fn currency_adjective(&self, code: &str) -> Option<&str> {
        self.currency_adjectives.get(code).copied()
    }

    /// Port of `Num2Word_EUR.pluralize`: `form = 0 if n == 1 else 1`.
    ///
    /// Reached only from the base float path — SV's own int branch open-codes
    /// the same choice instead of calling this.
    fn pluralize(&self, n: &BigInt, forms: &[String]) -> Result<String> {
        let form = if n.is_one() { 0 } else { 1 };
        // Python would raise IndexError on a 1-element tuple with n != 1.
        // Unreachable with SV's table (every entry has >= 2 forms), but the
        // exception *type* is part of the contract if it ever were.
        forms
            .get(form)
            .cloned()
            .ok_or_else(|| N2WError::Index("tuple index out of range".into()))
    }

    /// Port of `Num2Word_SV.to_currency`.
    ///
    /// Only true ints take SV's own branch; everything else is handed to
    /// `Num2Word_Base.to_currency` verbatim, which is what Python's
    /// `super().to_currency(...)` does. The int/non-int split is the whole
    /// point of the override — `1` renders "ett euro" while `1.0` renders
    /// "ett euro, noll cents".
    fn to_currency(
        &self,
        val: &CurrencyValue,
        currency: &str,
        cents: bool,
        separator: Option<&str>,
        adjective: bool,
    ) -> Result<String> {
        // Trait now hands us None when the caller omitted separator=;
        // resolve it to this language's own default before the ported body.
        let separator = separator.unwrap_or(self.default_separator());
        let v = match val {
            CurrencyValue::Int(v) => v,
            // "For floats, use the parent class implementation."
            CurrencyValue::Decimal { value: _, .. } => {
                return crate::currency::default_to_currency(
                    self, val, currency, cents, separator, adjective,
                )
            }
        };

        let forms = match self.currency_forms.get(currency) {
            Some(f) => f,
            // Python catches (KeyError, AttributeError) and re-enters
            // super().to_currency(), whose int branch looks the same code up
            // and raises NotImplementedError. Delegating reproduces that
            // exactly — including the class name in the message — without
            // duplicating the raise.
            None => {
                return crate::currency::default_to_currency(
                    self, val, currency, cents, separator, adjective,
                )
            }
        };

        // `minus_str = self.negword` — un-stripped, unlike every other call
        // site. The trailing space plus the format's own separator is the
        // double-space bug; see the module docs, bug 5.
        let minus_str = if v.is_negative() { self.negword() } else { "" };
        let abs_val = v.abs();
        // Python calls to_cardinal directly here, not _money_verbose. Same
        // result for SV, but kept literal.
        let money_str = self.to_cardinal(&abs_val)?;

        // Open-coded rather than routed through `pluralize`, mirroring the
        // Python. The isinstance(cr1, tuple) guards are vacuous — every entry
        // is a tuple — so this reduces to forms[0] / forms[1], with the
        // len-1 fallback preserved for structural fidelity.
        let currency_str = if abs_val.is_one() {
            &forms.unit[0]
        } else if forms.unit.len() > 1 {
            &forms.unit[1]
        } else {
            &forms.unit[0]
        };

        // `adjective` and `cents`/`separator` are deliberately unused here —
        // Python's int branch ignores all three. See the module docs, bug 6.
        Ok(format!("{} {} {}", minus_str, money_str, currency_str)
            .trim()
            .to_string())
    }
}