1use alloc::borrow::Cow;
38use alloc::string::String;
39use alloc::vec::Vec;
40
41#[must_use]
45pub fn decode_dvb_string(bytes: &[u8]) -> String {
46 if bytes.is_empty() {
47 return String::new();
48 }
49
50 let (charset, body) = split_charset(bytes);
51 let decoded = match charset {
52 Charset::Iso6937 => decode_iso_6937(body),
53 Charset::Iso8859(n) => decode_iso_8859(n, body),
54 Charset::Utf8 => String::from_utf8_lossy(body).into_owned(),
55 Charset::Ucs2Be => decode_ucs2_be(body),
56 #[cfg(feature = "std")]
57 Charset::Ksx1001 => decode_with(encoding_rs::EUC_KR, body),
58 #[cfg(feature = "std")]
59 Charset::Gb2312 => decode_with(encoding_rs::GBK, body),
60 #[cfg(feature = "std")]
61 Charset::Big5 => decode_with(encoding_rs::BIG5, body),
62 #[cfg(not(feature = "std"))]
66 Charset::Ksx1001 | Charset::Gb2312 | Charset::Big5 => {
67 body.iter().map(|_| '\u{FFFD}').collect()
68 }
69 Charset::Unsupported(_indicator) => body.iter().map(|_| '\u{FFFD}').collect(),
70 };
71
72 decoded
79 .chars()
80 .filter_map(|c| match c as u32 {
81 0x86 | 0x87 | 0xE086 | 0xE087 => None,
82 0x8A | 0xE08A => Some(' '),
83 0x0A => Some(' '),
84 0x00..0x20 => None,
85 0x80..0xA0 => None,
86 0xE080..0xE0A0 => None,
87 _ => Some(c),
88 })
89 .collect()
90}
91
92#[must_use]
95pub fn decode(bytes: &[u8]) -> Cow<'_, str> {
96 if bytes.iter().all(|&b| b.is_ascii() && b >= 0x20) {
97 return Cow::Borrowed(core::str::from_utf8(bytes).unwrap_or(""));
98 }
99 Cow::Owned(decode_dvb_string(bytes))
100}
101
102#[derive(Clone, Copy, PartialEq, Eq, Hash)]
106#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
107pub struct DvbText<'a>(&'a [u8]);
108
109impl<'a> DvbText<'a> {
110 #[must_use]
112 pub const fn new(raw: &'a [u8]) -> Self {
113 Self(raw)
114 }
115 #[must_use]
117 pub const fn raw(&self) -> &'a [u8] {
118 self.0
119 }
120 #[must_use]
124 pub fn decode(&self) -> Cow<'a, str> {
125 decode(self.0)
126 }
127}
128
129impl core::ops::Deref for DvbText<'_> {
130 type Target = [u8];
133 fn deref(&self) -> &[u8] {
134 self.0
135 }
136}
137
138impl core::fmt::Display for DvbText<'_> {
139 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
140 f.write_str(&self.decode())
141 }
142}
143
144impl core::fmt::Debug for DvbText<'_> {
145 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
146 write!(f, "DvbText({:?})", self.decode())
147 }
148}
149
150impl<'a> From<&'a [u8]> for DvbText<'a> {
151 fn from(raw: &'a [u8]) -> Self {
152 Self(raw)
153 }
154}
155
156#[cfg(feature = "serde")]
157impl serde::Serialize for DvbText<'_> {
158 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
159 s.serialize_str(&self.decode())
160 }
161}
162#[derive(Clone, Copy, PartialEq, Eq, Hash)]
167pub struct LangCode(pub [u8; 3]);
168
169impl LangCode {
170 #[must_use]
172 pub fn as_str(&self) -> Cow<'_, str> {
173 String::from_utf8_lossy(&self.0)
174 }
175}
176
177impl core::ops::Deref for LangCode {
178 type Target = [u8; 3];
179 fn deref(&self) -> &[u8; 3] {
180 &self.0
181 }
182}
183
184impl core::fmt::Display for LangCode {
185 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
186 f.write_str(&self.as_str())
187 }
188}
189
190impl core::fmt::Debug for LangCode {
191 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
192 write!(f, "LangCode({})", self.as_str())
193 }
194}
195
196#[cfg(feature = "serde")]
197impl serde::Serialize for LangCode {
198 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
199 s.serialize_str(&self.as_str())
200 }
201}
202
203#[derive(Debug)]
204enum Charset {
205 Iso6937,
206 Iso8859(u8),
207 Utf8,
208 Ucs2Be,
209 Ksx1001,
211 Gb2312,
213 Big5,
215 Unsupported(u8),
216}
217
218fn split_charset(bytes: &[u8]) -> (Charset, &[u8]) {
219 match bytes[0] {
220 b if b >= 0x20 => (Charset::Iso6937, bytes),
221 0x00 => (Charset::Iso6937, &bytes[1..]),
222 0x08 => (Charset::Unsupported(0x08), &bytes[1..]),
225 0x01..=0x0B => (Charset::Iso8859(bytes[0] + 4), &bytes[1..]),
226 0x10 if bytes.len() >= 3 && bytes[1] == 0x00 => (Charset::Iso8859(bytes[2]), &bytes[3..]),
227 0x11 => (Charset::Ucs2Be, &bytes[1..]),
228 0x12 => (Charset::Ksx1001, &bytes[1..]),
229 0x13 => (Charset::Gb2312, &bytes[1..]),
230 0x14 => (Charset::Big5, &bytes[1..]),
231 0x15 => (Charset::Utf8, &bytes[1..]),
232 0x1F if bytes.len() >= 2 => (Charset::Unsupported(0x1F), &bytes[2..]),
235 other => (Charset::Unsupported(other), &bytes[1..]),
236 }
237}
238
239fn decode_iso_6937(bytes: &[u8]) -> String {
240 let mut out = String::with_capacity(bytes.len());
241 let mut i = 0;
242 while i < bytes.len() {
243 let b = bytes[i];
244 if (0xC0..=0xCF).contains(&b) {
246 match combining_mark(b) {
247 Some(mark) if i + 1 < bytes.len() => {
248 let base = bytes[i + 1];
249 if let Some(c) = combine(b, base) {
250 out.push(c);
251 } else {
252 out.push(iso_6937_single(base));
255 out.push(mark);
256 }
257 i += 2;
258 }
259 _ => {
261 out.push('\u{FFFD}');
262 i += 1;
263 }
264 }
265 continue;
266 }
267 out.push(iso_6937_single(b));
268 i += 1;
269 }
270 out
271}
272
273fn iso_6937_single(b: u8) -> char {
281 match b {
282 0x00..=0x7F => b as char,
283 0x86 | 0x87 | 0x8A => b as char,
285 0x80..=0x9F => '\u{FFFD}',
286 0xA0 => '\u{00A0}', 0xA1 => '¡',
288 0xA2 => '¢',
289 0xA3 => '£',
290 0xA4 => '\u{20AC}', 0xA5 => '¥',
292 0xA6 => '\u{FFFD}', 0xA7 => '§',
294 0xA8 => '\u{00A4}', 0xA9 => '\u{2018}', 0xAA => '\u{201C}', 0xAB => '«',
298 0xAC => '\u{2190}', 0xAD => '\u{2191}', 0xAE => '\u{2192}', 0xAF => '\u{2193}', 0xB0 => '°',
303 0xB1 => '±',
304 0xB2 => '²',
305 0xB3 => '³',
306 0xB4 => '\u{00D7}', 0xB5 => 'µ',
308 0xB6 => '¶',
309 0xB7 => '·',
310 0xB8 => '\u{00F7}', 0xB9 => '\u{2019}', 0xBA => '\u{201D}', 0xBB => '»',
314 0xBC => '¼',
315 0xBD => '½',
316 0xBE => '¾',
317 0xBF => '¿',
318 0xC0..=0xCF => '\u{FFFD}',
320 0xD0 => '\u{2015}', 0xD1 => '¹',
322 0xD2 => '®',
323 0xD3 => '©',
324 0xD4 => '\u{2122}', 0xD5 => '\u{266A}', 0xD6 => '¬',
327 0xD7 => '\u{00A6}', 0xD8..=0xDB => '\u{FFFD}', 0xDC => '\u{215B}', 0xDD => '\u{215C}', 0xDE => '\u{215D}', 0xDF => '\u{215E}', 0xE0 => '\u{2126}', 0xE1 => 'Æ',
335 0xE2 => '\u{0110}', 0xE3 => 'ª',
337 0xE4 => '\u{0126}', 0xE5 => '\u{FFFD}', 0xE6 => '\u{0132}', 0xE7 => '\u{013F}', 0xE8 => '\u{0141}', 0xE9 => 'Ø',
343 0xEA => '\u{0152}', 0xEB => 'º',
345 0xEC => 'Þ',
346 0xED => '\u{0166}', 0xEE => '\u{014A}', 0xEF => '\u{0149}', 0xF0 => '\u{0138}', 0xF1 => 'æ',
351 0xF2 => '\u{0111}', 0xF3 => 'ð',
353 0xF4 => '\u{0127}', 0xF5 => '\u{0131}', 0xF6 => '\u{0133}', 0xF7 => '\u{0140}', 0xF8 => '\u{0142}', 0xF9 => 'ø',
359 0xFA => '\u{0153}', 0xFB => 'ß',
361 0xFC => '\u{00FE}', 0xFD => '\u{0167}', 0xFE => '\u{014B}', 0xFF => '\u{00AD}', }
366}
367
368fn combining_mark(prefix: u8) -> Option<char> {
371 Some(match prefix {
372 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,
386 })
387}
388
389fn combine(prefix: u8, base: u8) -> Option<char> {
390 Some(match (prefix, base) {
391 (0xC1, b'A') => 'À',
392 (0xC1, b'E') => 'È',
393 (0xC1, b'I') => 'Ì',
394 (0xC1, b'O') => 'Ò',
395 (0xC1, b'U') => 'Ù',
396 (0xC1, b'a') => 'à',
397 (0xC1, b'e') => 'è',
398 (0xC1, b'i') => 'ì',
399 (0xC1, b'o') => 'ò',
400 (0xC1, b'u') => 'ù',
401 (0xC2, b'A') => 'Á',
402 (0xC2, b'E') => 'É',
403 (0xC2, b'I') => 'Í',
404 (0xC2, b'O') => 'Ó',
405 (0xC2, b'U') => 'Ú',
406 (0xC2, b'Y') => 'Ý',
407 (0xC2, b'a') => 'á',
408 (0xC2, b'e') => 'é',
409 (0xC2, b'i') => 'í',
410 (0xC2, b'o') => 'ó',
411 (0xC2, b'u') => 'ú',
412 (0xC2, b'y') => 'ý',
413 (0xC2, b'C') => 'Ć',
414 (0xC2, b'c') => 'ć',
415 (0xC2, b'L') => 'Ĺ',
416 (0xC2, b'l') => 'ĺ',
417 (0xC2, b'N') => 'Ń',
418 (0xC2, b'n') => 'ń',
419 (0xC2, b'R') => 'Ŕ',
420 (0xC2, b'r') => 'ŕ',
421 (0xC2, b'S') => 'Ś',
422 (0xC2, b's') => 'ś',
423 (0xC2, b'Z') => 'Ź',
424 (0xC2, b'z') => 'ź',
425 (0xC3, b'A') => 'Â',
426 (0xC3, b'E') => 'Ê',
427 (0xC3, b'I') => 'Î',
428 (0xC3, b'O') => 'Ô',
429 (0xC3, b'U') => 'Û',
430 (0xC3, b'a') => 'â',
431 (0xC3, b'e') => 'ê',
432 (0xC3, b'i') => 'î',
433 (0xC3, b'o') => 'ô',
434 (0xC3, b'u') => 'û',
435 (0xC4, b'A') => 'Ã',
436 (0xC4, b'N') => 'Ñ',
437 (0xC4, b'O') => 'Õ',
438 (0xC4, b'a') => 'ã',
439 (0xC4, b'n') => 'ñ',
440 (0xC4, b'o') => 'õ',
441 (0xC4, b'I') => 'Ĩ',
442 (0xC4, b'i') => 'ĩ',
443 (0xC4, b'U') => 'Ũ',
444 (0xC4, b'u') => 'ũ',
445 (0xC5, b'A') => 'Ā',
447 (0xC5, b'a') => 'ā',
448 (0xC5, b'E') => 'Ē',
449 (0xC5, b'e') => 'ē',
450 (0xC5, b'I') => 'Ī',
451 (0xC5, b'i') => 'ī',
452 (0xC5, b'O') => 'Ō',
453 (0xC5, b'o') => 'ō',
454 (0xC5, b'U') => 'Ū',
455 (0xC5, b'u') => 'ū',
456 (0xC6, b'A') => 'Ă',
458 (0xC6, b'a') => 'ă',
459 (0xC6, b'G') => 'Ğ',
460 (0xC6, b'g') => 'ğ',
461 (0xC6, b'U') => 'Ŭ',
462 (0xC6, b'u') => 'ŭ',
463 (0xC7, b'C') => 'Ċ',
465 (0xC7, b'c') => 'ċ',
466 (0xC7, b'E') => 'Ė',
467 (0xC7, b'e') => 'ė',
468 (0xC7, b'G') => 'Ġ',
469 (0xC7, b'g') => 'ġ',
470 (0xC7, b'I') => 'İ',
471 (0xC7, b'Z') => 'Ż',
472 (0xC7, b'z') => 'ż',
473 (0xC8, b'A') => 'Ä',
474 (0xC8, b'E') => 'Ë',
475 (0xC8, b'I') => 'Ï',
476 (0xC8, b'O') => 'Ö',
477 (0xC8, b'U') => 'Ü',
478 (0xC8, b'Y') => 'Ÿ',
479 (0xC8, b'a') => 'ä',
480 (0xC8, b'e') => 'ë',
481 (0xC8, b'i') => 'ï',
482 (0xC8, b'o') => 'ö',
483 (0xC8, b'u') => 'ü',
484 (0xC8, b'y') => 'ÿ',
485 (0xCA, b'A') => 'Å',
487 (0xCA, b'a') => 'å',
488 (0xCA, b'U') => 'Ů',
489 (0xCA, b'u') => 'ů',
490 (0xCB, b'C') => 'Ç',
491 (0xCB, b'c') => 'ç',
492 (0xCB, b'G') => 'Ģ',
493 (0xCB, b'g') => 'ģ',
494 (0xCB, b'K') => 'Ķ',
495 (0xCB, b'k') => 'ķ',
496 (0xCB, b'L') => 'Ļ',
497 (0xCB, b'l') => 'ļ',
498 (0xCB, b'N') => 'Ņ',
499 (0xCB, b'n') => 'ņ',
500 (0xCB, b'R') => 'Ŗ',
501 (0xCB, b'r') => 'ŗ',
502 (0xCB, b'S') => 'Ş',
503 (0xCB, b's') => 'ş',
504 (0xCB, b'T') => 'Ţ',
505 (0xCB, b't') => 'ţ',
506 (0xCD, b'O') => 'Ő',
508 (0xCD, b'o') => 'ő',
509 (0xCD, b'U') => 'Ű',
510 (0xCD, b'u') => 'ű',
511 (0xCE, b'A') => 'Ą',
513 (0xCE, b'a') => 'ą',
514 (0xCE, b'E') => 'Ę',
515 (0xCE, b'e') => 'ę',
516 (0xCE, b'I') => 'Į',
517 (0xCE, b'i') => 'į',
518 (0xCE, b'U') => 'Ų',
519 (0xCE, b'u') => 'ų',
520 (0xCF, b'C') => 'Č',
522 (0xCF, b'c') => 'č',
523 (0xCF, b'D') => 'Ď',
524 (0xCF, b'd') => 'ď',
525 (0xCF, b'E') => 'Ě',
526 (0xCF, b'e') => 'ě',
527 (0xCF, b'L') => 'Ľ',
528 (0xCF, b'l') => 'ľ',
529 (0xCF, b'N') => 'Ň',
530 (0xCF, b'n') => 'ň',
531 (0xCF, b'R') => 'Ř',
532 (0xCF, b'r') => 'ř',
533 (0xCF, b'S') => 'Š',
534 (0xCF, b's') => 'š',
535 (0xCF, b'T') => 'Ť',
536 (0xCF, b't') => 'ť',
537 (0xCF, b'Z') => 'Ž',
538 (0xCF, b'z') => 'ž',
539 _ => return None,
540 })
541}
542
543fn decode_iso_8859(n: u8, bytes: &[u8]) -> String {
544 if n == 1 {
549 return bytes.iter().map(|&b| b as char).collect();
550 }
551 #[cfg(feature = "std")]
555 {
556 use encoding_rs::*;
557 let encoding: &'static Encoding = match n {
558 2 => ISO_8859_2,
559 3 => ISO_8859_3,
560 4 => ISO_8859_4,
561 5 => ISO_8859_5,
562 6 => ISO_8859_6,
563 7 => ISO_8859_7,
564 8 => ISO_8859_8,
565 9 => WINDOWS_1254,
566 10 => ISO_8859_10,
567 11 => WINDOWS_874,
568 13 => ISO_8859_13,
569 14 => ISO_8859_14,
570 15 => ISO_8859_15,
571 _ => return bytes.iter().map(|_| '\u{FFFD}').collect(),
572 };
573 let (cow, _, _) = encoding.decode(bytes);
574 cow.into_owned()
575 }
576 #[cfg(not(feature = "std"))]
577 {
578 let _ = n;
579 bytes.iter().map(|_| '\u{FFFD}').collect()
580 }
581}
582
583#[cfg(feature = "std")]
584fn decode_with(encoding: &'static encoding_rs::Encoding, bytes: &[u8]) -> String {
585 let (cow, _, _) = encoding.decode(bytes);
586 cow.into_owned()
587}
588
589fn decode_ucs2_be(bytes: &[u8]) -> String {
590 let code_units: Vec<u16> = bytes
591 .chunks_exact(2)
592 .map(|pair| u16::from_be_bytes(*pair.first_chunk::<2>().unwrap()))
593 .collect();
594 String::from_utf16_lossy(&code_units)
595}
596
597#[cfg(test)]
598mod tests {
599 use super::*;
600
601 #[test]
602 fn decode_empty_input_returns_empty_string() {
603 assert_eq!(decode_dvb_string(&[]), "");
604 }
605
606 #[test]
607 fn decode_plain_ascii_is_borrowed() {
608 let cow = decode(b"HELLO");
609 assert!(matches!(cow, Cow::Borrowed(_)));
610 assert_eq!(cow, "HELLO");
611 }
612
613 #[test]
614 fn decode_iso6937_latin_accent_chars() {
615 assert_eq!(decode_dvb_string(&[0x00, 0xC2, b'A']), "Á");
616 assert_eq!(decode_dvb_string(&[0x00, 0xC1, b'e']), "è");
617 assert_eq!(decode_dvb_string(&[0x00, 0xC8, b'o']), "ö");
618 }
619
620 #[test]
621 fn decode_selector_0x01_yields_iso8859_5_cyrillic() {
622 let s = decode_dvb_string(&[0x01, 0xB0, 0xB1]);
623 assert!(s.chars().all(|c| c != '\u{FFFD}'), "got: {s:?}");
624 assert!(!s.is_empty());
625 }
626
627 #[test]
628 fn decode_selector_0x10_extended_yields_iso8859_nn() {
629 let s = decode_dvb_string(&[0x10, 0x00, 0x09, b'A', b'B']);
630 assert_eq!(s, "AB");
631 }
632
633 #[test]
634 fn decode_selector_0x11_ucs2_be() {
635 let s = decode_dvb_string(&[0x11, 0x00, 0x41, 0x00, 0x42]);
636 assert_eq!(s, "AB");
637 }
638
639 #[test]
640 fn decode_selector_0x15_utf8_passthrough() {
641 let s = decode_dvb_string(&[0x15, 0xC3, 0xA9, 0xC3, 0xA9]);
642 assert_eq!(s, "éé");
643 }
644
645 #[test]
646 fn decode_control_chars_stripped_linefeed_becomes_space() {
647 let s = decode_dvb_string(b"A\x01B\nC");
648 assert_eq!(s, "AB C");
649 }
650
651 #[test]
652 fn emphasis_on_off_markers_stripped_per_annex_a2() {
653 let s = decode_dvb_string(&[0x00, b'A', 0x86, b'B', 0x87, b'C']);
656 assert_eq!(s, "ABC");
657 }
658
659 #[test]
660 fn decode_annex_a2_crlf_0x8a_becomes_space() {
661 let s = decode_dvb_string(&[0x00, b'A', 0x8A, b'B']);
663 assert_eq!(s, "A B");
664 }
665
666 #[test]
667 fn decode_selector_0x12_ksx1001_euc_kr() {
668 assert_eq!(decode_dvb_string(&[0x12, 0xB0, 0xA1]), "가");
670 }
671
672 #[test]
673 fn decode_selector_0x13_gb2312() {
674 assert_eq!(decode_dvb_string(&[0x13, 0xC4, 0xE3]), "你");
676 }
677
678 #[test]
679 fn decode_selector_0x14_big5() {
680 assert_eq!(decode_dvb_string(&[0x14, 0xA4, 0xA4]), "中");
682 }
683
684 #[test]
688 fn decode_selector_0x13_gbk_trail_byte_in_c1_range() {
689 assert_eq!(decode_dvb_string(&[0x13, 0x81, 0x80]), "亐");
690 }
691
692 #[test]
696 fn two_byte_control_codes_filtered() {
697 assert_eq!(decode_dvb_string(&[0x13, 0xAB, 0xCD]), " ");
698 assert_eq!(decode_dvb_string(&[0x13, 0xAB, 0xC3]), "");
699 }
700
701 #[test]
704 fn decode_selector_0x1f_encoding_type_id() {
705 let s = decode_dvb_string(&[0x1F, 0x01, 0x41, 0x42]);
706 assert_eq!(s.chars().count(), 2);
707 assert!(s.chars().all(|c| c == '\u{FFFD}'));
708 }
709
710 #[test]
712 fn reserved_selector_0x08_is_unsupported() {
713 let s = decode_dvb_string(&[0x08, 0x41, 0x42]);
714 assert!(s.chars().all(|c| c == '\u{FFFD}'));
715 assert_eq!(s.chars().count(), 2);
716 }
717
718 #[test]
719 fn unknown_selector_returns_replacement_characters() {
720 let s = decode_dvb_string(&[0x16, 0xAA, 0xBB, 0xCC]);
722 assert_eq!(s.chars().count(), 3);
723 assert!(s.chars().all(|c| c == '\u{FFFD}'));
724 }
725
726 #[test]
729 fn selector_0x10_iso_8859_1_decodes_latin1() {
730 let s = decode_dvb_string(&[0x10, 0x00, 0x01, 0x41, 0xE9]);
734 assert_eq!(s, "Aé");
735 }
736
737 #[test]
738 fn unsupported_iso_8859_12_yields_replacement() {
739 let s = decode_dvb_string(&[0x10, 0x00, 0x0C, 0x41, 0x42]);
742 assert!(s.chars().all(|c| c == '\u{FFFD}'), "got: {s:?}");
743 }
744
745 #[test]
750 fn figure_a1_gr_area_single_byte_mappings() {
751 let pins: &[(u8, char)] = &[
752 (0xA0, '\u{00A0}'), (0xA1, '¡'),
754 (0xA2, '¢'),
755 (0xA3, '£'),
756 (0xA4, '\u{20AC}'), (0xA5, '¥'),
758 (0xA7, '§'),
759 (0xA8, '\u{00A4}'), (0xA9, '\u{2018}'), (0xAA, '\u{201C}'), (0xAB, '«'),
763 (0xAC, '\u{2190}'), (0xAD, '\u{2191}'), (0xAE, '\u{2192}'), (0xAF, '\u{2193}'), (0xB0, '°'),
768 (0xB1, '±'),
769 (0xB2, '²'),
770 (0xB3, '³'),
771 (0xB4, '\u{00D7}'), (0xB5, 'µ'),
773 (0xB6, '¶'),
774 (0xB7, '·'),
775 (0xB8, '\u{00F7}'), (0xB9, '\u{2019}'), (0xBA, '\u{201D}'), (0xBB, '»'),
779 (0xBC, '¼'),
780 (0xBD, '½'),
781 (0xBE, '¾'),
782 (0xBF, '¿'),
783 (0xD0, '\u{2015}'), (0xD1, '¹'),
785 (0xD2, '®'),
786 (0xD3, '©'),
787 (0xD4, '\u{2122}'), (0xD5, '\u{266A}'), (0xD6, '¬'),
790 (0xD7, '\u{00A6}'), (0xDC, '\u{215B}'), (0xDD, '\u{215C}'), (0xDE, '\u{215D}'), (0xDF, '\u{215E}'), (0xE0, '\u{2126}'), (0xE1, 'Æ'),
797 (0xE2, '\u{0110}'), (0xE3, 'ª'),
799 (0xE4, '\u{0126}'), (0xE6, '\u{0132}'), (0xE7, '\u{013F}'), (0xE8, '\u{0141}'), (0xE9, 'Ø'),
804 (0xEA, '\u{0152}'), (0xEB, 'º'),
806 (0xEC, 'Þ'),
807 (0xED, '\u{0166}'), (0xEE, '\u{014A}'), (0xEF, '\u{0149}'), (0xF0, '\u{0138}'), (0xF1, 'æ'),
812 (0xF2, '\u{0111}'), (0xF3, 'ð'),
814 (0xF4, '\u{0127}'), (0xF5, '\u{0131}'), (0xF6, '\u{0133}'), (0xF7, '\u{0140}'), (0xF8, '\u{0142}'), (0xF9, 'ø'),
820 (0xFA, '\u{0153}'), (0xFB, 'ß'),
822 (0xFC, '\u{00FE}'), (0xFD, '\u{0167}'), (0xFE, '\u{014B}'), (0xFF, '\u{00AD}'), ];
827 for &(byte, want) in pins {
828 let got = decode_dvb_string(&[0x00, byte]);
829 assert_eq!(
830 got,
831 want.to_string(),
832 "byte {byte:#04x}: want {want:?} (U+{:04X}), got {got:?}",
833 want as u32
834 );
835 }
836 }
837
838 #[test]
840 fn figure_a1_undefined_positions_are_replacement() {
841 for byte in [0xA6u8, 0xD8, 0xD9, 0xDA, 0xDB, 0xE5] {
842 let got = decode_dvb_string(&[0x00, byte]);
843 assert_eq!(got, "\u{FFFD}", "byte {byte:#04x} should be U+FFFD");
844 }
845 }
846
847 #[test]
849 fn figure_a1_combining_precomposed() {
850 assert_eq!(decode_dvb_string(&[0x00, 0xCA, b'a']), "å"); assert_eq!(decode_dvb_string(&[0x00, 0xCA, b'A']), "Å");
852 assert_eq!(decode_dvb_string(&[0x00, 0xCF, b's']), "š"); assert_eq!(decode_dvb_string(&[0x00, 0xCF, b'Z']), "Ž");
854 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']), "ğ"); }
860
861 #[test]
864 fn figure_a1_combining_fallback_emits_base_plus_mark() {
865 assert_eq!(decode_dvb_string(&[0x00, 0xC5, b'x']), "x\u{0304}");
866 }
867
868 #[test]
871 fn figure_a1_combining_undefined_or_dangling_prefix() {
872 assert_eq!(decode_dvb_string(&[0x00, 0xC0, b'a']), "\u{FFFD}a");
873 assert_eq!(decode_dvb_string(&[0x00, 0xC9, b'a']), "\u{FFFD}a");
874 assert_eq!(decode_dvb_string(&[0x00, 0xCC, b'a']), "\u{FFFD}a");
875 assert_eq!(decode_dvb_string(&[0x00, 0xC2]), "\u{FFFD}");
876 }
877
878 #[test]
879 fn dvb_text_decodes_with_charset_selector() {
880 let t = DvbText::new(&[0x15, 0xC3, 0xA9]); assert_eq!(t.decode(), "é");
882 assert_eq!(t.raw(), &[0x15, 0xC3, 0xA9]);
883 assert_eq!(&t[..], &[0x15, 0xC3, 0xA9]); assert_eq!(format!("{t}"), "é");
885 }
886
887 #[test]
888 fn lang_code_as_str() {
889 assert_eq!(LangCode(*b"fre").as_str(), "fre");
890 assert_eq!(LangCode([0xFF, b'r', b'e']).as_str(), "\u{FFFD}re"); }
892
893 #[cfg(feature = "serde")]
894 #[test]
895 fn dvb_text_serializes_decoded() {
896 let t = DvbText::new(&[0x15, 0xC3, 0xA9]);
897 assert_eq!(serde_json::to_string(&t).unwrap(), "\"é\"");
898 }
899
900 #[cfg(feature = "serde")]
901 #[test]
902 fn lang_code_serializes_as_string() {
903 let lc = LangCode(*b"FRA");
906 assert_eq!(serde_json::to_string(&lc).unwrap(), "\"FRA\"");
907 }
908}