dvb-si 1.0.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
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
//! DVB-SI text decoding — ETSI EN 300 468 Annex A.
//!
//! Covers the common charsets: the default Latin table (Figure A.1, an ISO 6937
//! superset — see `iso_6937_single`), ISO 8859-n, UTF-8 (selector 0x15) and
//! UCS-2 BE (selector 0x11). Remaining Annex A coverage (Cyrillic/Arabic/Greek/
//! Hebrew figures via selectors already routed to ISO 8859-n, CJK selectors
//! 0x12–0x14, emphasis pairs) is future work.
//!
//! Glyph mappings are pinned to EN 300 468 V1.19.1 (2025-02) Figure A.1
//! "Character code table 00 - Latin alphabet with Unicode equivalents"
//! (PDF p. 159, vendored at `specs/etsi_en_300_468_v01.19.01_dvb_si.pdf`;
//! transcription in `dvb-si/docs/en_300_468.md`).

use std::borrow::Cow;

/// Decode a DVB text payload (e.g. short_event_descriptor event_name_char)
/// into an owned UTF-8 `String`. The first byte may be a charset indicator
/// per ETSI EN 300 468 Annex A Table A.3.
#[must_use]
pub fn decode_dvb_string(bytes: &[u8]) -> String {
    if bytes.is_empty() {
        return String::new();
    }

    let (charset, body) = split_charset(bytes);
    let decoded = match charset {
        Charset::Iso6937 => decode_iso_6937(body),
        Charset::Iso8859(n) => decode_iso_8859(n, body),
        Charset::Utf8 => String::from_utf8_lossy(body).into_owned(),
        Charset::Ucs2Be => decode_ucs2_be(body),
        Charset::Unsupported(_indicator) => body.iter().map(|_| '\u{FFFD}').collect(),
    };

    // Annex A.2 control codes:
    //   0x86 emphasis on, 0x87 emphasis off, 0x8A CR/LF -> space.
    //   Other C0/C1 controls are stripped.
    decoded
        .chars()
        .filter_map(|c| match c as u32 {
            0x86 | 0x87 => None,
            0x8A => Some(' '),
            0x0A => Some(' '),
            code if code < 0x20 => None,
            code if (0x80..0xA0).contains(&code) => None,
            _ => Some(c),
        })
        .collect()
}

/// Convenience wrapper returning `Cow::Borrowed` for pure-ASCII input,
/// `Cow::Owned` otherwise.
#[must_use]
pub fn decode(bytes: &[u8]) -> Cow<'_, str> {
    if bytes.iter().all(|&b| b.is_ascii() && b >= 0x20) {
        return Cow::Borrowed(std::str::from_utf8(bytes).unwrap_or(""));
    }
    Cow::Owned(decode_dvb_string(bytes))
}

#[derive(Debug)]
enum Charset {
    Iso6937,
    Iso8859(u8),
    Utf8,
    Ucs2Be,
    Unsupported(u8),
}

fn split_charset(bytes: &[u8]) -> (Charset, &[u8]) {
    match bytes[0] {
        b if b >= 0x20 => (Charset::Iso6937, bytes),
        0x00 => (Charset::Iso6937, &bytes[1..]),
        // Table A.3: 0x01..=0x0B map to ISO 8859-5..-15, EXCEPT 0x08 which is
        // "reserved for future use" (there is no ISO 8859-12).
        0x08 => (Charset::Unsupported(0x08), &bytes[1..]),
        0x01..=0x0B => (Charset::Iso8859(bytes[0] + 4), &bytes[1..]),
        0x10 if bytes.len() >= 3 && bytes[1] == 0x00 => {
            (Charset::Iso8859(bytes[2]), &bytes[3..])
        }
        0x11 => (Charset::Ucs2Be, &bytes[1..]),
        0x15 => (Charset::Utf8, &bytes[1..]),
        other => (Charset::Unsupported(other), &bytes[1..]),
    }
}

