1use std::borrow::Cow;
37
38#[must_use]
42pub fn decode_dvb_string(bytes: &[u8]) -> String {
43 if bytes.is_empty() {
44 return String::new();
45 }
46
47 let (charset, body) = split_charset(bytes);
48 let decoded = match charset {
49 Charset::Iso6937 => decode_iso_6937(body),
50 Charset::Iso8859(n) => decode_iso_8859(n, body),
51 Charset::Utf8 => String::from_utf8_lossy(body).into_owned(),
52 Charset::Ucs2Be => decode_ucs2_be(body),
53 Charset::Ksx1001 => decode_with(encoding_rs::EUC_KR, body),
54 Charset::Gb2312 => decode_with(encoding_rs::GBK, body),
55 Charset::Big5 => decode_with(encoding_rs::BIG5, body),
56 Charset::Unsupported(_indicator) => body.iter().map(|_| '\u{FFFD}').collect(),
57 };
58
59 decoded
66 .chars()
67 .filter_map(|c| match c as u32 {
68 0x86 | 0x87 | 0xE086 | 0xE087 => None,
69 0x8A | 0xE08A => Some(' '),
70 0x0A => Some(' '),
71 code if code < 0x20 => None,
72 code if (0x80..0xA0).contains(&code) => None,
73 code if (0xE080..0xE0A0).contains(&code) => None,
74 _ => Some(c),
75 })
76 .collect()
77}
78
79#[must_use]
82pub fn decode(bytes: &[u8]) -> Cow<'_, str> {
83 if bytes.iter().all(|&b| b.is_ascii() && b >= 0x20) {
84 return Cow::Borrowed(std::str::from_utf8(bytes).unwrap_or(""));
85 }
86 Cow::Owned(decode_dvb_string(bytes))
87}
88
89#[derive(Clone, Copy, PartialEq, Eq, Hash)]
93pub struct DvbText<'a>(&'a [u8]);
94
95impl<'a> DvbText<'a> {
96 #[must_use]
98 pub const fn new(raw: &'a [u8]) -> Self {
99 Self(raw)
100 }
101 #[must_use]
103 pub const fn raw(&self) -> &'a [u8] {
104 self.0
105 }
106 #[must_use]
110 pub fn decode(&self) -> Cow<'a, str> {
111 decode(self.0)
112 }
113}
114
115impl std::ops::Deref for DvbText<'_> {
116 type Target = [u8];
119 fn deref(&self) -> &[u8] {
120 self.0
121 }
122}
123
124impl std::fmt::Display for DvbText<'_> {
125 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126 f.write_str(&self.decode())
127 }
128}
129
130impl std::fmt::Debug for DvbText<'_> {
131 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132 write!(f, "DvbText({:?})", self.decode())
133 }
134}
135
136impl<'a> From<&'a [u8]> for DvbText<'a> {
137 fn from(raw: &'a [u8]) -> Self {
138 Self(raw)
139 }
140}
141
142#[cfg(feature = "serde")]
143impl serde::Serialize for DvbText<'_> {
144 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
145 s.serialize_str(&self.decode())
146 }
147}
148#[derive(Clone, Copy, PartialEq, Eq, Hash)]
153pub struct LangCode(pub [u8; 3]);
154
155impl LangCode {
156 #[must_use]
158 pub fn as_str(&self) -> Cow<'_, str> {
159 String::from_utf8_lossy(&self.0)
160 }
161}
162
163impl std::ops::Deref for LangCode {
164 type Target = [u8; 3];
165 fn deref(&self) -> &[u8; 3] {
166 &self.0
167 }
168}
169
170impl std::fmt::Display for LangCode {
171 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172 f.write_str(&self.as_str())
173 }
174}
175
176impl std::fmt::Debug for LangCode {
177 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
178 write!(f, "LangCode({})", self.as_str())
179 }
180}
181
182#[cfg(feature = "serde")]
183impl serde::Serialize for LangCode {
184 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
185 s.serialize_str(&self.as_str())
186 }
187}
188
189#[cfg(feature = "serde")]
190impl<'de> serde::Deserialize<'de> for LangCode {
191 fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
192 let s = <std::borrow::Cow<'_, str>>::deserialize(d)?;
193 let b = s.as_bytes();
194 if b.len() != 3 {
195 return Err(serde::de::Error::invalid_length(b.len(), &"a 3-byte code"));
196 }
197 Ok(LangCode([b[0], b[1], b[2]]))
198 }
199}
200
201#[derive(Debug)]
202enum Charset {
203 Iso6937,
204 Iso8859(u8),
205 Utf8,
206 Ucs2Be,
207 Ksx1001,
209 Gb2312,
211 Big5,
213 Unsupported(u8),
214}
215
216fn split_charset(bytes: &[u8]) -> (Charset, &[u8]) {
217 match bytes[0] {
218 b if b >= 0x20 => (Charset::Iso6937, bytes),
219 0x00 => (Charset::Iso6937, &bytes[1..]),
220 0x08 => (Charset::Unsupported(0x08), &bytes[1..]),
223 0x01..=0x0B => (Charset::Iso8859(bytes[0] + 4), &bytes[1..]),
224 0x10 if bytes.len() >= 3 && bytes[1] == 0x00 => (Charset::Iso8859(bytes[2]), &bytes[3..]),
225 0x11 => (Charset::Ucs2Be, &bytes[1..]),
226 0x12 => (Charset::Ksx1001, &bytes[1..]),
227 0x13 => (Charset::Gb2312, &bytes[1..]),
228 0x14 => (Charset::Big5, &bytes[1..]),
229 0x15 => (Charset::Utf8, &bytes[1..]),
230 0x1F if bytes.len() >= 2 => (Charset::Unsupported(0x1F), &bytes[2..]),
233 other => (Charset::Unsupported(other), &bytes[1..]),
234 }
235}
236
237fn decode_iso_6937(bytes: &[u8]) -> String {
238 let mut out = String::with_capacity(bytes.len());
239 let mut i = 0;
240 while i < bytes.len() {
241 let b = bytes[i];
242 if (0xC0..=0xCF).contains(&b) {
244 match combining_mark(b) {
245 Some(mark) if i + 1 < bytes.len() => {
246 let base = bytes[i + 1];
247 if let Some(c) = combine(b, base) {
248 out.push(c);
249 } else {
250 out.push(iso_6937_single(base));
253 out.push(mark);
254 }
255 i += 2;
256 }
257 _ => {
259 out.push('\u{FFFD}');
260 i += 1;
261 }
262 }
263 continue;
264 }
265 out.push(iso_6937_single(b));
266 i += 1;
267 }
268 out
269}
270
271fn iso_6937_single(b: u8) -> char {
279 match b {
280 0x00..=0x7F => b as char,
281 0x86 | 0x87 | 0x8A => b as char,
283 0x80..=0x9F => '\u{FFFD}',
284 0xA0 => '\u{00A0}', 0xA1 => '¡',
286 0xA2 => '¢',
287 0xA3 => '£',
288 0xA4 => '\u{20AC}', 0xA5 => '¥',
290 0xA6 => '\u{FFFD}', 0xA7 => '§',
292 0xA8 => '\u{00A4}', 0xA9 => '\u{2018}', 0xAA => '\u{201C}', 0xAB => '«',
296 0xAC => '\u{2190}', 0xAD => '\u{2191}', 0xAE => '\u{2192}', 0xAF => '\u{2193}', 0xB0 => '°',
301 0xB1 => '±',
302 0xB2 => '²',
303 0xB3 => '³',
304 0xB4 => '\u{00D7}', 0xB5 => 'µ',
306 0xB6 => '¶',
307 0xB7 => '·',
308 0xB8 => '\u{00F7}', 0xB9 => '\u{2019}', 0xBA => '\u{201D}', 0xBB => '»',
312 0xBC => '¼',
313 0xBD => '½',
314 0xBE => '¾',
315 0xBF => '¿',
316 0xC0..=0xCF => '\u{FFFD}',
318 0xD0 => '\u{2015}', 0xD1 => '¹',
320 0xD2 => '®',
321 0xD3 => '©',
322 0xD4 => '\u{2122}', 0xD5 => '\u{266A}', 0xD6 => '¬',
325 0xD7 => '\u{00A6}', 0xD8..=0xDB => '\u{FFFD}', 0xDC => '\u{215B}', 0xDD => '\u{215C}', 0xDE => '\u{215D}', 0xDF => '\u{215E}', 0xE0 => '\u{2126}', 0xE1 => 'Æ',
333 0xE2 => '\u{0110}', 0xE3 => 'ª',
335 0xE4 => '\u{0126}', 0xE5 => '\u{FFFD}', 0xE6 => '\u{0132}', 0xE7 => '\u{013F}', 0xE8 => '\u{0141}', 0xE9 => 'Ø',
341 0xEA => '\u{0152}', 0xEB => 'º',
343 0xEC => 'Þ',
344 0xED => '\u{0166}', 0xEE => '\u{014A}', 0xEF => '\u{0149}', 0xF0 => '\u{0138}', 0xF1 => 'æ',
349 0xF2 => '\u{0111}', 0xF3 => 'ð',
351 0xF4 => '\u{0127}', 0xF5 => '\u{0131}', 0xF6 => '\u{0133}', 0xF7 => '\u{0140}', 0xF8 => '\u{0142}', 0xF9 => 'ø',
357 0xFA => '\u{0153}', 0xFB => 'ß',
359 0xFC => '\u{00FE}', 0xFD => '\u{0167}', 0xFE => '\u{014B}', 0xFF => '\u{00AD}', }
364}
365
366fn combining_mark(prefix: u8) -> Option<char> {
369 Some(match prefix {
370 0xC1 => '\u{0300}', 0xC2 => '\u{0301}', 0xC3 => '\u{0302}', 0xC4 => '\u{0303}', 0xC5 => '\u{0304}', 0xC6 => '\u{0306}', 0xC7 => '\u{0307}', 0xC8 => '\u{0308}', 0xCA => '\u{030A}', 0xCB => '\u{0327}', 0xCD => '\u{030B}', 0xCE => '\u{0328}', 0xCF => '\u{030C}', _ => return None,
384 })
385}
386
387fn combine(prefix: u8, base: u8) -> Option<char> {
388 Some(match (prefix, base) {
389 (0xC1, b'A') => 'À',
390 (0xC1, b'E') => 'È',
391 (0xC1, b'I') => 'Ì',
392 (0xC1, b'O') => 'Ò',
393 (0xC1, b'U') => 'Ù',
394 (0xC1, b'a') => 'à',
395 (0xC1, b'e') => 'è',
396 (0xC1, b'i') => 'ì',
397 (0xC1, b'o') => 'ò',
398 (0xC1, b'u') => 'ù',
399 (0xC2, b'A') => 'Á',
400 (0xC2, b'E') => 'É',
401 (0xC2, b'I') => 'Í',
402 (0xC2, b'O') => 'Ó',
403 (0xC2, b'U') => 'Ú',
404 (0xC2, b'Y') => 'Ý',
405 (0xC2, b'a') => 'á',
406 (0xC2, b'e') => 'é',
407 (0xC2, b'i') => 'í',
408 (0xC2, b'o') => 'ó',
409 (0xC2, b'u') => 'ú',
410 (0xC2, b'y') => 'ý',
411 (0xC2, b'C') => 'Ć',
412 (0xC2, b'c') => 'ć',
413 (0xC2, b'L') => 'Ĺ',
414 (0xC2, b'l') => 'ĺ',
415 (0xC2, b'N') => 'Ń',
416 (0xC2, b'n') => 'ń',
417 (0xC2, b'R') => 'Ŕ',
418 (0xC2, b'r') => 'ŕ',
419 (0xC2, b'S') => 'Ś',
420 (0xC2, b's') => 'ś',
421 (0xC2, b'Z') => 'Ź',
422 (0xC2, b'z') => 'ź',
423 (0xC3, b'A') => 'Â',
424 (0xC3, b'E') => 'Ê',
425 (0xC3, b'I') => 'Î',
426 (0xC3, b'O') => 'Ô',
427 (0xC3, b'U') => 'Û',
428 (0xC3, b'a') => 'â',
429 (0xC3, b'e') => 'ê',
430 (0xC3, b'i') => 'î',
431 (0xC3, b'o') => 'ô',
432 (0xC3, b'u') => 'û',
433 (0xC4, b'A') => 'Ã',
434 (0xC4, b'N') => 'Ñ',
435 (0xC4, b'O') => 'Õ',
436 (0xC4, b'a') => 'ã',
437 (0xC4, b'n') => 'ñ',
438 (0xC4, b'o') => 'õ',
439 (0xC4, b'I') => 'Ĩ',
440 (0xC4, b'i') => 'ĩ',
441 (0xC4, b'U') => 'Ũ',
442 (0xC4, b'u') => 'ũ',
443 (0xC5, b'A') => 'Ā',
445 (0xC5, b'a') => 'ā',
446 (0xC5, b'E') => 'Ē',
447 (0xC5, b'e') => 'ē',
448 (0xC5, b'I') => 'Ī',
449 (0xC5, b'i') => 'ī',
450 (0xC5, b'O') => 'Ō',
451 (0xC5, b'o') => 'ō',
452 (0xC5, b'U') => 'Ū',
453 (0xC5, b'u') => 'ū',
454 (0xC6, b'A') => 'Ă',
456 (0xC6, b'a') => 'ă',
457 (0xC6, b'G') => 'Ğ',
458 (0xC6, b'g') => 'ğ',
459 (0xC6, b'U') => 'Ŭ',
460 (0xC6, b'u') => 'ŭ',
461 (0xC7, b'C') => 'Ċ',
463 (0xC7, b'c') => 'ċ',
464 (0xC7, b'E') => 'Ė',
465 (0xC7, b'e') => 'ė',
466 (0xC7, b'G') => 'Ġ',
467 (0xC7, b'g') => 'ġ',
468 (0xC7, b'I') => 'İ',
469 (0xC7, b'Z') => 'Ż',
470 (0xC7, b'z') => 'ż',
471 (0xC8, b'A') => 'Ä',
472 (0xC8, b'E') => 'Ë',
473 (0xC8, b'I') => 'Ï',
474 (0xC8, b'O') => 'Ö',
475 (0xC8, b'U') => 'Ü',
476 (0xC8, b'Y') => 'Ÿ',
477 (0xC8, b'a') => 'ä',
478 (0xC8, b'e') => 'ë',
479 (0xC8, b'i') => 'ï',
480 (0xC8, b'o') => 'ö',
481 (0xC8, b'u') => 'ü',
482 (0xC8, b'y') => 'ÿ',
483 (0xCA, b'A') => 'Å',
485 (0xCA, b'a') => 'å',
486 (0xCA, b'U') => 'Ů',
487 (0xCA, b'u') => 'ů',
488 (0xCB, b'C') => 'Ç',
489 (0xCB, b'c') => 'ç',
490 (0xCB, b'G') => 'Ģ',
491 (0xCB, b'g') => 'ģ',
492 (0xCB, b'K') => 'Ķ',
493 (0xCB, b'k') => 'ķ',
494 (0xCB, b'L') => 'Ļ',
495 (0xCB, b'l') => 'ļ',
496 (0xCB, b'N') => 'Ņ',
497 (0xCB, b'n') => 'ņ',
498 (0xCB, b'R') => 'Ŗ',
499 (0xCB, b'r') => 'ŗ',
500 (0xCB, b'S') => 'Ş',
501 (0xCB, b's') => 'ş',
502 (0xCB, b'T') => 'Ţ',
503 (0xCB, b't') => 'ţ',
504 (0xCD, b'O') => 'Ő',
506 (0xCD, b'o') => 'ő',
507 (0xCD, b'U') => 'Ű',
508 (0xCD, b'u') => 'ű',
509 (0xCE, b'A') => 'Ą',
511 (0xCE, b'a') => 'ą',
512 (0xCE, b'E') => 'Ę',
513 (0xCE, b'e') => 'ę',
514 (0xCE, b'I') => 'Į',
515 (0xCE, b'i') => 'į',
516 (0xCE, b'U') => 'Ų',
517 (0xCE, b'u') => 'ų',
518 (0xCF, b'C') => 'Č',
520 (0xCF, b'c') => 'č',
521 (0xCF, b'D') => 'Ď',
522 (0xCF, b'd') => 'ď',
523 (0xCF, b'E') => 'Ě',
524 (0xCF, b'e') => 'ě',
525 (0xCF, b'L') => 'Ľ',
526 (0xCF, b'l') => 'ľ',
527 (0xCF, b'N') => 'Ň',
528 (0xCF, b'n') => 'ň',
529 (0xCF, b'R') => 'Ř',
530 (0xCF, b'r') => 'ř',
531 (0xCF, b'S') => 'Š',
532 (0xCF, b's') => 'š',
533 (0xCF, b'T') => 'Ť',
534 (0xCF, b't') => 'ť',
535 (0xCF, b'Z') => 'Ž',
536 (0xCF, b'z') => 'ž',
537 _ => return None,
538 })
539}
540
541fn decode_iso_8859(n: u8, bytes: &[u8]) -> String {
542 use encoding_rs::*;
543 let encoding: &'static Encoding = match n {
544 2 => ISO_8859_2,
545 3 => ISO_8859_3,
546 4 => ISO_8859_4,
547 5 => ISO_8859_5,
548 6 => ISO_8859_6,
549 7 => ISO_8859_7,
550 8 => ISO_8859_8,
551 9 => WINDOWS_1254,
552 10 => ISO_8859_10,
553 11 => WINDOWS_874,
554 13 => ISO_8859_13,
555 14 => ISO_8859_14,
556 15 => ISO_8859_15,
557 _ => return bytes.iter().map(|&b| b as char).collect(),
558 };
559 let (cow, _, _) = encoding.decode(bytes);
560 cow.into_owned()
561}
562
563fn decode_with(encoding: &'static encoding_rs::Encoding, bytes: &[u8]) -> String {
564 let (cow, _, _) = encoding.decode(bytes);
565 cow.into_owned()
566}
567
568fn decode_ucs2_be(bytes: &[u8]) -> String {
569 let code_units: Vec<u16> = bytes
570 .chunks_exact(2)
571 .map(|pair| u16::from_be_bytes([pair[0], pair[1]]))
572 .collect();
573 String::from_utf16_lossy(&code_units)
574}
575
576#[cfg(test)]
577mod tests {
578 use super::*;
579
580 #[test]
581 fn decode_empty_input_returns_empty_string() {
582 assert_eq!(decode_dvb_string(&[]), "");
583 }
584
585 #[test]
586 fn decode_plain_ascii_is_borrowed() {
587 let cow = decode(b"HELLO");
588 assert!(matches!(cow, Cow::Borrowed(_)));
589 assert_eq!(cow, "HELLO");
590 }
591
592 #[test]
593 fn decode_iso6937_latin_accent_chars() {
594 assert_eq!(decode_dvb_string(&[0x00, 0xC2, b'A']), "Á");
595 assert_eq!(decode_dvb_string(&[0x00, 0xC1, b'e']), "è");
596 assert_eq!(decode_dvb_string(&[0x00, 0xC8, b'o']), "ö");
597 }
598
599 #[test]
600 fn decode_selector_0x01_yields_iso8859_5_cyrillic() {
601 let s = decode_dvb_string(&[0x01, 0xB0, 0xB1]);
602 assert!(s.chars().all(|c| c != '\u{FFFD}'), "got: {s:?}");
603 assert!(!s.is_empty());
604 }
605
606 #[test]
607 fn decode_selector_0x10_extended_yields_iso8859_nn() {
608 let s = decode_dvb_string(&[0x10, 0x00, 0x09, b'A', b'B']);
609 assert_eq!(s, "AB");
610 }
611
612 #[test]
613 fn decode_selector_0x11_ucs2_be() {
614 let s = decode_dvb_string(&[0x11, 0x00, 0x41, 0x00, 0x42]);
615 assert_eq!(s, "AB");
616 }
617
618 #[test]
619 fn decode_selector_0x15_utf8_passthrough() {
620 let s = decode_dvb_string(&[0x15, 0xC3, 0xA9, 0xC3, 0xA9]);
621 assert_eq!(s, "éé");
622 }
623
624 #[test]
625 fn decode_control_chars_stripped_linefeed_becomes_space() {
626 let s = decode_dvb_string(b"A\x01B\nC");
627 assert_eq!(s, "AB C");
628 }
629
630 #[test]
631 fn emphasis_on_off_markers_stripped_per_annex_a2() {
632 let s = decode_dvb_string(&[0x00, b'A', 0x86, b'B', 0x87, b'C']);
635 assert_eq!(s, "ABC");
636 }
637
638 #[test]
639 fn decode_annex_a2_crlf_0x8a_becomes_space() {
640 let s = decode_dvb_string(&[0x00, b'A', 0x8A, b'B']);
642 assert_eq!(s, "A B");
643 }
644
645 #[test]
646 fn decode_selector_0x12_ksx1001_euc_kr() {
647 assert_eq!(decode_dvb_string(&[0x12, 0xB0, 0xA1]), "가");
649 }
650
651 #[test]
652 fn decode_selector_0x13_gb2312() {
653 assert_eq!(decode_dvb_string(&[0x13, 0xC4, 0xE3]), "你");
655 }
656
657 #[test]
658 fn decode_selector_0x14_big5() {
659 assert_eq!(decode_dvb_string(&[0x14, 0xA4, 0xA4]), "中");
661 }
662
663 #[test]
667 fn decode_selector_0x13_gbk_trail_byte_in_c1_range() {
668 assert_eq!(decode_dvb_string(&[0x13, 0x81, 0x80]), "亐");
669 }
670
671 #[test]
675 fn two_byte_control_codes_filtered() {
676 assert_eq!(decode_dvb_string(&[0x13, 0xAB, 0xCD]), " ");
677 assert_eq!(decode_dvb_string(&[0x13, 0xAB, 0xC3]), "");
678 }
679
680 #[test]
683 fn decode_selector_0x1f_encoding_type_id() {
684 let s = decode_dvb_string(&[0x1F, 0x01, 0x41, 0x42]);
685 assert_eq!(s.chars().count(), 2);
686 assert!(s.chars().all(|c| c == '\u{FFFD}'));
687 }
688
689 #[test]
691 fn reserved_selector_0x08_is_unsupported() {
692 let s = decode_dvb_string(&[0x08, 0x41, 0x42]);
693 assert!(s.chars().all(|c| c == '\u{FFFD}'));
694 assert_eq!(s.chars().count(), 2);
695 }
696
697 #[test]
698 fn unknown_selector_returns_replacement_characters() {
699 let s = decode_dvb_string(&[0x16, 0xAA, 0xBB, 0xCC]);
701 assert_eq!(s.chars().count(), 3);
702 assert!(s.chars().all(|c| c == '\u{FFFD}'));
703 }
704
705 #[test]
710 fn figure_a1_gr_area_single_byte_mappings() {
711 let pins: &[(u8, char)] = &[
712 (0xA0, '\u{00A0}'), (0xA1, '¡'),
714 (0xA2, '¢'),
715 (0xA3, '£'),
716 (0xA4, '\u{20AC}'), (0xA5, '¥'),
718 (0xA7, '§'),
719 (0xA8, '\u{00A4}'), (0xA9, '\u{2018}'), (0xAA, '\u{201C}'), (0xAB, '«'),
723 (0xAC, '\u{2190}'), (0xAD, '\u{2191}'), (0xAE, '\u{2192}'), (0xAF, '\u{2193}'), (0xB0, '°'),
728 (0xB1, '±'),
729 (0xB2, '²'),
730 (0xB3, '³'),
731 (0xB4, '\u{00D7}'), (0xB5, 'µ'),
733 (0xB6, '¶'),
734 (0xB7, '·'),
735 (0xB8, '\u{00F7}'), (0xB9, '\u{2019}'), (0xBA, '\u{201D}'), (0xBB, '»'),
739 (0xBC, '¼'),
740 (0xBD, '½'),
741 (0xBE, '¾'),
742 (0xBF, '¿'),
743 (0xD0, '\u{2015}'), (0xD1, '¹'),
745 (0xD2, '®'),
746 (0xD3, '©'),
747 (0xD4, '\u{2122}'), (0xD5, '\u{266A}'), (0xD6, '¬'),
750 (0xD7, '\u{00A6}'), (0xDC, '\u{215B}'), (0xDD, '\u{215C}'), (0xDE, '\u{215D}'), (0xDF, '\u{215E}'), (0xE0, '\u{2126}'), (0xE1, 'Æ'),
757 (0xE2, '\u{0110}'), (0xE3, 'ª'),
759 (0xE4, '\u{0126}'), (0xE6, '\u{0132}'), (0xE7, '\u{013F}'), (0xE8, '\u{0141}'), (0xE9, 'Ø'),
764 (0xEA, '\u{0152}'), (0xEB, 'º'),
766 (0xEC, 'Þ'),
767 (0xED, '\u{0166}'), (0xEE, '\u{014A}'), (0xEF, '\u{0149}'), (0xF0, '\u{0138}'), (0xF1, 'æ'),
772 (0xF2, '\u{0111}'), (0xF3, 'ð'),
774 (0xF4, '\u{0127}'), (0xF5, '\u{0131}'), (0xF6, '\u{0133}'), (0xF7, '\u{0140}'), (0xF8, '\u{0142}'), (0xF9, 'ø'),
780 (0xFA, '\u{0153}'), (0xFB, 'ß'),
782 (0xFC, '\u{00FE}'), (0xFD, '\u{0167}'), (0xFE, '\u{014B}'), (0xFF, '\u{00AD}'), ];
787 for &(byte, want) in pins {
788 let got = decode_dvb_string(&[0x00, byte]);
789 assert_eq!(
790 got,
791 want.to_string(),
792 "byte {byte:#04x}: want {want:?} (U+{:04X}), got {got:?}",
793 want as u32
794 );
795 }
796 }
797
798 #[test]
800 fn figure_a1_undefined_positions_are_replacement() {
801 for byte in [0xA6u8, 0xD8, 0xD9, 0xDA, 0xDB, 0xE5] {
802 let got = decode_dvb_string(&[0x00, byte]);
803 assert_eq!(got, "\u{FFFD}", "byte {byte:#04x} should be U+FFFD");
804 }
805 }
806
807 #[test]
809 fn figure_a1_combining_precomposed() {
810 assert_eq!(decode_dvb_string(&[0x00, 0xCA, b'a']), "å"); assert_eq!(decode_dvb_string(&[0x00, 0xCA, b'A']), "Å");
812 assert_eq!(decode_dvb_string(&[0x00, 0xCF, b's']), "š"); assert_eq!(decode_dvb_string(&[0x00, 0xCF, b'Z']), "Ž");
814 assert_eq!(decode_dvb_string(&[0x00, 0xCE, b'e']), "ę"); assert_eq!(decode_dvb_string(&[0x00, 0xCD, b'o']), "ő"); assert_eq!(decode_dvb_string(&[0x00, 0xC7, b'z']), "ż"); assert_eq!(decode_dvb_string(&[0x00, 0xC5, b'a']), "ā"); assert_eq!(decode_dvb_string(&[0x00, 0xC6, b'g']), "ğ"); }
820
821 #[test]
824 fn figure_a1_combining_fallback_emits_base_plus_mark() {
825 assert_eq!(decode_dvb_string(&[0x00, 0xC5, b'x']), "x\u{0304}");
826 }
827
828 #[test]
831 fn figure_a1_combining_undefined_or_dangling_prefix() {
832 assert_eq!(decode_dvb_string(&[0x00, 0xC0, b'a']), "\u{FFFD}a");
833 assert_eq!(decode_dvb_string(&[0x00, 0xC9, b'a']), "\u{FFFD}a");
834 assert_eq!(decode_dvb_string(&[0x00, 0xCC, b'a']), "\u{FFFD}a");
835 assert_eq!(decode_dvb_string(&[0x00, 0xC2]), "\u{FFFD}");
836 }
837
838 #[test]
839 fn dvb_text_decodes_with_charset_selector() {
840 let t = DvbText::new(&[0x15, 0xC3, 0xA9]); assert_eq!(t.decode(), "é");
842 assert_eq!(t.raw(), &[0x15, 0xC3, 0xA9]);
843 assert_eq!(&t[..], &[0x15, 0xC3, 0xA9]); assert_eq!(format!("{t}"), "é");
845 }
846
847 #[test]
848 fn lang_code_as_str() {
849 assert_eq!(LangCode(*b"fre").as_str(), "fre");
850 assert_eq!(LangCode([0xFF, b'r', b'e']).as_str(), "\u{FFFD}re"); }
852
853 #[cfg(feature = "serde")]
854 #[test]
855 fn dvb_text_serializes_decoded() {
856 let t = DvbText::new(&[0x15, 0xC3, 0xA9]);
857 assert_eq!(serde_json::to_string(&t).unwrap(), "\"é\"");
858 }
859
860 #[cfg(feature = "serde")]
861 #[test]
862 fn lang_code_serde_round_trip() {
863 let lc = LangCode(*b"FRA");
864 let json = serde_json::to_string(&lc).unwrap();
865 assert_eq!(json, "\"FRA\"");
866 assert_eq!(serde_json::from_str::<LangCode>(&json).unwrap(), lc);
867 assert!(serde_json::from_str::<LangCode>("\"FRAN\"").is_err()); assert_eq!(
870 serde_json::from_str::<LangCode>("\"\\u0046RA\"").unwrap(),
871 LangCode(*b"FRA")
872 );
873 }
874}