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)]
93#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
94pub struct DvbText<'a>(&'a [u8]);
95
96impl<'a> DvbText<'a> {
97 #[must_use]
99 pub const fn new(raw: &'a [u8]) -> Self {
100 Self(raw)
101 }
102 #[must_use]
104 pub const fn raw(&self) -> &'a [u8] {
105 self.0
106 }
107 #[must_use]
111 pub fn decode(&self) -> Cow<'a, str> {
112 decode(self.0)
113 }
114}
115
116impl std::ops::Deref for DvbText<'_> {
117 type Target = [u8];
120 fn deref(&self) -> &[u8] {
121 self.0
122 }
123}
124
125impl std::fmt::Display for DvbText<'_> {
126 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127 f.write_str(&self.decode())
128 }
129}
130
131impl std::fmt::Debug for DvbText<'_> {
132 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133 write!(f, "DvbText({:?})", self.decode())
134 }
135}
136
137impl<'a> From<&'a [u8]> for DvbText<'a> {
138 fn from(raw: &'a [u8]) -> Self {
139 Self(raw)
140 }
141}
142
143#[cfg(feature = "serde")]
144impl serde::Serialize for DvbText<'_> {
145 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
146 s.serialize_str(&self.decode())
147 }
148}
149#[derive(Clone, Copy, PartialEq, Eq, Hash)]
154pub struct LangCode(pub [u8; 3]);
155
156impl LangCode {
157 #[must_use]
159 pub fn as_str(&self) -> Cow<'_, str> {
160 String::from_utf8_lossy(&self.0)
161 }
162}
163
164impl std::ops::Deref for LangCode {
165 type Target = [u8; 3];
166 fn deref(&self) -> &[u8; 3] {
167 &self.0
168 }
169}
170
171impl std::fmt::Display for LangCode {
172 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
173 f.write_str(&self.as_str())
174 }
175}
176
177impl std::fmt::Debug for LangCode {
178 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179 write!(f, "LangCode({})", self.as_str())
180 }
181}
182
183#[cfg(feature = "serde")]
184impl serde::Serialize for LangCode {
185 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
186 s.serialize_str(&self.as_str())
187 }
188}
189
190#[derive(Debug)]
191enum Charset {
192 Iso6937,
193 Iso8859(u8),
194 Utf8,
195 Ucs2Be,
196 Ksx1001,
198 Gb2312,
200 Big5,
202 Unsupported(u8),
203}
204
205fn split_charset(bytes: &[u8]) -> (Charset, &[u8]) {
206 match bytes[0] {
207 b if b >= 0x20 => (Charset::Iso6937, bytes),
208 0x00 => (Charset::Iso6937, &bytes[1..]),
209 0x08 => (Charset::Unsupported(0x08), &bytes[1..]),
212 0x01..=0x0B => (Charset::Iso8859(bytes[0] + 4), &bytes[1..]),
213 0x10 if bytes.len() >= 3 && bytes[1] == 0x00 => (Charset::Iso8859(bytes[2]), &bytes[3..]),
214 0x11 => (Charset::Ucs2Be, &bytes[1..]),
215 0x12 => (Charset::Ksx1001, &bytes[1..]),
216 0x13 => (Charset::Gb2312, &bytes[1..]),
217 0x14 => (Charset::Big5, &bytes[1..]),
218 0x15 => (Charset::Utf8, &bytes[1..]),
219 0x1F if bytes.len() >= 2 => (Charset::Unsupported(0x1F), &bytes[2..]),
222 other => (Charset::Unsupported(other), &bytes[1..]),
223 }
224}
225
226fn decode_iso_6937(bytes: &[u8]) -> String {
227 let mut out = String::with_capacity(bytes.len());
228 let mut i = 0;
229 while i < bytes.len() {
230 let b = bytes[i];
231 if (0xC0..=0xCF).contains(&b) {
233 match combining_mark(b) {
234 Some(mark) if i + 1 < bytes.len() => {
235 let base = bytes[i + 1];
236 if let Some(c) = combine(b, base) {
237 out.push(c);
238 } else {
239 out.push(iso_6937_single(base));
242 out.push(mark);
243 }
244 i += 2;
245 }
246 _ => {
248 out.push('\u{FFFD}');
249 i += 1;
250 }
251 }
252 continue;
253 }
254 out.push(iso_6937_single(b));
255 i += 1;
256 }
257 out
258}
259
260fn iso_6937_single(b: u8) -> char {
268 match b {
269 0x00..=0x7F => b as char,
270 0x86 | 0x87 | 0x8A => b as char,
272 0x80..=0x9F => '\u{FFFD}',
273 0xA0 => '\u{00A0}', 0xA1 => '¡',
275 0xA2 => '¢',
276 0xA3 => '£',
277 0xA4 => '\u{20AC}', 0xA5 => '¥',
279 0xA6 => '\u{FFFD}', 0xA7 => '§',
281 0xA8 => '\u{00A4}', 0xA9 => '\u{2018}', 0xAA => '\u{201C}', 0xAB => '«',
285 0xAC => '\u{2190}', 0xAD => '\u{2191}', 0xAE => '\u{2192}', 0xAF => '\u{2193}', 0xB0 => '°',
290 0xB1 => '±',
291 0xB2 => '²',
292 0xB3 => '³',
293 0xB4 => '\u{00D7}', 0xB5 => 'µ',
295 0xB6 => '¶',
296 0xB7 => '·',
297 0xB8 => '\u{00F7}', 0xB9 => '\u{2019}', 0xBA => '\u{201D}', 0xBB => '»',
301 0xBC => '¼',
302 0xBD => '½',
303 0xBE => '¾',
304 0xBF => '¿',
305 0xC0..=0xCF => '\u{FFFD}',
307 0xD0 => '\u{2015}', 0xD1 => '¹',
309 0xD2 => '®',
310 0xD3 => '©',
311 0xD4 => '\u{2122}', 0xD5 => '\u{266A}', 0xD6 => '¬',
314 0xD7 => '\u{00A6}', 0xD8..=0xDB => '\u{FFFD}', 0xDC => '\u{215B}', 0xDD => '\u{215C}', 0xDE => '\u{215D}', 0xDF => '\u{215E}', 0xE0 => '\u{2126}', 0xE1 => 'Æ',
322 0xE2 => '\u{0110}', 0xE3 => 'ª',
324 0xE4 => '\u{0126}', 0xE5 => '\u{FFFD}', 0xE6 => '\u{0132}', 0xE7 => '\u{013F}', 0xE8 => '\u{0141}', 0xE9 => 'Ø',
330 0xEA => '\u{0152}', 0xEB => 'º',
332 0xEC => 'Þ',
333 0xED => '\u{0166}', 0xEE => '\u{014A}', 0xEF => '\u{0149}', 0xF0 => '\u{0138}', 0xF1 => 'æ',
338 0xF2 => '\u{0111}', 0xF3 => 'ð',
340 0xF4 => '\u{0127}', 0xF5 => '\u{0131}', 0xF6 => '\u{0133}', 0xF7 => '\u{0140}', 0xF8 => '\u{0142}', 0xF9 => 'ø',
346 0xFA => '\u{0153}', 0xFB => 'ß',
348 0xFC => '\u{00FE}', 0xFD => '\u{0167}', 0xFE => '\u{014B}', 0xFF => '\u{00AD}', }
353}
354
355fn combining_mark(prefix: u8) -> Option<char> {
358 Some(match prefix {
359 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,
373 })
374}
375
376fn combine(prefix: u8, base: u8) -> Option<char> {
377 Some(match (prefix, base) {
378 (0xC1, b'A') => 'À',
379 (0xC1, b'E') => 'È',
380 (0xC1, b'I') => 'Ì',
381 (0xC1, b'O') => 'Ò',
382 (0xC1, b'U') => 'Ù',
383 (0xC1, b'a') => 'à',
384 (0xC1, b'e') => 'è',
385 (0xC1, b'i') => 'ì',
386 (0xC1, b'o') => 'ò',
387 (0xC1, b'u') => 'ù',
388 (0xC2, b'A') => 'Á',
389 (0xC2, b'E') => 'É',
390 (0xC2, b'I') => 'Í',
391 (0xC2, b'O') => 'Ó',
392 (0xC2, b'U') => 'Ú',
393 (0xC2, b'Y') => 'Ý',
394 (0xC2, b'a') => 'á',
395 (0xC2, b'e') => 'é',
396 (0xC2, b'i') => 'í',
397 (0xC2, b'o') => 'ó',
398 (0xC2, b'u') => 'ú',
399 (0xC2, b'y') => 'ý',
400 (0xC2, b'C') => 'Ć',
401 (0xC2, b'c') => 'ć',
402 (0xC2, b'L') => 'Ĺ',
403 (0xC2, b'l') => 'ĺ',
404 (0xC2, b'N') => 'Ń',
405 (0xC2, b'n') => 'ń',
406 (0xC2, b'R') => 'Ŕ',
407 (0xC2, b'r') => 'ŕ',
408 (0xC2, b'S') => 'Ś',
409 (0xC2, b's') => 'ś',
410 (0xC2, b'Z') => 'Ź',
411 (0xC2, b'z') => 'ź',
412 (0xC3, b'A') => 'Â',
413 (0xC3, b'E') => 'Ê',
414 (0xC3, b'I') => 'Î',
415 (0xC3, b'O') => 'Ô',
416 (0xC3, b'U') => 'Û',
417 (0xC3, b'a') => 'â',
418 (0xC3, b'e') => 'ê',
419 (0xC3, b'i') => 'î',
420 (0xC3, b'o') => 'ô',
421 (0xC3, b'u') => 'û',
422 (0xC4, b'A') => 'Ã',
423 (0xC4, b'N') => 'Ñ',
424 (0xC4, b'O') => 'Õ',
425 (0xC4, b'a') => 'ã',
426 (0xC4, b'n') => 'ñ',
427 (0xC4, b'o') => 'õ',
428 (0xC4, b'I') => 'Ĩ',
429 (0xC4, b'i') => 'ĩ',
430 (0xC4, b'U') => 'Ũ',
431 (0xC4, b'u') => 'ũ',
432 (0xC5, b'A') => 'Ā',
434 (0xC5, b'a') => 'ā',
435 (0xC5, b'E') => 'Ē',
436 (0xC5, b'e') => 'ē',
437 (0xC5, b'I') => 'Ī',
438 (0xC5, b'i') => 'ī',
439 (0xC5, b'O') => 'Ō',
440 (0xC5, b'o') => 'ō',
441 (0xC5, b'U') => 'Ū',
442 (0xC5, b'u') => 'ū',
443 (0xC6, b'A') => 'Ă',
445 (0xC6, b'a') => 'ă',
446 (0xC6, b'G') => 'Ğ',
447 (0xC6, b'g') => 'ğ',
448 (0xC6, b'U') => 'Ŭ',
449 (0xC6, b'u') => 'ŭ',
450 (0xC7, b'C') => 'Ċ',
452 (0xC7, b'c') => 'ċ',
453 (0xC7, b'E') => 'Ė',
454 (0xC7, b'e') => 'ė',
455 (0xC7, b'G') => 'Ġ',
456 (0xC7, b'g') => 'ġ',
457 (0xC7, b'I') => 'İ',
458 (0xC7, b'Z') => 'Ż',
459 (0xC7, b'z') => 'ż',
460 (0xC8, b'A') => 'Ä',
461 (0xC8, b'E') => 'Ë',
462 (0xC8, b'I') => 'Ï',
463 (0xC8, b'O') => 'Ö',
464 (0xC8, b'U') => 'Ü',
465 (0xC8, b'Y') => 'Ÿ',
466 (0xC8, b'a') => 'ä',
467 (0xC8, b'e') => 'ë',
468 (0xC8, b'i') => 'ï',
469 (0xC8, b'o') => 'ö',
470 (0xC8, b'u') => 'ü',
471 (0xC8, b'y') => 'ÿ',
472 (0xCA, b'A') => 'Å',
474 (0xCA, b'a') => 'å',
475 (0xCA, b'U') => 'Ů',
476 (0xCA, b'u') => 'ů',
477 (0xCB, b'C') => 'Ç',
478 (0xCB, b'c') => 'ç',
479 (0xCB, b'G') => 'Ģ',
480 (0xCB, b'g') => 'ģ',
481 (0xCB, b'K') => 'Ķ',
482 (0xCB, b'k') => 'ķ',
483 (0xCB, b'L') => 'Ļ',
484 (0xCB, b'l') => 'ļ',
485 (0xCB, b'N') => 'Ņ',
486 (0xCB, b'n') => 'ņ',
487 (0xCB, b'R') => 'Ŗ',
488 (0xCB, b'r') => 'ŗ',
489 (0xCB, b'S') => 'Ş',
490 (0xCB, b's') => 'ş',
491 (0xCB, b'T') => 'Ţ',
492 (0xCB, b't') => 'ţ',
493 (0xCD, b'O') => 'Ő',
495 (0xCD, b'o') => 'ő',
496 (0xCD, b'U') => 'Ű',
497 (0xCD, b'u') => 'ű',
498 (0xCE, b'A') => 'Ą',
500 (0xCE, b'a') => 'ą',
501 (0xCE, b'E') => 'Ę',
502 (0xCE, b'e') => 'ę',
503 (0xCE, b'I') => 'Į',
504 (0xCE, b'i') => 'į',
505 (0xCE, b'U') => 'Ų',
506 (0xCE, b'u') => 'ų',
507 (0xCF, b'C') => 'Č',
509 (0xCF, b'c') => 'č',
510 (0xCF, b'D') => 'Ď',
511 (0xCF, b'd') => 'ď',
512 (0xCF, b'E') => 'Ě',
513 (0xCF, b'e') => 'ě',
514 (0xCF, b'L') => 'Ľ',
515 (0xCF, b'l') => 'ľ',
516 (0xCF, b'N') => 'Ň',
517 (0xCF, b'n') => 'ň',
518 (0xCF, b'R') => 'Ř',
519 (0xCF, b'r') => 'ř',
520 (0xCF, b'S') => 'Š',
521 (0xCF, b's') => 'š',
522 (0xCF, b'T') => 'Ť',
523 (0xCF, b't') => 'ť',
524 (0xCF, b'Z') => 'Ž',
525 (0xCF, b'z') => 'ž',
526 _ => return None,
527 })
528}
529
530fn decode_iso_8859(n: u8, bytes: &[u8]) -> String {
531 use encoding_rs::*;
532 let encoding: &'static Encoding = match n {
533 2 => ISO_8859_2,
534 3 => ISO_8859_3,
535 4 => ISO_8859_4,
536 5 => ISO_8859_5,
537 6 => ISO_8859_6,
538 7 => ISO_8859_7,
539 8 => ISO_8859_8,
540 9 => WINDOWS_1254,
541 10 => ISO_8859_10,
542 11 => WINDOWS_874,
543 13 => ISO_8859_13,
544 14 => ISO_8859_14,
545 15 => ISO_8859_15,
546 _ => return bytes.iter().map(|&b| b as char).collect(),
547 };
548 let (cow, _, _) = encoding.decode(bytes);
549 cow.into_owned()
550}
551
552fn decode_with(encoding: &'static encoding_rs::Encoding, bytes: &[u8]) -> String {
553 let (cow, _, _) = encoding.decode(bytes);
554 cow.into_owned()
555}
556
557fn decode_ucs2_be(bytes: &[u8]) -> String {
558 let code_units: Vec<u16> = bytes
559 .chunks_exact(2)
560 .map(|pair| u16::from_be_bytes([pair[0], pair[1]]))
561 .collect();
562 String::from_utf16_lossy(&code_units)
563}
564
565#[cfg(test)]
566mod tests {
567 use super::*;
568
569 #[test]
570 fn decode_empty_input_returns_empty_string() {
571 assert_eq!(decode_dvb_string(&[]), "");
572 }
573
574 #[test]
575 fn decode_plain_ascii_is_borrowed() {
576 let cow = decode(b"HELLO");
577 assert!(matches!(cow, Cow::Borrowed(_)));
578 assert_eq!(cow, "HELLO");
579 }
580
581 #[test]
582 fn decode_iso6937_latin_accent_chars() {
583 assert_eq!(decode_dvb_string(&[0x00, 0xC2, b'A']), "Á");
584 assert_eq!(decode_dvb_string(&[0x00, 0xC1, b'e']), "è");
585 assert_eq!(decode_dvb_string(&[0x00, 0xC8, b'o']), "ö");
586 }
587
588 #[test]
589 fn decode_selector_0x01_yields_iso8859_5_cyrillic() {
590 let s = decode_dvb_string(&[0x01, 0xB0, 0xB1]);
591 assert!(s.chars().all(|c| c != '\u{FFFD}'), "got: {s:?}");
592 assert!(!s.is_empty());
593 }
594
595 #[test]
596 fn decode_selector_0x10_extended_yields_iso8859_nn() {
597 let s = decode_dvb_string(&[0x10, 0x00, 0x09, b'A', b'B']);
598 assert_eq!(s, "AB");
599 }
600
601 #[test]
602 fn decode_selector_0x11_ucs2_be() {
603 let s = decode_dvb_string(&[0x11, 0x00, 0x41, 0x00, 0x42]);
604 assert_eq!(s, "AB");
605 }
606
607 #[test]
608 fn decode_selector_0x15_utf8_passthrough() {
609 let s = decode_dvb_string(&[0x15, 0xC3, 0xA9, 0xC3, 0xA9]);
610 assert_eq!(s, "éé");
611 }
612
613 #[test]
614 fn decode_control_chars_stripped_linefeed_becomes_space() {
615 let s = decode_dvb_string(b"A\x01B\nC");
616 assert_eq!(s, "AB C");
617 }
618
619 #[test]
620 fn emphasis_on_off_markers_stripped_per_annex_a2() {
621 let s = decode_dvb_string(&[0x00, b'A', 0x86, b'B', 0x87, b'C']);
624 assert_eq!(s, "ABC");
625 }
626
627 #[test]
628 fn decode_annex_a2_crlf_0x8a_becomes_space() {
629 let s = decode_dvb_string(&[0x00, b'A', 0x8A, b'B']);
631 assert_eq!(s, "A B");
632 }
633
634 #[test]
635 fn decode_selector_0x12_ksx1001_euc_kr() {
636 assert_eq!(decode_dvb_string(&[0x12, 0xB0, 0xA1]), "가");
638 }
639
640 #[test]
641 fn decode_selector_0x13_gb2312() {
642 assert_eq!(decode_dvb_string(&[0x13, 0xC4, 0xE3]), "你");
644 }
645
646 #[test]
647 fn decode_selector_0x14_big5() {
648 assert_eq!(decode_dvb_string(&[0x14, 0xA4, 0xA4]), "中");
650 }
651
652 #[test]
656 fn decode_selector_0x13_gbk_trail_byte_in_c1_range() {
657 assert_eq!(decode_dvb_string(&[0x13, 0x81, 0x80]), "亐");
658 }
659
660 #[test]
664 fn two_byte_control_codes_filtered() {
665 assert_eq!(decode_dvb_string(&[0x13, 0xAB, 0xCD]), " ");
666 assert_eq!(decode_dvb_string(&[0x13, 0xAB, 0xC3]), "");
667 }
668
669 #[test]
672 fn decode_selector_0x1f_encoding_type_id() {
673 let s = decode_dvb_string(&[0x1F, 0x01, 0x41, 0x42]);
674 assert_eq!(s.chars().count(), 2);
675 assert!(s.chars().all(|c| c == '\u{FFFD}'));
676 }
677
678 #[test]
680 fn reserved_selector_0x08_is_unsupported() {
681 let s = decode_dvb_string(&[0x08, 0x41, 0x42]);
682 assert!(s.chars().all(|c| c == '\u{FFFD}'));
683 assert_eq!(s.chars().count(), 2);
684 }
685
686 #[test]
687 fn unknown_selector_returns_replacement_characters() {
688 let s = decode_dvb_string(&[0x16, 0xAA, 0xBB, 0xCC]);
690 assert_eq!(s.chars().count(), 3);
691 assert!(s.chars().all(|c| c == '\u{FFFD}'));
692 }
693
694 #[test]
699 fn figure_a1_gr_area_single_byte_mappings() {
700 let pins: &[(u8, char)] = &[
701 (0xA0, '\u{00A0}'), (0xA1, '¡'),
703 (0xA2, '¢'),
704 (0xA3, '£'),
705 (0xA4, '\u{20AC}'), (0xA5, '¥'),
707 (0xA7, '§'),
708 (0xA8, '\u{00A4}'), (0xA9, '\u{2018}'), (0xAA, '\u{201C}'), (0xAB, '«'),
712 (0xAC, '\u{2190}'), (0xAD, '\u{2191}'), (0xAE, '\u{2192}'), (0xAF, '\u{2193}'), (0xB0, '°'),
717 (0xB1, '±'),
718 (0xB2, '²'),
719 (0xB3, '³'),
720 (0xB4, '\u{00D7}'), (0xB5, 'µ'),
722 (0xB6, '¶'),
723 (0xB7, '·'),
724 (0xB8, '\u{00F7}'), (0xB9, '\u{2019}'), (0xBA, '\u{201D}'), (0xBB, '»'),
728 (0xBC, '¼'),
729 (0xBD, '½'),
730 (0xBE, '¾'),
731 (0xBF, '¿'),
732 (0xD0, '\u{2015}'), (0xD1, '¹'),
734 (0xD2, '®'),
735 (0xD3, '©'),
736 (0xD4, '\u{2122}'), (0xD5, '\u{266A}'), (0xD6, '¬'),
739 (0xD7, '\u{00A6}'), (0xDC, '\u{215B}'), (0xDD, '\u{215C}'), (0xDE, '\u{215D}'), (0xDF, '\u{215E}'), (0xE0, '\u{2126}'), (0xE1, 'Æ'),
746 (0xE2, '\u{0110}'), (0xE3, 'ª'),
748 (0xE4, '\u{0126}'), (0xE6, '\u{0132}'), (0xE7, '\u{013F}'), (0xE8, '\u{0141}'), (0xE9, 'Ø'),
753 (0xEA, '\u{0152}'), (0xEB, 'º'),
755 (0xEC, 'Þ'),
756 (0xED, '\u{0166}'), (0xEE, '\u{014A}'), (0xEF, '\u{0149}'), (0xF0, '\u{0138}'), (0xF1, 'æ'),
761 (0xF2, '\u{0111}'), (0xF3, 'ð'),
763 (0xF4, '\u{0127}'), (0xF5, '\u{0131}'), (0xF6, '\u{0133}'), (0xF7, '\u{0140}'), (0xF8, '\u{0142}'), (0xF9, 'ø'),
769 (0xFA, '\u{0153}'), (0xFB, 'ß'),
771 (0xFC, '\u{00FE}'), (0xFD, '\u{0167}'), (0xFE, '\u{014B}'), (0xFF, '\u{00AD}'), ];
776 for &(byte, want) in pins {
777 let got = decode_dvb_string(&[0x00, byte]);
778 assert_eq!(
779 got,
780 want.to_string(),
781 "byte {byte:#04x}: want {want:?} (U+{:04X}), got {got:?}",
782 want as u32
783 );
784 }
785 }
786
787 #[test]
789 fn figure_a1_undefined_positions_are_replacement() {
790 for byte in [0xA6u8, 0xD8, 0xD9, 0xDA, 0xDB, 0xE5] {
791 let got = decode_dvb_string(&[0x00, byte]);
792 assert_eq!(got, "\u{FFFD}", "byte {byte:#04x} should be U+FFFD");
793 }
794 }
795
796 #[test]
798 fn figure_a1_combining_precomposed() {
799 assert_eq!(decode_dvb_string(&[0x00, 0xCA, b'a']), "å"); assert_eq!(decode_dvb_string(&[0x00, 0xCA, b'A']), "Å");
801 assert_eq!(decode_dvb_string(&[0x00, 0xCF, b's']), "š"); assert_eq!(decode_dvb_string(&[0x00, 0xCF, b'Z']), "Ž");
803 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']), "ğ"); }
809
810 #[test]
813 fn figure_a1_combining_fallback_emits_base_plus_mark() {
814 assert_eq!(decode_dvb_string(&[0x00, 0xC5, b'x']), "x\u{0304}");
815 }
816
817 #[test]
820 fn figure_a1_combining_undefined_or_dangling_prefix() {
821 assert_eq!(decode_dvb_string(&[0x00, 0xC0, b'a']), "\u{FFFD}a");
822 assert_eq!(decode_dvb_string(&[0x00, 0xC9, b'a']), "\u{FFFD}a");
823 assert_eq!(decode_dvb_string(&[0x00, 0xCC, b'a']), "\u{FFFD}a");
824 assert_eq!(decode_dvb_string(&[0x00, 0xC2]), "\u{FFFD}");
825 }
826
827 #[test]
828 fn dvb_text_decodes_with_charset_selector() {
829 let t = DvbText::new(&[0x15, 0xC3, 0xA9]); assert_eq!(t.decode(), "é");
831 assert_eq!(t.raw(), &[0x15, 0xC3, 0xA9]);
832 assert_eq!(&t[..], &[0x15, 0xC3, 0xA9]); assert_eq!(format!("{t}"), "é");
834 }
835
836 #[test]
837 fn lang_code_as_str() {
838 assert_eq!(LangCode(*b"fre").as_str(), "fre");
839 assert_eq!(LangCode([0xFF, b'r', b'e']).as_str(), "\u{FFFD}re"); }
841
842 #[cfg(feature = "serde")]
843 #[test]
844 fn dvb_text_serializes_decoded() {
845 let t = DvbText::new(&[0x15, 0xC3, 0xA9]);
846 assert_eq!(serde_json::to_string(&t).unwrap(), "\"é\"");
847 }
848
849 #[cfg(feature = "serde")]
850 #[test]
851 fn lang_code_serializes_as_string() {
852 let lc = LangCode(*b"FRA");
855 assert_eq!(serde_json::to_string(&lc).unwrap(), "\"FRA\"");
856 }
857}