fn decode_iso_6937(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        // 0xC0..=0xCF is the Figure A.1 non-spacing (combining-prefix) row.
        if (0xC0..=0xCF).contains(&b) {
            match combining_mark(b) {
                Some(mark) if i + 1 < bytes.len() => {
                    let base = bytes[i + 1];
                    if let Some(c) = combine(b, base) {
                        out.push(c);
                    } else {
                        // No precomposed form — emit base + Unicode combining
                        // mark, which is canonically equivalent.
                        out.push(iso_6937_single(base));
                        out.push(mark);
                    }
                    i += 2;
                }
                // Undefined prefix (0xC0/0xC9/0xCC) or dangling prefix at end.
                _ => {
                    out.push('\u{FFFD}');
                    i += 1;
                }
            }
            continue;
        }
        out.push(iso_6937_single(b));
        i += 1;
    }
    out
}

/// Decode a single (non-combining) byte of the default Latin table.
///
/// Source: ETSI EN 300 468 V1.19.1 (2025-02) Figure A.1 — "Character code
/// table 00 - Latin alphabet with Unicode equivalents" (PDF p. 159). Per the
/// note under the figure, the table is a superset of ISO/IEC 6937 with the
/// Euro symbol (U+20AC) added at position 0xA4. Grey (undefined) positions
/// decode to U+FFFD.
fn iso_6937_single(b: u8) -> char {
    match b {
        0x00..=0x7F => b as char,
        // Preserve ETSI Annex A.2 C1 control codes so the post-filter can act on them.
        0x86 | 0x87 | 0x8A => b as char,
        0x80..=0x9F => '\u{FFFD}',
        0xA0 => '\u{00A0}', // NBSP
        0xA1 => '¡',
        0xA2 => '¢',
        0xA3 => '£',
        0xA4 => '\u{20AC}', // € — DVB addition (note under Figure A.1)
        0xA5 => '¥',
        0xA6 => '\u{FFFD}', // undefined
        0xA7 => '§',
        0xA8 => '\u{00A4}', // ¤ general currency sign
        0xA9 => '\u{2018}', // ' left single quotation mark
        0xAA => '\u{201C}', // " left double quotation mark
        0xAB => '«',
        0xAC => '\u{2190}', //        0xAD => '\u{2191}', //        0xAE => '\u{2192}', //        0xAF => '\u{2193}', //        0xB0 => '°',
        0xB1 => '±',
        0xB2 => '²',
        0xB3 => '³',
        0xB4 => '\u{00D7}', // ×
        0xB5 => 'µ',
        0xB6 => '',
        0xB7 => '·',
        0xB8 => '\u{00F7}', // ÷
        0xB9 => '\u{2019}', // ' right single quotation mark
        0xBA => '\u{201D}', // " right double quotation mark
        0xBB => '»',
        0xBC => '¼',
        0xBD => '½',
        0xBE => '¾',
        0xBF => '¿',
        // Combining-prefix row; reached only for a dangling/undefined prefix.
        0xC0..=0xCF => '\u{FFFD}',
        0xD0 => '\u{2015}', // ― horizontal bar
        0xD1 => '¹',
        0xD2 => '®',
        0xD3 => '©',
        0xD4 => '\u{2122}', //        0xD5 => '\u{266A}', // ♪ eighth note
        0xD6 => '¬',
        0xD7 => '\u{00A6}', // ¦ broken bar
        0xD8..=0xDB => '\u{FFFD}', // undefined
        0xDC => '\u{215B}', //        0xDD => '\u{215C}', //        0xDE => '\u{215D}', //        0xDF => '\u{215E}', //        0xE0 => '\u{2126}', // Ω OHM SIGN
        0xE1 => 'Æ',
        0xE2 => '\u{0110}', // Đ
        0xE3 => 'ª',
        0xE4 => '\u{0126}', // Ħ
        0xE5 => '\u{FFFD}', // undefined
        0xE6 => '\u{0132}', // IJ
        0xE7 => '\u{013F}', // Ŀ
        0xE8 => '\u{0141}', // Ł
        0xE9 => 'Ø',
        0xEA => '\u{0152}', // Œ
        0xEB => 'º',
        0xEC => 'Þ',
        0xED => '\u{0166}', // Ŧ
        0xEE => '\u{014A}', // Ŋ
        0xEF => '\u{0149}', // ʼn
        0xF0 => '\u{0138}', // ĸ
        0xF1 => 'æ',
        0xF2 => '\u{0111}', // đ
        0xF3 => 'ð',
        0xF4 => '\u{0127}', // ħ
        0xF5 => '\u{0131}', // ı dotless i
        0xF6 => '\u{0133}', // ij
        0xF7 => '\u{0140}', // ŀ
        0xF8 => '\u{0142}', // ł
        0xF9 => 'ø',
        0xFA => '\u{0153}', // œ
        0xFB => 'ß',
        0xFC => '\u{00FE}', // þ
        0xFD => '\u{0167}', // ŧ
        0xFE => '\u{014B}', // ŋ
        0xFF => '\u{00AD}', // SHY soft hyphen
    }
}

