Skip to main content

encoding_rs/
iso_2022_jp.rs

1// Copyright Mozilla Foundation. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use super::*;
11use crate::data::*;
12use crate::handles::*;
13use crate::variant::*;
14// Rust 1.14.0 requires the following despite the asterisk above.
15use super::in_inclusive_range16;
16
17#[derive(Copy, Clone, PartialEq)]
18enum Iso2022JpDecoderState {
19    Ascii,
20    Roman,
21    Katakana,
22    LeadByte,
23    TrailByte,
24    EscapeStart,
25    Escape,
26}
27
28pub struct Iso2022JpDecoder {
29    decoder_state: Iso2022JpDecoderState,
30    output_state: Iso2022JpDecoderState, // only takes 1 of first 4 values
31    lead: u8,
32    output_flag: bool,
33    pending_prepended: bool,
34}
35
36impl Iso2022JpDecoder {
37    pub fn new() -> VariantDecoder {
38        VariantDecoder::Iso2022Jp(Iso2022JpDecoder {
39            decoder_state: Iso2022JpDecoderState::Ascii,
40            output_state: Iso2022JpDecoderState::Ascii,
41            lead: 0u8,
42            output_flag: false,
43            pending_prepended: false,
44        })
45    }
46
47    pub fn in_neutral_state(&self) -> bool {
48        self.decoder_state == Iso2022JpDecoderState::Ascii
49            && self.output_state == Iso2022JpDecoderState::Ascii
50            && self.lead == 0u8
51            && !self.output_flag
52            && !self.pending_prepended
53    }
54
55    fn extra_to_input_from_state(&self, byte_length: usize) -> Option<usize> {
56        byte_length.checked_add(
57            if self.lead == 0 || self.pending_prepended {
58                0
59            } else {
60                1
61            } + match self.decoder_state {
62                Iso2022JpDecoderState::Escape | Iso2022JpDecoderState::EscapeStart => 1,
63                _ => 0,
64            },
65        )
66    }
67
68    fn extra_to_output_from_state(&self) -> usize {
69        if self.lead != 0 && self.pending_prepended {
70            1 + self.output_flag as usize
71        } else {
72            self.output_flag as usize
73        }
74    }
75
76    pub fn max_utf16_buffer_length(&self, byte_length: usize) -> Option<usize> {
77        checked_add(
78            self.extra_to_output_from_state(),
79            self.extra_to_input_from_state(byte_length),
80        )
81    }
82
83    pub fn max_utf8_buffer_length_without_replacement(&self, byte_length: usize) -> Option<usize> {
84        // worst case: 1 to 3 (half-width katakana)
85        self.max_utf8_buffer_length(byte_length)
86    }
87
88    pub fn max_utf8_buffer_length(&self, byte_length: usize) -> Option<usize> {
89        checked_mul(
90            3,
91            checked_add(
92                self.extra_to_output_from_state(),
93                self.extra_to_input_from_state(byte_length),
94            ),
95        )
96    }
97
98    decoder_functions!(
99        preamble = {
100            if self.pending_prepended {
101                // lead was set in EscapeStart and "prepended"
102                // in Escape.
103                debug_assert!(self.lead == 0x24u8 || self.lead == 0x28u8);
104                match dest.check_space_bmp() {
105                    Space::Full(_) => {
106                        return (DecoderResult::OutputFull, 0, 0);
107                    }
108                    Space::Available(destination_handle) => {
109                        self.pending_prepended = false;
110                        self.output_flag = false;
111                        match self.decoder_state {
112                            Iso2022JpDecoderState::Ascii | Iso2022JpDecoderState::Roman => {
113                                destination_handle.write_ascii(self.lead);
114                                self.lead = 0x0u8;
115                            }
116                            Iso2022JpDecoderState::Katakana => {
117                                destination_handle
118                                    .write_upper_bmp(u16::from(self.lead) - 0x21u16 + 0xFF61u16);
119                                self.lead = 0x0u8;
120                            }
121                            Iso2022JpDecoderState::LeadByte => {
122                                self.decoder_state = Iso2022JpDecoderState::TrailByte;
123                            }
124                            _ => unreachable!(),
125                        }
126                    }
127                }
128            }
129        },
130        loop_preamble = {},
131        eof = {
132            match self.decoder_state {
133                Iso2022JpDecoderState::TrailByte
134                | Iso2022JpDecoderState::EscapeStart
135                | Iso2022JpDecoderState::Escape => {
136                    // We need to check space without intent to write in order to
137                    // make sure that there is space for the replacement character.
138                    if let Space::Full(_) = dest.check_space_bmp() {
139                        return (DecoderResult::OutputFull, src_consumed, dest.written());
140                    }
141                }
142                _ => {}
143            }
144            match self.decoder_state {
145                Iso2022JpDecoderState::TrailByte | Iso2022JpDecoderState::EscapeStart => {
146                    self.decoder_state = self.output_state;
147                    return (DecoderResult::Malformed(1, 0), src_consumed, dest.written());
148                }
149                Iso2022JpDecoderState::Escape => {
150                    self.pending_prepended = true;
151                    self.decoder_state = self.output_state;
152                    return (DecoderResult::Malformed(1, 1), src_consumed, dest.written());
153                }
154                _ => {}
155            }
156        },
157        body = {
158            match self.decoder_state {
159                Iso2022JpDecoderState::Ascii => {
160                    if b == 0x1Bu8 {
161                        self.decoder_state = Iso2022JpDecoderState::EscapeStart;
162                        continue;
163                    }
164                    self.output_flag = false;
165                    if b > 0x7Fu8 || b == 0x0Eu8 || b == 0x0Fu8 {
166                        return (
167                            DecoderResult::Malformed(1, 0),
168                            unread_handle.consumed(),
169                            destination_handle.written(),
170                        );
171                    }
172                    destination_handle.write_ascii(b);
173                    continue;
174                }
175                Iso2022JpDecoderState::Roman => {
176                    if b == 0x1Bu8 {
177                        self.decoder_state = Iso2022JpDecoderState::EscapeStart;
178                        continue;
179                    }
180                    self.output_flag = false;
181                    if b == 0x5Cu8 {
182                        destination_handle.write_mid_bmp(0x00A5u16);
183                        continue;
184                    }
185                    if b == 0x7Eu8 {
186                        destination_handle.write_upper_bmp(0x203Eu16);
187                        continue;
188                    }
189                    if b > 0x7Fu8 || b == 0x0Eu8 || b == 0x0Fu8 {
190                        return (
191                            DecoderResult::Malformed(1, 0),
192                            unread_handle.consumed(),
193                            destination_handle.written(),
194                        );
195                    }
196                    destination_handle.write_ascii(b);
197                    continue;
198                }
199                Iso2022JpDecoderState::Katakana => {
200                    if b == 0x1Bu8 {
201                        self.decoder_state = Iso2022JpDecoderState::EscapeStart;
202                        continue;
203                    }
204                    self.output_flag = false;
205                    if (0x21u8..=0x5Fu8).contains(&b) {
206                        destination_handle.write_upper_bmp(u16::from(b) - 0x21u16 + 0xFF61u16);
207                        continue;
208                    }
209                    return (
210                        DecoderResult::Malformed(1, 0),
211                        unread_handle.consumed(),
212                        destination_handle.written(),
213                    );
214                }
215                Iso2022JpDecoderState::LeadByte => {
216                    if b == 0x1Bu8 {
217                        self.decoder_state = Iso2022JpDecoderState::EscapeStart;
218                        continue;
219                    }
220                    self.output_flag = false;
221                    if (0x21u8..=0x7Eu8).contains(&b) {
222                        self.lead = b;
223                        self.decoder_state = Iso2022JpDecoderState::TrailByte;
224                        continue;
225                    }
226                    return (
227                        DecoderResult::Malformed(1, 0),
228                        unread_handle.consumed(),
229                        destination_handle.written(),
230                    );
231                }
232                Iso2022JpDecoderState::TrailByte => {
233                    if b == 0x1Bu8 {
234                        self.decoder_state = Iso2022JpDecoderState::EscapeStart;
235                        // The byte in error is the previous
236                        // lead byte.
237                        return (
238                            DecoderResult::Malformed(1, 1),
239                            unread_handle.consumed(),
240                            destination_handle.written(),
241                        );
242                    }
243                    self.decoder_state = Iso2022JpDecoderState::LeadByte;
244                    let jis0208_lead_minus_offset = self.lead - 0x21;
245                    let byte = b;
246                    let handle = destination_handle;
247                    // The code below uses else after continue in
248                    // order to retain the structure seen in EUC-JP.
249                    let trail_minus_offset = byte.wrapping_sub(0x21);
250                    // Fast-track Hiragana (60% according to Lunde)
251                    // and Katakana (10% acconding to Lunde).
252                    if jis0208_lead_minus_offset == 0x03 && trail_minus_offset < 0x53 {
253                        // Hiragana
254                        handle.write_upper_bmp(0x3041 + u16::from(trail_minus_offset));
255                        continue;
256                    } else if jis0208_lead_minus_offset == 0x04 && trail_minus_offset < 0x56 {
257                        // Katakana
258                        handle.write_upper_bmp(0x30A1 + u16::from(trail_minus_offset));
259                        continue;
260                    } else if trail_minus_offset > (0xFE - 0xA1) {
261                        return (
262                            DecoderResult::Malformed(2, 0),
263                            unread_handle.consumed(),
264                            handle.written(),
265                        );
266                    } else {
267                        let pointer =
268                            mul_94(jis0208_lead_minus_offset) + trail_minus_offset as usize;
269                        let level1_pointer = pointer.wrapping_sub(1410);
270                        if level1_pointer < JIS0208_LEVEL1_KANJI.len() {
271                            handle.write_upper_bmp(JIS0208_LEVEL1_KANJI[level1_pointer]);
272                            continue;
273                        } else {
274                            let level2_pointer = pointer.wrapping_sub(4418);
275                            if level2_pointer < JIS0208_LEVEL2_AND_ADDITIONAL_KANJI.len() {
276                                handle.write_upper_bmp(
277                                    JIS0208_LEVEL2_AND_ADDITIONAL_KANJI[level2_pointer],
278                                );
279                                continue;
280                            } else {
281                                let ibm_pointer = pointer.wrapping_sub(8272);
282                                if ibm_pointer < IBM_KANJI.len() {
283                                    handle.write_upper_bmp(IBM_KANJI[ibm_pointer]);
284                                    continue;
285                                } else if let Some(bmp) = jis0208_symbol_decode(pointer) {
286                                    handle.write_bmp_excl_ascii(bmp);
287                                    continue;
288                                } else if let Some(bmp) = jis0208_range_decode(pointer) {
289                                    handle.write_bmp_excl_ascii(bmp);
290                                    continue;
291                                } else {
292                                    return (
293                                        DecoderResult::Malformed(2, 0),
294                                        unread_handle.consumed(),
295                                        handle.written(),
296                                    );
297                                }
298                            }
299                        }
300                    }
301                }
302                Iso2022JpDecoderState::EscapeStart => {
303                    if b == 0x24u8 || b == 0x28u8 {
304                        self.lead = b;
305                        self.decoder_state = Iso2022JpDecoderState::Escape;
306                        continue;
307                    }
308                    self.output_flag = false;
309                    self.decoder_state = self.output_state;
310                    return (
311                        DecoderResult::Malformed(1, 0),
312                        unread_handle.unread(),
313                        destination_handle.written(),
314                    );
315                }
316                Iso2022JpDecoderState::Escape => {
317                    let mut state: Option<Iso2022JpDecoderState> = None;
318                    if self.lead == 0x28u8 && b == 0x42u8 {
319                        state = Some(Iso2022JpDecoderState::Ascii);
320                    } else if self.lead == 0x28u8 && b == 0x4Au8 {
321                        state = Some(Iso2022JpDecoderState::Roman);
322                    } else if self.lead == 0x28u8 && b == 0x49u8 {
323                        state = Some(Iso2022JpDecoderState::Katakana);
324                    } else if self.lead == 0x24u8 && (b == 0x40u8 || b == 0x42u8) {
325                        state = Some(Iso2022JpDecoderState::LeadByte);
326                    }
327                    match state {
328                        Some(s) => {
329                            self.lead = 0x0u8;
330                            self.decoder_state = s;
331                            self.output_state = s;
332                            let flag = self.output_flag;
333                            self.output_flag = true;
334                            if flag {
335                                // We had an escape sequence
336                                // immediately following another
337                                // escape sequence. Therefore,
338                                // the first one of these was
339                                // useless.
340                                return (
341                                    DecoderResult::Malformed(3, 3),
342                                    unread_handle.consumed(),
343                                    destination_handle.written(),
344                                );
345                            }
346                            continue;
347                        }
348                        None => {
349                            // self.lead is still the previous
350                            // byte. It will be processed in
351                            // the preabmle upon next call.
352                            self.pending_prepended = true;
353                            self.output_flag = false;
354                            self.decoder_state = self.output_state;
355                            // The byte in error is not the
356                            // current or the previous byte but
357                            // the one before those (lone 0x1B).
358                            return (
359                                DecoderResult::Malformed(1, 1),
360                                unread_handle.unread(),
361                                destination_handle.written(),
362                            );
363                        }
364                    }
365                }
366            }
367        },
368        self = self,
369        src_consumed = src_consumed,
370        dest = dest,
371        source = source,
372        byte = b,
373        destination_handle = destination_handle,
374        unread_handle = unread_handle,
375        destination_check = check_space_bmp
376    );
377}
378
379#[cfg(feature = "fast-kanji-encode")]
380#[inline(always)]
381fn is_kanji_mapped(bmp: u16) -> bool {
382    // Use the shift_jis variant, because we don't care about the
383    // byte values here.
384    jis0208_kanji_shift_jis_encode(bmp).is_some()
385}
386
387#[cfg(not(feature = "fast-kanji-encode"))]
388#[allow(clippy::redundant_pattern_matching, clippy::if_same_then_else)]
389#[inline(always)]
390fn is_kanji_mapped(bmp: u16) -> bool {
391    0x4EDD == bmp
392        || jis0208_level1_kanji_shift_jis_encode(bmp).is_some()
393        // Use the shift_jis variant, because we don't care about the
394        // byte values here.
395        || jis0208_level2_and_additional_kanji_encode(bmp).is_some()
396        || position(&IBM_KANJI[..], bmp).is_some()
397}
398
399#[allow(clippy::redundant_pattern_matching, clippy::if_same_then_else)]
400fn is_mapped_for_two_byte_encode(bmp: u16) -> bool {
401    // The code below uses else after return to
402    // keep the same structure as in EUC-JP.
403    // Lunde says 60% Hiragana, 30% Kanji, 10% Katakana
404    let bmp_minus_hiragana = bmp.wrapping_sub(0x3041);
405    if bmp_minus_hiragana < 0x53 {
406        true
407    } else if in_inclusive_range16(bmp, 0x4E00, 0x9FA0) {
408        is_kanji_mapped(bmp)
409    } else {
410        let bmp_minus_katakana = bmp.wrapping_sub(0x30A1);
411        if bmp_minus_katakana < 0x56 {
412            true
413        } else {
414            let bmp_minus_space = bmp.wrapping_sub(0x3000);
415            // fast-track common punctuation
416            bmp_minus_space < 3
417                || in_inclusive_range16(bmp, 0xFF61, 0xFF9F)
418                || bmp == 0x2212
419                || jis0208_range_encode(bmp).is_some()
420                || in_inclusive_range16(bmp, 0xFA0E, 0xFA2D)
421                || bmp == 0xF929
422                || bmp == 0xF9DC
423                || ibm_symbol_encode(bmp).is_some()
424                || jis0208_symbol_encode(bmp).is_some()
425        }
426    }
427}
428
429#[cfg(feature = "fast-kanji-encode")]
430#[inline(always)]
431fn encode_kanji(bmp: u16) -> Option<(u8, u8)> {
432    jis0208_kanji_iso_2022_jp_encode(bmp)
433}
434
435#[cfg(not(feature = "fast-kanji-encode"))]
436#[inline(always)]
437fn encode_kanji(bmp: u16) -> Option<(u8, u8)> {
438    if 0x4EDD == bmp {
439        // Ideograph on the symbol row!
440        Some((0x21, 0xB8 - 0x80))
441    } else if let Some((lead, trail)) = jis0208_level1_kanji_iso_2022_jp_encode(bmp) {
442        Some((lead, trail))
443    } else if let Some(pos) = jis0208_level2_and_additional_kanji_encode(bmp) {
444        let lead = (pos / 94) + (0xD0 - 0x80);
445        let trail = (pos % 94) + 0x21;
446        Some((lead as u8, trail as u8))
447    } else if let Some(pos) = position(&IBM_KANJI[..], bmp) {
448        let lead = (pos / 94) + (0xF9 - 0x80);
449        let trail = (pos % 94) + 0x21;
450        Some((lead as u8, trail as u8))
451    } else {
452        None
453    }
454}
455
456enum Iso2022JpEncoderState {
457    Ascii,
458    Roman,
459    Jis0208,
460}
461
462pub struct Iso2022JpEncoder {
463    state: Iso2022JpEncoderState,
464}
465
466impl Iso2022JpEncoder {
467    pub fn new(encoding: &'static Encoding) -> Encoder {
468        Encoder::new(
469            encoding,
470            VariantEncoder::Iso2022Jp(Iso2022JpEncoder {
471                state: Iso2022JpEncoderState::Ascii,
472            }),
473        )
474    }
475
476    pub fn has_pending_state(&self) -> bool {
477        !matches!(self.state, Iso2022JpEncoderState::Ascii)
478    }
479
480    pub fn max_buffer_length_from_utf16_without_replacement(
481        &self,
482        u16_length: usize,
483    ) -> Option<usize> {
484        // Worst case: every other character is ASCII/Roman and every other
485        // JIS0208.
486        // Two UTF-16 input units:
487        // Transition to Roman: 3
488        // Roman/ASCII: 1
489        // Transition to JIS0208: 3
490        // JIS0208: 2
491        // End transition: 3
492        checked_add_opt(
493            checked_add(3, u16_length.checked_mul(4)),
494            checked_div(u16_length.checked_add(1), 2),
495        )
496    }
497
498    pub fn max_buffer_length_from_utf8_without_replacement(
499        &self,
500        byte_length: usize,
501    ) -> Option<usize> {
502        // Worst case: every other character is ASCII/Roman and every other
503        // JIS0208.
504        // Three UTF-8 input units: 1 ASCII, 2 JIS0208
505        // Transition to ASCII: 3
506        // Roman/ASCII: 1
507        // Transition to JIS0208: 3
508        // JIS0208: 2
509        // End transition: 3
510        checked_add(3, byte_length.checked_mul(3))
511    }
512
513    encoder_functions!(
514        eof = {
515            match self.state {
516                Iso2022JpEncoderState::Ascii => {}
517                _ => match dest.check_space_three() {
518                    Space::Full(dst_written) => {
519                        return (EncoderResult::OutputFull, src_consumed, dst_written);
520                    }
521                    Space::Available(destination_handle) => {
522                        self.state = Iso2022JpEncoderState::Ascii;
523                        destination_handle.write_three(0x1Bu8, 0x28u8, 0x42u8);
524                    }
525                },
526            }
527        },
528        body = {
529            match self.state {
530                Iso2022JpEncoderState::Ascii => {
531                    if c == '\u{0E}' || c == '\u{0F}' || c == '\u{1B}' {
532                        return (
533                            EncoderResult::Unmappable('\u{FFFD}'),
534                            unread_handle.consumed(),
535                            destination_handle.written(),
536                        );
537                    }
538                    if c <= '\u{7F}' {
539                        destination_handle.write_one(c as u8);
540                        continue;
541                    }
542                    if c == '\u{A5}' || c == '\u{203E}' {
543                        self.state = Iso2022JpEncoderState::Roman;
544                        destination_handle.write_three(0x1Bu8, 0x28u8, 0x4Au8);
545                        unread_handle.unread();
546                        continue;
547                    }
548                    if c > '\u{FFFF}' {
549                        return (
550                            EncoderResult::Unmappable(c),
551                            unread_handle.consumed(),
552                            destination_handle.written(),
553                        );
554                    }
555                    // Yes, if c is in index, we'll search
556                    // again in the Jis0208 state, but this
557                    // encoder is not worth optimizing.
558                    if is_mapped_for_two_byte_encode(c as u16) {
559                        self.state = Iso2022JpEncoderState::Jis0208;
560                        destination_handle.write_three(0x1Bu8, 0x24u8, 0x42u8);
561                        unread_handle.unread();
562                        continue;
563                    }
564                    return (
565                        EncoderResult::Unmappable(c),
566                        unread_handle.consumed(),
567                        destination_handle.written(),
568                    );
569                }
570                Iso2022JpEncoderState::Roman => {
571                    if c == '\u{0E}' || c == '\u{0F}' || c == '\u{1B}' {
572                        return (
573                            EncoderResult::Unmappable('\u{FFFD}'),
574                            unread_handle.consumed(),
575                            destination_handle.written(),
576                        );
577                    }
578                    if c == '\u{5C}' || c == '\u{7E}' {
579                        self.state = Iso2022JpEncoderState::Ascii;
580                        destination_handle.write_three(0x1Bu8, 0x28u8, 0x42u8);
581                        unread_handle.unread();
582                        continue;
583                    }
584                    if c <= '\u{7F}' {
585                        destination_handle.write_one(c as u8);
586                        continue;
587                    }
588                    if c == '\u{A5}' {
589                        destination_handle.write_one(0x5Cu8);
590                        continue;
591                    }
592                    if c == '\u{203E}' {
593                        destination_handle.write_one(0x7Eu8);
594                        continue;
595                    }
596                    if c > '\u{FFFF}' {
597                        return (
598                            EncoderResult::Unmappable(c),
599                            unread_handle.consumed(),
600                            destination_handle.written(),
601                        );
602                    }
603                    // Yes, if c is in index, we'll search
604                    // again in the Jis0208 state, but this
605                    // encoder is not worth optimizing.
606                    if is_mapped_for_two_byte_encode(c as u16) {
607                        self.state = Iso2022JpEncoderState::Jis0208;
608                        destination_handle.write_three(0x1Bu8, 0x24u8, 0x42u8);
609                        unread_handle.unread();
610                        continue;
611                    }
612                    return (
613                        EncoderResult::Unmappable(c),
614                        unread_handle.consumed(),
615                        destination_handle.written(),
616                    );
617                }
618                Iso2022JpEncoderState::Jis0208 => {
619                    if c <= '\u{7F}' {
620                        self.state = Iso2022JpEncoderState::Ascii;
621                        destination_handle.write_three(0x1Bu8, 0x28u8, 0x42u8);
622                        unread_handle.unread();
623                        continue;
624                    }
625                    if c == '\u{A5}' || c == '\u{203E}' {
626                        self.state = Iso2022JpEncoderState::Roman;
627                        destination_handle.write_three(0x1Bu8, 0x28u8, 0x4Au8);
628                        unread_handle.unread();
629                        continue;
630                    }
631                    if c > '\u{FFFF}' {
632                        // Transition to ASCII here in order
633                        // not to make it the responsibility
634                        // of the caller.
635                        self.state = Iso2022JpEncoderState::Ascii;
636                        return (
637                            EncoderResult::Unmappable(c),
638                            unread_handle.consumed(),
639                            destination_handle.write_three_return_written(0x1Bu8, 0x28u8, 0x42u8),
640                        );
641                    }
642                    let bmp = c as u16;
643                    let handle = destination_handle;
644                    // The code below uses else after continue to
645                    // keep the same structure as in EUC-JP.
646                    // Lunde says 60% Hiragana, 30% Kanji, 10% Katakana
647                    let bmp_minus_hiragana = bmp.wrapping_sub(0x3041);
648                    if bmp_minus_hiragana < 0x53 {
649                        handle.write_two(0x24, 0x21 + bmp_minus_hiragana as u8);
650                        continue;
651                    } else if in_inclusive_range16(bmp, 0x4E00, 0x9FA0) {
652                        if let Some((lead, trail)) = encode_kanji(bmp) {
653                            handle.write_two(lead, trail);
654                            continue;
655                        } else {
656                            self.state = Iso2022JpEncoderState::Ascii;
657                            return (
658                                EncoderResult::Unmappable(c),
659                                unread_handle.consumed(),
660                                handle.write_three_return_written(0x1Bu8, 0x28u8, 0x42u8),
661                            );
662                        }
663                    } else {
664                        let bmp_minus_katakana = bmp.wrapping_sub(0x30A1);
665                        if bmp_minus_katakana < 0x56 {
666                            handle.write_two(0x25, 0x21 + bmp_minus_katakana as u8);
667                            continue;
668                        } else {
669                            let bmp_minus_space = bmp.wrapping_sub(0x3000);
670                            if bmp_minus_space < 3 {
671                                // fast-track common punctuation
672                                handle.write_two(0x21, 0x21 + bmp_minus_space as u8);
673                                continue;
674                            }
675                            let bmp_minus_half_width = bmp.wrapping_sub(0xFF61);
676                            if bmp_minus_half_width <= (0xFF9F - 0xFF61) {
677                                // We have half-width katakana. The lead is either
678                                // row 1 or 5 of JIS X 0208, so the lookup table
679                                // only stores the trail.
680                                let lead =
681                                    if bmp != 0xFF70 && in_inclusive_range16(bmp, 0xFF66, 0xFF9D) {
682                                        0x25u8
683                                    } else {
684                                        0x21u8
685                                    };
686                                let trail =
687                                    ISO_2022_JP_HALF_WIDTH_TRAIL[bmp_minus_half_width as usize];
688                                handle.write_two(lead, trail);
689                                continue;
690                            } else if bmp == 0x2212 {
691                                handle.write_two(0x21, 0x5D);
692                                continue;
693                            } else if let Some(pointer) = jis0208_range_encode(bmp) {
694                                let lead = (pointer / 94) + 0x21;
695                                let trail = (pointer % 94) + 0x21;
696                                handle.write_two(lead as u8, trail as u8);
697                                continue;
698                            } else if in_inclusive_range16(bmp, 0xFA0E, 0xFA2D)
699                                || bmp == 0xF929
700                                || bmp == 0xF9DC
701                            {
702                                // Guaranteed to be found in IBM_KANJI
703                                let pos = position(&IBM_KANJI[..], bmp).unwrap();
704                                let lead = (pos / 94) + (0xF9 - 0x80);
705                                let trail = (pos % 94) + 0x21;
706                                handle.write_two(lead as u8, trail as u8);
707                                continue;
708                            } else if let Some(pointer) = ibm_symbol_encode(bmp) {
709                                let lead = (pointer / 94) + 0x21;
710                                let trail = (pointer % 94) + 0x21;
711                                handle.write_two(lead as u8, trail as u8);
712                                continue;
713                            } else if let Some(pointer) = jis0208_symbol_encode(bmp) {
714                                let lead = (pointer / 94) + 0x21;
715                                let trail = (pointer % 94) + 0x21;
716                                handle.write_two(lead as u8, trail as u8);
717                                continue;
718                            } else {
719                                self.state = Iso2022JpEncoderState::Ascii;
720                                return (
721                                    EncoderResult::Unmappable(c),
722                                    unread_handle.consumed(),
723                                    handle.write_three_return_written(0x1Bu8, 0x28u8, 0x42u8),
724                                );
725                            }
726                        }
727                    }
728                }
729            }
730        },
731        self = self,
732        src_consumed = src_consumed,
733        source = source,
734        dest = dest,
735        c = c,
736        destination_handle = destination_handle,
737        unread_handle = unread_handle,
738        destination_check = check_space_three
739    );
740}
741
742// Any copyright to the test code below this comment is dedicated to the
743// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
744
745#[cfg(all(test, feature = "alloc"))]
746mod tests {
747    use super::super::testing::*;
748    use super::super::*;
749
750    fn decode_iso_2022_jp(bytes: &[u8], expect: &str) {
751        decode(ISO_2022_JP, bytes, expect);
752    }
753
754    fn encode_iso_2022_jp(string: &str, expect: &[u8]) {
755        encode(ISO_2022_JP, string, expect);
756    }
757
758    #[test]
759    fn test_iso_2022_jp_decode() {
760        // Empty
761        decode_iso_2022_jp(b"", &"");
762
763        // ASCII
764        decode_iso_2022_jp(b"\x61\x62", "\u{0061}\u{0062}");
765        decode_iso_2022_jp(b"\x7F\x0E\x0F", "\u{007F}\u{FFFD}\u{FFFD}");
766
767        // Partial escapes
768        decode_iso_2022_jp(b"\x1B", "\u{FFFD}");
769        decode_iso_2022_jp(b"\x1B$", "\u{FFFD}$");
770        decode_iso_2022_jp(b"\x1B(", "\u{FFFD}(");
771        decode_iso_2022_jp(b"\x1B.", "\u{FFFD}.");
772
773        // ISO escapes
774        decode_iso_2022_jp(b"\x1B(B", ""); // ASCII
775        decode_iso_2022_jp(b"\x1B(J", ""); // Roman
776        decode_iso_2022_jp(b"\x1B$@", ""); // 0208
777        decode_iso_2022_jp(b"\x1B$B", ""); // 0208
778        decode_iso_2022_jp(b"\x1B$(D", "\u{FFFD}$(D"); // 2012
779        decode_iso_2022_jp(b"\x1B$A", "\u{FFFD}$A"); // GB2312
780        decode_iso_2022_jp(b"\x1B$(C", "\u{FFFD}$(C"); // KR
781        decode_iso_2022_jp(b"\x1B.A", "\u{FFFD}.A"); // Latin-1
782        decode_iso_2022_jp(b"\x1B.F", "\u{FFFD}.F"); // Greek
783        decode_iso_2022_jp(b"\x1B(I", ""); // Half-width Katakana
784        decode_iso_2022_jp(b"\x1B$(O", "\u{FFFD}$(O"); // 2013
785        decode_iso_2022_jp(b"\x1B$(P", "\u{FFFD}$(P"); // 2013
786        decode_iso_2022_jp(b"\x1B$(Q", "\u{FFFD}$(Q"); // 2013
787        decode_iso_2022_jp(b"\x1B$)C", "\u{FFFD}$)C"); // KR
788        decode_iso_2022_jp(b"\x1B$)A", "\u{FFFD}$)A"); // GB2312
789        decode_iso_2022_jp(b"\x1B$)G", "\u{FFFD}$)G"); // CNS
790        decode_iso_2022_jp(b"\x1B$*H", "\u{FFFD}$*H"); // CNS
791        decode_iso_2022_jp(b"\x1B$)E", "\u{FFFD}$)E"); // IR
792        decode_iso_2022_jp(b"\x1B$+I", "\u{FFFD}$+I"); // CNS
793        decode_iso_2022_jp(b"\x1B$+J", "\u{FFFD}$+J"); // CNS
794        decode_iso_2022_jp(b"\x1B$+K", "\u{FFFD}$+K"); // CNS
795        decode_iso_2022_jp(b"\x1B$+L", "\u{FFFD}$+L"); // CNS
796        decode_iso_2022_jp(b"\x1B$+M", "\u{FFFD}$+M"); // CNS
797        decode_iso_2022_jp(b"\x1B$(@", "\u{FFFD}$(@"); // 0208
798        decode_iso_2022_jp(b"\x1B$(A", "\u{FFFD}$(A"); // GB2312
799        decode_iso_2022_jp(b"\x1B$(B", "\u{FFFD}$(B"); // 0208
800        decode_iso_2022_jp(b"\x1B%G", "\u{FFFD}%G"); // UTF-8
801
802        // ASCII
803        decode_iso_2022_jp(b"\x5B", "\u{005B}");
804        decode_iso_2022_jp(b"\x5C", "\u{005C}");
805        decode_iso_2022_jp(b"\x7E", "\u{007E}");
806        decode_iso_2022_jp(b"\x0E", "\u{FFFD}");
807        decode_iso_2022_jp(b"\x0F", "\u{FFFD}");
808        decode_iso_2022_jp(b"\x80", "\u{FFFD}");
809        decode_iso_2022_jp(b"\xFF", "\u{FFFD}");
810        decode_iso_2022_jp(b"\x1B(B\x5B", "\u{005B}");
811        decode_iso_2022_jp(b"\x1B(B\x5C", "\u{005C}");
812        decode_iso_2022_jp(b"\x1B(B\x7E", "\u{007E}");
813        decode_iso_2022_jp(b"\x1B(B\x0E", "\u{FFFD}");
814        decode_iso_2022_jp(b"\x1B(B\x0F", "\u{FFFD}");
815        decode_iso_2022_jp(b"\x1B(B\x80", "\u{FFFD}");
816        decode_iso_2022_jp(b"\x1B(B\xFF", "\u{FFFD}");
817
818        // Roman
819        decode_iso_2022_jp(b"\x1B(J\x5B", "\u{005B}");
820        decode_iso_2022_jp(b"\x1B(J\x5C", "\u{00A5}");
821        decode_iso_2022_jp(b"\x1B(J\x7E", "\u{203E}");
822        decode_iso_2022_jp(b"\x1B(J\x0E", "\u{FFFD}");
823        decode_iso_2022_jp(b"\x1B(J\x0F", "\u{FFFD}");
824        decode_iso_2022_jp(b"\x1B(J\x80", "\u{FFFD}");
825        decode_iso_2022_jp(b"\x1B(J\xFF", "\u{FFFD}");
826
827        // Katakana
828        decode_iso_2022_jp(b"\x1B(I\x20", "\u{FFFD}");
829        decode_iso_2022_jp(b"\x1B(I\x21", "\u{FF61}");
830        decode_iso_2022_jp(b"\x1B(I\x5F", "\u{FF9F}");
831        decode_iso_2022_jp(b"\x1B(I\x60", "\u{FFFD}");
832        decode_iso_2022_jp(b"\x1B(I\x0E", "\u{FFFD}");
833        decode_iso_2022_jp(b"\x1B(I\x0F", "\u{FFFD}");
834        decode_iso_2022_jp(b"\x1B(I\x80", "\u{FFFD}");
835        decode_iso_2022_jp(b"\x1B(I\xFF", "\u{FFFD}");
836
837        // 0208 differences from 1978 to 1983
838        decode_iso_2022_jp(b"\x1B$@\x54\x64", "\u{58FA}");
839        decode_iso_2022_jp(b"\x1B$@\x44\x5B", "\u{58F7}");
840        decode_iso_2022_jp(b"\x1B$@\x74\x21", "\u{582F}");
841        decode_iso_2022_jp(b"\x1B$@\x36\x46", "\u{5C2D}");
842        decode_iso_2022_jp(b"\x1B$@\x28\x2E", "\u{250F}");
843        decode_iso_2022_jp(b"\x1B$B\x54\x64", "\u{58FA}");
844        decode_iso_2022_jp(b"\x1B$B\x44\x5B", "\u{58F7}");
845        decode_iso_2022_jp(b"\x1B$B\x74\x21", "\u{582F}");
846        decode_iso_2022_jp(b"\x1B$B\x36\x46", "\u{5C2D}");
847        decode_iso_2022_jp(b"\x1B$B\x28\x2E", "\u{250F}");
848
849        // Broken 0208
850        decode_iso_2022_jp(b"\x1B$B\x28\x41", "\u{FFFD}");
851        decode_iso_2022_jp(b"\x1B$@\x80\x54\x64", "\u{FFFD}\u{58FA}");
852        decode_iso_2022_jp(b"\x1B$B\x28\x80", "\u{FFFD}");
853
854        if cfg!(miri) {
855            // Miri is too slow
856            return;
857        }
858
859        // Transitions
860        decode_iso_2022_jp(b"\x1B(B\x5C\x1B(J\x5C", "\u{005C}\u{00A5}");
861        decode_iso_2022_jp(b"\x1B(B\x5C\x1B(I\x21", "\u{005C}\u{FF61}");
862        decode_iso_2022_jp(b"\x1B(B\x5C\x1B$@\x54\x64", "\u{005C}\u{58FA}");
863        decode_iso_2022_jp(b"\x1B(B\x5C\x1B$B\x54\x64", "\u{005C}\u{58FA}");
864
865        decode_iso_2022_jp(b"\x1B(J\x5C\x1B(B\x5C", "\u{00A5}\u{005C}");
866        decode_iso_2022_jp(b"\x1B(J\x5C\x1B(I\x21", "\u{00A5}\u{FF61}");
867        decode_iso_2022_jp(b"\x1B(J\x5C\x1B$@\x54\x64", "\u{00A5}\u{58FA}");
868        decode_iso_2022_jp(b"\x1B(J\x5C\x1B$B\x54\x64", "\u{00A5}\u{58FA}");
869
870        decode_iso_2022_jp(b"\x1B(I\x21\x1B(J\x5C", "\u{FF61}\u{00A5}");
871        decode_iso_2022_jp(b"\x1B(I\x21\x1B(B\x5C", "\u{FF61}\u{005C}");
872        decode_iso_2022_jp(b"\x1B(I\x21\x1B$@\x54\x64", "\u{FF61}\u{58FA}");
873        decode_iso_2022_jp(b"\x1B(I\x21\x1B$B\x54\x64", "\u{FF61}\u{58FA}");
874
875        decode_iso_2022_jp(b"\x1B$@\x54\x64\x1B(J\x5C", "\u{58FA}\u{00A5}");
876        decode_iso_2022_jp(b"\x1B$@\x54\x64\x1B(I\x21", "\u{58FA}\u{FF61}");
877        decode_iso_2022_jp(b"\x1B$@\x54\x64\x1B(B\x5C", "\u{58FA}\u{005C}");
878        decode_iso_2022_jp(b"\x1B$@\x54\x64\x1B$B\x54\x64", "\u{58FA}\u{58FA}");
879
880        decode_iso_2022_jp(b"\x1B$B\x54\x64\x1B(J\x5C", "\u{58FA}\u{00A5}");
881        decode_iso_2022_jp(b"\x1B$B\x54\x64\x1B(I\x21", "\u{58FA}\u{FF61}");
882        decode_iso_2022_jp(b"\x1B$B\x54\x64\x1B$@\x54\x64", "\u{58FA}\u{58FA}");
883        decode_iso_2022_jp(b"\x1B$B\x54\x64\x1B(B\x5C", "\u{58FA}\u{005C}");
884
885        // Empty transitions
886        decode_iso_2022_jp(b"\x1B(B\x1B(J", "\u{FFFD}");
887        decode_iso_2022_jp(b"\x1B(B\x1B(I", "\u{FFFD}");
888        decode_iso_2022_jp(b"\x1B(B\x1B$@", "\u{FFFD}");
889        decode_iso_2022_jp(b"\x1B(B\x1B$B", "\u{FFFD}");
890
891        decode_iso_2022_jp(b"\x1B(J\x1B(B", "\u{FFFD}");
892        decode_iso_2022_jp(b"\x1B(J\x1B(I", "\u{FFFD}");
893        decode_iso_2022_jp(b"\x1B(J\x1B$@", "\u{FFFD}");
894        decode_iso_2022_jp(b"\x1B(J\x1B$B", "\u{FFFD}");
895
896        decode_iso_2022_jp(b"\x1B(I\x1B(J", "\u{FFFD}");
897        decode_iso_2022_jp(b"\x1B(I\x1B(B", "\u{FFFD}");
898        decode_iso_2022_jp(b"\x1B(I\x1B$@", "\u{FFFD}");
899        decode_iso_2022_jp(b"\x1B(I\x1B$B", "\u{FFFD}");
900
901        decode_iso_2022_jp(b"\x1B$@\x1B(J", "\u{FFFD}");
902        decode_iso_2022_jp(b"\x1B$@\x1B(I", "\u{FFFD}");
903        decode_iso_2022_jp(b"\x1B$@\x1B(B", "\u{FFFD}");
904        decode_iso_2022_jp(b"\x1B$@\x1B$B", "\u{FFFD}");
905
906        decode_iso_2022_jp(b"\x1B$B\x1B(J", "\u{FFFD}");
907        decode_iso_2022_jp(b"\x1B$B\x1B(I", "\u{FFFD}");
908        decode_iso_2022_jp(b"\x1B$B\x1B$@", "\u{FFFD}");
909        decode_iso_2022_jp(b"\x1B$B\x1B(B", "\u{FFFD}");
910
911        // Transitions to self
912        decode_iso_2022_jp(b"\x1B(B\x5C\x1B(B\x5C", "\u{005C}\u{005C}");
913        decode_iso_2022_jp(b"\x1B(J\x5C\x1B(J\x5C", "\u{00A5}\u{00A5}");
914        decode_iso_2022_jp(b"\x1B(I\x21\x1B(I\x21", "\u{FF61}\u{FF61}");
915        decode_iso_2022_jp(b"\x1B$@\x54\x64\x1B$@\x54\x64", "\u{58FA}\u{58FA}");
916        decode_iso_2022_jp(b"\x1B$B\x54\x64\x1B$B\x54\x64", "\u{58FA}\u{58FA}");
917    }
918
919    #[test]
920    fn test_iso_2022_jp_decode_eof_with_small_output() {
921        let cases: [(&[u8], &str); 5] = [
922            (b"\x1B$B9\x1B", "\u{FFFD}\u{FFFD}"),
923            (b"\x1B$B9\x1B$", "\u{FFFD}\u{FFFD}\u{FFFD}"),
924            (b"\x1B$B9\x1B(", "\u{FFFD}\u{FFFD}\u{FFFD}"),
925            (b"\x80\x1B$", "\u{FFFD}\u{FFFD}$"),
926            (b"a\x1B$B9", "a\u{FFFD}"),
927        ];
928        for &(bytes, expect) in cases.iter() {
929            decode_iso_2022_jp(bytes, expect);
930        }
931        for &(bytes, expect) in cases.iter() {
932            for len in 4..16 {
933                let mut decoder = ISO_2022_JP.new_decoder_without_bom_handling();
934                let mut dst = vec![0u8; len];
935                let mut output = String::new();
936                let mut src = bytes;
937                loop {
938                    let (result, read, written, _) = decoder.decode_to_utf8(src, &mut dst, true);
939                    output.push_str(core::str::from_utf8(&dst[..written]).unwrap());
940                    src = &src[read..];
941                    if result == CoderResult::InputEmpty {
942                        break;
943                    }
944                }
945                assert_eq!(output, expect);
946            }
947            for len in 2..8 {
948                let mut decoder = ISO_2022_JP.new_decoder_without_bom_handling();
949                let mut dst = vec![0u16; len];
950                let mut output = Vec::new();
951                let mut src = bytes;
952                loop {
953                    let (result, read, written, _) = decoder.decode_to_utf16(src, &mut dst, true);
954                    output.extend_from_slice(&dst[..written]);
955                    src = &src[read..];
956                    if result == CoderResult::InputEmpty {
957                        break;
958                    }
959                }
960                assert_eq!(String::from_utf16(&output).unwrap(), expect);
961            }
962        }
963    }
964
965    #[test]
966    fn test_iso_2022_jp_encode() {
967        // Empty
968        encode_iso_2022_jp("", b"");
969
970        // ASCII
971        encode_iso_2022_jp("ab", b"ab");
972        encode_iso_2022_jp("\u{1F4A9}", b"&#128169;");
973        encode_iso_2022_jp("\x1B", b"&#65533;");
974        encode_iso_2022_jp("\x0E", b"&#65533;");
975        encode_iso_2022_jp("\x0F", b"&#65533;");
976
977        // Roman
978        encode_iso_2022_jp("a\u{00A5}b", b"a\x1B(J\x5Cb\x1B(B");
979        encode_iso_2022_jp("a\u{203E}b", b"a\x1B(J\x7Eb\x1B(B");
980        if !cfg!(miri) {
981            // Miri is too slow
982            encode_iso_2022_jp("a\u{00A5}b\x5C", b"a\x1B(J\x5Cb\x1B(B\x5C");
983            encode_iso_2022_jp("a\u{203E}b\x7E", b"a\x1B(J\x7Eb\x1B(B\x7E");
984            encode_iso_2022_jp("\u{00A5}\u{1F4A9}", b"\x1B(J\x5C&#128169;\x1B(B");
985            encode_iso_2022_jp("\u{00A5}\x1B", b"\x1B(J\x5C&#65533;\x1B(B");
986            encode_iso_2022_jp("\u{00A5}\x0E", b"\x1B(J\x5C&#65533;\x1B(B");
987            encode_iso_2022_jp("\u{00A5}\x0F", b"\x1B(J\x5C&#65533;\x1B(B");
988            encode_iso_2022_jp("\u{00A5}\u{58FA}", b"\x1B(J\x5C\x1B$B\x54\x64\x1B(B");
989        }
990
991        // Half-width Katakana
992        encode_iso_2022_jp("\u{FF61}", b"\x1B$B\x21\x23\x1B(B");
993        encode_iso_2022_jp("\u{FF65}", b"\x1B$B\x21\x26\x1B(B");
994        if !cfg!(miri) {
995            // Miri is too slow
996            encode_iso_2022_jp("\u{FF66}", b"\x1B$B\x25\x72\x1B(B");
997            encode_iso_2022_jp("\u{FF70}", b"\x1B$B\x21\x3C\x1B(B");
998            encode_iso_2022_jp("\u{FF9D}", b"\x1B$B\x25\x73\x1B(B");
999            encode_iso_2022_jp("\u{FF9E}", b"\x1B$B\x21\x2B\x1B(B");
1000            encode_iso_2022_jp("\u{FF9F}", b"\x1B$B\x21\x2C\x1B(B");
1001        }
1002
1003        // 0208
1004        encode_iso_2022_jp("\u{58FA}", b"\x1B$B\x54\x64\x1B(B");
1005        encode_iso_2022_jp("\u{58FA}\u{250F}", b"\x1B$B\x54\x64\x28\x2E\x1B(B");
1006        if !cfg!(miri) {
1007            // Miri is too slow
1008            encode_iso_2022_jp("\u{58FA}\u{1F4A9}", b"\x1B$B\x54\x64\x1B(B&#128169;");
1009            encode_iso_2022_jp("\u{58FA}\x1B", b"\x1B$B\x54\x64\x1B(B&#65533;");
1010            encode_iso_2022_jp("\u{58FA}\x0E", b"\x1B$B\x54\x64\x1B(B&#65533;");
1011            encode_iso_2022_jp("\u{58FA}\x0F", b"\x1B$B\x54\x64\x1B(B&#65533;");
1012            encode_iso_2022_jp("\u{58FA}\u{00A5}", b"\x1B$B\x54\x64\x1B(J\x5C\x1B(B");
1013            encode_iso_2022_jp("\u{58FA}a", b"\x1B$B\x54\x64\x1B(Ba");
1014        }
1015    }
1016
1017    #[test]
1018    fn test_iso_2022_jp_half_width_katakana_length() {
1019        let mut output = [0u8; 20];
1020        let mut decoder = ISO_2022_JP.new_decoder();
1021        {
1022            let (result, read, written) =
1023                decoder.decode_to_utf8_without_replacement(b"\x1B\x28\x49", &mut output, false);
1024            assert_eq!(result, DecoderResult::InputEmpty);
1025            assert_eq!(read, 3);
1026            assert_eq!(written, 0);
1027        }
1028        {
1029            let needed = decoder
1030                .max_utf8_buffer_length_without_replacement(1)
1031                .unwrap();
1032            let (result, read, written) =
1033                decoder.decode_to_utf8_without_replacement(b"\x21", &mut output[..needed], true);
1034            assert_eq!(result, DecoderResult::InputEmpty);
1035            assert_eq!(read, 1);
1036            assert_eq!(written, 3);
1037            assert_eq!(output[0], 0xEF);
1038            assert_eq!(output[1], 0xBD);
1039            assert_eq!(output[2], 0xA1);
1040        }
1041    }
1042
1043    #[test]
1044    fn test_iso_2022_jp_length_after_escape() {
1045        let mut output = [0u16; 20];
1046        let mut decoder = ISO_2022_JP.new_decoder();
1047        {
1048            let (result, read, written, had_errors) =
1049                decoder.decode_to_utf16(b"\x1B", &mut output, false);
1050            assert_eq!(result, CoderResult::InputEmpty);
1051            assert_eq!(read, 1);
1052            assert_eq!(written, 0);
1053            assert!(!had_errors);
1054        }
1055        {
1056            let needed = decoder.max_utf16_buffer_length(1).unwrap();
1057            let (result, read, written, had_errors) =
1058                decoder.decode_to_utf16(b"A", &mut output[..needed], true);
1059            assert_eq!(result, CoderResult::InputEmpty);
1060            assert_eq!(read, 1);
1061            assert_eq!(written, 2);
1062            assert!(had_errors);
1063            assert_eq!(output[0], 0xFFFD);
1064            assert_eq!(output[1], 0x0041);
1065        }
1066    }
1067
1068    #[test]
1069    fn test_iso_2022_jp_encode_from_two_low_surrogates() {
1070        let expectation = b"&#65533;&#65533;";
1071        let mut output = [0u8; 40];
1072        let mut encoder = ISO_2022_JP.new_encoder();
1073        let (result, read, written, had_errors) =
1074            encoder.encode_from_utf16(&[0xDC00u16, 0xDEDEu16], &mut output[..], true);
1075        assert_eq!(result, CoderResult::InputEmpty);
1076        assert_eq!(read, 2);
1077        assert_eq!(written, expectation.len());
1078        assert!(had_errors);
1079        assert_eq!(&output[..written], expectation);
1080    }
1081}