yosina 1.1.3

Japanese text transliteration library
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
use serde::{Deserialize, Serialize};
use std::borrow::Cow;

use crate::char::{Char, CharPool};
use crate::transliterator::{Transliterator, TransliteratorFactory};
use crate::{TransliterationError, TransliteratorFactoryError};

/// Character type flags for classifying Japanese characters
#[derive(Debug, Clone, Copy, PartialEq)]
struct CharType(u8);

impl CharType {
    const OTHER: CharType = CharType(0x00);
    const HIRAGANA: CharType = CharType(0x20);
    const KATAKANA: CharType = CharType(0x40);
    const ALPHABET: CharType = CharType(0x60);
    const DIGIT: CharType = CharType(0x80);
    const EITHER: CharType = CharType(0xa0);

    const HALFWIDTH: CharType = CharType(1 << 0);
    const VOWEL_ENDED: CharType = CharType(1 << 1);
    const HATSUON: CharType = CharType(1 << 2);
    const SOKUON: CharType = CharType(1 << 3);
    const PROLONGED_SOUND_MARK: CharType = CharType(1 << 4);

    // Compound types
    const HALFWIDTH_DIGIT: CharType = CharType(Self::DIGIT.0 | Self::HALFWIDTH.0);
    const FULLWIDTH_DIGIT: CharType = CharType(Self::DIGIT.0);
    const HALFWIDTH_ALPHABET: CharType = CharType(Self::ALPHABET.0 | Self::HALFWIDTH.0);
    const FULLWIDTH_ALPHABET: CharType = CharType(Self::ALPHABET.0);
    const ORDINARY_HIRAGANA: CharType = CharType(Self::HIRAGANA.0 | Self::VOWEL_ENDED.0);
    const ORDINARY_KATAKANA: CharType = CharType(Self::KATAKANA.0 | Self::VOWEL_ENDED.0);
    const ORDINARY_HALFWIDTH_KATAKANA: CharType =
        CharType(Self::KATAKANA.0 | Self::VOWEL_ENDED.0 | Self::HALFWIDTH.0);

    fn is_alnum(&self) -> bool {
        let masked = self.0 & 0xe0;
        masked == CharType::ALPHABET.0 || masked == CharType::DIGIT.0
    }

    fn is_halfwidth(&self) -> bool {
        self.0 & CharType::HALFWIDTH.0 != 0
    }

    /// Special character mappings for character type detection
    fn get_special_char_type(codepoint: u32) -> Option<Self> {
        match codepoint {
            0xff70 => {
                Some(CharType::KATAKANA | CharType::PROLONGED_SOUND_MARK | CharType::HALFWIDTH)
            }
            0x30fc => Some(CharType::EITHER | CharType::PROLONGED_SOUND_MARK),
            0x3063 => Some(CharType::HIRAGANA | CharType::SOKUON),
            0x3093 => Some(CharType::HIRAGANA | CharType::HATSUON),
            0x30c3 => Some(CharType::KATAKANA | CharType::SOKUON),
            0x30f3 => Some(CharType::KATAKANA | CharType::HATSUON),
            0xff6f => Some(CharType::KATAKANA | CharType::SOKUON | CharType::HALFWIDTH),
            0xff9d => Some(CharType::KATAKANA | CharType::HATSUON | CharType::HALFWIDTH),
            _ => None,
        }
    }

