gsm7-alt 0.1.2

GSM7 encoding and decoding by integer.
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
//! GSM 7-bit character encoding and decoding library.
//!
//! This library provides efficient encoding and decoding of text using the GSM 7-bit
//! character set as defined in GSM 03.38. This encoding is commonly used in SMS messages.
//!
//! # Example
//!
//! ```rust
//! use gsm7::{encode, decode};
//!
//! let text = "Hello {world} €!";
//! let encoded = encode(text)?;
//! let decoded = decode(&encoded)?;
//! assert_eq!(decoded, text);
//! # Ok::<(), gsm7::Gsm7Error>(())
//! ```

use once_cell::sync::Lazy;
use std::collections::HashMap;
use thiserror::Error;

/// Errors that can occur during GSM 7-bit encoding/decoding operations.
#[derive(Error, Debug, Clone, PartialEq)]
pub enum Gsm7Error {
    /// Character is not supported in GSM 7-bit encoding.
    #[error("Character not supported in GSM 7-bit: '{character}' (U+{code:04X})")]
    UnsupportedCharacter { character: char, code: u32 },

    /// Invalid escape sequence encountered during decoding.
    #[error("Invalid escape sequence: 0x1B followed by 0x{code:02X}")]
    InvalidEscapeSequence { code: u8 },

    /// Invalid byte encountered during decoding.
    #[error("Invalid GSM 7-bit byte: 0x{byte:02X}")]
    InvalidByte { byte: u8 },

    /// Input data is malformed.
    #[error("Malformed GSM 7-bit data: {reason}")]
    MalformedData { reason: String },
}

/// Result type for GSM 7-bit operations.
pub type Result<T> = std::result::Result<T, Gsm7Error>;

/// Configuration options for GSM 7-bit encoding/decoding.
#[derive(Debug, Clone)]
pub struct Gsm7Config {
    /// Whether to use strict mode (fail on unsupported characters) or replace them.
    pub strict: bool,
    /// Replacement character for unsupported characters in non-strict mode.
    pub replacement_char: char,
}

impl Default for Gsm7Config {
    fn default() -> Self {
        Self {
            strict: false,
            replacement_char: '',
        }
    }
}

impl Gsm7Config {
    /// Create a config with strict mode enabled.
    pub fn strict() -> Self {
        Self {
            strict: true,
            replacement_char: '',
        }
    }
}

/// Internal representation of GSM 7-bit codes.
#[derive(Debug, Clone)]
enum Code {
    /// Single byte code.
    Single(u8),
    /// Escape sequence (0x1B followed by another byte).
    Escape(u8),
}

/// GSM 7-bit character mappings (lazy-initialized static data).
static GSM_MAPS: Lazy<(HashMap<char, Code>, [Option<char>; 128], HashMap<u8, char>)> =
    Lazy::new(|| {
        let gsm_to_char = build_gsm_table();
        let gsm_ext = build_gsm_ext_table();

        let mut char_to_gsm = HashMap::new();
        let mut gsm_array = [None; 128];

        // Build character to GSM mapping and array for fast lookup
        for (&code, &ch) in &gsm_to_char {
            if let Some(character) = ch {
                char_to_gsm.insert(character, Code::Single(code));
                gsm_array[code as usize] = Some(character);
            }
        }

        // Add extension characters
        for (&code, &ch) in &gsm_ext {
            char_to_gsm.insert(ch, Code::Escape(code));
        }

        (char_to_gsm, gsm_array, gsm_ext)
    });

/// Encode a string using GSM 7-bit encoding.
///
/// # Arguments
///
/// * `content` - The string to encode
///
/// # Returns
///
/// A `Vec<u8>` containing the GSM 7-bit encoded bytes.
///
/// # Errors
///
/// Returns `Gsm7Error::UnsupportedCharacter` if the input contains characters
/// not supported by the GSM 7-bit character set.
///
/// # Example
///
/// ```rust
/// use gsm7::encode;
///
/// let encoded = encode("Hello World!")?;
/// assert!(!encoded.is_empty());
/// # Ok::<(), gsm7::Gsm7Error>(())
/// ```
pub fn encode(content: &str) -> Result<Vec<u8>> {
    encode_with_config(content, &Gsm7Config::default())
}