/// Unicode combining mark for a Figure A.1 non-spacing prefix byte
/// (row 0xC0..=0xCF). `None` for the undefined positions 0xC0/0xC9/0xCC.
fn combining_mark(prefix: u8) -> Option<char> {
    Some(match prefix {
        0xC1 => '\u{0300}', // grave
        0xC2 => '\u{0301}', // acute
        0xC3 => '\u{0302}', // circumflex
        0xC4 => '\u{0303}', // tilde
        0xC5 => '\u{0304}', // macron
        0xC6 => '\u{0306}', // breve
        0xC7 => '\u{0307}', // dot above
        0xC8 => '\u{0308}', // diaeresis
        0xCA => '\u{030A}', // ring above
        0xCB => '\u{0327}', // cedilla
        0xCD => '\u{030B}', // double acute
        0xCE => '\u{0328}', // ogonek
        0xCF => '\u{030C}', // caron
        _ => return None,
    })
}

fn combine(prefix: u8, base: u8) -> Option<char> {
    Some(match (prefix, base) {
        (0xC1, b'A') => 'À', (0xC1, b'E') => 'È', (0xC1, b'I') => 'Ì',
        (0xC1, b'O') => 'Ò', (0xC1, b'U') => 'Ù',
        (0xC1, b'a') => 'à', (0xC1, b'e') => 'è', (0xC1, b'i') => 'ì',
        (0xC1, b'o') => 'ò', (0xC1, b'u') => 'ù',
        (0xC2, b'A') => 'Á', (0xC2, b'E') => 'É', (0xC2, b'I') => 'Í',
        (0xC2, b'O') => 'Ó', (0xC2, b'U') => 'Ú', (0xC2, b'Y') => 'Ý',
        (0xC2, b'a') => 'á', (0xC2, b'e') => 'é', (0xC2, b'i') => 'í',
        (0xC2, b'o') => 'ó', (0xC2, b'u') => 'ú', (0xC2, b'y') => 'ý',
        (0xC2, b'C') => 'Ć', (0xC2, b'c') => 'ć', (0xC2, b'L') => 'Ĺ',
        (0xC2, b'l') => 'ĺ', (0xC2, b'N') => 'Ń', (0xC2, b'n') => 'ń',
        (0xC2, b'R') => 'Ŕ', (0xC2, b'r') => 'ŕ', (0xC2, b'S') => 'Ś',
        (0xC2, b's') => 'ś', (0xC2, b'Z') => 'Ź', (0xC2, b'z') => 'ź',
        (0xC3, b'A') => 'Â', (0xC3, b'E') => 'Ê', (0xC3, b'I') => 'Î',
        (0xC3, b'O') => 'Ô', (0xC3, b'U') => 'Û',
        (0xC3, b'a') => 'â', (0xC3, b'e') => 'ê', (0xC3, b'i') => 'î',
        (0xC3, b'o') => 'ô', (0xC3, b'u') => 'û',
        (0xC4, b'A') => 'Ã', (0xC4, b'N') => 'Ñ', (0xC4, b'O') => 'Õ',
        (0xC4, b'a') => 'ã', (0xC4, b'n') => 'ñ', (0xC4, b'o') => 'õ',
        (0xC4, b'I') => 'Ĩ', (0xC4, b'i') => 'ĩ', (0xC4, b'U') => 'Ũ',
        (0xC4, b'u') => 'ũ',
        // macron
        (0xC5, b'A') => 'Ā', (0xC5, b'a') => 'ā', (0xC5, b'E') => 'Ē',
        (0xC5, b'e') => 'ē', (0xC5, b'I') => 'Ī', (0xC5, b'i') => 'ī',
        (0xC5, b'O') => 'Ō', (0xC5, b'o') => 'ō', (0xC5, b'U') => 'Ū',
        (0xC5, b'u') => 'ū',
        // breve
        (0xC6, b'A') => 'Ă', (0xC6, b'a') => 'ă', (0xC6, b'G') => 'Ğ',
        (0xC6, b'g') => 'ğ', (0xC6, b'U') => 'Ŭ', (0xC6, b'u') => 'ŭ',
        // dot above
        (0xC7, b'C') => 'Ċ', (0xC7, b'c') => 'ċ', (0xC7, b'E') => 'Ė',
        (0xC7, b'e') => 'ė', (0xC7, b'G') => 'Ġ', (0xC7, b'g') => 'ġ',
        (0xC7, b'I') => 'İ', (0xC7, b'Z') => 'Ż', (0xC7, b'z') => 'ż',
        (0xC8, b'A') => 'Ä', (0xC8, b'E') => 'Ë', (0xC8, b'I') => 'Ï',
        (0xC8, b'O') => 'Ö', (0xC8, b'U') => 'Ü', (0xC8, b'Y') => 'Ÿ',
        (0xC8, b'a') => 'ä', (0xC8, b'e') => 'ë', (0xC8, b'i') => 'ï',
        (0xC8, b'o') => 'ö', (0xC8, b'u') => 'ü', (0xC8, b'y') => 'ÿ',
        // ring above
        (0xCA, b'A') => 'Å', (0xCA, b'a') => 'å', (0xCA, b'U') => 'Ů',
        (0xCA, b'u') => 'ů',
        (0xCB, b'C') => 'Ç', (0xCB, b'c') => 'ç', (0xCB, b'G') => 'Ģ',
        (0xCB, b'g') => 'ģ', (0xCB, b'K') => 'Ķ', (0xCB, b'k') => 'ķ',
        (0xCB, b'L') => 'Ļ', (0xCB, b'l') => 'ļ', (0xCB, b'N') => 'Ņ',
        (0xCB, b'n') => 'ņ', (0xCB, b'R') => 'Ŗ', (0xCB, b'r') => 'ŗ',
        (0xCB, b'S') => 'Ş', (0xCB, b's') => 'ş', (0xCB, b'T') => 'Ţ',
        (0xCB, b't') => 'ţ',
        // double acute
        (0xCD, b'O') => 'Ő', (0xCD, b'o') => 'ő', (0xCD, b'U') => 'Ű',
        (0xCD, b'u') => 'ű',
        // ogonek
        (0xCE, b'A') => 'Ą', (0xCE, b'a') => 'ą', (0xCE, b'E') => 'Ę',
        (0xCE, b'e') => 'ę', (0xCE, b'I') => 'Į', (0xCE, b'i') => 'į',
        (0xCE, b'U') => 'Ų', (0xCE, b'u') => 'ų',
        // caron
        (0xCF, b'C') => 'Č', (0xCF, b'c') => 'č', (0xCF, b'D') => 'Ď',
        (0xCF, b'd') => 'ď', (0xCF, b'E') => 'Ě', (0xCF, b'e') => 'ě',
        (0xCF, b'L') => 'Ľ', (0xCF, b'l') => 'ľ', (0xCF, b'N') => 'Ň',
        (0xCF, b'n') => 'ň', (0xCF, b'R') => 'Ř', (0xCF, b'r') => 'ř',
        (0xCF, b'S') => 'Š', (0xCF, b's') => 'š', (0xCF, b'T') => 'Ť',
        (0xCF, b't') => 'ť', (0xCF, b'Z') => 'Ž', (0xCF, b'z') => 'ž',
        _ => return None,
    })
}

