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
//! Port of `lang_KN.py` (Kannada), via its `lang_EUR` -> `Num2Word_Base` ancestry.
//!
//! Shape: **engine**. `Num2Word_KN` defines `low_numwords`/`mid_numwords`/
//! `high_numwords` plus `merge`, and lets `Num2Word_Base.to_cardinal` drive
//! `splitnum`/`clean`. So this file builds a `Cards` table in `new()`,
//! implements `merge`, and leaves `to_cardinal` at the trait default.
//!
//! # Inheritance notes
//!
//! `Num2Word_KN.setup()` does **not** call `super().setup()`, so none of
//! `Num2Word_EUR`'s Latin-prefix illion machinery (`gen_high_numwords`, the
//! `cent`/`m`/`b`/`tr` stems) is reachable. KN also overrides
//! `set_high_numwords` to a completely different contract:
//!
//! ```python
//! def set_high_numwords(self, high):
//!     for n, word in self.high_numwords:
//!         self.cards[10**n] = word
//! ```
//!
//! i.e. `high_numwords` holds `(exponent, word)` pairs -- `[(7, crore),
//! (5, lakh), (3, thousand)]` -- rather than EUR's bare word list zipped
//! against a computed exponent range. Note it ignores its `high` parameter and
//! reads `self.high_numwords` directly; same object, so no behavioural
//! difference. The result is the Indian numbering system: crore (10^7), lakh
//! (10^5), thousand (10^3), hundred (10^2), then 0..=99 as literal cards.
//!
//! Inherited unchanged from `Num2Word_Base` (trait defaults are correct):
//!   * `negword` = `"(-) "` -- KN never overrides it, so negative cardinals
//!     read "(-) <word>" (base does `"%s " % negword.strip()`).
//!   * `is_title` = False -> `title()` is the identity.
//!   * `to_year(value)` -> `to_cardinal(value)`, including for negative years;
//!     there is no BC/CE suffix, so `to_year(-500) == to_cardinal(-500)`.
//!
//! # MAXVAL
//!
//! `Num2Word_Base.__init__` sets `MAXVAL = 1000 * list(self.cards.keys())[0]`.
//! The OrderedDict is filled high -> mid -> low, so key[0] is 10^7 and
//! **MAXVAL == 10^10**. Anything `>= 10^10` raises `OverflowError`. Values are
//! therefore bounded in practice, but `BigInt` is kept throughout because the
//! overflow check runs against the caller's arbitrary-precision input.
//!
//! # Unicode: the source is NOT NFC-normalized
//!
//! This is the single biggest hazard in this port. `lang_KN.py` ships a **mix**
//! of precomposed and decomposed Kannada vowel signs, and the decomposed spellings
//! are load-bearing for byte-for-byte output parity. Eight card words are
//! decomposed in the source: 11, 15, 17, 41, 45, 81, 90, and -- most visibly --
//! 10^7 (crore), whose second vowel is stored as U+0CCA U+0CD5 rather than the
//! precomposed U+0CCB. Crore appears in every 10^7+ result, so normalizing it
//! would silently break a large slice of the corpus while still *rendering*
//! identically on screen.
//!
//! Every Kannada string below is therefore written as explicit `\u{...}`
//! escapes, generated mechanically from the Python source. Escapes are pure
//! ASCII and survive editors/tools that would otherwise silently NFC-normalize
//! literal Kannada text (which is exactly what happened on the first draft of
//! this file). The human-readable spelling of each entry lives in the trailing
//! comment, where it is inert. **Do not "clean up" these escapes into literals.**
//!
//! Reassuringly, NFC does not change the *final* character of any card word, so
//! the `merge`/`to_ordinal` modifier tests below behave the same either way --
//! only the emitted bytes differ.
//!
//! # Faithfully reproduced Python quirks
//!
//! This is a port, not a rewrite. All of the following are exactly what Python
//! emits, verified against the interpreter over every card boundary in 0..10^10:
//!
//! 1. **`modifiers[3]` is dead code.** The list has 15 entries, but entry 3 is
//!    the *decomposed* two-codepoint sequence U+0CBF U+0CD5 rather than the
//!    precomposed U+0CC0 that entry 4 already holds. Both `merge` and
//!    `to_ordinal` test membership with a **single character**
//!    (`ltext[-1] in self.modifiers` / `outwords[-1] in ...`), and a one-char
//!    string can never equal a two-char one, so entry 3 can never match. It is
//!    preserved verbatim anyway: dropping it would be behaviourally identical,
//!    but keeping it documents the source. This is why [`MODIFIERS`] is
//!    `[&str; 15]` and not `[char; 15]`.
//!
//! 2. **`to_ordinal` truncates a real letter, not just a vowel sign.** Stripping
//!    the trailing modifier from the cardinal for 10^7 removes the final vowel
//!    of "koti", yielding "...kota-ne" instead of "...koti-ne". Same for every
//!    crore value. Wrong-looking, but it is the spec.
//!
//! 3. **`to_ordinal_num` prepends the digits to the full ordinal *words***:
//!    `"%s%s" % (value, self.to_ordinal(value))`, so `to_ordinal_num(100)` is
//!    the numeral "100" glued directly onto the entire spelled-out ordinal with
//!    no separator. Most languages return a short suffix here (EN gives
//!    "100th"); KN does not.
//!
//! 4. **Typos and inconsistent spacing in `low_numwords`, kept verbatim.** The
//!    30s are especially ragged: 36/37/38 are missing the second "ta" that every
//!    other thirty-something carries. Spacing is arbitrary across the table --
//!    93/94 use a bare consonant plus a space (no virama), 83/84 use virama plus
//!    space, 53/54 run the words together with no space at all, while 22..=25 use
//!    virama plus space. Consequently `to_cardinal(31)` is joined but
//!    `to_cardinal(21)` is spaced. Do not normalise any of it.
//!
//! 5. **The `"%s-%s"` hyphen branch of `merge` is unreachable.** Every value
//!    below 100 has its own card, so `splitnum` always resolves it with `div == 1`
//!    and `merge` takes the first branch instead. The branch is ported anyway to
//!    keep the arm structure identical to Python.
//!
//! 6. **KN's currency table is whatever `lang_EN` last mutated it into.**
//!    See "Currency" below. This is cross-*language* state leakage in the
//!    Python original and it is observable, so it is reproduced.
//!
//! 7. **EN's currency *precisions* do NOT leak, but its *forms* do.** The two
//!    sit in adjacent lines of the same `__init__` and behave differently. See
//!    "Currency" below.
//!
//! # Currency
//!
//! `Num2Word_KN` defines nothing currency-related. It inherits `pluralize` from
//! `Num2Word_EUR` and the whole `to_currency`/`to_cheque` machinery from
//! `Num2Word_Base`. The tables, however, are not what `lang_EUR.py` reads like.
//!
//! `Num2Word_EN.__init__` runs this:
//!
//! ```python
//! self.CURRENCY_FORMS["GBP"] = (("pound", "pounds"), ("penny", "pence"))
//! self.CURRENCY_FORMS["CHF"] = (("franc", "francs"), ("rappen", "rappen"))
//! ...
//! self.CURRENCY_PRECISION = {"BHD": 1000, "KWD": 1000, ...}
//! ```
//!
//! `Num2Word_EN` declares no `CURRENCY_FORMS` of its own, so `self.CURRENCY_FORMS`
//! resolves up the MRO to **`Num2Word_EUR`'s dict object** and `__setitem__`
//! mutates it **in place**. Every `Num2Word_EUR` subclass -- KN included --
//! shares that one dict, so EN's edits are permanently visible to KN. Because
//! `num2words2/__init__.py` instantiates every converter at import time,
//! `Num2Word_EN()` has always run before any KN call, and the table is fully
//! mutated by the time `to_currency` reads it. Net effect for KN:
//!
//!   * 39 codes instead of EUR's 23 (CHF/CNY/KWD/BHD/AED/... arrive from EN).
//!   * `EUR` is `("euro", "euros")`, not EUR's own `("euro", "euro")`.
//!   * `GBP` is `("pound", "pounds")`, not `("pound sterling", "pounds sterling")`.
//!   * `SAR` is `("riyal", "riyals")`, not `("saudi riyal", "saudi riyals")`.
//!
//! The **last** line is different in kind: `self.CURRENCY_PRECISION = {...}` is
//! an *assignment*, not a mutation, so it binds an instance attribute on the EN
//! object alone and never touches the class. KN therefore keeps
//! `Num2Word_Base.CURRENCY_PRECISION == {}` and gets divisor 100 for every
//! code -- including the 3-decimal (KWD/BHD) and 0-decimal (JPY) currencies.
//! Both halves of this asymmetry are confirmed against the frozen corpus.
//!
//! [`CURRENCY_FORMS`] and [`CURRENCY_ADJECTIVES`] below were therefore dumped
//! from a live interpreter after a full `import num2words2`, not transcribed
//! from `lang_EUR.py`. Re-deriving them from the source file would silently
//! produce a different (and wrong) table. `CURRENCY_ADJECTIVES` happens to be
//! unmutated, but is dumped the same way so the two cannot drift apart.
//!
//! # Errors
//!
//! KN raises only what the base class raises: `OverflowError` for
//! `abs(value) >= 10^10` (from `to_cardinal`), and `TypeError` from
//! `verify_ordinal` for negative ordinals. There are no crash-style
//! Index/Key/Value paths in this module.