    /// Determine the character type for a given Unicode codepoint
    fn from_codepoint(codepoint: u32) -> Self {
        // Check digits
        if (0x30..=0x39).contains(&codepoint) {
            return CharType::HALFWIDTH_DIGIT;
        }
        if (0xff10..=0xff19).contains(&codepoint) {
            return CharType::FULLWIDTH_DIGIT;
        }

        // Check alphabet
        if (0x41..=0x5a).contains(&codepoint) || (0x61..=0x7a).contains(&codepoint) {
            return CharType::HALFWIDTH_ALPHABET;
        }
        if (0xff21..=0xff3a).contains(&codepoint) || (0xff41..=0xff5a).contains(&codepoint) {
            return CharType::FULLWIDTH_ALPHABET;
        }

        // Check special characters
        if let Some(special_type) = Self::get_special_char_type(codepoint) {
            return special_type;
        }

        // Check hiragana
        if (0x3041..=0x309c).contains(&codepoint) || codepoint == 0x309f {
            return CharType::ORDINARY_HIRAGANA;
        }

        // Check katakana
        if (0x30a1..=0x30fa).contains(&codepoint) || (0x30fd..=0x30ff).contains(&codepoint) {
            return CharType::ORDINARY_KATAKANA;
        }

        // Check halfwidth katakana
        if (0xff66..=0xff6f).contains(&codepoint) || (0xff71..=0xff9f).contains(&codepoint) {
            return CharType::ORDINARY_HALFWIDTH_KATAKANA;
        }

        CharType::OTHER
    }
}

impl std::ops::BitOr for CharType {
    type Output = CharType;

    fn bitor(self, rhs: CharType) -> CharType {
        CharType(self.0 | rhs.0)
    }
}

impl std::ops::BitOrAssign for CharType {
    fn bitor_assign(&mut self, rhs: CharType) {
        self.0 |= rhs.0;
    }
}

/// Options for the prolonged sound marks transliterator
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct ProlongedSoundMarksTransliteratorOptions {
    /// Skip characters that have already been transliterated
    #[serde(default)]
    pub skip_already_transliterated_chars: bool,

    /// Allow prolonged hatsuon (ん/ン) characters
    #[serde(default)]
    pub allow_prolonged_hatsuon: bool,

    /// Allow prolonged sokuon (っ/ッ) characters
    #[serde(default)]
    pub allow_prolonged_sokuon: bool,

    /// Replace prolonged voice marks following an alphanumeric
    #[serde(default)]
    pub replace_prolonged_marks_following_alnums: bool,
}

#[derive(Debug, Clone)]
pub struct ProlongedSoundMarksTransliterator {
    options: ProlongedSoundMarksTransliteratorOptions,
    prolongables: CharType,
}

impl ProlongedSoundMarksTransliterator {
    pub fn new(options: ProlongedSoundMarksTransliteratorOptions) -> Self {
        let mut prolongables = CharType::VOWEL_ENDED | CharType::PROLONGED_SOUND_MARK;

        if options.allow_prolonged_hatsuon {
            prolongables |= CharType::HATSUON;
        }
        if options.allow_prolonged_sokuon {
            prolongables |= CharType::SOKUON;
        }

        Self {
            options,
            prolongables,
        }
    }

    /// Check if a character is a prolonged sound mark or dash
    fn is_prolonged_mark(c: &str) -> bool {
        matches!(
            c,
            "\u{002D}" | // HYPHEN-MINUS
            "\u{2010}" | // HYPHEN
            "\u{2014}" | // EM DASH
            "\u{2015}" | // HORIZONTAL BAR
            "\u{2212}" | // MINUS SIGN
            "\u{FF0D}" | // FULLWIDTH HYPHEN-MINUS
            "\u{FF70}" | // HALFWIDTH KATAKANA PROLONGED SOUND MARK
            "\u{30FC}" // KATAKANA PROLONGED SOUND MARK
        )
    }
}