fn decode_iso_8859(n: u8, bytes: &[u8]) -> String {
    use encoding_rs::*;
    let encoding: &'static Encoding = match n {
        2 => ISO_8859_2,
        3 => ISO_8859_3,
        4 => ISO_8859_4,
        5 => ISO_8859_5,
        6 => ISO_8859_6,
        7 => ISO_8859_7,
        8 => ISO_8859_8,
        9 => WINDOWS_1254,
        10 => ISO_8859_10,
        11 => WINDOWS_874,
        13 => ISO_8859_13,
        14 => ISO_8859_14,
        15 => ISO_8859_15,
        _ => return bytes.iter().map(|&b| b as char).collect(),
    };
    let (cow, _, _) = encoding.decode(bytes);
    cow.into_owned()
}

fn decode_ucs2_be(bytes: &[u8]) -> String {
    let code_units: Vec<u16> = bytes
        .chunks_exact(2)
        .map(|pair| u16::from_be_bytes([pair[0], pair[1]]))
        .collect();
    String::from_utf16_lossy(&code_units)
}

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

    #[test]
    fn decode_empty_input_returns_empty_string() {
        assert_eq!(decode_dvb_string(&[]), "");
    }

    #[test]
    fn decode_plain_ascii_is_borrowed() {
        let cow = decode(b"HELLO");
        assert!(matches!(cow, Cow::Borrowed(_)));
        assert_eq!(cow, "HELLO");
    }

    #[test]
    fn decode_iso6937_latin_accent_chars() {
        assert_eq!(decode_dvb_string(&[0x00, 0xC2, b'A']), "Á");
        assert_eq!(decode_dvb_string(&[0x00, 0xC1, b'e']), "è");
        assert_eq!(decode_dvb_string(&[0x00, 0xC8, b'o']), "ö");
    }

    #[test]
    fn decode_selector_0x01_yields_iso8859_5_cyrillic() {
        let s = decode_dvb_string(&[0x01, 0xB0, 0xB1]);
        assert!(s.chars().all(|c| c != '\u{FFFD}'), "got: {s:?}");
        assert!(!s.is_empty());
    }

    #[test]
    fn decode_selector_0x10_extended_yields_iso8859_nn() {
        let s = decode_dvb_string(&[0x10, 0x00, 0x09, b'A', b'B']);
        assert_eq!(s, "AB");
    }

    #[test]
    fn decode_selector_0x11_ucs2_be() {
        let s = decode_dvb_string(&[0x11, 0x00, 0x41, 0x00, 0x42]);
        assert_eq!(s, "AB");
    }

    #[test]
    fn decode_selector_0x15_utf8_passthrough() {
        let s = decode_dvb_string(&[0x15, 0xC3, 0xA9, 0xC3, 0xA9]);
        assert_eq!(s, "éé");
    }

    #[test]
    fn decode_control_chars_stripped_linefeed_becomes_space() {
        let s = decode_dvb_string(b"A\x01B\nC");
        assert_eq!(s, "AB C");
    }

    #[test]
    fn emphasis_on_off_markers_stripped_per_annex_a2() {
        // 0x86 and 0x87 are emphasis on/off markers per ETSI Annex A.2 — not
        // representable in plain text, strip silently.
        let s = decode_dvb_string(&[0x00, b'A', 0x86, b'B', 0x87, b'C']);
        assert_eq!(s, "ABC");
    }

    #[test]
    fn decode_annex_a2_crlf_0x8a_becomes_space() {
        // 0x8A in DVB text maps to CR/LF per Annex A.2 — render as space.
        let s = decode_dvb_string(&[0x00, b'A', 0x8A, b'B']);
        assert_eq!(s, "A B");
    }

    /// Table A.3 marks single-byte selector 0x08 reserved (no ISO 8859-12).
    #[test]
    fn reserved_selector_0x08_is_unsupported() {
        let s = decode_dvb_string(&[0x08, 0x41, 0x42]);
        assert!(s.chars().all(|c| c == '\u{FFFD}'));
        assert_eq!(s.chars().count(), 2);
    }

    #[test]
    fn unknown_selector_returns_replacement_characters() {
        // Selector 0x1F is reserved/unsupported — each byte becomes U+FFFD.
        let s = decode_dvb_string(&[0x1F, 0xAA, 0xBB, 0xCC]);
        assert_eq!(s.chars().count(), 3);
        assert!(s.chars().all(|c| c == '\u{FFFD}'));
    }

    /// Pins the GR-area single-byte mappings to ETSI EN 300 468 V1.19.1
    /// (2025-02) Figure A.1 — "Character code table 00 - Latin alphabet with
    /// Unicode equivalents" (PDF p. 159; vendored at
    /// `specs/etsi_en_300_468_v01.19.01_dvb_si.pdf`).
    #[test]
    fn figure_a1_gr_area_single_byte_mappings() {
        let pins: &[(u8, char)] = &[
            (0xA0, '\u{00A0}'), // NBSP
            (0xA1, '¡'),
            (0xA2, '¢'),
            (0xA3, '£'),
            (0xA4, '\u{20AC}'), // € — DVB addition (note under Figure A.1)
            (0xA5, '¥'),
            (0xA7, '§'),
            (0xA8, '\u{00A4}'), // ¤ general currency sign
            (0xA9, '\u{2018}'), // '
            (0xAA, '\u{201C}'), // "
            (0xAB, '«'),
            (0xAC, '\u{2190}'), //            (0xAD, '\u{2191}'), //            (0xAE, '\u{2192}'), //            (0xAF, '\u{2193}'), //            (0xB0, '°'),
            (0xB1, '±'),
            (0xB2, '²'),
            (0xB3, '³'),
            (0xB4, '\u{00D7}'), // ×
            (0xB5, 'µ'),
            (0xB6, ''),
            (0xB7, '·'),
            (0xB8, '\u{00F7}'), // ÷
            (0xB9, '\u{2019}'), // '
            (0xBA, '\u{201D}'), // "
            (0xBB, '»'),
            (0xBC, '¼'),
            (0xBD, '½'),
            (0xBE, '¾'),
            (0xBF, '¿'),
            (0xD0, '\u{2015}'), //            (0xD1, '¹'),
            (0xD2, '®'),
            (0xD3, '©'),
            (0xD4, '\u{2122}'), //            (0xD5, '\u{266A}'), //            (0xD6, '¬'),
            (0xD7, '\u{00A6}'), // ¦
            (0xDC, '\u{215B}'), //            (0xDD, '\u{215C}'), //            (0xDE, '\u{215D}'), //            (0xDF, '\u{215E}'), //            (0xE0, '\u{2126}'), // Ω OHM SIGN
            (0xE1, 'Æ'),
            (0xE2, '\u{0110}'), // Đ
            (0xE3, 'ª'),
            (0xE4, '\u{0126}'), // Ħ
            (0xE6, '\u{0132}'), // IJ
            (0xE7, '\u{013F}'), // Ŀ
            (0xE8, '\u{0141}'), // Ł
            (0xE9, 'Ø'),
            (0xEA, '\u{0152}'), // Œ
            (0xEB, 'º'),
            (0xEC, 'Þ'),
            (0xED, '\u{0166}'), // Ŧ
            (0xEE, '\u{014A}'), // Ŋ
            (0xEF, '\u{0149}'), // ʼn
            (0xF0, '\u{0138}'), // ĸ
            (0xF1, 'æ'),
            (0xF2, '\u{0111}'), // đ
            (0xF3, 'ð'),
            (0xF4, '\u{0127}'), // ħ
            (0xF5, '\u{0131}'), // ı
            (0xF6, '\u{0133}'), // ij
            (0xF7, '\u{0140}'), // ŀ
            (0xF8, '\u{0142}'), // ł
            (0xF9, 'ø'),
            (0xFA, '\u{0153}'), // œ
            (0xFB, 'ß'),
            (0xFC, '\u{00FE}'), // þ
            (0xFD, '\u{0167}'), // ŧ
            (0xFE, '\u{014B}'), // ŋ
            (0xFF, '\u{00AD}'), // SHY soft hyphen
        ];
        for &(byte, want) in pins {
            let got = decode_dvb_string(&[0x00, byte]);
            assert_eq!(
                got,
                want.to_string(),
                "byte {byte:#04x}: want {want:?} (U+{:04X}), got {got:?}",
                want as u32
            );
        }
    }

    /// Bytes undefined (grey) in Figure A.1 decode to U+FFFD.
    #[test]
    fn figure_a1_undefined_positions_are_replacement() {
        for byte in [0xA6u8, 0xD8, 0xD9, 0xDA, 0xDB, 0xE5] {
            let got = decode_dvb_string(&[0x00, byte]);
            assert_eq!(got, "\u{FFFD}", "byte {byte:#04x} should be U+FFFD");
        }
    }

    /// C-row prefixes with precomposed entries (Figure A.1 non-spacing row).
    #[test]
    fn figure_a1_combining_precomposed() {
        assert_eq!(decode_dvb_string(&[0x00, 0xCA, b'a']), "å"); // ring U+030A
        assert_eq!(decode_dvb_string(&[0x00, 0xCA, b'A']), "Å");
        assert_eq!(decode_dvb_string(&[0x00, 0xCF, b's']), "š"); // caron U+030C
        assert_eq!(decode_dvb_string(&[0x00, 0xCF, b'Z']), "Ž");
        assert_eq!(decode_dvb_string(&[0x00, 0xCE, b'e']), "ę"); // ogonek U+0328
        assert_eq!(decode_dvb_string(&[0x00, 0xCD, b'o']), "ő"); // double acute U+030B
        assert_eq!(decode_dvb_string(&[0x00, 0xC7, b'z']), "ż"); // dot above U+0307
        assert_eq!(decode_dvb_string(&[0x00, 0xC5, b'a']), "ā"); // macron U+0304
        assert_eq!(decode_dvb_string(&[0x00, 0xC6, b'g']), "ğ"); // breve U+0306
    }

    /// A defined prefix with no precomposed form falls back to
    /// base + Unicode combining mark (canonically equivalent).
    #[test]
    fn figure_a1_combining_fallback_emits_base_plus_mark() {
        assert_eq!(decode_dvb_string(&[0x00, 0xC5, b'x']), "x\u{0304}");
    }

    /// Undefined C-row prefixes (0xC0, 0xC9, 0xCC) and a dangling prefix at
    /// end of input decode to U+FFFD.
    #[test]
    fn figure_a1_combining_undefined_or_dangling_prefix() {
        assert_eq!(decode_dvb_string(&[0x00, 0xC0, b'a']), "\u{FFFD}a");
        assert_eq!(decode_dvb_string(&[0x00, 0xC9, b'a']), "\u{FFFD}a");
        assert_eq!(decode_dvb_string(&[0x00, 0xCC, b'a']), "\u{FFFD}a");
        assert_eq!(decode_dvb_string(&[0x00, 0xC2]), "\u{FFFD}");
    }
}