base-d 3.0.34

Universal base encoder: Encode binary data to 33+ dictionaries including RFC standards, hieroglyphs, emoji, and more
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
//! Dictionary metadata and translation strategies for SIMD encoding
//!
//! This module analyzes dictionaries to determine optimal SIMD translation
//! strategies and defines dictionary variants for known encodings.

use crate::core::dictionary::Dictionary;

/// Base64 dictionary variants
///
/// Different base64 standards use different characters at positions 62 and 63:
/// - Standard (RFC 4648): uses '+' and '/'
/// - URL-safe (RFC 4648): uses '-' and '_'
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DictionaryVariant {
    /// Standard base64 dictionary: A-Za-z0-9+/
    Base64Standard,
    /// URL-safe base64 dictionary: A-Za-z0-9-_
    Base64Url,
}

/// Base32 dictionary variants
///
/// Different base32 standards use different character sets:
/// - RFC 4648 Standard: A-Z and 2-7
/// - RFC 4648 Extended Hex: 0-9 and A-V
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Base32Variant {
    /// RFC 4648 standard: A-Z, 2-7 with padding
    Rfc4648,
    /// RFC 4648 Extended Hex: 0-9, A-V
    Rfc4648Hex,
}

/// Identify which base64 dictionary variant a Dictionary uses
///
/// Returns `None` if the dictionary is not base64 (base != 64) or
/// if it doesn't match a known variant exactly.
///
/// This performs a strict check of all 64 positions to ensure the dictionary
/// matches the standard or URL-safe variant completely.
///
/// # Arguments
///
/// * `dict` - The Dictionary to identify
///
/// # Returns
///
/// - `Some(DictionaryVariant::Base64Standard)` if all positions match standard base64
/// - `Some(DictionaryVariant::Base64Url)` if all positions match URL-safe base64
/// - `None` if not base64 or doesn't match a known variant exactly
pub fn identify_base64_variant(dict: &Dictionary) -> Option<DictionaryVariant> {
    // Only works for base64
    if dict.base() != 64 {
        return None;
    }

    // Check characters at positions 62 and 63 for quick filtering
    let char_62 = dict.encode_digit(62)?;
    let char_63 = dict.encode_digit(63)?;

    let candidate = match (char_62, char_63) {
        ('+', '/') => DictionaryVariant::Base64Standard,
        ('-', '_') => DictionaryVariant::Base64Url,
        _ => return None,
    };

    // Verify all positions match the expected variant
    if verify_base64_dictionary(dict, candidate) {
        Some(candidate)
    } else {
        None
    }
}

/// Verify that a dictionary matches the expected base64 dictionary variant
///
/// Checks all 64 positions to ensure they match the expected dictionary.
///
/// # Arguments
///
/// * `dict` - The Dictionary to verify
/// * `variant` - The expected dictionary variant
///
/// # Returns
///
/// `true` if all positions match, `false` otherwise
pub fn verify_base64_dictionary(dict: &Dictionary, variant: DictionaryVariant) -> bool {
    if dict.base() != 64 {
        return false;
    }

    let expected = match variant {
        DictionaryVariant::Base64Standard => {
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
        }
        DictionaryVariant::Base64Url => {
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
        }
    };

    for (i, expected_char) in expected.chars().enumerate() {
        if dict.encode_digit(i) != Some(expected_char) {
            return false;
        }
    }

    true
}