use crate::base::{set_low_numwords, set_mid_numwords, Cards, Lang, N2WError, Result};
use crate::currency::CurrencyForms;
use crate::floatpath::FloatValue;
use num_bigint::BigInt;
use num_traits::{One, Signed, Zero};
use std::collections::HashMap;

// --- Data tables ---------------------------------------------------------
//
// Generated mechanically from lang_KN.py. Escapes, not literals -- see the
// "Unicode" section in the module docs above before touching these.

const LOW_NUMWORDS: [&str; 100] = [
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 99 = ತೊಂಬತ್ತೊಂಬತ್ತು
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0c82}\u{0c9f}\u{0cc1}", // 98 = ತೊಂಬತ್ತೆಂಟು
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc7}\u{0cb3}\u{0cc1}", // 97 = ತೊಂಬತ್ತೇಳು
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cbe}\u{0cb0}\u{0cc1}", // 96 = ತೊಂಬತ್ತಾರು
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc8}\u{0ca6}\u{0cc1}", // 95 = ತೊಂಬತ್ತೈದು
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0020}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 94 = ತೊಂಬತ್ತ ನಾಲ್ಕು
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0020}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 93 = ತೊಂಬತ್ತ ಮೂರು
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0cb0}\u{0ca1}\u{0cc1}", // 92 = ತೊಂಬತ್ತೆರಡು
    "\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0ca6}\u{0cc1}", // 91 = ತೊಂಬತ್ತೊಂದು
    "\u{0ca4}\u{0cc6}\u{0cc2}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 90 = ತೊಂಬತ್ತು (decomposed)
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 89 = ಎಂಬತ್ತೊಂಬತ್ತು
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0c82}\u{0c9f}\u{0cc1}", // 88 = ಎಂಬತ್ತೆಂಟು
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc7}\u{0cb3}\u{0cc1}", // 87 = ಎಂಬತ್ತೇಳು
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cbe}\u{0cb0}\u{0cc1}", // 86 = ಎಂಬತ್ತಾರು
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc8}\u{0ca6}\u{0cc1}", // 85 = ಎಂಬತ್ತೈದು
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 84 = ಎಂಬತ್ತ್ ನಾಲ್ಕು
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 83 = ಎಂಬತ್ತ್ ಮೂರು
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0cb0}\u{0ca1}\u{0cc1}", // 82 = ಎಂಬತ್ತೆರಡು
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0cc2}\u{0c82}\u{0ca6}\u{0cc1}", // 81 = ಎಂಬತ್ತೊಂದು (decomposed)
    "\u{0c8e}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 80 = ಎಂಬತ್ತು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 79 = ಎಪ್ಪತ್ತೊಂಬತ್ತು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0c82}\u{0c9f}\u{0cc1}", // 78 = ಎಪ್ಪತ್ತೆಂಟು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc7}\u{0cb3}\u{0cc1}", // 77 = ಎಪ್ಪತ್ತೇಳು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cbe}\u{0cb0}\u{0cc1}", // 76 = ಎಪ್ಪತ್ತಾರು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc8}\u{0ca6}\u{0cc1}", // 75 = ಎಪ್ಪತ್ತೈದು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 74 = ಎಪ್ಪತ್ತ್ ನಾಲ್ಕು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 73 = ಎಪ್ಪತ್ತ್ ಮೂರು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0cb0}\u{0ca1}\u{0cc1}", // 72 = ಎಪ್ಪತ್ತೆರಡು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0ca6}\u{0cc1}", // 71 = ಎಪ್ಪತ್ತೊಂದು
    "\u{0c8e}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 70 = ಎಪ್ಪತ್ತು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 69 = ಅರವತ್ತೊಂಬತ್ತು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0c82}\u{0c9f}\u{0cc1}", // 68 = ಅರವತ್ತೆಂಟು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc7}\u{0cb3}\u{0cc1}", // 67 = ಅರವತ್ತೇಳು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cbe}\u{0cb0}\u{0cc1}", // 66 = ಅರವತ್ತಾರು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc8}\u{0ca6}\u{0cc1}", // 65 = ಅರವತ್ತೈದು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 64 = ಅರವತ್ತ್ ನಾಲ್ಕು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 63 = ಅರವತ್ತ್ ಮೂರು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0cb0}\u{0ca1}\u{0cc1}", // 62 = ಅರವತ್ತೆರಡು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0ca6}\u{0cc1}", // 61 = ಅರವತ್ತೊಂದು
    "\u{0c85}\u{0cb0}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 60 = ಅರವತ್ತು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 59 = ಐವತ್ತೊಂಬತ್ತು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0c82}\u{0c9f}\u{0cc1}", // 58 = ಐವತ್ತೆಂಟು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc7}\u{0cb3}\u{0cc1}", // 57 = ಐವತ್ತೇಳು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cbe}\u{0cb0}\u{0cc1}", // 56 = ಐವತ್ತಾರು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc8}\u{0ca6}\u{0cc1}", // 55 = ಐವತ್ತೈದು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 54 = ಐವತ್ತ್ನಾಲ್ಕು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 53 = ಐವತ್ತಮೂರು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0cb0}\u{0ca1}\u{0cc1}", // 52 = ಐವತ್ತೆರಡು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0ca6}\u{0cc1}", // 51 = ಐವತ್ತೊಂದು
    "\u{0c90}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 50 = ಐವತ್ತು
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 49 = ನಲವತ್ತೊಂಬತ್ತು
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0c82}\u{0c9f}\u{0cc1}", // 48 = ನಲವತ್ತೆಂಟು
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc7}\u{0cb3}\u{0cc1}", // 47 = ನಲವತ್ತೇಳು
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cbe}\u{0cb0}\u{0cc1}", // 46 = ನಲವತ್ತಾರು
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0cd6}\u{0ca6}\u{0cc1}", // 45 = ನಲವತ್ತೈದು (decomposed)
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 44 = ನಲವತ್ತ್ ನಾಲ್ಕು
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 43 = ನಲವತ್ತ್ ಮೂರು
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0c8e}\u{0cb0}\u{0ca1}\u{0cc1}", // 42 = ನಲವತ್ತ್ ಎರಡು
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc6}\u{0cc2}\u{0c82}\u{0ca6}\u{0cc1}", // 41 = ನಲವತ್ತೊಂದು (decomposed)
    "\u{0ca8}\u{0cb2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 40 = ನಲವತ್ತು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0c92}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 39 = ಮೂವತ್ತ್ ಒಂಬತ್ತು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0c8e}\u{0c82}\u{0c9f}\u{0cc1}", // 38 = ಮೂವತ್ಎಂಟು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0c8f}\u{0cb3}\u{0cc1}", // 37 = ಮೂವತ್ಏಳು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0c86}\u{0cb0}\u{0cc1}", // 36 = ಮೂವತ್ಆರು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0c90}\u{0ca6}\u{0cc1}", // 35 = ಮೂವತ್ತ್ ಐದು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 34 = ಮೂವತ್ತ್ ನಾಲ್ಕು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 33 = ಮೂವತ್ತ್ ಮೂರು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0c8e}\u{0cb0}\u{0ca1}\u{0cc1}", // 32 = ಮೂವತ್ತ್ಎರಡು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0c92}\u{0c82}\u{0ca6}\u{0cc1}", // 31 = ಮೂವತ್ತ್ಒಂದು
    "\u{0cae}\u{0cc2}\u{0cb5}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 30 = ಮೂವತ್ತು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0c92}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 29 = ಇಪ್ಪತ್ತ್ಒಂಬತ್ತು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0c8e}\u{0c82}\u{0c9f}\u{0cc1}", // 28 = ಇಪ್ಪತ್ತ್ಎಂಟು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0c8f}\u{0cb3}\u{0cc1}", // 27 = ಇಪ್ಪತ್ತ್ಏಳು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0c86}\u{0cb0}\u{0cc1}", // 26 = ಇಪ್ಪತ್ತ್ಆರು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0c90}\u{0ca6}\u{0cc1}", // 25 = ಇಪ್ಪತ್ತ್ ಐದು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 24 = ಇಪ್ಪತ್ತ್ ನಾಲ್ಕು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 23 = ಇಪ್ಪತ್ತ್ ಮೂರು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0c8e}\u{0cb0}\u{0ca1}\u{0cc1}", // 22 = ಇಪ್ಪತ್ತ್ ಎರಡು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0ccd}\u{0020}\u{0c92}\u{0c82}\u{0ca6}\u{0cc1}", // 21 = ಇಪ್ಪತ್ತ್ ಒಂದು
    "\u{0c87}\u{0caa}\u{0ccd}\u{0caa}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 20 = ಇಪ್ಪತ್ತು
    "\u{0cb9}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cca}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 19 = ಹತ್ತೊಂಬತ್ತು
    "\u{0cb9}\u{0ca6}\u{0cbf}\u{0ca8}\u{0cc6}\u{0c82}\u{0c9f}\u{0cc1}", // 18 = ಹದಿನೆಂಟು
    "\u{0cb9}\u{0ca6}\u{0cbf}\u{0ca8}\u{0cc6}\u{0cd5}\u{0cb3}\u{0cc1}", // 17 = ಹದಿನೇಳು (decomposed)
    "\u{0cb9}\u{0ca6}\u{0cbf}\u{0ca8}\u{0cbe}\u{0cb0}\u{0cc1}", // 16 = ಹದಿನಾರು
    "\u{0cb9}\u{0ca6}\u{0cbf}\u{0ca8}\u{0cc6}\u{0cd6}\u{0ca6}\u{0cc1}", // 15 = ಹದಿನೈದು (decomposed)
    "\u{0cb9}\u{0ca6}\u{0cbf}\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 14 = ಹದಿನಾಲ್ಕು
    "\u{0cb9}\u{0ca6}\u{0cbf}\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 13 = ಹದಿಮೂರು
    "\u{0cb9}\u{0ca8}\u{0ccd}\u{0ca8}\u{0cc6}\u{0cb0}\u{0ca1}\u{0cc1}", // 12 = ಹನ್ನೆರಡು
    "\u{0cb9}\u{0ca8}\u{0ccd}\u{0ca8}\u{0cc6}\u{0cc2}\u{0c82}\u{0ca6}\u{0cc1}", // 11 = ಹನ್ನೊಂದು (decomposed)
    "\u{0cb9}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 10 = ಹತ್ತು
    "\u{0c92}\u{0c82}\u{0cac}\u{0ca4}\u{0ccd}\u{0ca4}\u{0cc1}", // 9 = ಒಂಬತ್ತು
    "\u{0c8e}\u{0c82}\u{0c9f}\u{0cc1}", // 8 = ಎಂಟು
    "\u{0c8f}\u{0cb3}\u{0cc1}", // 7 = ಏಳು
    "\u{0c86}\u{0cb0}\u{0cc1}", // 6 = ಆರು
    "\u{0c90}\u{0ca6}\u{0cc1}", // 5 = ಐದು
    "\u{0ca8}\u{0cbe}\u{0cb2}\u{0ccd}\u{0c95}\u{0cc1}", // 4 = ನಾಲ್ಕು
    "\u{0cae}\u{0cc2}\u{0cb0}\u{0cc1}", // 3 = ಮೂರು
    "\u{0c8e}\u{0cb0}\u{0ca1}\u{0cc1}", // 2 = ಎರಡು
    "\u{0c92}\u{0c82}\u{0ca6}\u{0cc1}", // 1 = ಒಂದು
    "\u{0cb8}\u{0cca}\u{0ca8}\u{0ccd}\u{0ca8}\u{0cc6}", // 0 = ಸೊನ್ನೆ
];