/// Encode a string using GSM 7-bit encoding with custom configuration.
///
/// # Arguments
///
/// * `content` - The string to encode
/// * `config` - Configuration options
///
/// # Returns
///
/// A `Vec<u8>` containing the GSM 7-bit encoded bytes.
pub fn encode_with_config(content: &str, config: &Gsm7Config) -> Result<Vec<u8>> {
    let (char_to_gsm, _, _) = &*GSM_MAPS;
    let mut bytes = Vec::with_capacity(content.len());

    for ch in content.chars() {
        match char_to_gsm.get(&ch) {
            Some(Code::Single(b)) => bytes.push(*b),
            Some(Code::Escape(b)) => {
                bytes.push(0x1B);
                bytes.push(*b);
            }
            None => {
                if config.strict {
                    return Err(Gsm7Error::UnsupportedCharacter {
                        character: ch,
                        code: ch as u32,
                    });
                } else {
                    // Use replacement character
                    if let Some(Code::Single(b)) = char_to_gsm.get(&config.replacement_char) {
                        bytes.push(*b);
                    } else {
                        bytes.push(0x20); // space as fallback
                    }
                }
            }
        }
    }

    Ok(bytes)
}

/// Decode GSM 7-bit encoded bytes to a string.
///
/// # Arguments
///
/// * `data` - The GSM 7-bit encoded bytes to decode
///
/// # Returns
///
/// A `String` containing the decoded text. Invalid bytes are replaced with '�'.
///
/// # Note
///
/// This function uses non-strict mode by default. Invalid bytes will be replaced
/// with the replacement character '�' instead of returning an error.
///
/// # Example
///
/// ```rust
/// use gsm7::{encode, decode};
///
/// let original = "Hello World!";
/// let encoded = encode(original)?;
/// let decoded = decode(&encoded)?;
/// assert_eq!(decoded, original);
///
/// // Invalid bytes are replaced
/// let invalid_data = vec![0x48, 0x81, 0x65]; // "H" + invalid + "e"
/// let decoded = decode(&invalid_data)?;
/// assert_eq!(decoded, "H�e");
/// # Ok::<(), gsm7::Gsm7Error>(())
/// ```
pub fn decode(data: &[u8]) -> Result<String> {
    decode_with_config(data, &Gsm7Config::default())
}

/// Decode GSM 7-bit encoded bytes to a string with custom configuration.
///
/// # Arguments
///
/// * `data` - The GSM 7-bit encoded bytes to decode
/// * `config` - Configuration options
///
/// # Returns
///
/// A `String` containing the decoded text.
pub fn decode_with_config(data: &[u8], config: &Gsm7Config) -> Result<String> {
    let (_, gsm_array, gsm_ext) = &*GSM_MAPS;
    let mut result = String::with_capacity(data.len());

    let mut i = 0;
    while i < data.len() {
        let code = data[i];

        if code == 0x1B {
            // Handle escape sequence
            if let Some(&next_code) = data.get(i + 1) {
                match gsm_ext.get(&next_code) {
                    Some(&ch) => {
                        result.push(ch);
                        i += 2;
                        continue;
                    }
                    None => {
                        if config.strict {
                            return Err(Gsm7Error::InvalidEscapeSequence { code: next_code });
                        } else {
                            result.push(config.replacement_char);
                            i += 2;
                            continue;
                        }
                    }
                }
            } else {
                if config.strict {
                    return Err(Gsm7Error::MalformedData {
                        reason: "Escape byte at end of input".to_string(),
                    });
                } else {
                    result.push(config.replacement_char);
                    i += 1;
                    continue;
                }
            }
        } else if code < 128 {
            // Handle regular character
            match gsm_array[code as usize] {
                Some(ch) => result.push(ch),
                None => {
                    if config.strict {
                        return Err(Gsm7Error::InvalidByte { byte: code });
                    } else {
                        result.push(config.replacement_char);
                    }
                }
            }
        } else {
            // Invalid byte (>= 128) - always replace with � character
            result.push(config.replacement_char);
        }
        i += 1;
    }

    Ok(result)
}

/// Calculate the number of bytes required to encode a string in GSM 7-bit.
///
/// This is useful for SMS length calculations.
///
/// # Arguments
///
/// * `content` - The string to measure
///
/// # Returns
///
/// The number of bytes required, or an error if the string contains
/// unsupported characters.
pub fn encoded_len(content: &str) -> Result<usize> {
    let (char_to_gsm, _, _) = &*GSM_MAPS;
    let mut len = 0;

    for ch in content.chars() {
        match char_to_gsm.get(&ch) {
            Some(Code::Single(_)) => len += 1,
            Some(Code::Escape(_)) => len += 2,
            None => {
                return Err(Gsm7Error::UnsupportedCharacter {
                    character: ch,
                    code: ch as u32,
                });
            }
        }
    }

    Ok(len)
}

/// Check if a string can be encoded in GSM 7-bit without errors.
///
/// # Arguments
///
/// * `content` - The string to check
///
/// # Returns
///
/// `true` if the string can be encoded, `false` otherwise.
pub fn is_gsm7_compatible(content: &str) -> bool {
    encoded_len(content).is_ok()
}