/// Identify which base32 dictionary variant a Dictionary uses
///
/// Returns `None` if the dictionary is not base32 (base != 32) or
/// if it doesn't match a known variant exactly.
///
/// # Arguments
///
/// * `dict` - The Dictionary to identify
///
/// # Returns
///
/// - `Some(Base32Variant::Rfc4648)` if all positions match RFC 4648 standard
/// - `Some(Base32Variant::Rfc4648Hex)` if all positions match RFC 4648 extended hex
/// - `None` if not base32 or doesn't match a known variant exactly
pub fn identify_base32_variant(dict: &Dictionary) -> Option<Base32Variant> {
    // Only works for base32
    if dict.base() != 32 {
        return None;
    }

    // Check character patterns
    let chars: Vec<char> = (0..32).filter_map(|i| dict.encode_digit(i)).collect();
    if chars.len() != 32 {
        return None;
    }

    // RFC 4648: A-Z, 2-7 (positions 0-25 = A-Z, 26-31 = 2-7)
    if chars[0] == 'A' && chars[25] == 'Z' && chars[26] == '2' && chars[31] == '7' {
        let candidate = Base32Variant::Rfc4648;
        if verify_base32_dictionary(dict, candidate) {
            return Some(candidate);
        }
    }

    // RFC 4648 Hex: 0-9, A-V (positions 0-9 = 0-9, 10-31 = A-V)
    if chars[0] == '0' && chars[9] == '9' && chars[10] == 'A' && chars[31] == 'V' {
        let candidate = Base32Variant::Rfc4648Hex;
        if verify_base32_dictionary(dict, candidate) {
            return Some(candidate);
        }
    }

    None
}

/// Verify that a dictionary matches the expected base32 dictionary variant
///
/// Checks all 32 positions to ensure they match the expected dictionary.
///
/// # Arguments
///
/// * `dict` - The Dictionary to verify
/// * `variant` - The expected dictionary variant
///
/// # Returns
///
/// `true` if all positions match, `false` otherwise
pub fn verify_base32_dictionary(dict: &Dictionary, variant: Base32Variant) -> bool {
    if dict.base() != 32 {
        return false;
    }

    let expected = match variant {
        Base32Variant::Rfc4648 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
        Base32Variant::Rfc4648Hex => "0123456789ABCDEFGHIJKLMNOPQRSTUV",
    };

    for (i, expected_char) in expected.chars().enumerate() {
        if dict.encode_digit(i) != Some(expected_char) {
            return false;
        }
    }

    true
}

/// Character range mapping for ranged translation strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CharRange {
    /// Index range: [start_index, end_index)
    pub index_start: u8,
    pub index_end: u8,
    /// Character range: [start_char, end_char)
    pub char_start: char,
    pub char_end: char,
    /// Offset to add: char = index + offset
    pub offset: i8,
}