const MODIFIERS: [&str; 15] = [
    "\u{0ccd}", // [0] ್
    "\u{0cbe}", // [1] ಾ
    "\u{0cbf}", // [2] ಿ
    "\u{0cbf}\u{0cd5}", // [3] ೀ  <- two codepoints: never matches a single-char test
    "\u{0cc0}", // [4] ೀ
    "\u{0cc1}", // [5] ು
    "\u{0cc2}", // [6] ೂ
    "\u{0cc3}", // [7] ೃ
    "\u{0cc6}", // [8] ೆ
    "\u{0cc7}", // [9] ೇ
    "\u{0cc8}", // [10] ೈ
    "\u{0cca}", // [11] ೊ
    "\u{0ccb}", // [12] ೋ
    "\u{0ccc}", // [13] ೌ
    "\u{0cd5}", // [14] ೕ
];

const ORDINAL_SUFFIX: &str = "\u{0ca8}\u{0cc7}"; // ನೇ
const GENITIVE: &str = "\u{0ca6}"; // ದ
const POINTWORD: &str = "\u{0cac}\u{0cbf}\u{0c82}\u{0ca6}\u{0cc1}"; // ಬಿಂದು
const HUNDRED: &str = "\u{0ca8}\u{0cc2}\u{0cb0}\u{0cc1}"; // ನೂರು
const THOUSAND: &str = "\u{0cb8}\u{0cbe}\u{0cb5}\u{0cbf}\u{0cb0}"; // ಸಾವಿರ
const LAKH: &str = "\u{0cb2}\u{0c95}\u{0ccd}\u{0cb7}"; // ಲಕ್ಷ
const CRORE: &str = "\u{0c95}\u{0cca}\u{0cd5}\u{0c9f}\u{0cbf}"; // ಕೋಟಿ (decomposed)