impl Transliterator for ProlongedSoundMarksTransliterator {
    fn transliterate<'a, 'b>(
        &self,
        pool: &mut CharPool<'a, 'b>,
        chars: &[&'a Char<'a, 'b>],
    ) -> Result<Vec<&'a Char<'a, 'b>>, TransliterationError> {
        let mut result = Vec::new();
        let mut lookahead_buf: Vec<&'a Char<'a, 'b>> = Vec::new();
        let mut last_non_prolonged_char: Option<(&'a Char<'a, 'b>, CharType)> = None;
        let mut processed_chars_in_lookahead = false;
        let mut offset = 0;

        for &current_char in chars {
            // Skip sentinel characters
            if current_char.c.is_empty() {
                result.push(current_char);
                continue;
            }

            // Handle lookahead buffer - when we've accumulated potential prolonged marks
            if !lookahead_buf.is_empty() {
                if Self::is_prolonged_mark(current_char.c.as_ref()) {
                    // Continue accumulating prolonged marks
                    if current_char.source.is_some() {
                        processed_chars_in_lookahead = true;
                    }
                    lookahead_buf.push(current_char);
                } else {
                    // Process the accumulated lookahead buffer
                    let prev_non_prolonged_char = last_non_prolonged_char;
                    let current_codepoint = current_char.c.chars().next().unwrap() as u32;
                    let current_char_type = CharType::from_codepoint(current_codepoint);
                    last_non_prolonged_char = Some((current_char, current_char_type));

                    if (prev_non_prolonged_char.is_none()
                        || prev_non_prolonged_char.unwrap().1.is_alnum())
                        && (!self.options.skip_already_transliterated_chars
                            || !processed_chars_in_lookahead)
                    {
                        let replacement = if match prev_non_prolonged_char {
                            Some(c) => c.1.is_halfwidth(),
                            None => current_char_type.is_halfwidth(),
                        } {
                            "\u{002D}" // HYPHEN-MINUS
                        } else {
                            "\u{FF0D}" // FULLWIDTH HYPHEN-MINUS
                        };

                        // Replace all marks in lookahead buffer with the chosen replacement
                        for &lookahead_char in &lookahead_buf {
                            let new_char = pool.new_char_from(
                                Cow::Borrowed(replacement),
                                offset,
                                lookahead_char,
                            );
                            offset += replacement.len();
                            result.push(new_char);
                        }
                    } else {
                        // Not between alphanumeric characters - preserve original
                        for &lookahead_char in &lookahead_buf {
                            let new_char = pool.new_with_offset(lookahead_char, offset);
                            offset += lookahead_char.c.len();
                            result.push(new_char);
                        }
                    }
                    lookahead_buf.clear();
                    result.push(current_char);
                    processed_chars_in_lookahead = false;
                }
                continue;
            }

            // Check if current character is a prolonged mark
            if Self::is_prolonged_mark(current_char.c.as_ref()) {
                // Check if we should process this prolonged mark
                let should_process = !self.options.skip_already_transliterated_chars
                    || current_char.source.is_none();
                if should_process {
                    if let Some(last_non_prolonged_char) = last_non_prolonged_char {
                        let (_last_char, last_char_type) = last_non_prolonged_char;
                        // Check if character is suitable for prolonged sound mark replacement
                        if (self.prolongables.0 & last_char_type.0) != 0 {
                            // Japanese character that can be prolonged
                            let replacement = if last_char_type.is_halfwidth() {
                                "\u{FF70}" // HALFWIDTH KATAKANA PROLONGED SOUND MARK
                            } else {
                                "\u{30FC}" // KATAKANA PROLONGED SOUND MARK
                            };

                            let nc = pool.new_char_from(
                                Cow::Borrowed(replacement),
                                offset,
                                current_char,
                            );
                            offset += replacement.len();
                            result.push(nc);
                            continue;
                        } else {
                            // Not a Japanese character
                            if self.options.replace_prolonged_marks_following_alnums
                                && last_char_type.is_alnum()
                            {
                                lookahead_buf.push(current_char);
                                continue;
                            }
                        }
                    }
                }
            } else {
                // Regular character - update last non-prolonged character
                let codepoint = current_char.c.chars().next().unwrap() as u32;
                let char_type = CharType::from_codepoint(codepoint);
                last_non_prolonged_char = Some((current_char, char_type));
            }

            // Default: preserve the original character
            result.push(pool.new_with_offset(current_char, offset));
        }

        Ok(result)
    }
}

impl TransliteratorFactory for ProlongedSoundMarksTransliteratorOptions {
    fn new_transliterator(&self) -> Result<Box<dyn Transliterator>, TransliteratorFactoryError> {
        Ok(Box::new(ProlongedSoundMarksTransliterator::new(
            self.clone(),
        )))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::char::{from_chars, CharPool};

    #[test]
    fn test_basic_prolonged_sound_replacement() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test basic hiragana prolonged sound replacement
        let input_chars = pool.build_char_array("かー");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should replace ー with ー (normalized prolonged sound mark)
        assert_eq!(result.len(), 3); // か + ー + sentinel
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{30FC}"); // KATAKANA PROLONGED SOUND MARK
        assert!(result[2].c.is_empty()); // sentinel
    }