/// Classification of how a dictionary's indices map to characters
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TranslationStrategy {
    /// Single contiguous Unicode range: index → char is `start + index`
    /// Example: 64 chars starting at U+0040 ('@')
    /// Encoding: add constant offset
    /// Decoding: subtract constant offset
    Sequential { start_codepoint: u32 },

    /// Multiple contiguous ranges with gaps
    /// Example: base64 (A-Z, a-z, 0-9, +/)
    /// Encoding: range check + offset per range
    /// Decoding: range check + offset per range
    Ranged { ranges: &'static [CharRange] },

    /// Arbitrary mapping requiring lookup table
    /// Example: custom shuffled dictionary
    /// Encoding: LUT required
    /// Decoding: reverse LUT/HashMap required
    Arbitrary { dictionary_size: usize },
}

/// LUT codec selection strategy based on dictionary size
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LutStrategy {
    /// Not applicable (sequential or ranged dictionary)
    NotApplicable,
    /// Small dictionary (<=16 chars): Direct pshufb/tbl lookup
    SmallDirect,
    /// Large dictionary (17-64 chars): Platform-dependent (vqtbl4q/vpermb/range-reduction)
    LargePlatformDependent,
    /// Very large (>64 chars): No SIMD benefit, scalar only
    ScalarOnly,
}

/// Known range definitions for standard dictionaries
static BASE64_STANDARD_RANGES: &[CharRange] = &[
    CharRange {
        index_start: 0,
        index_end: 26,
        char_start: 'A',
        char_end: 'Z',
        offset: 65,
    },
    CharRange {
        index_start: 26,
        index_end: 52,
        char_start: 'a',
        char_end: 'z',
        offset: 71,
    },
    CharRange {
        index_start: 52,
        index_end: 62,
        char_start: '0',
        char_end: '9',
        offset: -4,
    },
    // Special cases for +/ handled separately in SIMD
];

static BASE64_URL_RANGES: &[CharRange] = &[
    CharRange {
        index_start: 0,
        index_end: 26,
        char_start: 'A',
        char_end: 'Z',
        offset: 65,
    },
    CharRange {
        index_start: 26,
        index_end: 52,
        char_start: 'a',
        char_end: 'z',
        offset: 71,
    },
    CharRange {
        index_start: 52,
        index_end: 62,
        char_start: '0',
        char_end: '9',
        offset: -4,
    },
    // Special cases for -_ handled separately in SIMD
];

static HEX_UPPER_RANGES: &[CharRange] = &[
    CharRange {
        index_start: 0,
        index_end: 10,
        char_start: '0',
        char_end: '9',
        offset: 48,
    },
    CharRange {
        index_start: 10,
        index_end: 16,
        char_start: 'A',
        char_end: 'F',
        offset: 55,
    },
];

static HEX_LOWER_RANGES: &[CharRange] = &[
    CharRange {
        index_start: 0,
        index_end: 10,
        char_start: '0',
        char_end: '9',
        offset: 48,
    },
    CharRange {
        index_start: 10,
        index_end: 16,
        char_start: 'a',
        char_end: 'f',
        offset: 87,
    },
];

/// Metadata about a dictionary's structure for SIMD optimization
#[derive(Debug, Clone)]
pub struct DictionaryMetadata {
    /// Dictionary base (2, 4, 8, 16, 32, 64, 128, 256)
    pub base: usize,

    /// Bits per symbol (1, 2, 3, 4, 5, 6, 7, 8)
    pub bits_per_symbol: u8,

    /// Translation strategy
    pub strategy: TranslationStrategy,

    /// Whether SIMD acceleration is available
    pub simd_compatible: bool,
}

impl DictionaryMetadata {
    /// Returns whether SIMD acceleration is available for this dictionary
    pub fn simd_available(&self) -> bool {
        self.simd_compatible
    }

    /// Determine LUT codec suitability for arbitrary dictionaries
    pub fn lut_strategy(&self) -> LutStrategy {
        match self.strategy {
            TranslationStrategy::Arbitrary { dictionary_size } => {
                if dictionary_size <= 16 {
                    LutStrategy::SmallDirect
                } else if dictionary_size <= 64 {
                    LutStrategy::LargePlatformDependent
                } else {
                    LutStrategy::ScalarOnly
                }
            }
            _ => LutStrategy::NotApplicable,
        }
    }

    /// Analyze a Dictionary and determine its translation strategy
    pub fn from_dictionary(dict: &Dictionary) -> Self {
        let base = dict.base();

        // Check power-of-2 requirement
        if !base.is_power_of_two() {
            return Self {
                base,
                bits_per_symbol: 0,
                strategy: TranslationStrategy::Arbitrary {
                    dictionary_size: base,
                },
                simd_compatible: false,
            };
        }

        let bits_per_symbol = (base as f64).log2() as u8;

        // Analyze character sequence
        let strategy = Self::detect_strategy(dict);

        // SIMD compatible if:
        // 1. Power of 2 base
        // 2. Sequential or known ranged pattern
        // 3. Base supported by existing SIMD (4, 5, 6, 8 bits)
        let simd_compatible = matches!(bits_per_symbol, 4 | 5 | 6 | 8)
            && !matches!(strategy, TranslationStrategy::Arbitrary { .. });

        Self {
            base,
            bits_per_symbol,
            strategy,
            simd_compatible,
        }
    }

    fn detect_strategy(dict: &Dictionary) -> TranslationStrategy {
        let base = dict.base();
        let chars: Vec<char> = (0..base).filter_map(|i| dict.encode_digit(i)).collect();

        if chars.len() != base {
            return TranslationStrategy::Arbitrary {
                dictionary_size: chars.len(),
            };
        }

        // Check for sequential (all codepoints contiguous)
        let first_codepoint = chars[0] as u32;
        let is_sequential = chars
            .iter()
            .enumerate()
            .all(|(i, &c)| (c as u32) == first_codepoint + (i as u32));

        if is_sequential {
            return TranslationStrategy::Sequential {
                start_codepoint: first_codepoint,
            };
        }

        // Check for known ranged patterns
        if let Some(ranges) = Self::detect_ranges(&chars) {
            return TranslationStrategy::Ranged { ranges };
        }

        TranslationStrategy::Arbitrary {
            dictionary_size: base,
        }
    }

    fn detect_ranges(chars: &[char]) -> Option<&'static [CharRange]> {
        // Check against known patterns

        // Standard base64: A-Za-z0-9+/
        if chars.len() == 64 && Self::matches_base64_standard(chars) {
            return Some(BASE64_STANDARD_RANGES);
        }

        // URL-safe base64: A-Za-z0-9-_
        if chars.len() == 64 && Self::matches_base64_url(chars) {
            return Some(BASE64_URL_RANGES);
        }

        // Hex uppercase: 0-9A-F
        if chars.len() == 16 && Self::matches_hex_upper(chars) {
            return Some(HEX_UPPER_RANGES);
        }

        // Hex lowercase: 0-9a-f
        if chars.len() == 16 && Self::matches_hex_lower(chars) {
            return Some(HEX_LOWER_RANGES);
        }

        None
    }

    fn matches_base64_standard(chars: &[char]) -> bool {
        let expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        chars.iter().zip(expected.chars()).all(|(a, b)| *a == b)
    }

    fn matches_base64_url(chars: &[char]) -> bool {
        let expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
        chars.iter().zip(expected.chars()).all(|(a, b)| *a == b)
    }

    fn matches_hex_upper(chars: &[char]) -> bool {
        let expected = "0123456789ABCDEF";
        chars.iter().zip(expected.chars()).all(|(a, b)| *a == b)
    }

    fn matches_hex_lower(chars: &[char]) -> bool {
        let expected = "0123456789abcdef";
        chars.iter().zip(expected.chars()).all(|(a, b)| *a == b)
    }
}