// --- Currency ------------------------------------------------------------
//
// See the "Currency" section of the module docs: this is `Num2Word_EUR`'s
// CURRENCY_FORMS *as mutated in place by `Num2Word_EN.__init__`*, which is the
// dict KN actually reads at call time. Dumped from the live interpreter
// (`CONVERTER_CLASSES["kn"].CURRENCY_FORMS`) rather than transcribed from
// lang_EUR.py, because lang_EUR.py is not what KN ends up seeing.
//
// Non-ASCII is escaped for the same reason the card tables are (see "Unicode"
// above): the escapes survive tools that would NFC-normalize literals.

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

/// `Num2Word_EUR.CURRENCY_ADJECTIVES` -- 16 entries, **not** mutated by anyone
/// (verified against the live interpreter: it is still EUR's dict, unchanged).
///
/// A `const` slice rather than a map: it is consulted only when
/// `adjective=true`, and 16 entries make a linear scan cheaper than hashing.
/// Being `const` it also costs nothing to construct.
const CURRENCY_ADJECTIVES: [(&str, &str); 16] = [
    ("AUD", "Australian"),
    ("BYN", "Belarusian"),
    ("CAD", "Canadian"),
    ("EEK", "Estonian"),
    ("HUF", "Hungarian"),
    ("INR", "Indian"),
    ("ISK", "\u{00ed}slenskar"), // islenskar
    ("JPY", "Japanese"),
    ("KRW", "Korean"),
    ("MXN", "Mexican"),
    ("NOK", "Norwegian"),
    ("RON", "Romanian"),
    ("RUB", "Russian"),
    ("SAR", "Saudi"),
    ("USD", "US"),
    ("UZS", "Uzbekistan"),
];