    #[test]
    fn test_katakana_prolonged_sound_replacement() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test katakana prolonged sound replacement
        let input_chars = pool.build_char_array("カー");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 3);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{30FC}"); // KATAKANA PROLONGED SOUND MARK
        assert!(result[2].c.is_empty()); // sentinel
    }

    #[test]
    fn test_halfwidth_katakana_prolonged_sound() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test halfwidth katakana - should use halfwidth prolonged mark
        let input_chars = pool.build_char_array("カー");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 3);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{FF70}"); // HALFWIDTH KATAKANA PROLONGED SOUND MARK
        assert!(result[2].c.is_empty()); // sentinel
    }

    #[test]
    fn test_hyphen_replacement_between_alphabet() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            replace_prolonged_marks_following_alnums: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test hyphen between alphabetic characters
        let input_chars = pool.build_char_array("a-b");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 4);
        assert_eq!(result[0].c, "a");
        assert_eq!(result[1].c, "\u{002D}"); // HYPHEN-MINUS (halfwidth)
        assert_eq!(result[2].c, "b");
        assert!(result[3].c.is_empty()); // sentinel
    }

    #[test]
    fn test_em_dash_replacement_between_fullwidth() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            replace_prolonged_marks_following_alnums: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test em dash between fullwidth characters
        let input_chars = pool.build_char_array("a—b");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 4);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{FF0D}"); // FULLWIDTH HYPHEN-MINUS
        assert_eq!(result[2].c, "");
        assert!(result[3].c.is_empty()); // sentinel
    }

    #[test]
    fn test_allow_prolonged_hatsuon() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            allow_prolonged_hatsuon: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test prolonged hatsuon (ん)
        let input_chars = pool.build_char_array("んー");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 3);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{30FC}"); // Should be replaced
        assert!(result[2].c.is_empty()); // sentinel
    }

    #[test]
    fn test_allow_prolonged_sokuon() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            allow_prolonged_sokuon: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test prolonged sokuon (っ)
        let input_chars = pool.build_char_array("っー");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 3);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{30FC}"); // Should be replaced
        assert!(result[2].c.is_empty()); // sentinel
    }

    #[test]
    fn test_no_replacement_for_non_japanese() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test that non-Japanese characters don't trigger replacement
        let input_chars = pool.build_char_array("x-y");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should preserve original hyphen
        assert_eq!(result.len(), 4);
        assert_eq!(result[0].c, "x");
        assert_eq!(result[1].c, "-"); // Original hyphen preserved
        assert_eq!(result[2].c, "y");
        assert!(result[3].c.is_empty()); // sentinel
    }

    #[test]
    fn test_multiple_dashes_replacement() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            replace_prolonged_marks_following_alnums: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test multiple consecutive dashes
        let input_chars = pool.build_char_array("a--b");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 5);
        assert_eq!(result[0].c, "a");
        assert_eq!(result[1].c, "\u{002D}"); // Both dashes should be replaced
        assert_eq!(result[2].c, "\u{002D}");
        assert_eq!(result[3].c, "b");
        assert!(result[4].c.is_empty()); // sentinel
    }

    #[test]
    fn test_skip_already_transliterated() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            skip_already_transliterated_chars: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Create a character that has a source (already transliterated)
        let input_chars = pool.build_char_array("");
        let source_char = input_chars[0];

        // Create a new character with a dash that has source tracking
        let dash_char = pool.new_char_from(Cow::Borrowed("-"), 1, source_char);

        let chars_with_source = vec![source_char, dash_char, input_chars[1]]; // include sentinel
        let result = transliterator
            .transliterate(&mut pool, &chars_with_source)
            .unwrap();

        // The dash should not be processed because it has source tracking
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "-"); // Should be preserved
        assert!(result[2].c.is_empty()); // sentinel
    }

    #[test]
    fn test_mixed_script_text() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test mixed Japanese and non-Japanese text
        let input_chars = pool.build_char_array("コーヒー");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 5); // コ + ー + ヒ + ー + sentinel
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{30FC}"); // First prolonged mark
        assert_eq!(result[2].c, "");
        assert_eq!(result[3].c, "\u{30FC}"); // Second prolonged mark
        assert!(result[4].c.is_empty()); // sentinel
    }

    #[test]
    fn test_no_replacement_without_preceding_char() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test dash at beginning - should not be replaced
        let input_chars = pool.build_char_array("-か");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 3);
        assert_eq!(result[0].c, "-"); // Should be preserved
        assert_eq!(result[1].c, "");
        assert!(result[2].c.is_empty()); // sentinel
    }

    #[test]
    fn test_character_type_detection() {
        // Test various character type detections
        assert_eq!(
            CharType::from_codepoint('a' as u32),
            CharType::HALFWIDTH_ALPHABET
        );
        assert_eq!(
            CharType::from_codepoint('' as u32),
            CharType::FULLWIDTH_ALPHABET
        );
        assert_eq!(
            CharType::from_codepoint('1' as u32),
            CharType::HALFWIDTH_DIGIT
        );
        assert_eq!(
            CharType::from_codepoint('' as u32),
            CharType::FULLWIDTH_DIGIT
        );
        assert_eq!(
            CharType::from_codepoint('' as u32),
            CharType::ORDINARY_HIRAGANA
        );
        assert_eq!(
            CharType::from_codepoint('' as u32),
            CharType::ORDINARY_KATAKANA
        );
        assert_eq!(
            CharType::from_codepoint('' as u32),
            CharType::ORDINARY_HALFWIDTH_KATAKANA
        );

        // Test special characters
        assert_eq!(
            CharType::from_codepoint(0x30fc),
            CharType::EITHER | CharType::PROLONGED_SOUND_MARK
        );
        assert_eq!(
            CharType::from_codepoint(0xff70),
            CharType::KATAKANA | CharType::PROLONGED_SOUND_MARK | CharType::HALFWIDTH
        );
        assert_eq!(
            CharType::from_codepoint(0x3093),
            CharType::HIRAGANA | CharType::HATSUON
        );
        assert_eq!(
            CharType::from_codepoint(0x3063),
            CharType::HIRAGANA | CharType::SOKUON
        );
    }

    #[test]
    fn test_fullwidth_hyphen_replacement_in_katakana() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test case 1: イ-ハト-ヴォ with fullwidth hyphen-minus
        let input_chars = pool.build_char_array("\u{FF0D}ハト\u{FF0D}ヴォ");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 8); // イ + ー + ハ + ト + ー + ヴ + ォ + sentinel
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{30FC}"); // Should be replaced with prolonged sound mark
        assert_eq!(result[2].c, "");
        assert_eq!(result[3].c, "");
        assert_eq!(result[4].c, "\u{30FC}"); // Should be replaced with prolonged sound mark
        assert_eq!(result[5].c, "");
        assert_eq!(result[6].c, "");
        assert!(result[7].c.is_empty()); // sentinel

        // Test case 2: カトラリ- with fullwidth hyphen-minus at end
        let input_chars2 = pool.build_char_array("カトラリ\u{FF0D}");
        let result2 = transliterator
            .transliterate(&mut pool, &input_chars2)
            .unwrap();

        assert_eq!(result2.len(), 6); // カ + ト + ラ + リ + ー + sentinel
        assert_eq!(result2[0].c, "");
        assert_eq!(result2[1].c, "");
        assert_eq!(result2[2].c, "");
        assert_eq!(result2[3].c, "");
        assert_eq!(result2[4].c, "\u{30FC}"); // Should be replaced with prolonged sound mark
        assert!(result2[5].c.is_empty()); // sentinel
    }

    #[test]
    fn test_hyphen_minus_replacement_in_katakana() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test case 1: イ-ハト-ヴォ with regular hyphen-minus
        let input_chars = pool.build_char_array("\u{002D}ハト\u{002D}ヴォ");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        assert_eq!(result.len(), 8);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "\u{30FC}"); // Should be replaced with prolonged sound mark
        assert_eq!(result[2].c, "");
        assert_eq!(result[3].c, "");
        assert_eq!(result[4].c, "\u{30FC}"); // Should be replaced with prolonged sound mark
        assert_eq!(result[5].c, "");
        assert_eq!(result[6].c, "");
        assert!(result[7].c.is_empty()); // sentinel

        // Test case 2: カトラリ- with regular hyphen-minus at end
        let input_chars2 = pool.build_char_array("カトラリ\u{002D}");
        let result2 = transliterator
            .transliterate(&mut pool, &input_chars2)
            .unwrap();

        assert_eq!(result2.len(), 6);
        assert_eq!(result2[0].c, "");
        assert_eq!(result2[1].c, "");
        assert_eq!(result2[2].c, "");
        assert_eq!(result2[3].c, "");
        assert_eq!(result2[4].c, "\u{30FC}"); // Should be replaced with prolonged sound mark
        assert!(result2[5].c.is_empty()); // sentinel
    }

    #[test]
    fn test_prolonged_marks_following_digits_no_replacement() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test with replace_prolonged_marks_following_alnums disabled (default)
        let input_chars = pool.build_char_array("1\u{30FC}\u{FF0D}2\u{30FC}3");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should preserve all characters as-is
        assert_eq!(result.len(), 7);
        assert_eq!(result[0].c, "1");
        assert_eq!(result[1].c, "\u{30FC}");
        assert_eq!(result[2].c, "\u{FF0D}");
        assert_eq!(result[3].c, "2");
        assert_eq!(result[4].c, "\u{30FC}");
        assert_eq!(result[5].c, "3");
        assert!(result[6].c.is_empty()); // sentinel
    }

    #[test]
    fn test_prolonged_marks_following_digits_with_replacement() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            replace_prolonged_marks_following_alnums: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test with replace_prolonged_marks_following_alnums enabled
        let input_chars = pool.build_char_array("1\u{30FC}\u{FF0D}2\u{30FC}3");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should replace prolonged marks with hyphens between digits
        assert_eq!(result.len(), 7);
        assert_eq!(result[0].c, "1");
        assert_eq!(result[1].c, "\u{002D}"); // Replaced with halfwidth hyphen
        assert_eq!(result[2].c, "\u{002D}"); // Replaced with halfwidth hyphen
        assert_eq!(result[3].c, "2");
        assert_eq!(result[4].c, "\u{002D}"); // Replaced with halfwidth hyphen
        assert_eq!(result[5].c, "3");
        assert!(result[6].c.is_empty()); // sentinel
    }

    #[test]
    fn test_prolonged_marks_following_fullwidth_digits_with_replacement() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            replace_prolonged_marks_following_alnums: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test with fullwidth digits
        let input_chars = pool.build_char_array("\u{FF11}\u{30FC}\u{FF0D}\u{FF12}\u{30FC}\u{FF13}");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should replace prolonged marks with fullwidth hyphens between fullwidth digits
        assert_eq!(result.len(), 7);
        assert_eq!(result[0].c, "\u{FF11}"); // Fullwidth 1
        assert_eq!(result[1].c, "\u{FF0D}"); // Replaced with fullwidth hyphen
        assert_eq!(result[2].c, "\u{FF0D}"); // Already fullwidth hyphen
        assert_eq!(result[3].c, "\u{FF12}"); // Fullwidth 2
        assert_eq!(result[4].c, "\u{FF0D}"); // Replaced with fullwidth hyphen
        assert_eq!(result[5].c, "\u{FF13}"); // Fullwidth 3
        assert!(result[6].c.is_empty()); // sentinel
    }

    #[test]
    fn test_sokuon_without_allow_prolonged() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test sokuon (っ) without allow_prolonged_sokuon
        let input_chars = pool.build_char_array("ウッ\u{FF0D}ウン\u{FF0D}");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should not replace after sokuon
        assert_eq!(result.len(), 7);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "");
        assert_eq!(result[2].c, "\u{FF0D}"); // Should remain as fullwidth hyphen
        assert_eq!(result[3].c, "");
        assert_eq!(result[4].c, "");
        assert_eq!(result[5].c, "\u{FF0D}"); // Should remain as fullwidth hyphen
    }

    #[test]
    fn test_sokuon_with_allow_prolonged() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            allow_prolonged_sokuon: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test sokuon (っ) with allow_prolonged_sokuon
        let input_chars = pool.build_char_array("ウッ\u{FF0D}ウン\u{FF0D}");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should replace after sokuon but not after hatsuon
        assert_eq!(result.len(), 7);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "");
        assert_eq!(result[2].c, "\u{30FC}"); // Should be replaced with prolonged mark
        assert_eq!(result[3].c, "");
        assert_eq!(result[4].c, "");
        assert_eq!(result[5].c, "\u{FF0D}"); // Should remain as fullwidth hyphen
    }

    #[test]
    fn test_hatsuon_with_allow_prolonged() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            allow_prolonged_hatsuon: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test hatsuon (ン) with allow_prolonged_hatsuon
        let input_chars = pool.build_char_array("ウッ\u{FF0D}ウン\u{FF0D}");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should not replace after sokuon but should replace after hatsuon
        assert_eq!(result.len(), 7);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "");
        assert_eq!(result[2].c, "\u{FF0D}"); // Should remain as fullwidth hyphen
        assert_eq!(result[3].c, "");
        assert_eq!(result[4].c, "");
        assert_eq!(result[5].c, "\u{30FC}"); // Should be replaced with prolonged mark
    }

    #[test]
    fn test_both_sokuon_and_hatsuon_allowed() {
        let options = ProlongedSoundMarksTransliteratorOptions {
            allow_prolonged_sokuon: true,
            allow_prolonged_hatsuon: true,
            ..Default::default()
        };
        let transliterator = ProlongedSoundMarksTransliterator::new(options);
        let mut pool = CharPool::new();

        // Test with both options enabled
        let input_chars = pool.build_char_array("ウッ\u{FF0D}ウン\u{FF0D}");
        let result = transliterator
            .transliterate(&mut pool, &input_chars)
            .unwrap();

        // Should replace after both sokuon and hatsuon
        assert_eq!(result.len(), 7);
        assert_eq!(result[0].c, "");
        assert_eq!(result[1].c, "");
        assert_eq!(result[2].c, "\u{30FC}"); // Should be replaced with prolonged mark
        assert_eq!(result[3].c, "");
        assert_eq!(result[4].c, "");
        assert_eq!(result[5].c, "\u{30FC}"); // Should be replaced with prolonged mark
    }

    #[test]
    fn test_prolonged_sound_marks_factory() {
        let options = ProlongedSoundMarksTransliteratorOptions::default();
        let factory_result = options.new_transliterator();
        assert!(factory_result.is_ok());

        let transliterator = factory_result.unwrap();

        // Test that it preserves input (since it's a stub implementation)
        let test_cases = &[
            ("おかあさん", "おかあさん"),
            ("ラーメン", "ラーメン"),
            ("スーパー", "スーパー"),
            ("", ""),
        ];

        // Test each case
        for (input, expected) in test_cases {
            let mut pool = CharPool::new();
            let input_chars = pool.build_char_array(input);
            let result = transliterator
                .transliterate(&mut pool, &input_chars)
                .unwrap();
            let result_string = from_chars(result.iter().cloned());
            assert_eq!(result_string, *expected, "Failed for input '{input}'");
        }
    }
}