#[cfg(all(test, target_arch = "x86_64"))]
#[allow(deprecated)]
mod tests {
    use super::*;
    use crate::core::config::EncodingMode;

    fn make_base64_standard_dict() -> Dictionary {
        let chars: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
            .chars()
            .collect();
        Dictionary::new_with_mode(chars, EncodingMode::Chunked, Some('=')).unwrap()
    }

    fn make_base64_url_dict() -> Dictionary {
        let chars: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
            .chars()
            .collect();
        Dictionary::new_with_mode(chars, EncodingMode::Chunked, Some('=')).unwrap()
    }

    #[test]
    fn test_identify_standard_base64() {
        let dict = make_base64_standard_dict();
        assert_eq!(
            identify_base64_variant(&dict),
            Some(DictionaryVariant::Base64Standard)
        );
    }

    #[test]
    fn test_identify_base64_url() {
        let dict = make_base64_url_dict();
        assert_eq!(
            identify_base64_variant(&dict),
            Some(DictionaryVariant::Base64Url)
        );
    }

    #[test]
    fn test_identify_non_base64() {
        let chars: Vec<char> = "0123456789ABCDEF".chars().collect();
        let dict = Dictionary::new(chars).unwrap();
        assert_eq!(identify_base64_variant(&dict), None);
    }

    #[test]
    fn test_identify_unknown_variant() {
        // Custom base64 with different chars at positions 62-63
        let chars: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@$"
            .chars()
            .collect();
        let dict = Dictionary::new_with_mode(chars, EncodingMode::Chunked, None).unwrap();
        assert_eq!(identify_base64_variant(&dict), None);
    }

    #[test]
    fn test_verify_standard_dictionary() {
        let dict = make_base64_standard_dict();
        assert!(verify_base64_dictionary(
            &dict,
            DictionaryVariant::Base64Standard
        ));
        assert!(!verify_base64_dictionary(
            &dict,
            DictionaryVariant::Base64Url
        ));
    }

    #[test]
    fn test_verify_url_dictionary() {
        let dict = make_base64_url_dict();
        assert!(verify_base64_dictionary(
            &dict,
            DictionaryVariant::Base64Url
        ));
        assert!(!verify_base64_dictionary(
            &dict,
            DictionaryVariant::Base64Standard
        ));
    }