// --- Helpers -------------------------------------------------------------

/// Python's `x[-1] in self.modifiers`.
///
/// Python indexes by *character*, so this takes the final `char` and tests it
/// against the table as a one-character string. An empty `s` would be an
/// `IndexError` in Python; neither call site can produce one (every card word is
/// non-empty and `merge` only ever concatenates non-empty text), so `false` here
/// is unreachable rather than a papered-over divergence.
fn ends_with_modifier(s: &str) -> bool {
    match s.chars().next_back() {
        Some(c) => {
            let mut buf = [0u8; 4];
            let last: &str = c.encode_utf8(&mut buf);
            MODIFIERS.contains(&last)
        }
        None => false,
    }
}

/// Python's `x[:-1]` -- drop the final *character*, not the final byte.
fn strip_last_char(s: &str) -> String {
    let mut it = s.chars();
    it.next_back();
    it.as_str().to_string()
}

// --- Language ------------------------------------------------------------

pub struct LangKn {
    cards: Cards,
    maxval: BigInt,
    /// Built once in `new()`. Python resolves `self.CURRENCY_FORMS[code]` via a
    /// dict lookup on a table that already exists; rebuilding this per call
    /// would be pure overhead (and made an earlier revision of this port an
    /// order of magnitude slower than the Python it replaces).
    currency_forms: HashMap<&'static str, CurrencyForms>,
}

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