/// Base GSM 7-bit character table as defined in GSM 03.38.
fn build_gsm_table() -> HashMap<u8, Option<char>> {
    let mut map = HashMap::new();

    let table: &[(u8, Option<char>)] = &[
        (0x00, Some('@')),
        (0x01, Some('£')),
        (0x02, Some('$')),
        (0x03, Some('¥')),
        (0x04, Some('è')),
        (0x05, Some('é')),
        (0x06, Some('ù')),
        (0x07, Some('ì')),
        (0x08, Some('ò')),
        (0x09, Some('Ç')),
        (0x0A, Some('\n')),
        (0x0B, Some('Ø')),
        (0x0C, Some('ø')),
        (0x0D, Some('\r')),
        (0x0E, Some('Å')),
        (0x0F, Some('å')),
        (0x10, Some('Δ')),
        (0x11, Some('_')),
        (0x12, Some('Φ')),
        (0x13, Some('Γ')),
        (0x14, Some('Λ')),
        (0x15, Some('Ω')),
        (0x16, Some('Π')),
        (0x17, Some('Ψ')),
        (0x18, Some('Σ')),
        (0x19, Some('Θ')),
        (0x1A, Some('Ξ')),
        (0x1B, None), // ESC - no character representation
        (0x1C, Some('Æ')),
        (0x1D, Some('æ')),
        (0x1E, Some('ß')),
        (0x1F, Some('É')),
        (0x20, Some(' ')),
        (0x21, Some('!')),
        (0x22, Some('"')),
        (0x23, Some('#')),
        (0x24, Some('¤')),
        (0x25, Some('%')),
        (0x26, Some('&')),
        (0x27, Some('\'')),
        (0x28, Some('(')),
        (0x29, Some(')')),
        (0x2A, Some('*')),
        (0x2B, Some('+')),
        (0x2C, Some(',')),
        (0x2D, Some('-')),
        (0x2E, Some('.')),
        (0x2F, Some('/')),
        (0x30, Some('0')),
        (0x31, Some('1')),
        (0x32, Some('2')),
        (0x33, Some('3')),
        (0x34, Some('4')),
        (0x35, Some('5')),
        (0x36, Some('6')),
        (0x37, Some('7')),
        (0x38, Some('8')),
        (0x39, Some('9')),
        (0x3A, Some(':')),
        (0x3B, Some(';')),
        (0x3C, Some('<')),
        (0x3D, Some('=')),
        (0x3E, Some('>')),
        (0x3F, Some('?')),
        (0x40, Some('¡')),
        (0x41, Some('A')),
        (0x42, Some('B')),
        (0x43, Some('C')),
        (0x44, Some('D')),
        (0x45, Some('E')),
        (0x46, Some('F')),
        (0x47, Some('G')),
        (0x48, Some('H')),
        (0x49, Some('I')),
        (0x4A, Some('J')),
        (0x4B, Some('K')),
        (0x4C, Some('L')),
        (0x4D, Some('M')),
        (0x4E, Some('N')),
        (0x4F, Some('O')),
        (0x50, Some('P')),
        (0x51, Some('Q')),
        (0x52, Some('R')),
        (0x53, Some('S')),
        (0x54, Some('T')),
        (0x55, Some('U')),
        (0x56, Some('V')),
        (0x57, Some('W')),
        (0x58, Some('X')),
        (0x59, Some('Y')),
        (0x5A, Some('Z')),
        (0x5B, Some('Ä')),
        (0x5C, Some('Ö')),
        (0x5D, Some('Ñ')),
        (0x5E, Some('Ü')),
        (0x5F, Some('§')),
        (0x60, Some('¿')),
        (0x61, Some('a')),
        (0x62, Some('b')),
        (0x63, Some('c')),
        (0x64, Some('d')),
        (0x65, Some('e')),
        (0x66, Some('f')),
        (0x67, Some('g')),
        (0x68, Some('h')),
        (0x69, Some('i')),
        (0x6A, Some('j')),
        (0x6B, Some('k')),
        (0x6C, Some('l')),
        (0x6D, Some('m')),
        (0x6E, Some('n')),
        (0x6F, Some('o')),
        (0x70, Some('p')),
        (0x71, Some('q')),
        (0x72, Some('r')),
        (0x73, Some('s')),
        (0x74, Some('t')),
        (0x75, Some('u')),
        (0x76, Some('v')),
        (0x77, Some('w')),
        (0x78, Some('x')),
        (0x79, Some('y')),
        (0x7A, Some('z')),
        (0x7B, Some('ä')),
        (0x7C, Some('ö')),
        (0x7D, Some('ñ')),
        (0x7E, Some('ü')),
        (0x7F, Some('à')),
    ];

    for &(code, ch) in table {
        map.insert(code, ch);
    }

    map
}