    #[test]
    fn test_sequential_dictionary_detection() {
        // Create a sequential dictionary: 64 chars starting at Latin Extended-A (U+0100)
        let chars: Vec<char> = (0x100..0x140)
            .map(|cp| char::from_u32(cp).unwrap())
            .collect();
        let dict = Dictionary::new_with_mode(chars, EncodingMode::Chunked, None).unwrap();

        let metadata = DictionaryMetadata::from_dictionary(&dict);
        assert_eq!(metadata.base, 64);
        assert_eq!(metadata.bits_per_symbol, 6);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Sequential {
                start_codepoint: 0x100
            }
        ));
        assert!(metadata.simd_compatible);
    }

    #[test]
    fn test_ranged_base64_standard_detection() {
        let dict = make_base64_standard_dict();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 64);
        assert_eq!(metadata.bits_per_symbol, 6);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Ranged { .. }
        ));
        assert!(metadata.simd_compatible);
    }

    #[test]
    fn test_ranged_base64_url_detection() {
        let dict = make_base64_url_dict();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 64);
        assert_eq!(metadata.bits_per_symbol, 6);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Ranged { .. }
        ));
        assert!(metadata.simd_compatible);
    }

    #[test]
    fn test_ranged_hex_upper_detection() {
        let chars: Vec<char> = "0123456789ABCDEF".chars().collect();
        let dict = Dictionary::new(chars).unwrap();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 16);
        assert_eq!(metadata.bits_per_symbol, 4);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Ranged { .. }
        ));
        assert!(metadata.simd_compatible);
    }

    #[test]
    fn test_ranged_hex_lower_detection() {
        let chars: Vec<char> = "0123456789abcdef".chars().collect();
        let dict = Dictionary::new(chars).unwrap();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 16);
        assert_eq!(metadata.bits_per_symbol, 4);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Ranged { .. }
        ));
        assert!(metadata.simd_compatible);
    }

    #[test]
    fn test_arbitrary_dictionary_detection() {
        // Create a shuffled/arbitrary dictionary
        let chars: Vec<char> = "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210+/"
            .chars()
            .collect();
        let dict = Dictionary::new_with_mode(chars, EncodingMode::Chunked, None).unwrap();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 64);
        assert_eq!(metadata.bits_per_symbol, 6);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Arbitrary {
                dictionary_size: 64
            }
        ));
        assert!(!metadata.simd_compatible);
    }

    #[test]
    fn test_non_power_of_two_detection() {
        // Base 10 is not power of 2
        let chars: Vec<char> = "0123456789".chars().collect();
        let dict = Dictionary::new(chars).unwrap();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 10);
        assert_eq!(metadata.bits_per_symbol, 0);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Arbitrary {
                dictionary_size: 10
            }
        ));
        assert!(!metadata.simd_compatible);
    }

    #[test]
    fn test_base32_sequential() {
        // Create sequential base32 dictionary
        let chars: Vec<char> = (0x41..0x61).map(|cp| char::from_u32(cp).unwrap()).collect();
        let dict = Dictionary::new(chars).unwrap();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 32);
        assert_eq!(metadata.bits_per_symbol, 5);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Sequential {
                start_codepoint: 0x41
            }
        ));
        // base32 (5 bits) is now SIMD compatible
        assert!(metadata.simd_compatible);
    }

    #[test]
    fn test_base256_sequential() {
        // Create sequential base256 dictionary using valid Unicode range
        // Use a range starting from 0x100 (Latin Extended-A) to avoid control characters
        let chars: Vec<char> = (0x100..0x200)
            .map(|cp| char::from_u32(cp).unwrap())
            .collect();
        let dict = Dictionary::new(chars).unwrap();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 256);
        assert_eq!(metadata.bits_per_symbol, 8);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Sequential {
                start_codepoint: 0x100
            }
        ));
        assert!(metadata.simd_compatible);
    }

    #[test]
    fn test_sequential_starting_at_printable() {
        // Base16 sequential starting at '!' (U+0021)
        let chars: Vec<char> = (0x21..0x31).map(|cp| char::from_u32(cp).unwrap()).collect();
        let dict = Dictionary::new(chars).unwrap();
        let metadata = DictionaryMetadata::from_dictionary(&dict);

        assert_eq!(metadata.base, 16);
        assert_eq!(metadata.bits_per_symbol, 4);
        assert!(matches!(
            metadata.strategy,
            TranslationStrategy::Sequential {
                start_codepoint: 0x21
            }
        ));
        assert!(metadata.simd_compatible);
    }
}