impl LangKn {
    pub fn new() -> Self {
        let mut cards = Cards::new();

        // KN's set_high_numwords:
        //     for n, word in self.high_numwords: self.cards[10**n] = word
        // with high_numwords = [(7, crore), (5, lakh), (3, thousand)].
        for (n, word) in [(7u32, CRORE), (5, LAKH), (3, THOUSAND)] {
            cards.insert(BigInt::from(10u8).pow(n), word);
        }

        set_mid_numwords(&mut cards, &[(100, HUNDRED)]);
        set_low_numwords(&mut cards, &LOW_NUMWORDS);

        // Num2Word_Base.__init__: MAXVAL = 1000 * list(self.cards.keys())[0].
        // The OrderedDict is filled high -> mid -> low, so key[0] is the highest
        // card (10^7) and MAXVAL lands on 10^10.
        let maxval = cards.highest().cloned().unwrap_or_else(BigInt::zero) * BigInt::from(1000);

        let currency_forms = CURRENCY_FORMS
            .iter()
            .map(|(code, unit, subunit)| (*code, CurrencyForms::new(unit, subunit)))
            .collect();

        LangKn {
            cards,
            maxval,
            currency_forms,
        }
    }

    /// Port of `Num2Word_Base.verify_ordinal`.
    ///
    /// The float check (`errmsg_floatord`) cannot fire on integer input, so only
    /// the negative check is modelled.
    fn verify_ordinal(&self, value: &BigInt) -> Result<()> {
        if value.is_negative() {
            return Err(N2WError::Type(format!(
                "Cannot treat negative num {} as ordinal.",
                value
            )));
        }
        Ok(())
    }
}