/// GSM 7-bit extension table (characters prefixed with 0x1B).
fn build_gsm_ext_table() -> HashMap<u8, char> {
    let mut map = HashMap::new();

    let table: &[(u8, char)] = &[
        (0x0A, '\x0C'), // Form feed
        (0x14, '^'),
        (0x28, '{'),
        (0x29, '}'),
        (0x2F, '\\'),
        (0x3C, '['),
        (0x3D, '~'),
        (0x3E, ']'),
        (0x40, '|'),
        (0x65, ''),
    ];

    for &(code, ch) in table {
        map.insert(code, ch);
    }

    map
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_encode_decode_roundtrip() {
        let test_cases = [
            "Hello World!",
            "Hello {world} €!",
            "GSM 7-bit test: àáâãäåæçèéêë",
            "Greek letters: ΔΦΓΛΩΠΨΣΘΞ",
            "Special chars: @£$¥èé",
            "Extension chars: {[]}\\~€|^",
            "Numbers: 0123456789",
            "Punctuation: !\"#¤%&'()*+,-./:;<=>?¡¿§",
        ];

        for text in &test_cases {
            let encoded = encode(text).unwrap();
            let decoded = decode(&encoded).unwrap();
            assert_eq!(decoded, *text, "Failed roundtrip for: {}", text);
        }
    }

    #[test]
    fn test_unsupported_character() {
        // With default config (non-strict), should replace with �
        let encoded = encode("Hello 🦀 World").unwrap();
        let decoded = decode(&encoded).unwrap();
        assert_eq!(decoded, "Hello � World");
    }

    #[test]
    fn test_empty_string() {
        let encoded = encode("").unwrap();
        assert!(encoded.is_empty());
        let decoded = decode(&encoded).unwrap();
        assert!(decoded.is_empty());
    }

    #[test]
    fn test_encoded_len() {
        assert_eq!(encoded_len("Hello").unwrap(), 5);
        assert_eq!(encoded_len("Hello €").unwrap(), 7); // € is 2 bytes
        assert_eq!(encoded_len("{[]}").unwrap(), 8); // All extension chars
    }

    #[test]
    fn test_is_gsm7_compatible() {
        assert!(is_gsm7_compatible("Hello World!"));
        assert!(is_gsm7_compatible("Hello {world} €!"));
        assert!(!is_gsm7_compatible("Hello 🦀 World"));
    }

    #[test]
    fn test_non_strict_mode() {
        let config = Gsm7Config {
            strict: false,
            replacement_char: '?',
        };

        let encoded = encode_with_config("Hello 🦀 World", &config).unwrap();
        let decoded = decode(&encoded).unwrap();
        assert_eq!(decoded, "Hello ? World");
    }

    #[test]
    fn test_invalid_byte_replaced() {
        // Test byte 0x81 (outside valid range)
        let invalid_data = vec![0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x81, 0x20, 0x57]; // "Hello" + 0x81 + " W"
        let decoded = decode(&invalid_data).unwrap();
        assert_eq!(decoded, "Hello� W");
    }

    #[test]
    fn test_multiple_invalid_bytes() {
        // Test multiple invalid bytes interspersed with valid ones
        let invalid_data = vec![0x48, 0x81, 0x65, 0x82, 0x6C, 0x83]; // "H" + 0x81 + "e" + 0x82 + "l" + 0x83
        let decoded = decode(&invalid_data).unwrap();
        assert_eq!(decoded, "H�e�l�");
    }

    #[test]
    fn test_invalid_escape_sequence() {
        // Create invalid data: escape followed by unsupported code
        let invalid_data = [0x1B, 0xFF];
        let decoded = decode(&invalid_data).unwrap();
        assert_eq!(decoded, "");
    }

    #[test]
    fn test_malformed_data() {
        // Escape at end of input - should replace with �
        let invalid_data = [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x1B];
        let decoded = decode(&invalid_data).unwrap();
        assert_eq!(decoded, "Hello�");
    }

    #[test]
    fn test_all_basic_characters() {
        // Test that all basic ASCII-range characters can be encoded/decoded
        for i in 0x20..=0x7F {
            if let Some(ch) = build_gsm_table().get(&i).and_then(|&opt_ch| opt_ch) {
                let text = ch.to_string();
                let encoded = encode(&text).unwrap();
                let decoded = decode(&encoded).unwrap();
                assert_eq!(decoded, text, "Failed for character: {} (0x{:02X})", ch, i);
            }
        }
    }

    #[test]
    fn test_all_extension_characters() {
        let ext_table = build_gsm_ext_table();
        for &ch in ext_table.values() {
            let text = ch.to_string();
            let encoded = encode(&text).unwrap();
            let decoded = decode(&encoded).unwrap();
            assert_eq!(decoded, text, "Failed for extension character: {}", ch);
        }
    }
}