impl Lang for LangKn {
    /// 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
    }

    // negword: KN does not override Num2Word_Base's "(-) " -> trait default.
    // is_title / exclude_title: KN does not override -> trait defaults.

    fn pointword(&self) -> &str {
        "ಬಿಂದು"
    }

    /// Port of `Num2Word_KN.merge`.
    ///
    /// ```python
    /// if lnum == 1 and rnum < 100:  return (rtext, rnum)
    /// elif 100 > lnum > rnum:       return ("%s-%s" % (ltext, rtext), lnum + rnum)
    /// elif lnum >= 100 > rnum:
    ///     if ltext[-1] in self.modifiers:
    ///         return ("%s %s" % (ltext[:-1], rtext), lnum + rnum)
    ///     else:
    ///         return ("%s %s" % (ltext + GENITIVE, rtext), lnum + rnum)
    /// elif rnum > lnum:             return ("%s %s" % (ltext, rtext), lnum * rnum)
    /// return ("%s %s" % (ltext, rtext), lnum + rnum)
    /// ```
    ///
    /// The third arm drives the sandhi: a left word ending in a vowel sign loses
    /// it (100 + 1 -> "ondu nura ondu"), otherwise the genitive "da" is suffixed
    /// (2000 + 1 -> "eradu savirada ondu").
    ///
    /// Python's `100 > lnum > rnum` and `lnum >= 100 > rnum` are chained
    /// comparisons; they are expanded here into explicit conjunctions.
    fn merge(&self, l: (&str, &BigInt), r: (&str, &BigInt)) -> (String, BigInt) {
        let (ltext, lnum) = l;
        let (rtext, rnum) = r;
        let hundred = BigInt::from(100);

        if lnum.is_one() && rnum < &hundred {
            (rtext.to_string(), rnum.clone())
        } else if &hundred > lnum && lnum > rnum {
            // Unreachable for KN (every value < 100 is its own card), but ported
            // to keep the arm structure identical. See quirk 5 in the module docs.
            (format!("{}-{}", ltext, rtext), lnum + rnum)
        } else if lnum >= &hundred && &hundred > rnum {
            if ends_with_modifier(ltext) {
                (format!("{} {}", strip_last_char(ltext), rtext), lnum + rnum)
            } else {
                (format!("{}{} {}", ltext, GENITIVE, rtext), lnum + rnum)
            }
        } else if rnum > lnum {
            (format!("{} {}", ltext, rtext), lnum * rnum)
        } else {
            (format!("{} {}", ltext, rtext), lnum + rnum)
        }
    }

    /// Port of `Num2Word_KN.to_ordinal`.
    ///
    /// ```python
    /// self.verify_ordinal(value)
    /// outwords = self.to_cardinal(value)
    /// if outwords[-1] in self.modifiers:
    ///     outwords = outwords[:-1]
    /// return outwords + ORDINAL_SUFFIX
    /// ```
    ///
    /// Strips at most **one** character, unconditionally, whenever the cardinal
    /// ends in a vowel sign -- which is why the crore ordinal is mangled (quirk 2).
    /// Cardinals ending in a bare consonant (2000, 10^5) are left alone and simply
    /// take the suffix.
    fn to_ordinal(&self, value: &BigInt) -> Result<String> {
        self.verify_ordinal(value)?;
        let outwords = self.to_cardinal(value)?;
        let stem = if ends_with_modifier(&outwords) {
            strip_last_char(&outwords)
        } else {
            outwords
        };
        Ok(format!("{}{}", stem, ORDINAL_SUFFIX))
    }

    /// Port of `Num2Word_KN.to_ordinal_num`.
    ///
    /// `"%s%s" % (value, self.to_ordinal(value))` -- the numeral glued to the
    /// *entire* ordinal phrase with no separator (quirk 3).
    ///
    /// `verify_ordinal` runs here *and* again inside `to_ordinal`, exactly as in
    /// Python; the duplicate check is harmless and observationally identical
    /// (same TypeError, same message).
    fn to_ordinal_num(&self, value: &BigInt) -> Result<String> {
        self.verify_ordinal(value)?;
        Ok(format!("{}{}", value, self.to_ordinal(value)?))
    }

    // to_year: KN does not override Num2Word_Base.to_year, which delegates to
    // to_cardinal -> the trait default is correct. There is no BC/CE handling.

    /// `to_ordinal(float/Decimal)` — `verify_ordinal` gates the float domain:
    /// a fractional value raises TypeError ("Cannot treat float %s as
    /// ordinal.") before the negative check ("Cannot treat negative num %s
    /// as ordinal."); -0.0 passes both (`abs(-0.0) == -0.0`). A surviving
    /// whole value takes the integer ordinal path — base `to_cardinal`
    /// routes a whole float through `int(value)` — so `5.00` is "ಐದನೇ" and
    /// `1e+12` still overflows inside `to_cardinal` (MAXVAL is 10**10).
    fn ordinal_float_entry(&self, value: &FloatValue) -> Result<String> {
        let i = verify_ordinal_float(value, &ordinal_float_repr(value))?;
        self.to_ordinal(&i)
    }

    /// `to_ordinal_num(float/Decimal)` — the same gate, then
    /// `"%s%s" % (value, self.to_ordinal(value))`: the repr glued to the
    /// *entire* ordinal phrase ("5.0ಐದನೇ"). `to_ordinal` runs for real, so a
    /// whole value past MAXVAL ("1E+20") raises OverflowError here, unlike
    /// suffix-only languages.
    fn ordinal_num_float_entry(&self, value: &FloatValue, repr_str: &str) -> Result<String> {
        let i = verify_ordinal_float(value, repr_str)?;
        Ok(format!("{}{}", repr_str, self.to_ordinal(&i)?))
    }

    // ---- currency --------------------------------------------------------
    //
    // `Num2Word_KN` defines *nothing* currency-related itself. Everything is
    // inherited: the tables from `Num2Word_EUR` (as mutated at import time --
    // see the module docs), `pluralize` from `Num2Word_EUR`, and
    // `to_currency`/`to_cheque`/`_money_verbose`/`_cents_verbose`/
    // `_cents_terse` from `Num2Word_Base`. So only the three data hooks plus
    // `pluralize` and `lang_name` are overridden here; the rest of the trait
    // defaults already mirror `Num2Word_Base` exactly.

    /// Drives the NotImplementedError text:
    /// `Currency code "XXX" not implemented for "Num2Word_KN"`.
    fn lang_name(&self) -> &str {
        "Num2Word_KN"
    }

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

    fn currency_adjective(&self, code: &str) -> Option<&str> {
        CURRENCY_ADJECTIVES
            .iter()
            .find(|(k, _)| *k == code)
            .map(|(_, v)| *v)
    }

    // currency_precision: deliberately NOT overridden. `Num2Word_KN` inherits
    // `Num2Word_Base.CURRENCY_PRECISION`, which is `{}` -- so
    // `CURRENCY_PRECISION.get(code, 100)` is 100 for *every* code and the trait
    // default (100) is already exact.
    //
    // This looks wrong for KWD/BHD (3-decimal) and JPY (0-decimal), and it is
    // -- but it is what Python does, and the corpus agrees. The reason is quirk
    // 7 in the module docs: `Num2Word_EN.__init__` *assigns*
    // `self.CURRENCY_PRECISION = {...}`, which binds an INSTANCE attribute on
    // the EN object only, whereas its sibling `self.CURRENCY_FORMS[k] = v`
    // lines MUTATE the shared class dict. So EN's forms leak into KN but EN's
    // precisions do not. Consequently KN renders KWD 12.34 as
    // "<12> dinars, <34> fils" (divisor 100, not 1000) and JPY 12.34 with a
    // sen segment instead of rounding to a whole yen. Verified against the
    // frozen corpus for all of JPY/KWD/BHD.

    /// Port of `Num2Word_EUR.pluralize`.
    ///
    /// ```python
    /// def pluralize(self, n, forms):
    ///     form = 0 if n == 1 else 1
    ///     return forms[form]
    /// ```
    ///
    /// Index 2 is never reached, so PLN's "zlotu" and RON's "de lei"/"de bani"
    /// are dead weight for KN -- but the arity is kept (see [`CURRENCY_FORMS`]).
    ///
    /// The out-of-range arm mirrors Python's `IndexError`, message included --
    /// `forms` is a tuple in Python, so the text is exactly
    /// "tuple index out of range". It is unreachable with the tables above
    /// (every entry has >= 2 forms), but `forms` is a caller-supplied slice, so
    /// the failure mode is modelled rather than papered over with a panic or a
    /// silent fallback.
    fn pluralize(&self, n: &BigInt, forms: &[String]) -> Result<String> {
        let form = if n.is_one() { 0 } else { 1 };
        forms
            .get(form)
            .cloned()
            .ok_or_else(|| N2WError::Index("tuple index out of range".into()))
    }

    // money_verbose / cents_verbose: Num2Word_Base's versions just delegate to
    // to_cardinal, which is what the trait defaults do. Not overridden.
    //
    // cents_terse: Num2Word_Base._cents_terse zero-pads to
    // `len(str(divisor)) - 1` digits; with divisor 100 that is "%02d". The
    // trait default calls default_cents_terse(n, self.currency_precision(c)),
    // which is the same function. Not overridden.
    //
    // cardinal_from_decimal: left at the default (raises NotImplemented). It is
    // reached only for fractional cents -- e.g. to_currency(12.345, "EUR"),
    // where Python's base falls through to to_cardinal_float(34.5) and yields
    // "<34> <pointword> <5> cents". That is the float/Decimal cardinal path,
    // which PORTING_CURRENCY.md defers to a later phase; no corpus row reaches
    // it (no corpus currency value has a third decimal). Flagged in the report.
}

/// Python's `Num2Word_Base.verify_ordinal` over the float/Decimal domain:
///
/// ```python
/// if not value == int(value):
///     raise TypeError(self.errmsg_floatord % value)
/// if not abs(value) == value:
///     raise TypeError(self.errmsg_negord % value)
/// ```
///
/// The float check runs first, so `-1.5` reports the *float* message; the
/// negative check compares numerically, so `-0.0` (== `abs(-0.0)`) passes.
/// `repr` is Python's `str(value)`, interpolated verbatim into the message.
fn verify_ordinal_float(value: &FloatValue, repr: &str) -> Result<BigInt> {
    match value.as_whole_int() {
        None => Err(N2WError::Type(format!(
            "Cannot treat float {} as ordinal.",
            repr
        ))),
        Some(i) if i.is_negative() => Err(N2WError::Type(format!(
            "Cannot treat negative num {} as ordinal.",
            repr
        ))),
        Some(i) => Ok(i),
    }
}

/// Best-effort `str(value)` for the TypeError messages raised by
/// [`verify_ordinal_float`] when no repr was handed in (the `to_ordinal`
/// entry). Exact for every reachable message: `str(Decimal)` is the spec
/// transcription, a fractional float's shortest round-trip matches Rust's
/// `Display` in the non-exponent range, and a whole float re-gains its
/// Python ".0" tail. (A whole float >= 1e16 would diverge — Python uses
/// exponent form — but such values pass verification and never reach a
/// message.)
fn ordinal_float_repr(value: &FloatValue) -> String {
    match value {
        FloatValue::Decimal { value, .. } => crate::strnum::python_decimal_str(value),
        FloatValue::Float { value, .. } => {
            if value.is_finite() && value.fract() == 0.0 && value.abs() < 1e16 {
                format!("{:.1}", value)
            } else {
                value.to_string()
            }
        }
    }
}