encoding_rs/lib.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
10// The above license applies to code in this file. The label data in
11// this file is generated from WHATWG's encodings.json, which came under
12// the following license:
13
14// Copyright © WHATWG (Apple, Google, Mozilla, Microsoft).
15//
16// Redistribution and use in source and binary forms, with or without
17// modification, are permitted provided that the following conditions are met:
18//
19// 1. Redistributions of source code must retain the above copyright notice, this
20// list of conditions and the following disclaimer.
21//
22// 2. Redistributions in binary form must reproduce the above copyright notice,
23// this list of conditions and the following disclaimer in the documentation
24// and/or other materials provided with the distribution.
25//
26// 3. Neither the name of the copyright holder nor the names of its
27// contributors may be used to endorse or promote products derived from
28// this software without specific prior written permission.
29//
30// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
34// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
38// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41#![allow(
42 clippy::doc_markdown,
43 clippy::inline_always,
44 clippy::new_ret_no_self,
45 clippy::redundant_static_lifetimes
46)]
47
48//! encoding_rs is a Gecko-oriented Free Software / Open Source implementation
49//! of the [Encoding Standard](https://encoding.spec.whatwg.org/) in Rust.
50//! Gecko-oriented means that converting to and from UTF-16 is supported in
51//! addition to converting to and from UTF-8, that the performance and
52//! streamability goals are browser-oriented, and that FFI-friendliness is a
53//! goal.
54//!
55//! Additionally, the `mem` module provides functions that are useful for
56//! applications that need to be able to deal with legacy in-memory
57//! representations of Unicode.
58//!
59//! For expectation setting, please be sure to read the sections
60//! [_UTF-16LE, UTF-16BE and Unicode Encoding Schemes_](#utf-16le-utf-16be-and-unicode-encoding-schemes),
61//! [_ISO-8859-1_](#iso-8859-1) and [_Web / Browser Focus_](#web--browser-focus) below.
62//!
63//! There is a [long-form write-up](https://hsivonen.fi/encoding_rs/) about the
64//! design and internals of the crate.
65//!
66//! # Availability
67//!
68//! The code is available under the
69//! [Apache license, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
70//! or the [MIT license](https://opensource.org/licenses/MIT), at your option.
71//! See the
72//! [`COPYRIGHT`](https://github.com/hsivonen/encoding_rs/blob/main/COPYRIGHT)
73//! file for details.
74//! The [repository is on GitHub](https://github.com/hsivonen/encoding_rs). The
75//! [crate is available on crates.io](https://crates.io/crates/encoding_rs).
76//!
77//! # Integration with `std::io`
78//!
79//! This crate doesn't implement traits from `std::io`. However, for the case of
80//! wrapping a `std::io::Read` in a decoder that implements `std::io::Read` and
81//! presents the data from the wrapped `std::io::Read` as UTF-8 is addressed by
82//! the [`encoding_rs_io`](https://docs.rs/encoding_rs_io/) crate.
83//!
84//! # Examples
85//!
86//! Example programs:
87//!
88//! * [Rust](https://github.com/hsivonen/recode_rs)
89//! * [C](https://github.com/hsivonen/recode_c)
90//! * [C++](https://github.com/hsivonen/recode_cpp)
91//!
92//! Decode using the non-streaming API:
93//!
94//! ```
95//! #[cfg(feature = "alloc")] {
96//! use encoding_rs::*;
97//!
98//! let expectation = "\u{30CF}\u{30ED}\u{30FC}\u{30FB}\u{30EF}\u{30FC}\u{30EB}\u{30C9}";
99//! let bytes = b"\x83n\x83\x8D\x81[\x81E\x83\x8F\x81[\x83\x8B\x83h";
100//!
101//! let (cow, encoding_used, had_errors) = SHIFT_JIS.decode(bytes);
102//! assert_eq!(&cow[..], expectation);
103//! assert_eq!(encoding_used, SHIFT_JIS);
104//! assert!(!had_errors);
105//! }
106//! ```
107//!
108//! Decode using the streaming API:
109//!
110//! ```
111//! use encoding_rs::*;
112//!
113//! let expectation = "\u{30CF}\u{30ED}\u{30FC}\u{30FB}\u{30EF}\u{30FC}\u{30EB}\u{30C9}";
114//!
115//! // Use an array of byte slices to demonstrate content arriving piece by
116//! // piece from the network.
117//! let bytes: [&'static [u8]; 4] = [b"\x83",
118//! b"n\x83\x8D\x81",
119//! b"[\x81E\x83\x8F\x81[\x83",
120//! b"\x8B\x83h"];
121//!
122//! // Very short output buffer to demonstrate the output buffer getting full.
123//! // Normally, you'd use something like `[0u8; 2048]`.
124//! let mut buffer_bytes = [0u8; 8];
125//! let mut buffer: &mut str = std::str::from_utf8_mut(&mut buffer_bytes[..]).unwrap();
126//!
127//! // How many bytes in the buffer currently hold significant data.
128//! let mut bytes_in_buffer = 0usize;
129//!
130//! // Collect the output to a string for demonstration purposes.
131//! let mut output = String::new();
132//!
133//! // The `Decoder`
134//! let mut decoder = SHIFT_JIS.new_decoder();
135//!
136//! // Track whether we see errors.
137//! let mut total_had_errors = false;
138//!
139//! // Decode using a fixed-size intermediate buffer (for demonstrating the
140//! // use of a fixed-size buffer; normally when the output of an incremental
141//! // decode goes to a `String` one would use `Decoder.decode_to_string()` to
142//! // avoid the intermediate buffer).
143//! for input in &bytes[..] {
144//! // The number of bytes already read from current `input` in total.
145//! let mut total_read_from_current_input = 0usize;
146//!
147//! loop {
148//! let (result, read, written, had_errors) =
149//! decoder.decode_to_str(&input[total_read_from_current_input..],
150//! &mut buffer[bytes_in_buffer..],
151//! false);
152//! total_read_from_current_input += read;
153//! bytes_in_buffer += written;
154//! total_had_errors |= had_errors;
155//! match result {
156//! CoderResult::InputEmpty => {
157//! // We have consumed the current input buffer. Break out of
158//! // the inner loop to get the next input buffer from the
159//! // outer loop.
160//! break;
161//! },
162//! CoderResult::OutputFull => {
163//! // Write the current buffer out and consider the buffer
164//! // empty.
165//! output.push_str(&buffer[..bytes_in_buffer]);
166//! bytes_in_buffer = 0usize;
167//! continue;
168//! }
169//! }
170//! }
171//! }
172//!
173//! // Process EOF
174//! loop {
175//! let (result, _, written, had_errors) =
176//! decoder.decode_to_str(b"",
177//! &mut buffer[bytes_in_buffer..],
178//! true);
179//! bytes_in_buffer += written;
180//! total_had_errors |= had_errors;
181//! // Write the current buffer out and consider the buffer empty.
182//! // Need to do this here for both `match` arms, because we exit the
183//! // loop on `CoderResult::InputEmpty`.
184//! output.push_str(&buffer[..bytes_in_buffer]);
185//! bytes_in_buffer = 0usize;
186//! match result {
187//! CoderResult::InputEmpty => {
188//! // Done!
189//! break;
190//! },
191//! CoderResult::OutputFull => {
192//! continue;
193//! }
194//! }
195//! }
196//!
197//! assert_eq!(&output[..], expectation);
198//! assert!(!total_had_errors);
199//! ```
200//!
201//! ## UTF-16LE, UTF-16BE and Unicode Encoding Schemes
202//!
203//! The Encoding Standard doesn't specify encoders for UTF-16LE and UTF-16BE,
204//! __so this crate does not provide encoders for those encodings__!
205//! Along with the replacement encoding, their _output encoding_ (i.e. the
206//! encoding used for form submission and error handling in the query string
207//! of URLs) is UTF-8, so you get an UTF-8 encoder if you request an encoder
208//! for them.
209//!
210//! Additionally, the Encoding Standard factors BOM handling into wrapper
211//! algorithms so that BOM handling isn't part of the definition of the
212//! encodings themselves. The Unicode _encoding schemes_ in the Unicode
213//! Standard define BOM handling or lack thereof as part of the encoding
214//! scheme.
215//!
216//! When used with the `_without_bom_handling` entry points, the UTF-16LE
217//! and UTF-16BE _encodings_ match the same-named _encoding schemes_ from
218//! the Unicode Standard.
219//!
220//! When used with the `_with_bom_removal` entry points, the UTF-8
221//! _encoding_ matches the UTF-8 _encoding scheme_ from the Unicode
222//! Standard.
223//!
224//! This crate does not provide a mode that matches the UTF-16 _encoding
225//! scheme_ from the Unicode Stardard. The UTF-16BE encoding used with
226//! the entry points without `_bom_` qualifiers is the closest match,
227//! but in that case, the UTF-8 BOM triggers UTF-8 decoding, which is
228//! not part of the behavior of the UTF-16 _encoding scheme_ per the
229//! Unicode Standard.
230//!
231//! The UTF-32 family of Unicode encoding schemes is not supported
232//! by this crate. The Encoding Standard doesn't define any UTF-32
233//! family encodings, since they aren't necessary for consuming Web
234//! content.
235//!
236//! While gb18030 is capable of representing U+FEFF, the Encoding
237//! Standard does not treat the gb18030 byte representation of U+FEFF
238//! as a BOM, so neither does this crate.
239//!
240//! ## ISO-8859-1
241//!
242//! ISO-8859-1 does not exist as a distinct encoding from windows-1252 in
243//! the Encoding Standard. Therefore, an encoding that maps the unsigned
244//! byte value to the same Unicode scalar value is not available via
245//! `Encoding` in this crate.
246//!
247//! However, the functions whose name starts with `convert` and contains
248//! `latin1` in the `mem` module support such conversions, which are known as
249//! [_isomorphic decode_](https://infra.spec.whatwg.org/#isomorphic-decode)
250//! and [_isomorphic encode_](https://infra.spec.whatwg.org/#isomorphic-encode)
251//! in the [Infra Standard](https://infra.spec.whatwg.org/).
252//!
253//! ## Web / Browser Focus
254//!
255//! Both in terms of scope and performance, the focus is on the Web. For scope,
256//! this means that encoding_rs implements the Encoding Standard fully and
257//! doesn't implement encodings that are not specified in the Encoding
258//! Standard. For performance, this means that decoding performance is
259//! important as well as performance for encoding into UTF-8 or encoding the
260//! Basic Latin range (ASCII) into legacy encodings. Non-Basic Latin needs to
261//! be encoded into legacy encodings in only two places in the Web platform: in
262//! the query part of URLs, in which case it's a matter of relatively rare
263//! error handling, and in form submission, in which case the user action and
264//! networking tend to hide the performance of the encoder.
265//!
266//! Deemphasizing performance of encoding non-Basic Latin text into legacy
267//! encodings enables smaller code size thanks to the encoder side using the
268//! decode-optimized data tables without having encode-optimized data tables at
269//! all. Even in decoders, smaller lookup table size is preferred over avoiding
270//! multiplication operations.
271//!
272//! Additionally, performance is a non-goal for the ASCII-incompatible
273//! ISO-2022-JP encoding, which are rarely used on the Web. Instead of
274//! performance, the decoder for ISO-2022-JP optimizes for ease/clarity
275//! of implementation.
276//!
277//! Despite the browser focus, the hope is that non-browser applications
278//! that wish to consume Web content or submit Web forms in a Web-compatible
279//! way will find encoding_rs useful. While encoding_rs does not try to match
280//! Windows behavior, many of the encodings are close enough to legacy
281//! encodings implemented by Windows that applications that need to consume
282//! data in legacy Windows encodins may find encoding_rs useful. The
283//! [codepage](https://crates.io/crates/codepage) crate maps from Windows
284//! code page identifiers onto encoding_rs `Encoding`s and vice versa.
285//!
286//! For decoding email, UTF-7 support is needed (unfortunately) in additition
287//! to the encodings defined in the Encoding Standard. The
288//! [charset](https://crates.io/crates/charset) wraps encoding_rs and adds
289//! UTF-7 decoding for email purposes.
290//!
291//! For single-byte DOS encodings beyond the ones supported by the Encoding
292//! Standard, there is the [`oem_cp`](https://crates.io/crates/oem_cp) crate.
293//!
294//! # Preparing Text for the Encoders
295//!
296//! Normalizing text into Unicode Normalization Form C prior to encoding text
297//! into a legacy encoding minimizes unmappable characters. Text can be
298//! normalized to Unicode Normalization Form C using the
299//! [`icu_normalizer`](https://crates.io/crates/icu_normalizer) crate, which
300//! is part of [ICU4X](https://icu4x.unicode.org/).
301//!
302//! The exception is windows-1258, which after normalizing to Unicode
303//! Normalization Form C requires tone marks to be decomposed in order to
304//! minimize unmappable characters. Vietnamese tone marks can be decomposed
305//! using the [`detone`](https://crates.io/crates/detone) crate.
306//!
307//! # Streaming & Non-Streaming; Rust & C/C++
308//!
309//! The API in Rust has two modes of operation: streaming and non-streaming.
310//! The streaming API is the foundation of the implementation and should be
311//! used when processing data that arrives piecemeal from an i/o stream. The
312//! streaming API has an FFI wrapper (as a [separate crate][1]) that exposes it
313//! to C callers. The non-streaming part of the API is for Rust callers only and
314//! is smart about borrowing instead of copying when possible. When
315//! streamability is not needed, the non-streaming API should be preferrer in
316//! order to avoid copying data when a borrow suffices.
317//!
318//! There is no analogous C API exposed via FFI, mainly because C doesn't have
319//! standard types for growable byte buffers and Unicode strings that know
320//! their length.
321//!
322//! The C API (header file generated at `target/include/encoding_rs.h` when
323//! building encoding_rs) can, in turn, be wrapped for use from C++. Such a
324//! C++ wrapper can re-create the non-streaming API in C++ for C++ callers.
325//! The C binding comes with a [C++17 wrapper][2] that uses standard library +
326//! [GSL][3] types and that recreates the non-streaming API in C++ on top of
327//! the streaming API. A C++ wrapper with XPCOM/MFBT types is available as
328//! [`mozilla::Encoding`][4].
329//!
330//! The `Encoding` type is common to both the streaming and non-streaming
331//! modes. In the streaming mode, decoding operations are performed with a
332//! `Decoder` and encoding operations with an `Encoder` object obtained via
333//! `Encoding`. In the non-streaming mode, decoding and encoding operations are
334//! performed using methods on `Encoding` objects themselves, so the `Decoder`
335//! and `Encoder` objects are not used at all.
336//!
337//! [1]: https://github.com/hsivonen/encoding_c
338//! [2]: https://github.com/hsivonen/encoding_c/blob/main/include/encoding_rs_cpp.h
339//! [3]: https://github.com/Microsoft/GSL/
340//! [4]: https://searchfox.org/mozilla-central/source/intl/Encoding.h
341//!
342//! # Memory management
343//!
344//! The non-streaming mode never performs heap allocations (even the methods
345//! that write into a `Vec<u8>` or a `String` by taking them as arguments do
346//! not reallocate the backing buffer of the `Vec<u8>` or the `String`). That
347//! is, the non-streaming mode uses caller-allocated buffers exclusively.
348//!
349//! The methods of the streaming mode that return a `Vec<u8>` or a `String`
350//! perform heap allocations but only to allocate the backing buffer of the
351//! `Vec<u8>` or the `String`.
352//!
353//! `Encoding` is always statically allocated. `Decoder` and `Encoder` need no
354//! `Drop` cleanup.
355//!
356//! # Buffer reading and writing behavior
357//!
358//! Based on experience gained with the `java.nio.charset` encoding converter
359//! API and with the Gecko uconv encoding converter API, the buffer reading
360//! and writing behaviors of encoding_rs are asymmetric: input buffers are
361//! fully drained but output buffers are not always fully filled.
362//!
363//! When reading from an input buffer, encoding_rs always consumes all input
364//! up to the next error or to the end of the buffer. In particular, when
365//! decoding, even if the input buffer ends in the middle of a byte sequence
366//! for a character, the decoder consumes all input. This has the benefit that
367//! the caller of the API can always fill the next buffer from the start from
368//! whatever source the bytes come from and never has to first copy the last
369//! bytes of the previous buffer to the start of the next buffer. However, when
370//! encoding, the UTF-8 input buffers have to end at a character boundary, which
371//! is a requirement for the Rust `str` type anyway, and UTF-16 input buffer
372//! boundaries falling in the middle of a surrogate pair result in both
373//! suggorates being treated individually as unpaired surrogates.
374//!
375//! Additionally, decoders guarantee that they can be fed even one byte at a
376//! time and encoders guarantee that they can be fed even one code point at a
377//! time. This has the benefit of not placing restrictions on the size of
378//! chunks the content arrives e.g. from network.
379//!
380//! When writing into an output buffer, encoding_rs makes sure that the code
381//! unit sequence for a character is never split across output buffer
382//! boundaries. This may result in wasted space at the end of an output buffer,
383//! but the advantages are that the output side of both decoders and encoders
384//! is greatly simplified compared to designs that attempt to fill output
385//! buffers exactly even when that entails splitting a code unit sequence and
386//! when encoding_rs methods return to the caller, the output produces thus
387//! far is always valid taken as whole. (In the case of encoding to ISO-2022-JP,
388//! the output needs to be considered as a whole, because the latest output
389//! buffer taken alone might not be valid taken alone if the transition away
390//! from the ASCII state occurred in an earlier output buffer. However, since
391//! the ISO-2022-JP decoder doesn't treat streams that don't end in the ASCII
392//! state as being in error despite the encoder generating a transition to the
393//! ASCII state at the end, the claim about the partial output taken as a whole
394//! being valid is true even for ISO-2022-JP.)
395//!
396//! # Error Reporting
397//!
398//! Based on experience gained with the `java.nio.charset` encoding converter
399//! API and with the Gecko uconv encoding converter API, the error reporting
400//! behaviors of encoding_rs are asymmetric: decoder errors include offsets
401//! that leave it up to the caller to extract the erroneous bytes from the
402//! input stream if the caller wishes to do so but encoder errors provide the
403//! code point associated with the error without requiring the caller to
404//! extract it from the input on its own.
405//!
406//! On the encoder side, an error is always triggered by the most recently
407//! pushed Unicode scalar, which makes it simple to pass the `char` to the
408//! caller. Also, it's very typical for the caller to wish to do something with
409//! this data: generate a numeric escape for the character. Additionally, the
410//! ISO-2022-JP encoder reports U+FFFD instead of the actual input character in
411//! certain cases, so requiring the caller to extract the character from the
412//! input buffer would require the caller to handle ISO-2022-JP details.
413//! Furthermore, requiring the caller to extract the character from the input
414//! buffer would require the caller to implement UTF-8 or UTF-16 math, which is
415//! the job of an encoding conversion library.
416//!
417//! On the decoder side, errors are triggered in more complex ways. For
418//! example, when decoding the sequence ESC, '$', _buffer boundary_, 'A' as
419//! ISO-2022-JP, the ESC byte is in error, but this is discovered only after
420//! the buffer boundary when processing 'A'. Thus, the bytes in error might not
421//! be the ones most recently pushed to the decoder and the error might not even
422//! be in the current buffer.
423//!
424//! Some encoding conversion APIs address the problem by not acknowledging
425//! trailing bytes of an input buffer as consumed if it's still possible for
426//! future bytes to cause the trailing bytes to be in error. This way, error
427//! reporting can always refer to the most recently pushed buffer. This has the
428//! problem that the caller of the API has to copy the unconsumed trailing
429//! bytes to the start of the next buffer before being able to fill the rest
430//! of the next buffer. This is annoying, error-prone and inefficient.
431//!
432//! A possible solution would be making the decoder remember recently consumed
433//! bytes in order to be able to include a copy of the erroneous bytes when
434//! reporting an error. This has two problem: First, callers a rarely
435//! interested in the erroneous bytes, so attempts to identify them are most
436//! often just overhead anyway. Second, the rare applications that are
437//! interested typically care about the location of the error in the input
438//! stream.
439//!
440//! To keep the API convenient for common uses and the overhead low while making
441//! it possible to develop applications, such as HTML validators, that care
442//! about which bytes were in error, encoding_rs reports the length of the
443//! erroneous sequence and the number of bytes consumed after the erroneous
444//! sequence. As long as the caller doesn't discard the 6 most recent bytes,
445//! this makes it possible for callers that care about the erroneous bytes to
446//! locate them.
447//!
448//! # No Convenience API for Custom Replacements
449//!
450//! The Web Platform and, therefore, the Encoding Standard supports only one
451//! error recovery mode for decoders and only one error recovery mode for
452//! encoders. The supported error recovery mode for decoders is emitting the
453//! REPLACEMENT CHARACTER on error. The supported error recovery mode for
454//! encoders is emitting an HTML decimal numeric character reference for
455//! unmappable characters.
456//!
457//! Since encoding_rs is Web-focused, these are the only error recovery modes
458//! for which convenient support is provided. Moreover, on the decoder side,
459//! there aren't really good alternatives for emitting the REPLACEMENT CHARACTER
460//! on error (other than treating errors as fatal). In particular, simply
461//! ignoring errors is a
462//! [security problem](http://www.unicode.org/reports/tr36/#Substituting_for_Ill_Formed_Subsequences),
463//! so it would be a bad idea for encoding_rs to provide a mode that encouraged
464//! callers to ignore errors.
465//!
466//! On the encoder side, there are plausible alternatives for HTML decimal
467//! numeric character references. For example, when outputting CSS, CSS-style
468//! escapes would seem to make sense. However, instead of facilitating the
469//! output of CSS, JS, etc. in non-UTF-8 encodings, encoding_rs takes the design
470//! position that you shouldn't generate output in encodings other than UTF-8,
471//! except where backward compatibility with interacting with the legacy Web
472//! requires it. The legacy Web requires it only when parsing the query strings
473//! of URLs and when submitting forms, and those two both use HTML decimal
474//! numeric character references.
475//!
476//! While encoding_rs doesn't make encoder replacements other than HTML decimal
477//! numeric character references easy, it does make them _possible_.
478//! `encode_from_utf8()`, which emits HTML decimal numeric character references
479//! for unmappable characters, is implemented on top of
480//! `encode_from_utf8_without_replacement()`. Applications that really, really
481//! want other replacement schemes for unmappable characters can likewise
482//! implement them on top of `encode_from_utf8_without_replacement()`.
483//!
484//! # No Extensibility by Design
485//!
486//! The set of encodings supported by encoding_rs is not extensible by design.
487//! That is, `Encoding`, `Decoder` and `Encoder` are intentionally `struct`s
488//! rather than `trait`s. encoding_rs takes the design position that all future
489//! text interchange should be done using UTF-8, which can represent all of
490//! Unicode. (It is, in fact, the only encoding supported by the Encoding
491//! Standard and encoding_rs that can represent all of Unicode and that has
492//! encoder support. UTF-16LE and UTF-16BE don't have encoder support, and
493//! gb18030 cannot encode U+E5E5.) The other encodings are supported merely for
494//! legacy compatibility and not due to non-UTF-8 encodings having benefits
495//! other than being able to consume legacy content.
496//!
497//! Considering that UTF-8 can represent all of Unicode and is already supported
498//! by all Web browsers, introducing a new encoding wouldn't add to the
499//! expressiveness but would add to compatibility problems. In that sense,
500//! adding new encodings to the Web Platform doesn't make sense, and, in fact,
501//! post-UTF-8 attempts at encodings, such as BOCU-1, have been rejected from
502//! the Web Platform. On the other hand, the set of legacy encodings that must
503//! be supported for a Web browser to be able to be successful is not going to
504//! expand. Empirically, the set of encodings specified in the Encoding Standard
505//! is already sufficient and the set of legacy encodings won't grow
506//! retroactively.
507//!
508//! Since extensibility doesn't make sense considering the Web focus of
509//! encoding_rs and adding encodings to Web clients would be actively harmful,
510//! it makes sense to make the set of encodings that encoding_rs supports
511//! non-extensible and to take the (admittedly small) benefits arising from
512//! that, such as the size of `Decoder` and `Encoder` objects being known ahead
513//! of time, which enables stack allocation thereof.
514//!
515//! This does have downsides for applications that might want to put encoding_rs
516//! to non-Web uses if those non-Web uses involve legacy encodings that aren't
517//! needed for Web uses. The needs of such applications should not complicate
518//! encoding_rs itself, though. It is up to those applications to provide a
519//! framework that delegates the operations with encodings that encoding_rs
520//! supports to encoding_rs and operations with other encodings to something
521//! else (as opposed to encoding_rs itself providing an extensibility
522//! framework).
523//!
524//! # Panics
525//!
526//! Methods in encoding_rs can panic if the API is used against the requirements
527//! stated in the documentation, if a state that's supposed to be impossible
528//! is reached due to an internal bug or on integer overflow. When used
529//! according to documentation with buffer sizes that stay below integer
530//! overflow, in the absence of internal bugs, encoding_rs does not panic.
531//!
532//! Panics arising from API misuse aren't documented beyond this on individual
533//! methods.
534//!
535//! # At-Risk Parts of the API
536//!
537//! The foreseeable source of partially backward-incompatible API change is the
538//! way the instances of `Encoding` are made available.
539//!
540//! If Rust changes to allow the entries of `[&'static Encoding; N]` to be
541//! initialized with `static`s of type `&'static Encoding`, the non-reference
542//! `FOO_INIT` public `Encoding` instances will be removed from the public API.
543//!
544//! If Rust changes to make the referent of `pub const FOO: &'static Encoding`
545//! unique when the constant is used in different crates, the reference-typed
546//! `static`s for the encoding instances will be changed from `static` to
547//! `const` and the non-reference-typed `_INIT` instances will be removed.
548//!
549//! # Mapping Spec Concepts onto the API
550//!
551//! <table>
552//! <thead>
553//! <tr><th>Spec Concept</th><th>Streaming</th><th>Non-Streaming</th></tr>
554//! </thead>
555//! <tbody>
556//! <tr><td><a href="https://encoding.spec.whatwg.org/#encoding">encoding</a></td><td><code>&'static Encoding</code></td><td><code>&'static Encoding</code></td></tr>
557//! <tr><td><a href="https://encoding.spec.whatwg.org/#utf-8">UTF-8 encoding</a></td><td><code>UTF_8</code></td><td><code>UTF_8</code></td></tr>
558//! <tr><td><a href="https://encoding.spec.whatwg.org/#concept-encoding-get">get an encoding</a></td><td><code>Encoding::for_label(<var>label</var>)</code></td><td><code>Encoding::for_label(<var>label</var>)</code></td></tr>
559//! <tr><td><a href="https://encoding.spec.whatwg.org/#name">name</a></td><td><code><var>encoding</var>.name()</code></td><td><code><var>encoding</var>.name()</code></td></tr>
560//! <tr><td><a href="https://encoding.spec.whatwg.org/#get-an-output-encoding">get an output encoding</a></td><td><code><var>encoding</var>.output_encoding()</code></td><td><code><var>encoding</var>.output_encoding()</code></td></tr>
561//! <tr><td><a href="https://encoding.spec.whatwg.org/#decode">decode</a></td><td><code>let d = <var>encoding</var>.new_decoder();<br>let res = d.decode_to_<var>*</var>(<var>src</var>, <var>dst</var>, false);<br>// …</br>let last_res = d.decode_to_<var>*</var>(<var>src</var>, <var>dst</var>, true);</code></td><td><code><var>encoding</var>.decode(<var>src</var>)</code></td></tr>
562//! <tr><td><a href="https://encoding.spec.whatwg.org/#utf-8-decode">UTF-8 decode</a></td><td><code>let d = UTF_8.new_decoder_with_bom_removal();<br>let res = d.decode_to_<var>*</var>(<var>src</var>, <var>dst</var>, false);<br>// …</br>let last_res = d.decode_to_<var>*</var>(<var>src</var>, <var>dst</var>, true);</code></td><td><code>UTF_8.decode_with_bom_removal(<var>src</var>)</code></td></tr>
563//! <tr><td><a href="https://encoding.spec.whatwg.org/#utf-8-decode-without-bom">UTF-8 decode without BOM</a></td><td><code>let d = UTF_8.new_decoder_without_bom_handling();<br>let res = d.decode_to_<var>*</var>(<var>src</var>, <var>dst</var>, false);<br>// …</br>let last_res = d.decode_to_<var>*</var>(<var>src</var>, <var>dst</var>, true);</code></td><td><code>UTF_8.decode_without_bom_handling(<var>src</var>)</code></td></tr>
564//! <tr><td><a href="https://encoding.spec.whatwg.org/#utf-8-decode-without-bom-or-fail">UTF-8 decode without BOM or fail</a></td><td><code>let d = UTF_8.new_decoder_without_bom_handling();<br>let res = d.decode_to_<var>*</var>_without_replacement(<var>src</var>, <var>dst</var>, false);<br>// … (fail if malformed)</br>let last_res = d.decode_to_<var>*</var>_without_replacement(<var>src</var>, <var>dst</var>, true);<br>// (fail if malformed)</code></td><td><code>UTF_8.decode_without_bom_handling_and_without_replacement(<var>src</var>)</code></td></tr>
565//! <tr><td><a href="https://encoding.spec.whatwg.org/#encode">encode</a></td><td><code>let e = <var>encoding</var>.new_encoder();<br>let res = e.encode_to_<var>*</var>(<var>src</var>, <var>dst</var>, false);<br>// …</br>let last_res = e.encode_to_<var>*</var>(<var>src</var>, <var>dst</var>, true);</code></td><td><code><var>encoding</var>.encode(<var>src</var>)</code></td></tr>
566//! <tr><td><a href="https://encoding.spec.whatwg.org/#utf-8-encode">UTF-8 encode</a></td><td>Use the UTF-8 nature of Rust strings directly:<br><code><var>write</var>(<var>src</var>.as_bytes());<br>// refill src<br><var>write</var>(<var>src</var>.as_bytes());<br>// refill src<br><var>write</var>(<var>src</var>.as_bytes());<br>// …</code></td><td>Use the UTF-8 nature of Rust strings directly:<br><code><var>src</var>.as_bytes()</code></td></tr>
567//! </tbody>
568//! </table>
569//!
570//! # Compatibility with the rust-encoding API
571//!
572//! The crate
573//! [encoding_rs_compat](https://github.com/hsivonen/encoding_rs_compat/)
574//! is a drop-in replacement for rust-encoding 0.2.32 that implements (most of)
575//! the API of rust-encoding 0.2.32 on top of encoding_rs.
576//!
577//! # Mapping rust-encoding concepts to encoding_rs concepts
578//!
579//! The following table provides a mapping from rust-encoding constructs to
580//! encoding_rs ones.
581//!
582//! <table>
583//! <thead>
584//! <tr><th>rust-encoding</th><th>encoding_rs</th></tr>
585//! </thead>
586//! <tbody>
587//! <tr><td><code>encoding::EncodingRef</code></td><td><code>&'static encoding_rs::Encoding</code></td></tr>
588//! <tr><td><code>encoding::all::<var>WINDOWS_31J</var></code> (not based on the WHATWG name for some encodings)</td><td><code>encoding_rs::<var>SHIFT_JIS</var></code> (always the WHATWG name uppercased and hyphens replaced with underscores)</td></tr>
589//! <tr><td><code>encoding::all::ERROR</code></td><td>Not available because not in the Encoding Standard</td></tr>
590//! <tr><td><code>encoding::all::ASCII</code></td><td>Not available because not in the Encoding Standard</td></tr>
591//! <tr><td><code>encoding::all::ISO_8859_1</code></td><td>Not available because not in the Encoding Standard</td></tr>
592//! <tr><td><code>encoding::all::HZ</code></td><td>Not available because not in the Encoding Standard</td></tr>
593//! <tr><td><code>encoding::label::encoding_from_whatwg_label(<var>string</var>)</code></td><td><code>encoding_rs::Encoding::for_label(<var>string</var>)</code></td></tr>
594//! <tr><td><code><var>enc</var>.whatwg_name()</code> (always lower case)</td><td><code><var>enc</var>.name()</code> (potentially mixed case)</td></tr>
595//! <tr><td><code><var>enc</var>.name()</code></td><td>Not available because not in the Encoding Standard</td></tr>
596//! <tr><td><code>encoding::decode(<var>bytes</var>, encoding::DecoderTrap::Replace, <var>enc</var>)</code></td><td><code><var>enc</var>.decode(<var>bytes</var>)</code></td></tr>
597//! <tr><td><code><var>enc</var>.decode(<var>bytes</var>, encoding::DecoderTrap::Replace)</code></td><td><code><var>enc</var>.decode_without_bom_handling(<var>bytes</var>)</code></td></tr>
598//! <tr><td><code><var>enc</var>.encode(<var>string</var>, encoding::EncoderTrap::NcrEscape)</code></td><td><code><var>enc</var>.encode(<var>string</var>)</code></td></tr>
599//! <tr><td><code><var>enc</var>.raw_decoder()</code></td><td><code><var>enc</var>.new_decoder_without_bom_handling()</code></td></tr>
600//! <tr><td><code><var>enc</var>.raw_encoder()</code></td><td><code><var>enc</var>.new_encoder()</code></td></tr>
601//! <tr><td><code>encoding::RawDecoder</code></td><td><code>encoding_rs::Decoder</code></td></tr>
602//! <tr><td><code>encoding::RawEncoder</code></td><td><code>encoding_rs::Encoder</code></td></tr>
603//! <tr><td><code><var>raw_decoder</var>.raw_feed(<var>src</var>, <var>dst_string</var>)</code></td><td><code><var>dst_string</var>.reserve(<var>decoder</var>.max_utf8_buffer_length_without_replacement(<var>src</var>.len()));<br><var>decoder</var>.decode_to_string_without_replacement(<var>src</var>, <var>dst_string</var>, false)</code></td></tr>
604//! <tr><td><code><var>raw_encoder</var>.raw_feed(<var>src</var>, <var>dst_vec</var>)</code></td><td><code><var>dst_vec</var>.reserve(<var>encoder</var>.max_buffer_length_from_utf8_without_replacement(<var>src</var>.len()));<br><var>encoder</var>.encode_from_utf8_to_vec_without_replacement(<var>src</var>, <var>dst_vec</var>, false)</code></td></tr>
605//! <tr><td><code><var>raw_decoder</var>.raw_finish(<var>dst</var>)</code></td><td><code><var>dst_string</var>.reserve(<var>decoder</var>.max_utf8_buffer_length_without_replacement(0));<br><var>decoder</var>.decode_to_string_without_replacement(b"", <var>dst</var>, true)</code></td></tr>
606//! <tr><td><code><var>raw_encoder</var>.raw_finish(<var>dst</var>)</code></td><td><code><var>dst_vec</var>.reserve(<var>encoder</var>.max_buffer_length_from_utf8_without_replacement(0));<br><var>encoder</var>.encode_from_utf8_to_vec_without_replacement("", <var>dst</var>, true)</code></td></tr>
607//! <tr><td><code>encoding::DecoderTrap::Strict</code></td><td><code>decode*</code> methods that have <code>_without_replacement</code> in their name (and treating the `Malformed` result as fatal).</td></tr>
608//! <tr><td><code>encoding::DecoderTrap::Replace</code></td><td><code>decode*</code> methods that <i>do not</i> have <code>_without_replacement</code> in their name.</td></tr>
609//! <tr><td><code>encoding::DecoderTrap::Ignore</code></td><td>It is a bad idea to ignore errors due to security issues, but this could be implemented using <code>decode*</code> methods that have <code>_without_replacement</code> in their name.</td></tr>
610//! <tr><td><code>encoding::DecoderTrap::Call(DecoderTrapFunc)</code></td><td>Can be implemented using <code>decode*</code> methods that have <code>_without_replacement</code> in their name.</td></tr>
611//! <tr><td><code>encoding::EncoderTrap::Strict</code></td><td><code>encode*</code> methods that have <code>_without_replacement</code> in their name (and treating the `Unmappable` result as fatal).</td></tr>
612//! <tr><td><code>encoding::EncoderTrap::Replace</code></td><td>Can be implemented using <code>encode*</code> methods that have <code>_without_replacement</code> in their name.</td></tr>
613//! <tr><td><code>encoding::EncoderTrap::Ignore</code></td><td>It is a bad idea to ignore errors due to security issues, but this could be implemented using <code>encode*</code> methods that have <code>_without_replacement</code> in their name.</td></tr>
614//! <tr><td><code>encoding::EncoderTrap::NcrEscape</code></td><td><code>encode*</code> methods that <i>do not</i> have <code>_without_replacement</code> in their name.</td></tr>
615//! <tr><td><code>encoding::EncoderTrap::Call(EncoderTrapFunc)</code></td><td>Can be implemented using <code>encode*</code> methods that have <code>_without_replacement</code> in their name.</td></tr>
616//! </tbody>
617//! </table>
618//!
619//! # Relationship with Windows Code Pages
620//!
621//! Despite the Web and browser focus, the encodings defined by the Encoding
622//! Standard and implemented by this crate may be useful for decoding legacy
623//! data that uses Windows code pages. The following table names the single-byte
624//! encodings
625//! that have a closely related Windows code page, the number of the closest
626//! code page, a column indicating whether Windows maps unassigned code points
627//! to the Unicode Private Use Area instead of U+FFFD and a remark number
628//! indicating remarks in the list after the table.
629//!
630//! <table>
631//! <thead>
632//! <tr><th>Encoding</th><th>Code Page</th><th>PUA</th><th>Remarks</th></tr>
633//! </thead>
634//! <tbody>
635//! <tr><td>Shift_JIS</td><td>932</td><td></td><td></td></tr>
636//! <tr><td>GBK</td><td>936</td><td></td><td></td></tr>
637//! <tr><td>EUC-KR</td><td>949</td><td></td><td></td></tr>
638//! <tr><td>Big5</td><td>950</td><td></td><td></td></tr>
639//! <tr><td>IBM866</td><td>866</td><td></td><td></td></tr>
640//! <tr><td>windows-874</td><td>874</td><td>•</td><td></td></tr>
641//! <tr><td>UTF-16LE</td><td>1200</td><td></td><td></td></tr>
642//! <tr><td>UTF-16BE</td><td>1201</td><td></td><td></td></tr>
643//! <tr><td>windows-1250</td><td>1250</td><td></td><td></td></tr>
644//! <tr><td>windows-1251</td><td>1251</td><td></td><td></td></tr>
645//! <tr><td>windows-1252</td><td>1252</td><td></td><td></td></tr>
646//! <tr><td>windows-1253</td><td>1253</td><td>•</td><td></td></tr>
647//! <tr><td>windows-1254</td><td>1254</td><td></td><td></td></tr>
648//! <tr><td>windows-1255</td><td>1255</td><td>•</td><td></td></tr>
649//! <tr><td>windows-1256</td><td>1256</td><td></td><td></td></tr>
650//! <tr><td>windows-1257</td><td>1257</td><td>•</td><td></td></tr>
651//! <tr><td>windows-1258</td><td>1258</td><td></td><td></td></tr>
652//! <tr><td>macintosh</td><td>10000</td><td></td><td>1</td></tr>
653//! <tr><td>x-mac-cyrillic</td><td>10017</td><td></td><td>2</td></tr>
654//! <tr><td>KOI8-R</td><td>20866</td><td></td><td></td></tr>
655//! <tr><td>EUC-JP</td><td>20932</td><td></td><td></td></tr>
656//! <tr><td>KOI8-U</td><td>21866</td><td></td><td></td></tr>
657//! <tr><td>ISO-8859-2</td><td>28592</td><td></td><td></td></tr>
658//! <tr><td>ISO-8859-3</td><td>28593</td><td></td><td></td></tr>
659//! <tr><td>ISO-8859-4</td><td>28594</td><td></td><td></td></tr>
660//! <tr><td>ISO-8859-5</td><td>28595</td><td></td><td></td></tr>
661//! <tr><td>ISO-8859-6</td><td>28596</td><td>•</td><td></td></tr>
662//! <tr><td>ISO-8859-7</td><td>28597</td><td>•</td><td>3</td></tr>
663//! <tr><td>ISO-8859-8</td><td>28598</td><td>•</td><td>4</td></tr>
664//! <tr><td>ISO-8859-13</td><td>28603</td><td>•</td><td></td></tr>
665//! <tr><td>ISO-8859-15</td><td>28605</td><td></td><td></td></tr>
666//! <tr><td>ISO-8859-8-I</td><td>38598</td><td></td><td>5</td></tr>
667//! <tr><td>ISO-2022-JP</td><td>50220</td><td></td><td></td></tr>
668//! <tr><td>gb18030</td><td>54936</td><td></td><td></td></tr>
669//! <tr><td>UTF-8</td><td>65001</td><td></td><td></td></tr>
670//! </tbody>
671//! </table>
672//!
673//! 1. Windows decodes 0xBD to U+2126 OHM SIGN instead of U+03A9 GREEK CAPITAL LETTER OMEGA.
674//! 2. Windows decodes 0xFF to U+00A4 CURRENCY SIGN instead of U+20AC EURO SIGN.
675//! 3. Windows decodes the currency signs at 0xA4 and 0xA5 as well as 0xAA,
676//! which should be U+037A GREEK YPOGEGRAMMENI, to PUA code points. Windows
677//! decodes 0xA1 to U+02BD MODIFIER LETTER REVERSED COMMA instead of U+2018
678//! LEFT SINGLE QUOTATION MARK and 0xA2 to U+02BC MODIFIER LETTER APOSTROPHE
679//! instead of U+2019 RIGHT SINGLE QUOTATION MARK.
680//! 4. Windows decodes 0xAF to OVERLINE instead of MACRON and 0xFE and 0xFD to PUA instead
681//! of LRM and RLM.
682//! 5. Remarks from the previous item apply.
683//!
684//! The differences between this crate and Windows in the case of multibyte encodings
685//! are not yet fully documented here. The lack of remarks above should not be taken
686//! as indication of lack of differences.
687//!
688//! # Notable Differences from IANA Naming
689//!
690//! In some cases, the Encoding Standard specifies the popular unextended encoding
691//! name where in IANA terms one of the other labels would be more precise considering
692//! the extensions that the Encoding Standard has unified into the encoding.
693//!
694//! <table>
695//! <thead>
696//! <tr><th>Encoding</th><th>IANA</th></tr>
697//! </thead>
698//! <tbody>
699//! <tr><td>Big5</td><td>Big5-HKSCS</td></tr>
700//! <tr><td>EUC-KR</td><td>windows-949</td></tr>
701//! <tr><td>Shift_JIS</td><td>windows-31j</td></tr>
702//! <tr><td>x-mac-cyrillic</td><td>x-mac-ukrainian</td></tr>
703//! </tbody>
704//! </table>
705//!
706//! In other cases where the Encoding Standard unifies unextended and extended
707//! variants of an encoding, the encoding gets the name of the extended
708//! variant.
709//!
710//! <table>
711//! <thead>
712//! <tr><th>IANA</th><th>Unified into Encoding</th></tr>
713//! </thead>
714//! <tbody>
715//! <tr><td>ISO-8859-1</td><td>windows-1252</td></tr>
716//! <tr><td>ISO-8859-9</td><td>windows-1254</td></tr>
717//! <tr><td>TIS-620</td><td>windows-874</td></tr>
718//! </tbody>
719//! </table>
720//!
721//! See the section [_UTF-16LE, UTF-16BE and Unicode Encoding Schemes_](#utf-16le-utf-16be-and-unicode-encoding-schemes)
722//! for discussion about the UTF-16 family.
723
724#![cfg_attr(not(feature = "std"), no_std)]
725#![cfg_attr(feature = "simd-accel", allow(internal_features))]
726#![cfg_attr(feature = "simd-accel", feature(core_intrinsics))]
727#![cfg_attr(
728 all(feature = "simd-accel", target_endian = "little"),
729 feature(portable_simd)
730)]
731// These are for working around
732// https://github.com/rust-lang/stdarch/issues/2208
733// https://github.com/rust-lang/rust/issues/159464
734// https://github.com/rust-lang/rust/pull/161558
735#![cfg_attr(
736 all(slow_mm_packus_epi16, feature = "simd-accel", target_feature = "sse2"),
737 feature(link_llvm_intrinsics)
738)]
739#![cfg_attr(
740 all(slow_mm_packus_epi16, feature = "simd-accel", target_feature = "sse2"),
741 feature(abi_unadjusted)
742)]
743#![cfg_attr(
744 all(slow_mm_packus_epi16, feature = "simd-accel", target_feature = "sse2"),
745 feature(simd_ffi)
746)]
747
748#[cfg(feature = "alloc")]
749#[cfg_attr(test, macro_use)]
750extern crate alloc;
751
752extern crate core;
753#[macro_use]
754extern crate cfg_if;
755
756#[cfg(feature = "serde")]
757extern crate serde;
758
759#[cfg(all(test, feature = "serde"))]
760extern crate bincode;
761#[cfg(all(test, feature = "serde"))]
762#[macro_use]
763extern crate serde_derive;
764#[cfg(all(test, feature = "serde"))]
765extern crate serde_json;
766
767// Build time optimization.
768cfg_if! {
769 if #[cfg(all(feature = "simd-accel", feature = "std", any(target_arch = "x86_64", target_arch = "x86"), not(all(target_feature = "avx2", target_feature = "bmi1"))))] {
770 use multiversion::multiversion;
771 } else {
772 use multiversion_no_op::multiversion;
773 }
774}
775
776#[macro_use]
777mod macros;
778
779#[cfg(all(feature = "simd-accel", target_endian = "little",))]
780mod simd_funcs;
781
782#[cfg(all(test, feature = "alloc"))]
783mod testing;
784
785mod big5;
786mod euc_jp;
787mod euc_kr;
788mod gb18030;
789mod gb18030_2022;
790mod iso_2022_jp;
791mod replacement;
792mod shift_jis;
793mod single_byte;
794mod utf_16;
795mod utf_8;
796mod x_user_defined;
797
798mod ascii;
799mod data;
800mod handles;
801mod variant;
802
803pub mod mem;
804
805use crate::ascii::ascii_valid_up_to;
806use crate::ascii::iso_2022_jp_ascii_valid_up_to;
807use crate::utf_8::utf8_valid_up_to;
808use crate::variant::*;
809
810#[cfg(feature = "alloc")]
811use alloc::borrow::Cow;
812#[cfg(feature = "alloc")]
813use alloc::string::String;
814#[cfg(feature = "alloc")]
815use alloc::vec::Vec;
816#[cfg(feature = "alloc")]
817use core::mem::MaybeUninit;
818
819use core::cmp::Ordering;
820use core::hash::Hash;
821use core::hash::Hasher;
822
823#[cfg(feature = "serde")]
824use serde::de::Visitor;
825#[cfg(feature = "serde")]
826use serde::{Deserialize, Deserializer, Serialize, Serializer};
827
828/// This has to be the max length of an NCR instead of max
829/// minus one, because we can't rely on getting the minus
830/// one from the space reserved for the current unmappable,
831/// because the ISO-2022-JP encoder can fill up that space
832/// with a state transition escape.
833const NCR_EXTRA: usize = 10; // 
834
835// BEGIN GENERATED CODE. PLEASE DO NOT EDIT.
836// Instead, please regenerate using generate-encoding-data.py
837
838const LONGEST_LABEL_LENGTH: usize = 19; // cseucpkdfmtjapanese
839
840/// The initializer for the [Big5](static.BIG5.html) encoding.
841///
842/// For use only for taking the address of this form when
843/// Rust prohibits the use of the non-`_INIT` form directly,
844/// such as in initializers of other `static`s. If in doubt,
845/// use the corresponding non-`_INIT` reference-typed `static`.
846///
847/// This part of the public API will go away if Rust changes
848/// to make the referent of `pub const FOO: &'static Encoding`
849/// unique cross-crate or if Rust starts allowing static arrays
850/// to be initialized with `pub static FOO: &'static Encoding`
851/// items.
852pub static BIG5_INIT: Encoding = Encoding {
853 name: "Big5",
854 variant: VariantEncoding::Big5,
855};
856
857/// The Big5 encoding.
858///
859/// This is Big5 with HKSCS with mappings to more recent Unicode assignments
860/// instead of the Private Use Area code points that have been used historically.
861/// It is believed to be able to decode existing Web content in a way that makes
862/// sense.
863///
864/// To avoid form submissions generating data that Web servers don't understand,
865/// the encoder doesn't use the HKSCS byte sequences that precede the unextended
866/// Big5 in the lexical order.
867///
868/// [Index visualization](https://encoding.spec.whatwg.org/big5.html),
869/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/big5-bmp.html)
870///
871/// This encoding is designed to be suited for decoding the Windows code page 950
872/// and its HKSCS patched "951" variant such that the text makes sense, given
873/// assignments that Unicode has made after those encodings used Private Use
874/// Area characters.
875///
876/// This will change from `static` to `const` if Rust changes
877/// to make the referent of `pub const FOO: &'static Encoding`
878/// unique cross-crate, so don't take the address of this
879/// `static`.
880pub static BIG5: &'static Encoding = &BIG5_INIT;
881
882/// The initializer for the [EUC-JP](static.EUC_JP.html) encoding.
883///
884/// For use only for taking the address of this form when
885/// Rust prohibits the use of the non-`_INIT` form directly,
886/// such as in initializers of other `static`s. If in doubt,
887/// use the corresponding non-`_INIT` reference-typed `static`.
888///
889/// This part of the public API will go away if Rust changes
890/// to make the referent of `pub const FOO: &'static Encoding`
891/// unique cross-crate or if Rust starts allowing static arrays
892/// to be initialized with `pub static FOO: &'static Encoding`
893/// items.
894pub static EUC_JP_INIT: Encoding = Encoding {
895 name: "EUC-JP",
896 variant: VariantEncoding::EucJp,
897};
898
899/// The EUC-JP encoding.
900///
901/// This is the legacy Unix encoding for Japanese.
902///
903/// For compatibility with Web servers that don't expect three-byte sequences
904/// in form submissions, the encoder doesn't generate three-byte sequences.
905/// That is, the JIS X 0212 support is decode-only.
906///
907/// [Index visualization](https://encoding.spec.whatwg.org/jis0208.html),
908/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/jis0208-bmp.html)
909/// [Index visualization for decode-only JIS X 0212](https://encoding.spec.whatwg.org/jis0212.html),
910/// [Visualization of BMP coverage for decode-only JIS X 0212](https://encoding.spec.whatwg.org/jis0212-bmp.html)
911///
912/// This encoding roughly matches the Windows code page 20932. There are error
913/// handling differences and a handful of 2-byte sequences that decode differently.
914/// Additionall, Windows doesn't support 3-byte sequences.
915///
916/// This will change from `static` to `const` if Rust changes
917/// to make the referent of `pub const FOO: &'static Encoding`
918/// unique cross-crate, so don't take the address of this
919/// `static`.
920pub static EUC_JP: &'static Encoding = &EUC_JP_INIT;
921
922/// The initializer for the [EUC-KR](static.EUC_KR.html) encoding.
923///
924/// For use only for taking the address of this form when
925/// Rust prohibits the use of the non-`_INIT` form directly,
926/// such as in initializers of other `static`s. If in doubt,
927/// use the corresponding non-`_INIT` reference-typed `static`.
928///
929/// This part of the public API will go away if Rust changes
930/// to make the referent of `pub const FOO: &'static Encoding`
931/// unique cross-crate or if Rust starts allowing static arrays
932/// to be initialized with `pub static FOO: &'static Encoding`
933/// items.
934pub static EUC_KR_INIT: Encoding = Encoding {
935 name: "EUC-KR",
936 variant: VariantEncoding::EucKr,
937};
938
939/// The EUC-KR encoding.
940///
941/// This is the Korean encoding for Windows. It extends the Unix legacy encoding
942/// for Korean, based on KS X 1001 (which also formed the base of MacKorean on Mac OS
943/// Classic), with all the characters from the Hangul Syllables block of Unicode.
944///
945/// [Index visualization](https://encoding.spec.whatwg.org/euc-kr.html),
946/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/euc-kr-bmp.html)
947///
948/// This encoding matches the Windows code page 949, except Windows decodes byte 0x80
949/// to U+0080 and some byte sequences that are error per the Encoding Standard to
950/// the question mark or the Private Use Area.
951///
952/// This will change from `static` to `const` if Rust changes
953/// to make the referent of `pub const FOO: &'static Encoding`
954/// unique cross-crate, so don't take the address of this
955/// `static`.
956pub static EUC_KR: &'static Encoding = &EUC_KR_INIT;
957
958/// The initializer for the [GBK](static.GBK.html) encoding.
959///
960/// For use only for taking the address of this form when
961/// Rust prohibits the use of the non-`_INIT` form directly,
962/// such as in initializers of other `static`s. If in doubt,
963/// use the corresponding non-`_INIT` reference-typed `static`.
964///
965/// This part of the public API will go away if Rust changes
966/// to make the referent of `pub const FOO: &'static Encoding`
967/// unique cross-crate or if Rust starts allowing static arrays
968/// to be initialized with `pub static FOO: &'static Encoding`
969/// items.
970pub static GBK_INIT: Encoding = Encoding {
971 name: "GBK",
972 variant: VariantEncoding::Gbk,
973};
974
975/// The GBK encoding.
976///
977/// The decoder for this encoding is the same as the decoder for gb18030.
978/// The encoder side of this encoding is GBK with Windows code page 936 euro
979/// sign behavior and with the changes to two-byte sequences made in GB18030-2022.
980/// GBK extends GB2312-80 to cover the CJK Unified Ideographs Unicode block as
981/// well as a handful of ideographs from the CJK Unified Ideographs Extension A
982/// and CJK Compatibility Ideographs blocks.
983///
984/// Unlike e.g. in the case of ISO-8859-1 and windows-1252, GBK encoder wasn't
985/// unified with the gb18030 encoder in the Encoding Standard out of concern
986/// that servers that expect GBK form submissions might not be able to handle
987/// the four-byte sequences.
988///
989/// [Index visualization for the two-byte sequences](https://encoding.spec.whatwg.org/gb18030.html),
990/// [Visualization of BMP coverage of the two-byte index](https://encoding.spec.whatwg.org/gb18030-bmp.html)
991///
992/// The encoder of this encoding roughly matches the Windows code page 936.
993/// The decoder side is a superset.
994///
995/// This will change from `static` to `const` if Rust changes
996/// to make the referent of `pub const FOO: &'static Encoding`
997/// unique cross-crate, so don't take the address of this
998/// `static`.
999pub static GBK: &'static Encoding = &GBK_INIT;
1000
1001/// The initializer for the [IBM866](static.IBM866.html) encoding.
1002///
1003/// For use only for taking the address of this form when
1004/// Rust prohibits the use of the non-`_INIT` form directly,
1005/// such as in initializers of other `static`s. If in doubt,
1006/// use the corresponding non-`_INIT` reference-typed `static`.
1007///
1008/// This part of the public API will go away if Rust changes
1009/// to make the referent of `pub const FOO: &'static Encoding`
1010/// unique cross-crate or if Rust starts allowing static arrays
1011/// to be initialized with `pub static FOO: &'static Encoding`
1012/// items.
1013pub static IBM866_INIT: Encoding = Encoding {
1014 name: "IBM866",
1015 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.ibm866, 0x0440, 96, 16),
1016};
1017
1018/// The IBM866 encoding.
1019///
1020/// This the most notable one of the DOS Cyrillic code pages. It has the same
1021/// box drawing characters as code page 437, so it can be used for decoding
1022/// DOS-era ASCII + box drawing data.
1023///
1024/// [Index visualization](https://encoding.spec.whatwg.org/ibm866.html),
1025/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/ibm866-bmp.html)
1026///
1027/// This encoding matches the Windows code page 866.
1028///
1029/// This will change from `static` to `const` if Rust changes
1030/// to make the referent of `pub const FOO: &'static Encoding`
1031/// unique cross-crate, so don't take the address of this
1032/// `static`.
1033pub static IBM866: &'static Encoding = &IBM866_INIT;
1034
1035/// The initializer for the [ISO-2022-JP](static.ISO_2022_JP.html) encoding.
1036///
1037/// For use only for taking the address of this form when
1038/// Rust prohibits the use of the non-`_INIT` form directly,
1039/// such as in initializers of other `static`s. If in doubt,
1040/// use the corresponding non-`_INIT` reference-typed `static`.
1041///
1042/// This part of the public API will go away if Rust changes
1043/// to make the referent of `pub const FOO: &'static Encoding`
1044/// unique cross-crate or if Rust starts allowing static arrays
1045/// to be initialized with `pub static FOO: &'static Encoding`
1046/// items.
1047pub static ISO_2022_JP_INIT: Encoding = Encoding {
1048 name: "ISO-2022-JP",
1049 variant: VariantEncoding::Iso2022Jp,
1050};
1051
1052/// The ISO-2022-JP encoding.
1053///
1054/// This the primary pre-UTF-8 encoding for Japanese email. It uses the ASCII
1055/// byte range to encode non-Basic Latin characters. It's the only encoding
1056/// supported by this crate whose encoder is stateful.
1057///
1058/// [Index visualization](https://encoding.spec.whatwg.org/jis0208.html),
1059/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/jis0208-bmp.html)
1060///
1061/// This encoding roughly matches the Windows code page 50220. Notably, Windows
1062/// uses U+30FB in place of the REPLACEMENT CHARACTER and otherwise differs in
1063/// error handling.
1064///
1065/// This will change from `static` to `const` if Rust changes
1066/// to make the referent of `pub const FOO: &'static Encoding`
1067/// unique cross-crate, so don't take the address of this
1068/// `static`.
1069pub static ISO_2022_JP: &'static Encoding = &ISO_2022_JP_INIT;
1070
1071/// The initializer for the [ISO-8859-10](static.ISO_8859_10.html) encoding.
1072///
1073/// For use only for taking the address of this form when
1074/// Rust prohibits the use of the non-`_INIT` form directly,
1075/// such as in initializers of other `static`s. If in doubt,
1076/// use the corresponding non-`_INIT` reference-typed `static`.
1077///
1078/// This part of the public API will go away if Rust changes
1079/// to make the referent of `pub const FOO: &'static Encoding`
1080/// unique cross-crate or if Rust starts allowing static arrays
1081/// to be initialized with `pub static FOO: &'static Encoding`
1082/// items.
1083pub static ISO_8859_10_INIT: Encoding = Encoding {
1084 name: "ISO-8859-10",
1085 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_10, 0x00DA, 90, 6),
1086};
1087
1088/// The ISO-8859-10 encoding.
1089///
1090/// This is the Nordic part of the ISO/IEC 8859 encoding family. This encoding
1091/// is also known as Latin 6.
1092///
1093/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-10.html),
1094/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-10-bmp.html)
1095///
1096/// The Windows code page number for this encoding is 28600, but kernel32.dll
1097/// does not support this encoding.
1098///
1099/// This will change from `static` to `const` if Rust changes
1100/// to make the referent of `pub const FOO: &'static Encoding`
1101/// unique cross-crate, so don't take the address of this
1102/// `static`.
1103pub static ISO_8859_10: &'static Encoding = &ISO_8859_10_INIT;
1104
1105/// The initializer for the [ISO-8859-13](static.ISO_8859_13.html) encoding.
1106///
1107/// For use only for taking the address of this form when
1108/// Rust prohibits the use of the non-`_INIT` form directly,
1109/// such as in initializers of other `static`s. If in doubt,
1110/// use the corresponding non-`_INIT` reference-typed `static`.
1111///
1112/// This part of the public API will go away if Rust changes
1113/// to make the referent of `pub const FOO: &'static Encoding`
1114/// unique cross-crate or if Rust starts allowing static arrays
1115/// to be initialized with `pub static FOO: &'static Encoding`
1116/// items.
1117pub static ISO_8859_13_INIT: Encoding = Encoding {
1118 name: "ISO-8859-13",
1119 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_13, 0x00DF, 95, 1),
1120};
1121
1122/// The ISO-8859-13 encoding.
1123///
1124/// This is the Baltic part of the ISO/IEC 8859 encoding family. This encoding
1125/// is also known as Latin 7.
1126///
1127/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-13.html),
1128/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-13-bmp.html)
1129///
1130/// This encoding matches the Windows code page 28603, except Windows decodes
1131/// unassigned code points to the Private Use Area of Unicode.
1132///
1133/// This will change from `static` to `const` if Rust changes
1134/// to make the referent of `pub const FOO: &'static Encoding`
1135/// unique cross-crate, so don't take the address of this
1136/// `static`.
1137pub static ISO_8859_13: &'static Encoding = &ISO_8859_13_INIT;
1138
1139/// The initializer for the [ISO-8859-14](static.ISO_8859_14.html) encoding.
1140///
1141/// For use only for taking the address of this form when
1142/// Rust prohibits the use of the non-`_INIT` form directly,
1143/// such as in initializers of other `static`s. If in doubt,
1144/// use the corresponding non-`_INIT` reference-typed `static`.
1145///
1146/// This part of the public API will go away if Rust changes
1147/// to make the referent of `pub const FOO: &'static Encoding`
1148/// unique cross-crate or if Rust starts allowing static arrays
1149/// to be initialized with `pub static FOO: &'static Encoding`
1150/// items.
1151pub static ISO_8859_14_INIT: Encoding = Encoding {
1152 name: "ISO-8859-14",
1153 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_14, 0x00DF, 95, 17),
1154};
1155
1156/// The ISO-8859-14 encoding.
1157///
1158/// This is the Celtic part of the ISO/IEC 8859 encoding family. This encoding
1159/// is also known as Latin 8.
1160///
1161/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-14.html),
1162/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-14-bmp.html)
1163///
1164/// The Windows code page number for this encoding is 28604, but kernel32.dll
1165/// does not support this encoding.
1166///
1167/// This will change from `static` to `const` if Rust changes
1168/// to make the referent of `pub const FOO: &'static Encoding`
1169/// unique cross-crate, so don't take the address of this
1170/// `static`.
1171pub static ISO_8859_14: &'static Encoding = &ISO_8859_14_INIT;
1172
1173/// The initializer for the [ISO-8859-15](static.ISO_8859_15.html) encoding.
1174///
1175/// For use only for taking the address of this form when
1176/// Rust prohibits the use of the non-`_INIT` form directly,
1177/// such as in initializers of other `static`s. If in doubt,
1178/// use the corresponding non-`_INIT` reference-typed `static`.
1179///
1180/// This part of the public API will go away if Rust changes
1181/// to make the referent of `pub const FOO: &'static Encoding`
1182/// unique cross-crate or if Rust starts allowing static arrays
1183/// to be initialized with `pub static FOO: &'static Encoding`
1184/// items.
1185pub static ISO_8859_15_INIT: Encoding = Encoding {
1186 name: "ISO-8859-15",
1187 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_15, 0x00BF, 63, 65),
1188};
1189
1190/// The ISO-8859-15 encoding.
1191///
1192/// This is the revised Western European part of the ISO/IEC 8859 encoding
1193/// family. This encoding is also known as Latin 9.
1194///
1195/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-15.html),
1196/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-15-bmp.html)
1197///
1198/// This encoding matches the Windows code page 28605.
1199///
1200/// This will change from `static` to `const` if Rust changes
1201/// to make the referent of `pub const FOO: &'static Encoding`
1202/// unique cross-crate, so don't take the address of this
1203/// `static`.
1204pub static ISO_8859_15: &'static Encoding = &ISO_8859_15_INIT;
1205
1206/// The initializer for the [ISO-8859-16](static.ISO_8859_16.html) encoding.
1207///
1208/// For use only for taking the address of this form when
1209/// Rust prohibits the use of the non-`_INIT` form directly,
1210/// such as in initializers of other `static`s. If in doubt,
1211/// use the corresponding non-`_INIT` reference-typed `static`.
1212///
1213/// This part of the public API will go away if Rust changes
1214/// to make the referent of `pub const FOO: &'static Encoding`
1215/// unique cross-crate or if Rust starts allowing static arrays
1216/// to be initialized with `pub static FOO: &'static Encoding`
1217/// items.
1218pub static ISO_8859_16_INIT: Encoding = Encoding {
1219 name: "ISO-8859-16",
1220 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_16, 0x00DF, 95, 4),
1221};
1222
1223/// The ISO-8859-16 encoding.
1224///
1225/// This is the South-Eastern European part of the ISO/IEC 8859 encoding
1226/// family. This encoding is also known as Latin 10.
1227///
1228/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-16.html),
1229/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-16-bmp.html)
1230///
1231/// The Windows code page number for this encoding is 28606, but kernel32.dll
1232/// does not support this encoding.
1233///
1234/// This will change from `static` to `const` if Rust changes
1235/// to make the referent of `pub const FOO: &'static Encoding`
1236/// unique cross-crate, so don't take the address of this
1237/// `static`.
1238pub static ISO_8859_16: &'static Encoding = &ISO_8859_16_INIT;
1239
1240/// The initializer for the [ISO-8859-2](static.ISO_8859_2.html) encoding.
1241///
1242/// For use only for taking the address of this form when
1243/// Rust prohibits the use of the non-`_INIT` form directly,
1244/// such as in initializers of other `static`s. If in doubt,
1245/// use the corresponding non-`_INIT` reference-typed `static`.
1246///
1247/// This part of the public API will go away if Rust changes
1248/// to make the referent of `pub const FOO: &'static Encoding`
1249/// unique cross-crate or if Rust starts allowing static arrays
1250/// to be initialized with `pub static FOO: &'static Encoding`
1251/// items.
1252pub static ISO_8859_2_INIT: Encoding = Encoding {
1253 name: "ISO-8859-2",
1254 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_2, 0x00DF, 95, 1),
1255};
1256
1257/// The ISO-8859-2 encoding.
1258///
1259/// This is the Central European part of the ISO/IEC 8859 encoding family. This encoding is also known as Latin 2.
1260///
1261/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-2.html),
1262/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-2-bmp.html)
1263///
1264/// This encoding matches the Windows code page 28592.
1265///
1266/// This will change from `static` to `const` if Rust changes
1267/// to make the referent of `pub const FOO: &'static Encoding`
1268/// unique cross-crate, so don't take the address of this
1269/// `static`.
1270pub static ISO_8859_2: &'static Encoding = &ISO_8859_2_INIT;
1271
1272/// The initializer for the [ISO-8859-3](static.ISO_8859_3.html) encoding.
1273///
1274/// For use only for taking the address of this form when
1275/// Rust prohibits the use of the non-`_INIT` form directly,
1276/// such as in initializers of other `static`s. If in doubt,
1277/// use the corresponding non-`_INIT` reference-typed `static`.
1278///
1279/// This part of the public API will go away if Rust changes
1280/// to make the referent of `pub const FOO: &'static Encoding`
1281/// unique cross-crate or if Rust starts allowing static arrays
1282/// to be initialized with `pub static FOO: &'static Encoding`
1283/// items.
1284pub static ISO_8859_3_INIT: Encoding = Encoding {
1285 name: "ISO-8859-3",
1286 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_3, 0x00DF, 95, 4),
1287};
1288
1289/// The ISO-8859-3 encoding.
1290///
1291/// This is the South European part of the ISO/IEC 8859 encoding family. This encoding is also known as Latin 3.
1292///
1293/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-3.html),
1294/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-3-bmp.html)
1295///
1296/// This encoding matches the Windows code page 28593.
1297///
1298/// This will change from `static` to `const` if Rust changes
1299/// to make the referent of `pub const FOO: &'static Encoding`
1300/// unique cross-crate, so don't take the address of this
1301/// `static`.
1302pub static ISO_8859_3: &'static Encoding = &ISO_8859_3_INIT;
1303
1304/// The initializer for the [ISO-8859-4](static.ISO_8859_4.html) encoding.
1305///
1306/// For use only for taking the address of this form when
1307/// Rust prohibits the use of the non-`_INIT` form directly,
1308/// such as in initializers of other `static`s. If in doubt,
1309/// use the corresponding non-`_INIT` reference-typed `static`.
1310///
1311/// This part of the public API will go away if Rust changes
1312/// to make the referent of `pub const FOO: &'static Encoding`
1313/// unique cross-crate or if Rust starts allowing static arrays
1314/// to be initialized with `pub static FOO: &'static Encoding`
1315/// items.
1316pub static ISO_8859_4_INIT: Encoding = Encoding {
1317 name: "ISO-8859-4",
1318 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_4, 0x00DF, 95, 1),
1319};
1320
1321/// The ISO-8859-4 encoding.
1322///
1323/// This is the North European part of the ISO/IEC 8859 encoding family. This encoding is also known as Latin 4.
1324///
1325/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-4.html),
1326/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-4-bmp.html)
1327///
1328/// This encoding matches the Windows code page 28594.
1329///
1330/// This will change from `static` to `const` if Rust changes
1331/// to make the referent of `pub const FOO: &'static Encoding`
1332/// unique cross-crate, so don't take the address of this
1333/// `static`.
1334pub static ISO_8859_4: &'static Encoding = &ISO_8859_4_INIT;
1335
1336/// The initializer for the [ISO-8859-5](static.ISO_8859_5.html) encoding.
1337///
1338/// For use only for taking the address of this form when
1339/// Rust prohibits the use of the non-`_INIT` form directly,
1340/// such as in initializers of other `static`s. If in doubt,
1341/// use the corresponding non-`_INIT` reference-typed `static`.
1342///
1343/// This part of the public API will go away if Rust changes
1344/// to make the referent of `pub const FOO: &'static Encoding`
1345/// unique cross-crate or if Rust starts allowing static arrays
1346/// to be initialized with `pub static FOO: &'static Encoding`
1347/// items.
1348pub static ISO_8859_5_INIT: Encoding = Encoding {
1349 name: "ISO-8859-5",
1350 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_5, 0x040E, 46, 66),
1351};
1352
1353/// The ISO-8859-5 encoding.
1354///
1355/// This is the Cyrillic part of the ISO/IEC 8859 encoding family.
1356///
1357/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-5.html),
1358/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-5-bmp.html)
1359///
1360/// This encoding matches the Windows code page 28595.
1361///
1362/// This will change from `static` to `const` if Rust changes
1363/// to make the referent of `pub const FOO: &'static Encoding`
1364/// unique cross-crate, so don't take the address of this
1365/// `static`.
1366pub static ISO_8859_5: &'static Encoding = &ISO_8859_5_INIT;
1367
1368/// The initializer for the [ISO-8859-6](static.ISO_8859_6.html) encoding.
1369///
1370/// For use only for taking the address of this form when
1371/// Rust prohibits the use of the non-`_INIT` form directly,
1372/// such as in initializers of other `static`s. If in doubt,
1373/// use the corresponding non-`_INIT` reference-typed `static`.
1374///
1375/// This part of the public API will go away if Rust changes
1376/// to make the referent of `pub const FOO: &'static Encoding`
1377/// unique cross-crate or if Rust starts allowing static arrays
1378/// to be initialized with `pub static FOO: &'static Encoding`
1379/// items.
1380pub static ISO_8859_6_INIT: Encoding = Encoding {
1381 name: "ISO-8859-6",
1382 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_6, 0x0621, 65, 26),
1383};
1384
1385/// The ISO-8859-6 encoding.
1386///
1387/// This is the Arabic part of the ISO/IEC 8859 encoding family.
1388///
1389/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-6.html),
1390/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-6-bmp.html)
1391///
1392/// This encoding matches the Windows code page 28596, except Windows decodes
1393/// unassigned code points to the Private Use Area of Unicode.
1394///
1395/// This will change from `static` to `const` if Rust changes
1396/// to make the referent of `pub const FOO: &'static Encoding`
1397/// unique cross-crate, so don't take the address of this
1398/// `static`.
1399pub static ISO_8859_6: &'static Encoding = &ISO_8859_6_INIT;
1400
1401/// The initializer for the [ISO-8859-7](static.ISO_8859_7.html) encoding.
1402///
1403/// For use only for taking the address of this form when
1404/// Rust prohibits the use of the non-`_INIT` form directly,
1405/// such as in initializers of other `static`s. If in doubt,
1406/// use the corresponding non-`_INIT` reference-typed `static`.
1407///
1408/// This part of the public API will go away if Rust changes
1409/// to make the referent of `pub const FOO: &'static Encoding`
1410/// unique cross-crate or if Rust starts allowing static arrays
1411/// to be initialized with `pub static FOO: &'static Encoding`
1412/// items.
1413pub static ISO_8859_7_INIT: Encoding = Encoding {
1414 name: "ISO-8859-7",
1415 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_7, 0x03A3, 83, 44),
1416};
1417
1418/// The ISO-8859-7 encoding.
1419///
1420/// This is the Greek part of the ISO/IEC 8859 encoding family.
1421///
1422/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-7.html),
1423/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-7-bmp.html)
1424///
1425/// This encoding roughly matches the Windows code page 28597. Windows decodes
1426/// unassigned code points, the currency signs at 0xA4 and 0xA5 as well as
1427/// 0xAA, which should be U+037A GREEK YPOGEGRAMMENI, to the Private Use Area
1428/// of Unicode. Windows decodes 0xA1 to U+02BD MODIFIER LETTER REVERSED COMMA
1429/// instead of U+2018 LEFT SINGLE QUOTATION MARK and 0xA2 to U+02BC MODIFIER
1430/// LETTER APOSTROPHE instead of U+2019 RIGHT SINGLE QUOTATION MARK.
1431///
1432/// This will change from `static` to `const` if Rust changes
1433/// to make the referent of `pub const FOO: &'static Encoding`
1434/// unique cross-crate, so don't take the address of this
1435/// `static`.
1436pub static ISO_8859_7: &'static Encoding = &ISO_8859_7_INIT;
1437
1438/// The initializer for the [ISO-8859-8](static.ISO_8859_8.html) encoding.
1439///
1440/// For use only for taking the address of this form when
1441/// Rust prohibits the use of the non-`_INIT` form directly,
1442/// such as in initializers of other `static`s. If in doubt,
1443/// use the corresponding non-`_INIT` reference-typed `static`.
1444///
1445/// This part of the public API will go away if Rust changes
1446/// to make the referent of `pub const FOO: &'static Encoding`
1447/// unique cross-crate or if Rust starts allowing static arrays
1448/// to be initialized with `pub static FOO: &'static Encoding`
1449/// items.
1450pub static ISO_8859_8_INIT: Encoding = Encoding {
1451 name: "ISO-8859-8",
1452 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_8, 0x05D0, 96, 27),
1453};
1454
1455/// The ISO-8859-8 encoding.
1456///
1457/// This is the Hebrew part of the ISO/IEC 8859 encoding family in visual order.
1458///
1459/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-8.html),
1460/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-8-bmp.html)
1461///
1462/// This encoding roughly matches the Windows code page 28598. Windows decodes
1463/// 0xAF to OVERLINE instead of MACRON and 0xFE and 0xFD to the Private Use
1464/// Area instead of LRM and RLM. Windows decodes unassigned code points to
1465/// the private use area.
1466///
1467/// This will change from `static` to `const` if Rust changes
1468/// to make the referent of `pub const FOO: &'static Encoding`
1469/// unique cross-crate, so don't take the address of this
1470/// `static`.
1471pub static ISO_8859_8: &'static Encoding = &ISO_8859_8_INIT;
1472
1473/// The initializer for the [ISO-8859-8-I](static.ISO_8859_8_I.html) encoding.
1474///
1475/// For use only for taking the address of this form when
1476/// Rust prohibits the use of the non-`_INIT` form directly,
1477/// such as in initializers of other `static`s. If in doubt,
1478/// use the corresponding non-`_INIT` reference-typed `static`.
1479///
1480/// This part of the public API will go away if Rust changes
1481/// to make the referent of `pub const FOO: &'static Encoding`
1482/// unique cross-crate or if Rust starts allowing static arrays
1483/// to be initialized with `pub static FOO: &'static Encoding`
1484/// items.
1485pub static ISO_8859_8_I_INIT: Encoding = Encoding {
1486 name: "ISO-8859-8-I",
1487 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.iso_8859_8, 0x05D0, 96, 27),
1488};
1489
1490/// The ISO-8859-8-I encoding.
1491///
1492/// This is the Hebrew part of the ISO/IEC 8859 encoding family in logical order.
1493///
1494/// [Index visualization](https://encoding.spec.whatwg.org/iso-8859-8.html),
1495/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/iso-8859-8-bmp.html)
1496///
1497/// This encoding roughly matches the Windows code page 38598. Windows decodes
1498/// 0xAF to OVERLINE instead of MACRON and 0xFE and 0xFD to the Private Use
1499/// Area instead of LRM and RLM. Windows decodes unassigned code points to
1500/// the private use area.
1501///
1502/// This will change from `static` to `const` if Rust changes
1503/// to make the referent of `pub const FOO: &'static Encoding`
1504/// unique cross-crate, so don't take the address of this
1505/// `static`.
1506pub static ISO_8859_8_I: &'static Encoding = &ISO_8859_8_I_INIT;
1507
1508/// The initializer for the [KOI8-R](static.KOI8_R.html) encoding.
1509///
1510/// For use only for taking the address of this form when
1511/// Rust prohibits the use of the non-`_INIT` form directly,
1512/// such as in initializers of other `static`s. If in doubt,
1513/// use the corresponding non-`_INIT` reference-typed `static`.
1514///
1515/// This part of the public API will go away if Rust changes
1516/// to make the referent of `pub const FOO: &'static Encoding`
1517/// unique cross-crate or if Rust starts allowing static arrays
1518/// to be initialized with `pub static FOO: &'static Encoding`
1519/// items.
1520pub static KOI8_R_INIT: Encoding = Encoding {
1521 name: "KOI8-R",
1522 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.koi8_r, 0x044E, 64, 1),
1523};
1524
1525/// The KOI8-R encoding.
1526///
1527/// This is an encoding for Russian from [RFC 1489](https://tools.ietf.org/html/rfc1489).
1528///
1529/// [Index visualization](https://encoding.spec.whatwg.org/koi8-r.html),
1530/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/koi8-r-bmp.html)
1531///
1532/// This encoding matches the Windows code page 20866.
1533///
1534/// This will change from `static` to `const` if Rust changes
1535/// to make the referent of `pub const FOO: &'static Encoding`
1536/// unique cross-crate, so don't take the address of this
1537/// `static`.
1538pub static KOI8_R: &'static Encoding = &KOI8_R_INIT;
1539
1540/// The initializer for the [KOI8-U](static.KOI8_U.html) encoding.
1541///
1542/// For use only for taking the address of this form when
1543/// Rust prohibits the use of the non-`_INIT` form directly,
1544/// such as in initializers of other `static`s. If in doubt,
1545/// use the corresponding non-`_INIT` reference-typed `static`.
1546///
1547/// This part of the public API will go away if Rust changes
1548/// to make the referent of `pub const FOO: &'static Encoding`
1549/// unique cross-crate or if Rust starts allowing static arrays
1550/// to be initialized with `pub static FOO: &'static Encoding`
1551/// items.
1552pub static KOI8_U_INIT: Encoding = Encoding {
1553 name: "KOI8-U",
1554 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.koi8_u, 0x044E, 64, 1),
1555};
1556
1557/// The KOI8-U encoding.
1558///
1559/// This is an encoding for Ukrainian adapted from KOI8-R.
1560///
1561/// [Index visualization](https://encoding.spec.whatwg.org/koi8-u.html),
1562/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/koi8-u-bmp.html)
1563///
1564/// This encoding matches the Windows code page 21866.
1565///
1566/// This will change from `static` to `const` if Rust changes
1567/// to make the referent of `pub const FOO: &'static Encoding`
1568/// unique cross-crate, so don't take the address of this
1569/// `static`.
1570pub static KOI8_U: &'static Encoding = &KOI8_U_INIT;
1571
1572/// The initializer for the [Shift_JIS](static.SHIFT_JIS.html) encoding.
1573///
1574/// For use only for taking the address of this form when
1575/// Rust prohibits the use of the non-`_INIT` form directly,
1576/// such as in initializers of other `static`s. If in doubt,
1577/// use the corresponding non-`_INIT` reference-typed `static`.
1578///
1579/// This part of the public API will go away if Rust changes
1580/// to make the referent of `pub const FOO: &'static Encoding`
1581/// unique cross-crate or if Rust starts allowing static arrays
1582/// to be initialized with `pub static FOO: &'static Encoding`
1583/// items.
1584pub static SHIFT_JIS_INIT: Encoding = Encoding {
1585 name: "Shift_JIS",
1586 variant: VariantEncoding::ShiftJis,
1587};
1588
1589/// The Shift_JIS encoding.
1590///
1591/// This is the Japanese encoding for Windows.
1592///
1593/// [Index visualization](https://encoding.spec.whatwg.org/shift_jis.html),
1594/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/shift_jis-bmp.html)
1595///
1596/// This encoding matches the Windows code page 932, except Windows decodes some byte
1597/// sequences that are error per the Encoding Standard to the question mark or the
1598/// Private Use Area and generally uses U+30FB in place of the REPLACEMENT CHARACTER.
1599///
1600/// This will change from `static` to `const` if Rust changes
1601/// to make the referent of `pub const FOO: &'static Encoding`
1602/// unique cross-crate, so don't take the address of this
1603/// `static`.
1604pub static SHIFT_JIS: &'static Encoding = &SHIFT_JIS_INIT;
1605
1606/// The initializer for the [UTF-16BE](static.UTF_16BE.html) encoding.
1607///
1608/// For use only for taking the address of this form when
1609/// Rust prohibits the use of the non-`_INIT` form directly,
1610/// such as in initializers of other `static`s. If in doubt,
1611/// use the corresponding non-`_INIT` reference-typed `static`.
1612///
1613/// This part of the public API will go away if Rust changes
1614/// to make the referent of `pub const FOO: &'static Encoding`
1615/// unique cross-crate or if Rust starts allowing static arrays
1616/// to be initialized with `pub static FOO: &'static Encoding`
1617/// items.
1618pub static UTF_16BE_INIT: Encoding = Encoding {
1619 name: "UTF-16BE",
1620 variant: VariantEncoding::Utf16Be,
1621};
1622
1623/// The UTF-16BE encoding.
1624///
1625/// This decode-only encoding uses 16-bit code units due to Unicode originally
1626/// having been designed as a 16-bit reportoire. In the absence of a byte order
1627/// mark the big endian byte order is assumed.
1628///
1629/// There is no corresponding encoder in this crate or in the Encoding
1630/// Standard. The output encoding of this encoding is UTF-8.
1631///
1632/// This encoding matches the Windows code page 1201.
1633///
1634/// This will change from `static` to `const` if Rust changes
1635/// to make the referent of `pub const FOO: &'static Encoding`
1636/// unique cross-crate, so don't take the address of this
1637/// `static`.
1638pub static UTF_16BE: &'static Encoding = &UTF_16BE_INIT;
1639
1640/// The initializer for the [UTF-16LE](static.UTF_16LE.html) encoding.
1641///
1642/// For use only for taking the address of this form when
1643/// Rust prohibits the use of the non-`_INIT` form directly,
1644/// such as in initializers of other `static`s. If in doubt,
1645/// use the corresponding non-`_INIT` reference-typed `static`.
1646///
1647/// This part of the public API will go away if Rust changes
1648/// to make the referent of `pub const FOO: &'static Encoding`
1649/// unique cross-crate or if Rust starts allowing static arrays
1650/// to be initialized with `pub static FOO: &'static Encoding`
1651/// items.
1652pub static UTF_16LE_INIT: Encoding = Encoding {
1653 name: "UTF-16LE",
1654 variant: VariantEncoding::Utf16Le,
1655};
1656
1657/// The UTF-16LE encoding.
1658///
1659/// This decode-only encoding uses 16-bit code units due to Unicode originally
1660/// having been designed as a 16-bit reportoire. In the absence of a byte order
1661/// mark the little endian byte order is assumed.
1662///
1663/// There is no corresponding encoder in this crate or in the Encoding
1664/// Standard. The output encoding of this encoding is UTF-8.
1665///
1666/// This encoding matches the Windows code page 1200.
1667///
1668/// This will change from `static` to `const` if Rust changes
1669/// to make the referent of `pub const FOO: &'static Encoding`
1670/// unique cross-crate, so don't take the address of this
1671/// `static`.
1672pub static UTF_16LE: &'static Encoding = &UTF_16LE_INIT;
1673
1674/// The initializer for the [UTF-8](static.UTF_8.html) encoding.
1675///
1676/// For use only for taking the address of this form when
1677/// Rust prohibits the use of the non-`_INIT` form directly,
1678/// such as in initializers of other `static`s. If in doubt,
1679/// use the corresponding non-`_INIT` reference-typed `static`.
1680///
1681/// This part of the public API will go away if Rust changes
1682/// to make the referent of `pub const FOO: &'static Encoding`
1683/// unique cross-crate or if Rust starts allowing static arrays
1684/// to be initialized with `pub static FOO: &'static Encoding`
1685/// items.
1686pub static UTF_8_INIT: Encoding = Encoding {
1687 name: "UTF-8",
1688 variant: VariantEncoding::Utf8,
1689};
1690
1691/// The UTF-8 encoding.
1692///
1693/// This is the encoding that should be used for all new development as it can
1694/// represent all of Unicode.
1695///
1696/// This encoding matches the Windows code page 65001, except Windows differs
1697/// in the number of errors generated for some erroneous byte sequences.
1698///
1699/// This will change from `static` to `const` if Rust changes
1700/// to make the referent of `pub const FOO: &'static Encoding`
1701/// unique cross-crate, so don't take the address of this
1702/// `static`.
1703pub static UTF_8: &'static Encoding = &UTF_8_INIT;
1704
1705/// The initializer for the [gb18030](static.GB18030.html) encoding.
1706///
1707/// For use only for taking the address of this form when
1708/// Rust prohibits the use of the non-`_INIT` form directly,
1709/// such as in initializers of other `static`s. If in doubt,
1710/// use the corresponding non-`_INIT` reference-typed `static`.
1711///
1712/// This part of the public API will go away if Rust changes
1713/// to make the referent of `pub const FOO: &'static Encoding`
1714/// unique cross-crate or if Rust starts allowing static arrays
1715/// to be initialized with `pub static FOO: &'static Encoding`
1716/// items.
1717pub static GB18030_INIT: Encoding = Encoding {
1718 name: "gb18030",
1719 variant: VariantEncoding::Gb18030,
1720};
1721
1722/// The gb18030 encoding.
1723///
1724/// This encoding matches GB18030-2022 except the two-byte sequence 0xA3 0xA0
1725/// maps to U+3000 for compatibility with existing Web content and the four-byte
1726/// sequences for the non-PUA characters that got two-byte sequences still decode
1727/// to the same non-PUA characters as in GB18030-2005. As a result, this encoding
1728/// can represent all of Unicode except for 19 private-use characters.
1729///
1730/// [Index visualization for the two-byte sequences](https://encoding.spec.whatwg.org/gb18030.html),
1731/// [Visualization of BMP coverage of the two-byte index](https://encoding.spec.whatwg.org/gb18030-bmp.html)
1732///
1733/// This encoding matches the Windows code page 54936.
1734///
1735/// This will change from `static` to `const` if Rust changes
1736/// to make the referent of `pub const FOO: &'static Encoding`
1737/// unique cross-crate, so don't take the address of this
1738/// `static`.
1739pub static GB18030: &'static Encoding = &GB18030_INIT;
1740
1741/// The initializer for the [macintosh](static.MACINTOSH.html) encoding.
1742///
1743/// For use only for taking the address of this form when
1744/// Rust prohibits the use of the non-`_INIT` form directly,
1745/// such as in initializers of other `static`s. If in doubt,
1746/// use the corresponding non-`_INIT` reference-typed `static`.
1747///
1748/// This part of the public API will go away if Rust changes
1749/// to make the referent of `pub const FOO: &'static Encoding`
1750/// unique cross-crate or if Rust starts allowing static arrays
1751/// to be initialized with `pub static FOO: &'static Encoding`
1752/// items.
1753pub static MACINTOSH_INIT: Encoding = Encoding {
1754 name: "macintosh",
1755 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.macintosh, 0x00CD, 106, 3),
1756};
1757
1758/// The macintosh encoding.
1759///
1760/// This is the MacRoman encoding from Mac OS Classic.
1761///
1762/// [Index visualization](https://encoding.spec.whatwg.org/macintosh.html),
1763/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/macintosh-bmp.html)
1764///
1765/// This encoding matches the Windows code page 10000, except Windows decodes
1766/// 0xBD to U+2126 OHM SIGN instead of U+03A9 GREEK CAPITAL LETTER OMEGA.
1767///
1768/// This will change from `static` to `const` if Rust changes
1769/// to make the referent of `pub const FOO: &'static Encoding`
1770/// unique cross-crate, so don't take the address of this
1771/// `static`.
1772pub static MACINTOSH: &'static Encoding = &MACINTOSH_INIT;
1773
1774/// The initializer for the [replacement](static.REPLACEMENT.html) encoding.
1775///
1776/// For use only for taking the address of this form when
1777/// Rust prohibits the use of the non-`_INIT` form directly,
1778/// such as in initializers of other `static`s. If in doubt,
1779/// use the corresponding non-`_INIT` reference-typed `static`.
1780///
1781/// This part of the public API will go away if Rust changes
1782/// to make the referent of `pub const FOO: &'static Encoding`
1783/// unique cross-crate or if Rust starts allowing static arrays
1784/// to be initialized with `pub static FOO: &'static Encoding`
1785/// items.
1786pub static REPLACEMENT_INIT: Encoding = Encoding {
1787 name: "replacement",
1788 variant: VariantEncoding::Replacement,
1789};
1790
1791/// The replacement encoding.
1792///
1793/// This decode-only encoding decodes all non-zero-length streams to a single
1794/// REPLACEMENT CHARACTER. Its purpose is to avoid the use of an
1795/// ASCII-compatible fallback encoding (typically windows-1252) for some
1796/// encodings that are no longer supported by the Web Platform and that
1797/// would be dangerous to treat as ASCII-compatible.
1798///
1799/// There is no corresponding encoder. The output encoding of this encoding
1800/// is UTF-8.
1801///
1802/// This encoding does not have a Windows code page number.
1803///
1804/// This will change from `static` to `const` if Rust changes
1805/// to make the referent of `pub const FOO: &'static Encoding`
1806/// unique cross-crate, so don't take the address of this
1807/// `static`.
1808pub static REPLACEMENT: &'static Encoding = &REPLACEMENT_INIT;
1809
1810/// The initializer for the [windows-1250](static.WINDOWS_1250.html) encoding.
1811///
1812/// For use only for taking the address of this form when
1813/// Rust prohibits the use of the non-`_INIT` form directly,
1814/// such as in initializers of other `static`s. If in doubt,
1815/// use the corresponding non-`_INIT` reference-typed `static`.
1816///
1817/// This part of the public API will go away if Rust changes
1818/// to make the referent of `pub const FOO: &'static Encoding`
1819/// unique cross-crate or if Rust starts allowing static arrays
1820/// to be initialized with `pub static FOO: &'static Encoding`
1821/// items.
1822pub static WINDOWS_1250_INIT: Encoding = Encoding {
1823 name: "windows-1250",
1824 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1250, 0x00DC, 92, 2),
1825};
1826
1827/// The windows-1250 encoding.
1828///
1829/// This is the Central European encoding for Windows.
1830///
1831/// [Index visualization](https://encoding.spec.whatwg.org/windows-1250.html),
1832/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1250-bmp.html)
1833///
1834/// This encoding matches the Windows code page 1250.
1835///
1836/// This will change from `static` to `const` if Rust changes
1837/// to make the referent of `pub const FOO: &'static Encoding`
1838/// unique cross-crate, so don't take the address of this
1839/// `static`.
1840pub static WINDOWS_1250: &'static Encoding = &WINDOWS_1250_INIT;
1841
1842/// The initializer for the [windows-1251](static.WINDOWS_1251.html) encoding.
1843///
1844/// For use only for taking the address of this form when
1845/// Rust prohibits the use of the non-`_INIT` form directly,
1846/// such as in initializers of other `static`s. If in doubt,
1847/// use the corresponding non-`_INIT` reference-typed `static`.
1848///
1849/// This part of the public API will go away if Rust changes
1850/// to make the referent of `pub const FOO: &'static Encoding`
1851/// unique cross-crate or if Rust starts allowing static arrays
1852/// to be initialized with `pub static FOO: &'static Encoding`
1853/// items.
1854pub static WINDOWS_1251_INIT: Encoding = Encoding {
1855 name: "windows-1251",
1856 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1251, 0x0410, 64, 64),
1857};
1858
1859/// The windows-1251 encoding.
1860///
1861/// This is the Cyrillic encoding for Windows.
1862///
1863/// [Index visualization](https://encoding.spec.whatwg.org/windows-1251.html),
1864/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1251-bmp.html)
1865///
1866/// This encoding matches the Windows code page 1251.
1867///
1868/// This will change from `static` to `const` if Rust changes
1869/// to make the referent of `pub const FOO: &'static Encoding`
1870/// unique cross-crate, so don't take the address of this
1871/// `static`.
1872pub static WINDOWS_1251: &'static Encoding = &WINDOWS_1251_INIT;
1873
1874/// The initializer for the [windows-1252](static.WINDOWS_1252.html) encoding.
1875///
1876/// For use only for taking the address of this form when
1877/// Rust prohibits the use of the non-`_INIT` form directly,
1878/// such as in initializers of other `static`s. If in doubt,
1879/// use the corresponding non-`_INIT` reference-typed `static`.
1880///
1881/// This part of the public API will go away if Rust changes
1882/// to make the referent of `pub const FOO: &'static Encoding`
1883/// unique cross-crate or if Rust starts allowing static arrays
1884/// to be initialized with `pub static FOO: &'static Encoding`
1885/// items.
1886pub static WINDOWS_1252_INIT: Encoding = Encoding {
1887 name: "windows-1252",
1888 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1252, 0x00A0, 32, 96),
1889};
1890
1891/// The windows-1252 encoding.
1892///
1893/// This is the Western encoding for Windows. It is an extension of ISO-8859-1,
1894/// which is known as Latin 1.
1895///
1896/// [Index visualization](https://encoding.spec.whatwg.org/windows-1252.html),
1897/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1252-bmp.html)
1898///
1899/// This encoding matches the Windows code page 1252.
1900///
1901/// This will change from `static` to `const` if Rust changes
1902/// to make the referent of `pub const FOO: &'static Encoding`
1903/// unique cross-crate, so don't take the address of this
1904/// `static`.
1905pub static WINDOWS_1252: &'static Encoding = &WINDOWS_1252_INIT;
1906
1907/// The initializer for the [windows-1253](static.WINDOWS_1253.html) encoding.
1908///
1909/// For use only for taking the address of this form when
1910/// Rust prohibits the use of the non-`_INIT` form directly,
1911/// such as in initializers of other `static`s. If in doubt,
1912/// use the corresponding non-`_INIT` reference-typed `static`.
1913///
1914/// This part of the public API will go away if Rust changes
1915/// to make the referent of `pub const FOO: &'static Encoding`
1916/// unique cross-crate or if Rust starts allowing static arrays
1917/// to be initialized with `pub static FOO: &'static Encoding`
1918/// items.
1919pub static WINDOWS_1253_INIT: Encoding = Encoding {
1920 name: "windows-1253",
1921 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1253, 0x03A3, 83, 44),
1922};
1923
1924/// The windows-1253 encoding.
1925///
1926/// This is the Greek encoding for Windows. It is mostly an extension of
1927/// ISO-8859-7, but U+0386 is mapped to a different byte.
1928///
1929/// [Index visualization](https://encoding.spec.whatwg.org/windows-1253.html),
1930/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1253-bmp.html)
1931///
1932/// This encoding matches the Windows code page 1253, except Windows decodes
1933/// unassigned code points to the Private Use Area of Unicode.
1934///
1935/// This will change from `static` to `const` if Rust changes
1936/// to make the referent of `pub const FOO: &'static Encoding`
1937/// unique cross-crate, so don't take the address of this
1938/// `static`.
1939pub static WINDOWS_1253: &'static Encoding = &WINDOWS_1253_INIT;
1940
1941/// The initializer for the [windows-1254](static.WINDOWS_1254.html) encoding.
1942///
1943/// For use only for taking the address of this form when
1944/// Rust prohibits the use of the non-`_INIT` form directly,
1945/// such as in initializers of other `static`s. If in doubt,
1946/// use the corresponding non-`_INIT` reference-typed `static`.
1947///
1948/// This part of the public API will go away if Rust changes
1949/// to make the referent of `pub const FOO: &'static Encoding`
1950/// unique cross-crate or if Rust starts allowing static arrays
1951/// to be initialized with `pub static FOO: &'static Encoding`
1952/// items.
1953pub static WINDOWS_1254_INIT: Encoding = Encoding {
1954 name: "windows-1254",
1955 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1254, 0x00DF, 95, 17),
1956};
1957
1958/// The windows-1254 encoding.
1959///
1960/// This is the Turkish encoding for Windows. It is an extension of ISO-8859-9,
1961/// which is known as Latin 5.
1962///
1963/// [Index visualization](https://encoding.spec.whatwg.org/windows-1254.html),
1964/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1254-bmp.html)
1965///
1966/// This encoding matches the Windows code page 1254.
1967///
1968/// This will change from `static` to `const` if Rust changes
1969/// to make the referent of `pub const FOO: &'static Encoding`
1970/// unique cross-crate, so don't take the address of this
1971/// `static`.
1972pub static WINDOWS_1254: &'static Encoding = &WINDOWS_1254_INIT;
1973
1974/// The initializer for the [windows-1255](static.WINDOWS_1255.html) encoding.
1975///
1976/// For use only for taking the address of this form when
1977/// Rust prohibits the use of the non-`_INIT` form directly,
1978/// such as in initializers of other `static`s. If in doubt,
1979/// use the corresponding non-`_INIT` reference-typed `static`.
1980///
1981/// This part of the public API will go away if Rust changes
1982/// to make the referent of `pub const FOO: &'static Encoding`
1983/// unique cross-crate or if Rust starts allowing static arrays
1984/// to be initialized with `pub static FOO: &'static Encoding`
1985/// items.
1986pub static WINDOWS_1255_INIT: Encoding = Encoding {
1987 name: "windows-1255",
1988 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1255, 0x05D0, 96, 27),
1989};
1990
1991/// The windows-1255 encoding.
1992///
1993/// This is the Hebrew encoding for Windows. It is an extension of ISO-8859-8-I,
1994/// except for a currency sign swap.
1995///
1996/// [Index visualization](https://encoding.spec.whatwg.org/windows-1255.html),
1997/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1255-bmp.html)
1998///
1999/// This encoding matches the Windows code page 1255, except Windows decodes
2000/// unassigned code points to the Private Use Area of Unicode.
2001///
2002/// This will change from `static` to `const` if Rust changes
2003/// to make the referent of `pub const FOO: &'static Encoding`
2004/// unique cross-crate, so don't take the address of this
2005/// `static`.
2006pub static WINDOWS_1255: &'static Encoding = &WINDOWS_1255_INIT;
2007
2008/// The initializer for the [windows-1256](static.WINDOWS_1256.html) encoding.
2009///
2010/// For use only for taking the address of this form when
2011/// Rust prohibits the use of the non-`_INIT` form directly,
2012/// such as in initializers of other `static`s. If in doubt,
2013/// use the corresponding non-`_INIT` reference-typed `static`.
2014///
2015/// This part of the public API will go away if Rust changes
2016/// to make the referent of `pub const FOO: &'static Encoding`
2017/// unique cross-crate or if Rust starts allowing static arrays
2018/// to be initialized with `pub static FOO: &'static Encoding`
2019/// items.
2020pub static WINDOWS_1256_INIT: Encoding = Encoding {
2021 name: "windows-1256",
2022 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1256, 0x0621, 65, 22),
2023};
2024
2025/// The windows-1256 encoding.
2026///
2027/// This is the Arabic encoding for Windows.
2028///
2029/// [Index visualization](https://encoding.spec.whatwg.org/windows-1256.html),
2030/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1256-bmp.html)
2031///
2032/// This encoding matches the Windows code page 1256.
2033///
2034/// This will change from `static` to `const` if Rust changes
2035/// to make the referent of `pub const FOO: &'static Encoding`
2036/// unique cross-crate, so don't take the address of this
2037/// `static`.
2038pub static WINDOWS_1256: &'static Encoding = &WINDOWS_1256_INIT;
2039
2040/// The initializer for the [windows-1257](static.WINDOWS_1257.html) encoding.
2041///
2042/// For use only for taking the address of this form when
2043/// Rust prohibits the use of the non-`_INIT` form directly,
2044/// such as in initializers of other `static`s. If in doubt,
2045/// use the corresponding non-`_INIT` reference-typed `static`.
2046///
2047/// This part of the public API will go away if Rust changes
2048/// to make the referent of `pub const FOO: &'static Encoding`
2049/// unique cross-crate or if Rust starts allowing static arrays
2050/// to be initialized with `pub static FOO: &'static Encoding`
2051/// items.
2052pub static WINDOWS_1257_INIT: Encoding = Encoding {
2053 name: "windows-1257",
2054 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1257, 0x00DF, 95, 1),
2055};
2056
2057/// The windows-1257 encoding.
2058///
2059/// This is the Baltic encoding for Windows.
2060///
2061/// [Index visualization](https://encoding.spec.whatwg.org/windows-1257.html),
2062/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1257-bmp.html)
2063///
2064/// This encoding matches the Windows code page 1257, except Windows decodes
2065/// unassigned code points to the Private Use Area of Unicode.
2066///
2067/// This will change from `static` to `const` if Rust changes
2068/// to make the referent of `pub const FOO: &'static Encoding`
2069/// unique cross-crate, so don't take the address of this
2070/// `static`.
2071pub static WINDOWS_1257: &'static Encoding = &WINDOWS_1257_INIT;
2072
2073/// The initializer for the [windows-1258](static.WINDOWS_1258.html) encoding.
2074///
2075/// For use only for taking the address of this form when
2076/// Rust prohibits the use of the non-`_INIT` form directly,
2077/// such as in initializers of other `static`s. If in doubt,
2078/// use the corresponding non-`_INIT` reference-typed `static`.
2079///
2080/// This part of the public API will go away if Rust changes
2081/// to make the referent of `pub const FOO: &'static Encoding`
2082/// unique cross-crate or if Rust starts allowing static arrays
2083/// to be initialized with `pub static FOO: &'static Encoding`
2084/// items.
2085pub static WINDOWS_1258_INIT: Encoding = Encoding {
2086 name: "windows-1258",
2087 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_1258, 0x00DF, 95, 4),
2088};
2089
2090/// The windows-1258 encoding.
2091///
2092/// This is the Vietnamese encoding for Windows.
2093///
2094/// [Index visualization](https://encoding.spec.whatwg.org/windows-1258.html),
2095/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-1258-bmp.html)
2096///
2097/// This encoding matches the Windows code page 1258 when used in the
2098/// non-normalizing mode. Unlike with the other single-byte encodings, the
2099/// result of decoding is not necessarily in Normalization Form C. On the
2100/// other hand, input in the Normalization Form C is not encoded without
2101/// replacement. In general, it's a bad idea to encode to encodings other
2102/// than UTF-8, but this encoding is especially hazardous to encode to.
2103///
2104/// This will change from `static` to `const` if Rust changes
2105/// to make the referent of `pub const FOO: &'static Encoding`
2106/// unique cross-crate, so don't take the address of this
2107/// `static`.
2108pub static WINDOWS_1258: &'static Encoding = &WINDOWS_1258_INIT;
2109
2110/// The initializer for the [windows-874](static.WINDOWS_874.html) encoding.
2111///
2112/// For use only for taking the address of this form when
2113/// Rust prohibits the use of the non-`_INIT` form directly,
2114/// such as in initializers of other `static`s. If in doubt,
2115/// use the corresponding non-`_INIT` reference-typed `static`.
2116///
2117/// This part of the public API will go away if Rust changes
2118/// to make the referent of `pub const FOO: &'static Encoding`
2119/// unique cross-crate or if Rust starts allowing static arrays
2120/// to be initialized with `pub static FOO: &'static Encoding`
2121/// items.
2122pub static WINDOWS_874_INIT: Encoding = Encoding {
2123 name: "windows-874",
2124 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.windows_874, 0x0E01, 33, 58),
2125};
2126
2127/// The windows-874 encoding.
2128///
2129/// This is the Thai encoding for Windows. It is an extension of TIS-620 / ISO-8859-11.
2130///
2131/// [Index visualization](https://encoding.spec.whatwg.org/windows-874.html),
2132/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/windows-874-bmp.html)
2133///
2134/// This encoding matches the Windows code page 874, except Windows decodes
2135/// unassigned code points to the Private Use Area of Unicode.
2136///
2137/// This will change from `static` to `const` if Rust changes
2138/// to make the referent of `pub const FOO: &'static Encoding`
2139/// unique cross-crate, so don't take the address of this
2140/// `static`.
2141pub static WINDOWS_874: &'static Encoding = &WINDOWS_874_INIT;
2142
2143/// The initializer for the [x-mac-cyrillic](static.X_MAC_CYRILLIC.html) encoding.
2144///
2145/// For use only for taking the address of this form when
2146/// Rust prohibits the use of the non-`_INIT` form directly,
2147/// such as in initializers of other `static`s. If in doubt,
2148/// use the corresponding non-`_INIT` reference-typed `static`.
2149///
2150/// This part of the public API will go away if Rust changes
2151/// to make the referent of `pub const FOO: &'static Encoding`
2152/// unique cross-crate or if Rust starts allowing static arrays
2153/// to be initialized with `pub static FOO: &'static Encoding`
2154/// items.
2155pub static X_MAC_CYRILLIC_INIT: Encoding = Encoding {
2156 name: "x-mac-cyrillic",
2157 variant: VariantEncoding::SingleByte(&data::SINGLE_BYTE_DATA.x_mac_cyrillic, 0x0430, 96, 31),
2158};
2159
2160/// The x-mac-cyrillic encoding.
2161///
2162/// This is the MacUkrainian encoding from Mac OS Classic.
2163///
2164/// [Index visualization](https://encoding.spec.whatwg.org/x-mac-cyrillic.html),
2165/// [Visualization of BMP coverage](https://encoding.spec.whatwg.org/x-mac-cyrillic-bmp.html)
2166///
2167/// This encoding matches the Windows code page 10017.
2168///
2169/// This will change from `static` to `const` if Rust changes
2170/// to make the referent of `pub const FOO: &'static Encoding`
2171/// unique cross-crate, so don't take the address of this
2172/// `static`.
2173pub static X_MAC_CYRILLIC: &'static Encoding = &X_MAC_CYRILLIC_INIT;
2174
2175/// The initializer for the [x-user-defined](static.X_USER_DEFINED.html) encoding.
2176///
2177/// For use only for taking the address of this form when
2178/// Rust prohibits the use of the non-`_INIT` form directly,
2179/// such as in initializers of other `static`s. If in doubt,
2180/// use the corresponding non-`_INIT` reference-typed `static`.
2181///
2182/// This part of the public API will go away if Rust changes
2183/// to make the referent of `pub const FOO: &'static Encoding`
2184/// unique cross-crate or if Rust starts allowing static arrays
2185/// to be initialized with `pub static FOO: &'static Encoding`
2186/// items.
2187pub static X_USER_DEFINED_INIT: Encoding = Encoding {
2188 name: "x-user-defined",
2189 variant: VariantEncoding::UserDefined,
2190};
2191
2192/// The x-user-defined encoding.
2193///
2194/// This encoding offsets the non-ASCII bytes by `0xF700` thereby decoding
2195/// them to the Private Use Area of Unicode. It was used for loading binary
2196/// data into a JavaScript string using `XMLHttpRequest` before XHR supported
2197/// the `"arraybuffer"` response type.
2198///
2199/// This encoding does not have a Windows code page number.
2200///
2201/// This will change from `static` to `const` if Rust changes
2202/// to make the referent of `pub const FOO: &'static Encoding`
2203/// unique cross-crate, so don't take the address of this
2204/// `static`.
2205pub static X_USER_DEFINED: &'static Encoding = &X_USER_DEFINED_INIT;
2206
2207static LABELS_SORTED: [&'static str; 228] = [
2208 "l1",
2209 "l2",
2210 "l3",
2211 "l4",
2212 "l5",
2213 "l6",
2214 "l9",
2215 "866",
2216 "mac",
2217 "koi",
2218 "gbk",
2219 "big5",
2220 "utf8",
2221 "koi8",
2222 "sjis",
2223 "ucs-2",
2224 "ms932",
2225 "cp866",
2226 "utf-8",
2227 "cp819",
2228 "ascii",
2229 "x-gbk",
2230 "greek",
2231 "cp1250",
2232 "cp1251",
2233 "latin1",
2234 "gb2312",
2235 "cp1252",
2236 "latin2",
2237 "cp1253",
2238 "latin3",
2239 "cp1254",
2240 "latin4",
2241 "cp1255",
2242 "csbig5",
2243 "latin5",
2244 "utf-16",
2245 "cp1256",
2246 "ibm866",
2247 "latin6",
2248 "cp1257",
2249 "cp1258",
2250 "greek8",
2251 "ibm819",
2252 "arabic",
2253 "visual",
2254 "korean",
2255 "euc-jp",
2256 "koi8-r",
2257 "koi8_r",
2258 "euc-kr",
2259 "x-sjis",
2260 "koi8-u",
2261 "hebrew",
2262 "tis-620",
2263 "gb18030",
2264 "ksc5601",
2265 "gb_2312",
2266 "dos-874",
2267 "cn-big5",
2268 "unicode",
2269 "chinese",
2270 "logical",
2271 "cskoi8r",
2272 "cseuckr",
2273 "koi8-ru",
2274 "x-cp1250",
2275 "ksc_5601",
2276 "x-cp1251",
2277 "iso88591",
2278 "csgb2312",
2279 "x-cp1252",
2280 "iso88592",
2281 "x-cp1253",
2282 "iso88593",
2283 "ecma-114",
2284 "x-cp1254",
2285 "iso88594",
2286 "x-cp1255",
2287 "iso88595",
2288 "x-x-big5",
2289 "x-cp1256",
2290 "csibm866",
2291 "iso88596",
2292 "x-cp1257",
2293 "iso88597",
2294 "asmo-708",
2295 "ecma-118",
2296 "elot_928",
2297 "x-cp1258",
2298 "iso88598",
2299 "iso88599",
2300 "cyrillic",
2301 "utf-16be",
2302 "utf-16le",
2303 "us-ascii",
2304 "ms_kanji",
2305 "x-euc-jp",
2306 "iso885910",
2307 "iso8859-1",
2308 "iso885911",
2309 "iso8859-2",
2310 "iso8859-3",
2311 "iso885913",
2312 "iso8859-4",
2313 "iso885914",
2314 "iso8859-5",
2315 "iso885915",
2316 "iso8859-6",
2317 "iso8859-7",
2318 "iso8859-8",
2319 "iso-ir-58",
2320 "iso8859-9",
2321 "csunicode",
2322 "macintosh",
2323 "shift-jis",
2324 "shift_jis",
2325 "iso-ir-100",
2326 "iso8859-10",
2327 "iso-ir-110",
2328 "gb_2312-80",
2329 "iso-8859-1",
2330 "iso_8859-1",
2331 "iso-ir-101",
2332 "iso8859-11",
2333 "iso-8859-2",
2334 "iso_8859-2",
2335 "hz-gb-2312",
2336 "iso-8859-3",
2337 "iso_8859-3",
2338 "iso8859-13",
2339 "iso-8859-4",
2340 "iso_8859-4",
2341 "iso8859-14",
2342 "iso-ir-144",
2343 "iso-8859-5",
2344 "iso_8859-5",
2345 "iso8859-15",
2346 "iso-8859-6",
2347 "iso_8859-6",
2348 "iso-ir-126",
2349 "iso-8859-7",
2350 "iso_8859-7",
2351 "iso-ir-127",
2352 "iso-ir-157",
2353 "iso-8859-8",
2354 "iso_8859-8",
2355 "iso-ir-138",
2356 "iso-ir-148",
2357 "iso-8859-9",
2358 "iso_8859-9",
2359 "iso-ir-109",
2360 "iso-ir-149",
2361 "big5-hkscs",
2362 "csshiftjis",
2363 "iso-8859-10",
2364 "iso-8859-11",
2365 "csisolatin1",
2366 "csisolatin2",
2367 "iso-8859-13",
2368 "csisolatin3",
2369 "iso-8859-14",
2370 "windows-874",
2371 "csisolatin4",
2372 "iso-8859-15",
2373 "iso_8859-15",
2374 "csisolatin5",
2375 "iso-8859-16",
2376 "csisolatin6",
2377 "windows-949",
2378 "csisolatin9",
2379 "csiso88596e",
2380 "csiso88598e",
2381 "unicodefffe",
2382 "unicodefeff",
2383 "csmacintosh",
2384 "csiso88596i",
2385 "csiso88598i",
2386 "windows-31j",
2387 "x-mac-roman",
2388 "iso-2022-cn",
2389 "iso-2022-jp",
2390 "csiso2022jp",
2391 "iso-2022-kr",
2392 "csiso2022kr",
2393 "replacement",
2394 "windows-1250",
2395 "windows-1251",
2396 "windows-1252",
2397 "windows-1253",
2398 "windows-1254",
2399 "windows-1255",
2400 "windows-1256",
2401 "windows-1257",
2402 "windows-1258",
2403 "iso-8859-6-e",
2404 "iso-8859-8-e",
2405 "iso-8859-6-i",
2406 "iso-8859-8-i",
2407 "sun_eu_greek",
2408 "csksc56011987",
2409 "unicode20utf8",
2410 "unicode11utf8",
2411 "ks_c_5601-1987",
2412 "ansi_x3.4-1968",
2413 "ks_c_5601-1989",
2414 "x-mac-cyrillic",
2415 "x-user-defined",
2416 "csiso58gb231280",
2417 "iso-10646-ucs-2",
2418 "iso_8859-1:1987",
2419 "iso_8859-2:1987",
2420 "iso_8859-6:1987",
2421 "iso_8859-7:1987",
2422 "iso_8859-3:1988",
2423 "iso_8859-4:1988",
2424 "iso_8859-5:1988",
2425 "iso_8859-8:1988",
2426 "x-unicode20utf8",
2427 "iso_8859-9:1989",
2428 "csisolatingreek",
2429 "x-mac-ukrainian",
2430 "iso-2022-cn-ext",
2431 "csisolatinarabic",
2432 "csisolatinhebrew",
2433 "unicode-1-1-utf-8",
2434 "csisolatincyrillic",
2435 "cseucpkdfmtjapanese",
2436];
2437
2438static ENCODINGS_IN_LABEL_SORT: [&'static Encoding; 228] = [
2439 &WINDOWS_1252_INIT,
2440 &ISO_8859_2_INIT,
2441 &ISO_8859_3_INIT,
2442 &ISO_8859_4_INIT,
2443 &WINDOWS_1254_INIT,
2444 &ISO_8859_10_INIT,
2445 &ISO_8859_15_INIT,
2446 &IBM866_INIT,
2447 &MACINTOSH_INIT,
2448 &KOI8_R_INIT,
2449 &GBK_INIT,
2450 &BIG5_INIT,
2451 &UTF_8_INIT,
2452 &KOI8_R_INIT,
2453 &SHIFT_JIS_INIT,
2454 &UTF_16LE_INIT,
2455 &SHIFT_JIS_INIT,
2456 &IBM866_INIT,
2457 &UTF_8_INIT,
2458 &WINDOWS_1252_INIT,
2459 &WINDOWS_1252_INIT,
2460 &GBK_INIT,
2461 &ISO_8859_7_INIT,
2462 &WINDOWS_1250_INIT,
2463 &WINDOWS_1251_INIT,
2464 &WINDOWS_1252_INIT,
2465 &GBK_INIT,
2466 &WINDOWS_1252_INIT,
2467 &ISO_8859_2_INIT,
2468 &WINDOWS_1253_INIT,
2469 &ISO_8859_3_INIT,
2470 &WINDOWS_1254_INIT,
2471 &ISO_8859_4_INIT,
2472 &WINDOWS_1255_INIT,
2473 &BIG5_INIT,
2474 &WINDOWS_1254_INIT,
2475 &UTF_16LE_INIT,
2476 &WINDOWS_1256_INIT,
2477 &IBM866_INIT,
2478 &ISO_8859_10_INIT,
2479 &WINDOWS_1257_INIT,
2480 &WINDOWS_1258_INIT,
2481 &ISO_8859_7_INIT,
2482 &WINDOWS_1252_INIT,
2483 &ISO_8859_6_INIT,
2484 &ISO_8859_8_INIT,
2485 &EUC_KR_INIT,
2486 &EUC_JP_INIT,
2487 &KOI8_R_INIT,
2488 &KOI8_R_INIT,
2489 &EUC_KR_INIT,
2490 &SHIFT_JIS_INIT,
2491 &KOI8_U_INIT,
2492 &ISO_8859_8_INIT,
2493 &WINDOWS_874_INIT,
2494 &GB18030_INIT,
2495 &EUC_KR_INIT,
2496 &GBK_INIT,
2497 &WINDOWS_874_INIT,
2498 &BIG5_INIT,
2499 &UTF_16LE_INIT,
2500 &GBK_INIT,
2501 &ISO_8859_8_I_INIT,
2502 &KOI8_R_INIT,
2503 &EUC_KR_INIT,
2504 &KOI8_U_INIT,
2505 &WINDOWS_1250_INIT,
2506 &EUC_KR_INIT,
2507 &WINDOWS_1251_INIT,
2508 &WINDOWS_1252_INIT,
2509 &GBK_INIT,
2510 &WINDOWS_1252_INIT,
2511 &ISO_8859_2_INIT,
2512 &WINDOWS_1253_INIT,
2513 &ISO_8859_3_INIT,
2514 &ISO_8859_6_INIT,
2515 &WINDOWS_1254_INIT,
2516 &ISO_8859_4_INIT,
2517 &WINDOWS_1255_INIT,
2518 &ISO_8859_5_INIT,
2519 &BIG5_INIT,
2520 &WINDOWS_1256_INIT,
2521 &IBM866_INIT,
2522 &ISO_8859_6_INIT,
2523 &WINDOWS_1257_INIT,
2524 &ISO_8859_7_INIT,
2525 &ISO_8859_6_INIT,
2526 &ISO_8859_7_INIT,
2527 &ISO_8859_7_INIT,
2528 &WINDOWS_1258_INIT,
2529 &ISO_8859_8_INIT,
2530 &WINDOWS_1254_INIT,
2531 &ISO_8859_5_INIT,
2532 &UTF_16BE_INIT,
2533 &UTF_16LE_INIT,
2534 &WINDOWS_1252_INIT,
2535 &SHIFT_JIS_INIT,
2536 &EUC_JP_INIT,
2537 &ISO_8859_10_INIT,
2538 &WINDOWS_1252_INIT,
2539 &WINDOWS_874_INIT,
2540 &ISO_8859_2_INIT,
2541 &ISO_8859_3_INIT,
2542 &ISO_8859_13_INIT,
2543 &ISO_8859_4_INIT,
2544 &ISO_8859_14_INIT,
2545 &ISO_8859_5_INIT,
2546 &ISO_8859_15_INIT,
2547 &ISO_8859_6_INIT,
2548 &ISO_8859_7_INIT,
2549 &ISO_8859_8_INIT,
2550 &GBK_INIT,
2551 &WINDOWS_1254_INIT,
2552 &UTF_16LE_INIT,
2553 &MACINTOSH_INIT,
2554 &SHIFT_JIS_INIT,
2555 &SHIFT_JIS_INIT,
2556 &WINDOWS_1252_INIT,
2557 &ISO_8859_10_INIT,
2558 &ISO_8859_4_INIT,
2559 &GBK_INIT,
2560 &WINDOWS_1252_INIT,
2561 &WINDOWS_1252_INIT,
2562 &ISO_8859_2_INIT,
2563 &WINDOWS_874_INIT,
2564 &ISO_8859_2_INIT,
2565 &ISO_8859_2_INIT,
2566 &REPLACEMENT_INIT,
2567 &ISO_8859_3_INIT,
2568 &ISO_8859_3_INIT,
2569 &ISO_8859_13_INIT,
2570 &ISO_8859_4_INIT,
2571 &ISO_8859_4_INIT,
2572 &ISO_8859_14_INIT,
2573 &ISO_8859_5_INIT,
2574 &ISO_8859_5_INIT,
2575 &ISO_8859_5_INIT,
2576 &ISO_8859_15_INIT,
2577 &ISO_8859_6_INIT,
2578 &ISO_8859_6_INIT,
2579 &ISO_8859_7_INIT,
2580 &ISO_8859_7_INIT,
2581 &ISO_8859_7_INIT,
2582 &ISO_8859_6_INIT,
2583 &ISO_8859_10_INIT,
2584 &ISO_8859_8_INIT,
2585 &ISO_8859_8_INIT,
2586 &ISO_8859_8_INIT,
2587 &WINDOWS_1254_INIT,
2588 &WINDOWS_1254_INIT,
2589 &WINDOWS_1254_INIT,
2590 &ISO_8859_3_INIT,
2591 &EUC_KR_INIT,
2592 &BIG5_INIT,
2593 &SHIFT_JIS_INIT,
2594 &ISO_8859_10_INIT,
2595 &WINDOWS_874_INIT,
2596 &WINDOWS_1252_INIT,
2597 &ISO_8859_2_INIT,
2598 &ISO_8859_13_INIT,
2599 &ISO_8859_3_INIT,
2600 &ISO_8859_14_INIT,
2601 &WINDOWS_874_INIT,
2602 &ISO_8859_4_INIT,
2603 &ISO_8859_15_INIT,
2604 &ISO_8859_15_INIT,
2605 &WINDOWS_1254_INIT,
2606 &ISO_8859_16_INIT,
2607 &ISO_8859_10_INIT,
2608 &EUC_KR_INIT,
2609 &ISO_8859_15_INIT,
2610 &ISO_8859_6_INIT,
2611 &ISO_8859_8_INIT,
2612 &UTF_16BE_INIT,
2613 &UTF_16LE_INIT,
2614 &MACINTOSH_INIT,
2615 &ISO_8859_6_INIT,
2616 &ISO_8859_8_I_INIT,
2617 &SHIFT_JIS_INIT,
2618 &MACINTOSH_INIT,
2619 &REPLACEMENT_INIT,
2620 &ISO_2022_JP_INIT,
2621 &ISO_2022_JP_INIT,
2622 &REPLACEMENT_INIT,
2623 &REPLACEMENT_INIT,
2624 &REPLACEMENT_INIT,
2625 &WINDOWS_1250_INIT,
2626 &WINDOWS_1251_INIT,
2627 &WINDOWS_1252_INIT,
2628 &WINDOWS_1253_INIT,
2629 &WINDOWS_1254_INIT,
2630 &WINDOWS_1255_INIT,
2631 &WINDOWS_1256_INIT,
2632 &WINDOWS_1257_INIT,
2633 &WINDOWS_1258_INIT,
2634 &ISO_8859_6_INIT,
2635 &ISO_8859_8_INIT,
2636 &ISO_8859_6_INIT,
2637 &ISO_8859_8_I_INIT,
2638 &ISO_8859_7_INIT,
2639 &EUC_KR_INIT,
2640 &UTF_8_INIT,
2641 &UTF_8_INIT,
2642 &EUC_KR_INIT,
2643 &WINDOWS_1252_INIT,
2644 &EUC_KR_INIT,
2645 &X_MAC_CYRILLIC_INIT,
2646 &X_USER_DEFINED_INIT,
2647 &GBK_INIT,
2648 &UTF_16LE_INIT,
2649 &WINDOWS_1252_INIT,
2650 &ISO_8859_2_INIT,
2651 &ISO_8859_6_INIT,
2652 &ISO_8859_7_INIT,
2653 &ISO_8859_3_INIT,
2654 &ISO_8859_4_INIT,
2655 &ISO_8859_5_INIT,
2656 &ISO_8859_8_INIT,
2657 &UTF_8_INIT,
2658 &WINDOWS_1254_INIT,
2659 &ISO_8859_7_INIT,
2660 &X_MAC_CYRILLIC_INIT,
2661 &REPLACEMENT_INIT,
2662 &ISO_8859_6_INIT,
2663 &ISO_8859_8_INIT,
2664 &UTF_8_INIT,
2665 &ISO_8859_5_INIT,
2666 &EUC_JP_INIT,
2667];
2668
2669// END GENERATED CODE
2670
2671/// An encoding as defined in the [Encoding Standard][1].
2672///
2673/// An _encoding_ defines a mapping from a `u8` sequence to a `char` sequence
2674/// and, in most cases, vice versa. Each encoding has a name, an output
2675/// encoding, and one or more labels.
2676///
2677/// _Labels_ are ASCII-case-insensitive strings that are used to identify an
2678/// encoding in formats and protocols. The _name_ of the encoding is the
2679/// preferred label in the case appropriate for returning from the
2680/// [`characterSet`][2] property of the `Document` DOM interface.
2681///
2682/// The _output encoding_ is the encoding used for form submission and URL
2683/// parsing on Web pages in the encoding. This is UTF-8 for the replacement,
2684/// UTF-16LE and UTF-16BE encodings and the encoding itself for other
2685/// encodings.
2686///
2687/// [1]: https://encoding.spec.whatwg.org/
2688/// [2]: https://dom.spec.whatwg.org/#dom-document-characterset
2689///
2690/// # Streaming vs. Non-Streaming
2691///
2692/// When you have the entire input in a single buffer, you can use the
2693/// methods [`decode()`][3], [`decode_with_bom_removal()`][4],
2694/// [`decode_without_bom_handling()`][5],
2695/// [`decode_without_bom_handling_and_without_replacement()`][6] and
2696/// [`encode()`][7]. (These methods are available to Rust callers only and are
2697/// not available in the C API.) Unlike the rest of the API available to Rust,
2698/// these methods perform heap allocations. You should the `Decoder` and
2699/// `Encoder` objects when your input is split into multiple buffers or when
2700/// you want to control the allocation of the output buffers.
2701///
2702/// [3]: #method.decode
2703/// [4]: #method.decode_with_bom_removal
2704/// [5]: #method.decode_without_bom_handling
2705/// [6]: #method.decode_without_bom_handling_and_without_replacement
2706/// [7]: #method.encode
2707///
2708/// # Instances
2709///
2710/// All instances of `Encoding` are statically allocated and have the `'static`
2711/// lifetime. There is precisely one unique `Encoding` instance for each
2712/// encoding defined in the Encoding Standard.
2713///
2714/// To obtain a reference to a particular encoding whose identity you know at
2715/// compile time, use a `static` that refers to encoding. There is a `static`
2716/// for each encoding. The `static`s are named in all caps with hyphens
2717/// replaced with underscores (and in C/C++ have `_ENCODING` appended to the
2718/// name). For example, if you know at compile time that you will want to
2719/// decode using the UTF-8 encoding, use the `UTF_8` `static` (`UTF_8_ENCODING`
2720/// in C/C++).
2721///
2722/// Additionally, there are non-reference-typed forms ending with `_INIT` to
2723/// work around the problem that `static`s of the type `&'static Encoding`
2724/// cannot be used to initialize items of an array whose type is
2725/// `[&'static Encoding; N]`.
2726///
2727/// If you don't know what encoding you need at compile time and need to
2728/// dynamically get an encoding by label, use
2729/// <code>Encoding::<a href="#method.for_label">for_label</a>(<var>label</var>)</code>.
2730///
2731/// Instances of `Encoding` can be compared with `==` (in both Rust and in
2732/// C/C++).
2733pub struct Encoding {
2734 name: &'static str,
2735 variant: VariantEncoding,
2736}
2737
2738impl Encoding {
2739 /// Implements the
2740 /// [_get an encoding_](https://encoding.spec.whatwg.org/#concept-encoding-get)
2741 /// algorithm.
2742 ///
2743 /// If, after ASCII-lowercasing and removing leading and trailing
2744 /// whitespace, the argument matches a label defined in the Encoding
2745 /// Standard, `Some(&'static Encoding)` representing the corresponding
2746 /// encoding is returned. If there is no match, `None` is returned.
2747 ///
2748 /// This is the right method to use if the action upon the method returning
2749 /// `None` is to use a fallback encoding (e.g. `WINDOWS_1252`) instead.
2750 /// When the action upon the method returning `None` is not to proceed with
2751 /// a fallback but to refuse processing, `for_label_no_replacement()` is more
2752 /// appropriate.
2753 ///
2754 /// The argument is of type `&[u8]` instead of `&str` to save callers
2755 /// that are extracting the label from a non-UTF-8 protocol the trouble
2756 /// of conversion to UTF-8. (If you have a `&str`, just call `.as_bytes()`
2757 /// on it.)
2758 ///
2759 /// Available via the C wrapper.
2760 ///
2761 /// # Example
2762 /// ```
2763 /// use encoding_rs::Encoding;
2764 ///
2765 /// assert_eq!(Some(encoding_rs::UTF_8), Encoding::for_label(b"utf-8"));
2766 /// assert_eq!(Some(encoding_rs::UTF_8), Encoding::for_label(b"unicode11utf8"));
2767 ///
2768 /// assert_eq!(Some(encoding_rs::ISO_8859_2), Encoding::for_label(b"latin2"));
2769 ///
2770 /// assert_eq!(Some(encoding_rs::UTF_16BE), Encoding::for_label(b"utf-16be"));
2771 ///
2772 /// assert_eq!(None, Encoding::for_label(b"unrecognized label"));
2773 /// ```
2774 pub fn for_label(label: &[u8]) -> Option<&'static Encoding> {
2775 let mut trimmed = [0u8; LONGEST_LABEL_LENGTH];
2776 let mut trimmed_pos = 0usize;
2777 let mut iter = label.iter();
2778 // before
2779 loop {
2780 match iter.next() {
2781 None => {
2782 return None;
2783 }
2784 Some(byte) => {
2785 // The characters used in labels are:
2786 // a-z (except q, but excluding it below seems excessive)
2787 // 0-9
2788 // . _ - :
2789 match *byte {
2790 0x09u8 | 0x0Au8 | 0x0Cu8 | 0x0Du8 | 0x20u8 => {
2791 continue;
2792 }
2793 b'A'..=b'Z' => {
2794 trimmed[trimmed_pos] = *byte + 0x20u8;
2795 trimmed_pos = 1usize;
2796 break;
2797 }
2798 b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b':' | b'.' => {
2799 trimmed[trimmed_pos] = *byte;
2800 trimmed_pos = 1usize;
2801 break;
2802 }
2803 _ => {
2804 return None;
2805 }
2806 }
2807 }
2808 }
2809 }
2810 // inside
2811 loop {
2812 match iter.next() {
2813 None => {
2814 break;
2815 }
2816 Some(byte) => {
2817 match *byte {
2818 0x09u8 | 0x0Au8 | 0x0Cu8 | 0x0Du8 | 0x20u8 => {
2819 break;
2820 }
2821 b'A'..=b'Z' => {
2822 if trimmed_pos == LONGEST_LABEL_LENGTH {
2823 // There's no encoding with a label this long
2824 return None;
2825 }
2826 trimmed[trimmed_pos] = *byte + 0x20u8;
2827 trimmed_pos += 1usize;
2828 continue;
2829 }
2830 b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b':' | b'.' => {
2831 if trimmed_pos == LONGEST_LABEL_LENGTH {
2832 // There's no encoding with a label this long
2833 return None;
2834 }
2835 trimmed[trimmed_pos] = *byte;
2836 trimmed_pos += 1usize;
2837 continue;
2838 }
2839 _ => {
2840 return None;
2841 }
2842 }
2843 }
2844 }
2845 }
2846 // after
2847 loop {
2848 match iter.next() {
2849 None => {
2850 break;
2851 }
2852 Some(byte) => {
2853 match *byte {
2854 0x09u8 | 0x0Au8 | 0x0Cu8 | 0x0Du8 | 0x20u8 => {
2855 continue;
2856 }
2857 _ => {
2858 // There's no label with space in the middle
2859 return None;
2860 }
2861 }
2862 }
2863 }
2864 }
2865 let candidate = &trimmed[..trimmed_pos];
2866 match LABELS_SORTED.binary_search_by(|probe| {
2867 let bytes = probe.as_bytes();
2868 let c = bytes.len().cmp(&candidate.len());
2869 if c != Ordering::Equal {
2870 return c;
2871 }
2872 let probe_iter = bytes.iter().rev();
2873 let candidate_iter = candidate.iter().rev();
2874 probe_iter.cmp(candidate_iter)
2875 }) {
2876 Ok(i) => Some(ENCODINGS_IN_LABEL_SORT[i]),
2877 Err(_) => None,
2878 }
2879 }
2880
2881 /// This method behaves the same as `for_label()`, except when `for_label()`
2882 /// would return `Some(REPLACEMENT)`, this method returns `None` instead.
2883 ///
2884 /// This method is useful in scenarios where a fatal error is required
2885 /// upon invalid label, because in those cases the caller typically wishes
2886 /// to treat the labels that map to the replacement encoding as fatal
2887 /// errors, too.
2888 ///
2889 /// It is not OK to use this method when the action upon the method returning
2890 /// `None` is to use a fallback encoding (e.g. `WINDOWS_1252`). In such a
2891 /// case, the `for_label()` method should be used instead in order to avoid
2892 /// unsafe fallback for labels that `for_label()` maps to `Some(REPLACEMENT)`.
2893 ///
2894 /// Available via the C wrapper.
2895 #[inline]
2896 pub fn for_label_no_replacement(label: &[u8]) -> Option<&'static Encoding> {
2897 match Encoding::for_label(label) {
2898 None => None,
2899 Some(encoding) => {
2900 if encoding == REPLACEMENT {
2901 None
2902 } else {
2903 Some(encoding)
2904 }
2905 }
2906 }
2907 }
2908
2909 /// Performs non-incremental BOM sniffing.
2910 ///
2911 /// The argument must either be a buffer representing the entire input
2912 /// stream (non-streaming case) or a buffer representing at least the first
2913 /// three bytes of the input stream (streaming case).
2914 ///
2915 /// Returns `Some((UTF_8, 3))`, `Some((UTF_16LE, 2))` or
2916 /// `Some((UTF_16BE, 2))` if the argument starts with the UTF-8, UTF-16LE
2917 /// or UTF-16BE BOM or `None` otherwise.
2918 ///
2919 /// Available via the C wrapper.
2920 #[inline]
2921 pub fn for_bom(buffer: &[u8]) -> Option<(&'static Encoding, usize)> {
2922 if buffer.starts_with(b"\xEF\xBB\xBF") {
2923 Some((UTF_8, 3))
2924 } else if buffer.starts_with(b"\xFF\xFE") {
2925 Some((UTF_16LE, 2))
2926 } else if buffer.starts_with(b"\xFE\xFF") {
2927 Some((UTF_16BE, 2))
2928 } else {
2929 None
2930 }
2931 }
2932
2933 /// Returns the name of this encoding.
2934 ///
2935 /// This name is appropriate to return as-is from the DOM
2936 /// `document.characterSet` property.
2937 ///
2938 /// Available via the C wrapper.
2939 #[inline]
2940 pub fn name(&'static self) -> &'static str {
2941 self.name
2942 }
2943
2944 /// Checks whether the _output encoding_ of this encoding can encode every
2945 /// `char`. (Only true if the output encoding is UTF-8.)
2946 ///
2947 /// Available via the C wrapper.
2948 #[inline]
2949 pub fn can_encode_everything(&'static self) -> bool {
2950 self.output_encoding() == UTF_8
2951 }
2952
2953 /// Checks whether the bytes 0x00...0x7F map exclusively to the characters
2954 /// U+0000...U+007F and vice versa.
2955 ///
2956 /// Available via the C wrapper.
2957 #[inline]
2958 pub fn is_ascii_compatible(&'static self) -> bool {
2959 !(self == REPLACEMENT || self == UTF_16BE || self == UTF_16LE || self == ISO_2022_JP)
2960 }
2961
2962 /// Checks whether this encoding maps one byte to one Basic Multilingual
2963 /// Plane code point (i.e. byte length equals decoded UTF-16 length) and
2964 /// vice versa (for mappable characters).
2965 ///
2966 /// `true` iff this encoding is on the list of [Legacy single-byte
2967 /// encodings](https://encoding.spec.whatwg.org/#legacy-single-byte-encodings)
2968 /// in the spec or x-user-defined.
2969 ///
2970 /// Available via the C wrapper.
2971 #[inline]
2972 pub fn is_single_byte(&'static self) -> bool {
2973 self.variant.is_single_byte()
2974 }
2975
2976 /// Checks whether the bytes 0x00...0x7F map mostly to the characters
2977 /// U+0000...U+007F and vice versa.
2978 #[cfg(feature = "alloc")]
2979 #[inline]
2980 fn is_potentially_borrowable(&'static self) -> bool {
2981 !(self == REPLACEMENT || self == UTF_16BE || self == UTF_16LE)
2982 }
2983
2984 /// Returns the _output encoding_ of this encoding. This is UTF-8 for
2985 /// UTF-16BE, UTF-16LE, and replacement and the encoding itself otherwise.
2986 ///
2987 /// _Note:_ The _output encoding_ concept is needed for form submission and
2988 /// error handling in the query strings of URLs in the Web Platform.
2989 ///
2990 /// Available via the C wrapper.
2991 #[inline]
2992 pub fn output_encoding(&'static self) -> &'static Encoding {
2993 if self == REPLACEMENT || self == UTF_16BE || self == UTF_16LE {
2994 UTF_8
2995 } else {
2996 self
2997 }
2998 }
2999
3000 /// Decode complete input to `Cow<'a, str>` _with BOM sniffing_ and with
3001 /// malformed sequences replaced with the REPLACEMENT CHARACTER when the
3002 /// entire input is available as a single buffer (i.e. the end of the
3003 /// buffer marks the end of the stream).
3004 ///
3005 /// The BOM, if any, does not appear in the output.
3006 ///
3007 /// This method implements the (non-streaming version of) the
3008 /// [_decode_](https://encoding.spec.whatwg.org/#decode) spec concept.
3009 ///
3010 /// The second item in the returned tuple is the encoding that was actually
3011 /// used (which may differ from this encoding thanks to BOM sniffing).
3012 ///
3013 /// The third item in the returned tuple indicates whether there were
3014 /// malformed sequences (that were replaced with the REPLACEMENT CHARACTER).
3015 ///
3016 /// _Note:_ It is wrong to use this when the input buffer represents only
3017 /// a segment of the input instead of the whole input. Use `new_decoder()`
3018 /// when decoding segmented input.
3019 ///
3020 /// This method performs a one or two heap allocations for the backing
3021 /// buffer of the `String` when unable to borrow. (One allocation if not
3022 /// errors and potentially another one in the presence of errors.) The
3023 /// first allocation assumes jemalloc and may not be optimal with
3024 /// allocators that do not use power-of-two buckets. A borrow is performed
3025 /// if decoding UTF-8 and the input is valid UTF-8, if decoding an
3026 /// ASCII-compatible encoding and the input is ASCII-only, or when decoding
3027 /// ISO-2022-JP and the input is entirely in the ASCII state without state
3028 /// transitions.
3029 ///
3030 /// # Panics
3031 ///
3032 /// If the size calculation for a heap-allocated backing buffer overflows
3033 /// `usize`.
3034 ///
3035 /// Available to Rust only and only with the `alloc` feature enabled (enabled
3036 /// by default).
3037 #[cfg(feature = "alloc")]
3038 #[inline]
3039 pub fn decode<'a>(&'static self, bytes: &'a [u8]) -> (Cow<'a, str>, &'static Encoding, bool) {
3040 let (encoding, without_bom) = match Encoding::for_bom(bytes) {
3041 Some((encoding, bom_length)) => (encoding, &bytes[bom_length..]),
3042 None => (self, bytes),
3043 };
3044 let (cow, had_errors) = encoding.decode_without_bom_handling(without_bom);
3045 (cow, encoding, had_errors)
3046 }
3047
3048 /// Decode complete input to `Cow<'a, str>` _with BOM removal_ and with
3049 /// malformed sequences replaced with the REPLACEMENT CHARACTER when the
3050 /// entire input is available as a single buffer (i.e. the end of the
3051 /// buffer marks the end of the stream).
3052 ///
3053 /// Only an initial byte sequence that is a BOM for this encoding is removed.
3054 ///
3055 /// When invoked on `UTF_8`, this method implements the (non-streaming
3056 /// version of) the
3057 /// [_UTF-8 decode_](https://encoding.spec.whatwg.org/#utf-8-decode) spec
3058 /// concept.
3059 ///
3060 /// The second item in the returned pair indicates whether there were
3061 /// malformed sequences (that were replaced with the REPLACEMENT CHARACTER).
3062 ///
3063 /// _Note:_ It is wrong to use this when the input buffer represents only
3064 /// a segment of the input instead of the whole input. Use
3065 /// `new_decoder_with_bom_removal()` when decoding segmented input.
3066 ///
3067 /// This method performs a one or two heap allocations for the backing
3068 /// buffer of the `String` when unable to borrow. (One allocation if not
3069 /// errors and potentially another one in the presence of errors.) The
3070 /// first allocation assumes jemalloc and may not be optimal with
3071 /// allocators that do not use power-of-two buckets. A borrow is performed
3072 /// if decoding UTF-8 and the input is valid UTF-8, if decoding an
3073 /// ASCII-compatible encoding and the input is ASCII-only, or when decoding
3074 /// ISO-2022-JP and the input is entirely in the ASCII state without state
3075 /// transitions.
3076 ///
3077 /// # Panics
3078 ///
3079 /// If the size calculation for a heap-allocated backing buffer overflows
3080 /// `usize`.
3081 ///
3082 /// Available to Rust only and only with the `alloc` feature enabled (enabled
3083 /// by default).
3084 #[cfg(feature = "alloc")]
3085 #[inline]
3086 pub fn decode_with_bom_removal<'a>(&'static self, bytes: &'a [u8]) -> (Cow<'a, str>, bool) {
3087 let without_bom = if self == UTF_8 && bytes.starts_with(b"\xEF\xBB\xBF") {
3088 &bytes[3..]
3089 } else if (self == UTF_16LE && bytes.starts_with(b"\xFF\xFE"))
3090 || (self == UTF_16BE && bytes.starts_with(b"\xFE\xFF"))
3091 {
3092 &bytes[2..]
3093 } else {
3094 bytes
3095 };
3096 self.decode_without_bom_handling(without_bom)
3097 }
3098
3099 /// Decode complete input to `Cow<'a, str>` _without BOM handling_ and
3100 /// with malformed sequences replaced with the REPLACEMENT CHARACTER when
3101 /// the entire input is available as a single buffer (i.e. the end of the
3102 /// buffer marks the end of the stream).
3103 ///
3104 /// When invoked on `UTF_8`, this method implements the (non-streaming
3105 /// version of) the
3106 /// [_UTF-8 decode without BOM_](https://encoding.spec.whatwg.org/#utf-8-decode-without-bom)
3107 /// spec concept.
3108 ///
3109 /// The second item in the returned pair indicates whether there were
3110 /// malformed sequences (that were replaced with the REPLACEMENT CHARACTER).
3111 ///
3112 /// _Note:_ It is wrong to use this when the input buffer represents only
3113 /// a segment of the input instead of the whole input. Use
3114 /// `new_decoder_without_bom_handling()` when decoding segmented input.
3115 ///
3116 /// This method performs a one or two heap allocations for the backing
3117 /// buffer of the `String` when unable to borrow. (One allocation if not
3118 /// errors and potentially another one in the presence of errors.) The
3119 /// first allocation assumes jemalloc and may not be optimal with
3120 /// allocators that do not use power-of-two buckets. A borrow is performed
3121 /// if decoding UTF-8 and the input is valid UTF-8, if decoding an
3122 /// ASCII-compatible encoding and the input is ASCII-only, or when decoding
3123 /// ISO-2022-JP and the input is entirely in the ASCII state without state
3124 /// transitions.
3125 ///
3126 /// # Panics
3127 ///
3128 /// If the size calculation for a heap-allocated backing buffer overflows
3129 /// `usize`.
3130 ///
3131 /// Available to Rust only and only with the `alloc` feature enabled (enabled
3132 /// by default).
3133 #[cfg(feature = "alloc")]
3134 pub fn decode_without_bom_handling<'a>(&'static self, bytes: &'a [u8]) -> (Cow<'a, str>, bool) {
3135 let (mut decoder, mut string, mut total_read) = if self.is_potentially_borrowable() {
3136 let valid_up_to = if self == UTF_8 {
3137 utf8_valid_up_to(bytes)
3138 } else if self == ISO_2022_JP {
3139 iso_2022_jp_ascii_valid_up_to(bytes)
3140 } else {
3141 ascii_valid_up_to(bytes)
3142 };
3143 if valid_up_to == bytes.len() {
3144 let str: &str = unsafe { core::str::from_utf8_unchecked(bytes) };
3145 return (Cow::Borrowed(str), false);
3146 }
3147 let decoder = self.new_decoder_without_bom_handling();
3148
3149 let rounded_without_replacement = checked_next_power_of_two(checked_add(
3150 valid_up_to,
3151 decoder.max_utf8_buffer_length_without_replacement(bytes.len() - valid_up_to),
3152 ));
3153 let with_replacement = checked_add(
3154 valid_up_to,
3155 decoder.max_utf8_buffer_length(bytes.len() - valid_up_to),
3156 );
3157 let mut string = String::with_capacity(
3158 checked_min(rounded_without_replacement, with_replacement).unwrap(),
3159 );
3160
3161 // SAFETY: We have validated that `bytes[..valid_up_to]` is valid UTF-8,
3162 // so it's OK to write that into `String` via `Vec`.
3163 let vec = unsafe { string.as_mut_vec() };
3164 vec.extend_from_slice(&bytes[..valid_up_to]);
3165 (decoder, string, valid_up_to)
3166 } else {
3167 let decoder = self.new_decoder_without_bom_handling();
3168 let rounded_without_replacement = checked_next_power_of_two(
3169 decoder.max_utf8_buffer_length_without_replacement(bytes.len()),
3170 );
3171 let with_replacement = decoder.max_utf8_buffer_length(bytes.len());
3172 let string = String::with_capacity(
3173 checked_min(rounded_without_replacement, with_replacement).unwrap(),
3174 );
3175 (decoder, string, 0)
3176 };
3177
3178 let mut total_had_errors = false;
3179 loop {
3180 let (result, read, had_errors) =
3181 decoder.decode_to_string(&bytes[total_read..], &mut string, true);
3182 total_read += read;
3183 total_had_errors |= had_errors;
3184 match result {
3185 CoderResult::InputEmpty => {
3186 debug_assert_eq!(total_read, bytes.len());
3187 return (Cow::Owned(string), total_had_errors);
3188 }
3189 CoderResult::OutputFull => {
3190 // Allocate for the worst case. That is, we should come
3191 // here at most once per invocation of this method.
3192 let needed = decoder.max_utf8_buffer_length(bytes.len() - total_read);
3193 string.reserve(needed.unwrap());
3194 }
3195 }
3196 }
3197 }
3198
3199 /// Decode complete input to `Cow<'a, str>` _without BOM handling_ and
3200 /// _with malformed sequences treated as fatal_ when the entire input is
3201 /// available as a single buffer (i.e. the end of the buffer marks the end
3202 /// of the stream).
3203 ///
3204 /// When invoked on `UTF_8`, this method implements the (non-streaming
3205 /// version of) the
3206 /// [_UTF-8 decode without BOM or fail_](https://encoding.spec.whatwg.org/#utf-8-decode-without-bom-or-fail)
3207 /// spec concept.
3208 ///
3209 /// Returns `None` if a malformed sequence was encountered and the result
3210 /// of the decode as `Some(String)` otherwise.
3211 ///
3212 /// _Note:_ It is wrong to use this when the input buffer represents only
3213 /// a segment of the input instead of the whole input. Use
3214 /// `new_decoder_without_bom_handling()` when decoding segmented input.
3215 ///
3216 /// This method performs a single heap allocation for the backing
3217 /// buffer of the `String` when unable to borrow. A borrow is performed if
3218 /// decoding UTF-8 and the input is valid UTF-8, if decoding an
3219 /// ASCII-compatible encoding and the input is ASCII-only, or when decoding
3220 /// ISO-2022-JP and the input is entirely in the ASCII state without state
3221 /// transitions.
3222 ///
3223 /// # Panics
3224 ///
3225 /// If the size calculation for a heap-allocated backing buffer overflows
3226 /// `usize`.
3227 ///
3228 /// Available to Rust only and only with the `alloc` feature enabled (enabled
3229 /// by default).
3230 #[cfg(feature = "alloc")]
3231 pub fn decode_without_bom_handling_and_without_replacement<'a>(
3232 &'static self,
3233 bytes: &'a [u8],
3234 ) -> Option<Cow<'a, str>> {
3235 if self == UTF_8 {
3236 let valid_up_to = utf8_valid_up_to(bytes);
3237 if valid_up_to == bytes.len() {
3238 let str: &str = unsafe { core::str::from_utf8_unchecked(bytes) };
3239 return Some(Cow::Borrowed(str));
3240 }
3241 return None;
3242 }
3243 let (mut decoder, mut string, input) = if self.is_potentially_borrowable() {
3244 let valid_up_to = if self == ISO_2022_JP {
3245 iso_2022_jp_ascii_valid_up_to(bytes)
3246 } else {
3247 ascii_valid_up_to(bytes)
3248 };
3249 if valid_up_to == bytes.len() {
3250 let str: &str = unsafe { core::str::from_utf8_unchecked(bytes) };
3251 return Some(Cow::Borrowed(str));
3252 }
3253 let decoder = self.new_decoder_without_bom_handling();
3254 let mut string = String::with_capacity(
3255 checked_add(
3256 valid_up_to,
3257 decoder.max_utf8_buffer_length_without_replacement(bytes.len() - valid_up_to),
3258 )
3259 .unwrap(),
3260 );
3261 // SAFETY: We have validated that `bytes[..valid_up_to]` is valid UTF-8,
3262 // so it's OK to write that into `String` via `Vec`.
3263 let vec = unsafe { string.as_mut_vec() };
3264 vec.extend_from_slice(&bytes[..valid_up_to]);
3265 (decoder, string, &bytes[valid_up_to..])
3266 } else {
3267 let decoder = self.new_decoder_without_bom_handling();
3268 let string = String::with_capacity(
3269 decoder
3270 .max_utf8_buffer_length_without_replacement(bytes.len())
3271 .unwrap(),
3272 );
3273 (decoder, string, bytes)
3274 };
3275 let (result, read) = decoder.decode_to_string_without_replacement(input, &mut string, true);
3276 match result {
3277 DecoderResult::InputEmpty => {
3278 debug_assert_eq!(read, input.len());
3279 Some(Cow::Owned(string))
3280 }
3281 DecoderResult::Malformed(_, _) => None,
3282 DecoderResult::OutputFull => unreachable!(),
3283 }
3284 }
3285
3286 /// Encode complete input to `Cow<'a, [u8]>` using the
3287 /// [_output encoding_](Encoding::output_encoding) of this encoding with
3288 /// unmappable characters replaced with decimal numeric character references
3289 /// when the entire input is available as a single buffer (i.e. the end of
3290 /// the buffer marks the end of the stream).
3291 ///
3292 /// This method implements the (non-streaming version of) the
3293 /// [_encode_](https://encoding.spec.whatwg.org/#encode) spec concept. For
3294 /// the [_UTF-8 encode_](https://encoding.spec.whatwg.org/#utf-8-encode)
3295 /// spec concept, it is slightly more efficient to use
3296 /// <code><var>string</var>.as_bytes()</code> instead of invoking this
3297 /// method on `UTF_8`.
3298 ///
3299 /// The second item in the returned tuple is the encoding that was actually
3300 /// used (*which may differ from this encoding thanks to some encodings
3301 /// having UTF-8 as their output encoding*).
3302 ///
3303 /// The third item in the returned tuple indicates whether there were
3304 /// unmappable characters (that were replaced with HTML numeric character
3305 /// references).
3306 ///
3307 /// _Note:_ It is wrong to use this when the input buffer represents only
3308 /// a segment of the input instead of the whole input. Use `new_encoder()`
3309 /// when encoding segmented output.
3310 ///
3311 /// When encoding to UTF-8 or when encoding an ASCII-only input to a
3312 /// ASCII-compatible encoding, this method returns a borrow of the input
3313 /// without a heap allocation. Otherwise, this method performs a single
3314 /// heap allocation for the backing buffer of the `Vec<u8>` if there are no
3315 /// unmappable characters and potentially multiple heap allocations if
3316 /// there are. These allocations are tuned for jemalloc and may not be
3317 /// optimal when using a different allocator that doesn't use power-of-two
3318 /// buckets.
3319 ///
3320 /// # Panics
3321 ///
3322 /// If the size calculation for a heap-allocated backing buffer overflows
3323 /// `usize`.
3324 ///
3325 /// Available to Rust only and only with the `alloc` feature enabled (enabled
3326 /// by default).
3327 #[cfg(feature = "alloc")]
3328 pub fn encode<'a>(&'static self, string: &'a str) -> (Cow<'a, [u8]>, &'static Encoding, bool) {
3329 let output_encoding = self.output_encoding();
3330 if output_encoding == UTF_8 {
3331 return (Cow::Borrowed(string.as_bytes()), output_encoding, false);
3332 }
3333 debug_assert!(output_encoding.is_potentially_borrowable());
3334 let bytes = string.as_bytes();
3335 let valid_up_to = if output_encoding == ISO_2022_JP {
3336 iso_2022_jp_ascii_valid_up_to(bytes)
3337 } else {
3338 ascii_valid_up_to(bytes)
3339 };
3340 if valid_up_to == bytes.len() {
3341 return (Cow::Borrowed(bytes), output_encoding, false);
3342 }
3343 let mut encoder = output_encoding.new_encoder();
3344 let mut vec: Vec<u8> = Vec::with_capacity(
3345 (checked_add(
3346 valid_up_to,
3347 encoder.max_buffer_length_from_utf8_if_no_unmappables(string.len() - valid_up_to),
3348 ))
3349 .unwrap()
3350 .next_power_of_two(),
3351 );
3352 vec.extend_from_slice(&bytes[..valid_up_to]);
3353 let mut total_read = valid_up_to;
3354 let mut total_had_errors = false;
3355 loop {
3356 let (result, read, had_errors) =
3357 encoder.encode_from_utf8_to_vec(&string[total_read..], &mut vec, true);
3358 total_read += read;
3359 total_had_errors |= had_errors;
3360 match result {
3361 CoderResult::InputEmpty => {
3362 debug_assert_eq!(total_read, string.len());
3363 return (Cow::Owned(vec), output_encoding, total_had_errors);
3364 }
3365 CoderResult::OutputFull => {
3366 // reserve_exact wants to know how much more on top of current
3367 // length--not current capacity.
3368 let needed = encoder
3369 .max_buffer_length_from_utf8_if_no_unmappables(string.len() - total_read);
3370 let rounded = (checked_add(vec.capacity(), needed))
3371 .unwrap()
3372 .next_power_of_two();
3373 let additional = rounded - vec.len();
3374 vec.reserve_exact(additional);
3375 }
3376 }
3377 }
3378 }
3379
3380 fn new_variant_decoder(&'static self) -> VariantDecoder {
3381 self.variant.new_variant_decoder()
3382 }
3383
3384 /// Instantiates a new decoder for this encoding with BOM sniffing enabled.
3385 ///
3386 /// BOM sniffing may cause the returned decoder to morph into a decoder
3387 /// for UTF-8, UTF-16LE or UTF-16BE instead of this encoding. The BOM
3388 /// does not appear in the output.
3389 ///
3390 /// Available via the C wrapper.
3391 #[inline]
3392 pub fn new_decoder(&'static self) -> Decoder {
3393 Decoder::new(self, self.new_variant_decoder(), BomHandling::Sniff)
3394 }
3395
3396 /// Instantiates a new decoder for this encoding with BOM removal.
3397 ///
3398 /// If the input starts with bytes that are the BOM for this encoding,
3399 /// those bytes are removed. However, the decoder never morphs into a
3400 /// decoder for another encoding: A BOM for another encoding is treated as
3401 /// (potentially malformed) input to the decoding algorithm for this
3402 /// encoding.
3403 ///
3404 /// Available via the C wrapper.
3405 #[inline]
3406 pub fn new_decoder_with_bom_removal(&'static self) -> Decoder {
3407 Decoder::new(self, self.new_variant_decoder(), BomHandling::Remove)
3408 }
3409
3410 /// Instantiates a new decoder for this encoding with BOM handling disabled.
3411 ///
3412 /// If the input starts with bytes that look like a BOM, those bytes are
3413 /// not treated as a BOM. (Hence, the decoder never morphs into a decoder
3414 /// for another encoding.)
3415 ///
3416 /// _Note:_ If the caller has performed BOM sniffing on its own but has not
3417 /// removed the BOM, the caller should use `new_decoder_with_bom_removal()`
3418 /// instead of this method to cause the BOM to be removed.
3419 ///
3420 /// Available via the C wrapper.
3421 #[inline]
3422 pub fn new_decoder_without_bom_handling(&'static self) -> Decoder {
3423 Decoder::new(self, self.new_variant_decoder(), BomHandling::Off)
3424 }
3425
3426 /// Instantiates a new encoder for the [_output encoding_](Encoding::output_encoding)
3427 /// of this encoding.
3428 ///
3429 /// _Note:_ The output encoding of UTF-16BE, UTF-16LE, and replacement is UTF-8. There
3430 /// is no encoder for UTF-16BE, UTF-16LE, and replacement themselves.
3431 ///
3432 /// Available via the C wrapper.
3433 #[inline]
3434 pub fn new_encoder(&'static self) -> Encoder {
3435 let enc = self.output_encoding();
3436 enc.variant.new_encoder(enc)
3437 }
3438
3439 /// Validates UTF-8.
3440 ///
3441 /// Returns the index of the first byte that makes the input malformed as
3442 /// UTF-8 or the length of the slice if the slice is entirely valid.
3443 ///
3444 /// This is currently faster than the corresponding standard library
3445 /// functionality. If this implementation gets upstreamed to the standard
3446 /// library, this method may be removed in the future.
3447 ///
3448 /// Available via the C wrapper.
3449 pub fn utf8_valid_up_to(bytes: &[u8]) -> usize {
3450 utf8_valid_up_to(bytes)
3451 }
3452
3453 /// Validates ASCII.
3454 ///
3455 /// Returns the index of the first byte that makes the input malformed as
3456 /// ASCII or the length of the slice if the slice is entirely valid.
3457 ///
3458 /// Available via the C wrapper.
3459 pub fn ascii_valid_up_to(bytes: &[u8]) -> usize {
3460 ascii_valid_up_to(bytes)
3461 }
3462
3463 /// Validates ISO-2022-JP ASCII-state data.
3464 ///
3465 /// Returns the index of the first byte that makes the input not
3466 /// representable in the ASCII state of ISO-2022-JP or the length of the
3467 /// slice if the slice is entirely representable in the ASCII state of
3468 /// ISO-2022-JP.
3469 ///
3470 /// Available via the C wrapper.
3471 pub fn iso_2022_jp_ascii_valid_up_to(bytes: &[u8]) -> usize {
3472 iso_2022_jp_ascii_valid_up_to(bytes)
3473 }
3474}
3475
3476impl PartialEq for Encoding {
3477 #[inline]
3478 fn eq(&self, other: &Encoding) -> bool {
3479 ::core::ptr::eq(self, other)
3480 }
3481}
3482
3483impl Eq for Encoding {}
3484
3485#[cfg(test)]
3486impl PartialOrd for Encoding {
3487 #[inline]
3488 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
3489 (self as *const Encoding as usize).partial_cmp(&(other as *const Encoding as usize))
3490 }
3491}
3492
3493#[cfg(test)]
3494impl Ord for Encoding {
3495 #[inline]
3496 fn cmp(&self, other: &Self) -> Ordering {
3497 (self as *const Encoding as usize).cmp(&(other as *const Encoding as usize))
3498 }
3499}
3500
3501impl Hash for Encoding {
3502 #[inline]
3503 fn hash<H: Hasher>(&self, state: &mut H) {
3504 (self as *const Encoding).hash(state);
3505 }
3506}
3507
3508impl core::fmt::Debug for Encoding {
3509 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
3510 f.debug_struct("Encoding")
3511 .field("name", &self.name)
3512 .finish_non_exhaustive()
3513 }
3514}
3515
3516#[cfg(feature = "serde")]
3517impl Serialize for Encoding {
3518 #[inline]
3519 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
3520 where
3521 S: Serializer,
3522 {
3523 serializer.serialize_str(self.name)
3524 }
3525}
3526
3527#[cfg(feature = "serde")]
3528struct EncodingVisitor;
3529
3530#[cfg(feature = "serde")]
3531impl<'de> Visitor<'de> for EncodingVisitor {
3532 type Value = &'static Encoding;
3533
3534 fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
3535 formatter.write_str("a valid encoding label")
3536 }
3537
3538 fn visit_str<E>(self, value: &str) -> Result<&'static Encoding, E>
3539 where
3540 E: serde::de::Error,
3541 {
3542 if let Some(enc) = Encoding::for_label(value.as_bytes()) {
3543 Ok(enc)
3544 } else {
3545 cfg_if! {
3546 if #[cfg(feature = "alloc")] {
3547 Err(E::custom(alloc::format!(
3548 "invalid encoding label: {}",
3549 value
3550 )))
3551 } else {
3552 Err(E::custom("invalid encoding label"))
3553 }
3554 }
3555 }
3556 }
3557}
3558
3559#[cfg(feature = "serde")]
3560impl<'de> Deserialize<'de> for &'static Encoding {
3561 fn deserialize<D>(deserializer: D) -> Result<&'static Encoding, D::Error>
3562 where
3563 D: Deserializer<'de>,
3564 {
3565 deserializer.deserialize_str(EncodingVisitor)
3566 }
3567}
3568
3569/// Tracks the life cycle of a decoder from BOM sniffing to conversion to end.
3570#[derive(PartialEq, Debug, Copy, Clone)]
3571enum DecoderLifeCycle {
3572 /// The decoder has seen no input yet.
3573 AtStart,
3574 /// The decoder has seen no input yet but expects UTF-8.
3575 AtUtf8Start,
3576 /// The decoder has seen no input yet but expects UTF-16BE.
3577 AtUtf16BeStart,
3578 /// The decoder has seen no input yet but expects UTF-16LE.
3579 AtUtf16LeStart,
3580 /// The decoder has seen EF.
3581 SeenUtf8First,
3582 /// The decoder has seen EF, BB.
3583 SeenUtf8Second,
3584 /// The decoder has seen FE.
3585 SeenUtf16BeFirst,
3586 /// The decoder has seen FF.
3587 SeenUtf16LeFirst,
3588 /// Saw EF, BB but not BF, there was a buffer boundary after BB and the
3589 /// underlying decoder reported EF as an error, so we need to remember to
3590 /// push BB before the next buffer.
3591 ConvertingWithPendingBB,
3592 /// No longer looking for a BOM and EOF not yet seen.
3593 Converting,
3594 /// EOF has been seen.
3595 Finished,
3596}
3597
3598/// Communicate the BOM handling mode.
3599#[derive(Debug, Copy, Clone)]
3600enum BomHandling {
3601 /// Don't handle the BOM
3602 Off,
3603 /// Sniff for UTF-8, UTF-16BE or UTF-16LE BOM
3604 Sniff,
3605 /// Remove the BOM only if it's the BOM for this encoding
3606 Remove,
3607}
3608
3609/// Result of a (potentially partial) decode or encode operation with
3610/// replacement.
3611#[must_use]
3612#[derive(Debug, PartialEq, Eq)]
3613pub enum CoderResult {
3614 /// The input was exhausted.
3615 ///
3616 /// If this result was returned from a call where `last` was `true`, the
3617 /// conversion process has completed. Otherwise, the caller should call a
3618 /// decode or encode method again with more input.
3619 InputEmpty,
3620
3621 /// The converter cannot produce another unit of output, because the output
3622 /// buffer does not have enough space left.
3623 ///
3624 /// The caller must provide more output space upon the next call and re-push
3625 /// the remaining input to the converter.
3626 OutputFull,
3627}
3628
3629/// Result of a (potentially partial) decode operation without replacement.
3630#[must_use]
3631#[derive(Debug, PartialEq, Eq)]
3632pub enum DecoderResult {
3633 /// The input was exhausted.
3634 ///
3635 /// If this result was returned from a call where `last` was `true`, the
3636 /// decoding process has completed. Otherwise, the caller should call a
3637 /// decode method again with more input.
3638 InputEmpty,
3639
3640 /// The decoder cannot produce another unit of output, because the output
3641 /// buffer does not have enough space left.
3642 ///
3643 /// The caller must provide more output space upon the next call and re-push
3644 /// the remaining input to the decoder.
3645 OutputFull,
3646
3647 /// The decoder encountered a malformed byte sequence.
3648 ///
3649 /// The caller must either treat this as a fatal error or must append one
3650 /// REPLACEMENT CHARACTER (U+FFFD) to the output and then re-push the
3651 /// the remaining input to the decoder.
3652 ///
3653 /// The first wrapped integer indicates the length of the malformed byte
3654 /// sequence. The second wrapped integer indicates the number of bytes
3655 /// that were consumed after the malformed sequence. If the second
3656 /// integer is zero, the last byte that was consumed is the last byte of
3657 /// the malformed sequence. Note that the malformed bytes may have been part
3658 /// of an earlier input buffer.
3659 ///
3660 /// The first wrapped integer can have values 1, 2, 3 or 4. The second
3661 /// wrapped integer can have values 0, 1, 2 or 3. The worst-case sum
3662 /// of the two is 6, which happens with ISO-2022-JP.
3663 Malformed(u8, u8), // u8 instead of usize to avoid useless bloat
3664}
3665
3666/// A converter that decodes a byte stream into Unicode according to a
3667/// character encoding in a streaming (incremental) manner.
3668///
3669/// The various `decode_*` methods take an input buffer (`src`) and an output
3670/// buffer `dst` both of which are caller-allocated. There are variants for
3671/// both UTF-8 and UTF-16 output buffers.
3672///
3673/// A `decode_*` method decodes bytes from `src` into Unicode characters stored
3674/// into `dst` until one of the following three things happens:
3675///
3676/// 1. A malformed byte sequence is encountered (`*_without_replacement`
3677/// variants only).
3678///
3679/// 2. The output buffer has been filled so near capacity that the decoder
3680/// cannot be sure that processing an additional byte of input wouldn't
3681/// cause so much output that the output buffer would overflow.
3682///
3683/// 3. All the input bytes have been processed.
3684///
3685/// The `decode_*` method then returns tuple of a status indicating which one
3686/// of the three reasons to return happened, how many input bytes were read,
3687/// how many output code units (`u8` when decoding into UTF-8 and `u16`
3688/// when decoding to UTF-16) were written (except when decoding into `String`,
3689/// whose length change indicates this), and in the case of the
3690/// variants performing replacement, a boolean indicating whether an error was
3691/// replaced with the REPLACEMENT CHARACTER during the call.
3692///
3693/// The number of bytes "written" is what's logically written. Garbage may be
3694/// written in the output buffer beyond the point logically written to.
3695/// Therefore, if you wish to decode into an `&mut str`, you should use the
3696/// methods that take an `&mut str` argument instead of the ones that take an
3697/// `&mut [u8]` argument. The former take care of overwriting the trailing
3698/// garbage to ensure the UTF-8 validity of the `&mut str` as a whole, but the
3699/// latter don't.
3700///
3701/// In the case of the `*_without_replacement` variants, the status is a
3702/// [`DecoderResult`][1] enumeration (possibilities `Malformed`, `OutputFull` and
3703/// `InputEmpty` corresponding to the three cases listed above).
3704///
3705/// In the case of methods whose name does not end with
3706/// `*_without_replacement`, malformed sequences are automatically replaced
3707/// with the REPLACEMENT CHARACTER and errors do not cause the methods to
3708/// return early.
3709///
3710/// When decoding to UTF-8, the output buffer must have at least 4 bytes of
3711/// space. When decoding to UTF-16, the output buffer must have at least two
3712/// UTF-16 code units (`u16`) of space.
3713///
3714/// When decoding to UTF-8 without replacement, the methods are guaranteed
3715/// not to return indicating that more output space is needed if the length
3716/// of the output buffer is at least the length returned by
3717/// [`max_utf8_buffer_length_without_replacement()`][2]. When decoding to UTF-8
3718/// with replacement, the length of the output buffer that guarantees the
3719/// methods not to return indicating that more output space is needed is given
3720/// by [`max_utf8_buffer_length()`][3]. When decoding to UTF-16 with
3721/// or without replacement, the length of the output buffer that guarantees
3722/// the methods not to return indicating that more output space is needed is
3723/// given by [`max_utf16_buffer_length()`][4].
3724///
3725/// The output written into `dst` is guaranteed to be valid UTF-8 or UTF-16,
3726/// and the output after each `decode_*` call is guaranteed to consist of
3727/// complete characters. (I.e. the code unit sequence for the last character is
3728/// guaranteed not to be split across output buffers.)
3729///
3730/// The boolean argument `last` indicates that the end of the stream is reached
3731/// when all the bytes in `src` have been consumed.
3732///
3733/// A `Decoder` object can be used to incrementally decode a byte stream.
3734///
3735/// During the processing of a single stream, the caller must call `decode_*`
3736/// zero or more times with `last` set to `false` and then call `decode_*` at
3737/// least once with `last` set to `true`. If `decode_*` returns `InputEmpty`,
3738/// the processing of the stream has ended. Otherwise, the caller must call
3739/// `decode_*` again with `last` set to `true` (or treat a `Malformed` result as
3740/// a fatal error).
3741///
3742/// Once the stream has ended, the `Decoder` object must not be used anymore.
3743/// That is, you need to create another one to process another stream.
3744///
3745/// When the decoder returns `OutputFull` or the decoder returns `Malformed` and
3746/// the caller does not wish to treat it as a fatal error, the input buffer
3747/// `src` may not have been completely consumed. In that case, the caller must
3748/// pass the unconsumed contents of `src` to `decode_*` again upon the next
3749/// call.
3750///
3751/// [1]: enum.DecoderResult.html
3752/// [2]: #method.max_utf8_buffer_length_without_replacement
3753/// [3]: #method.max_utf8_buffer_length
3754/// [4]: #method.max_utf16_buffer_length
3755///
3756/// # Infinite loops
3757///
3758/// When converting with a fixed-size output buffer whose size is too small to
3759/// accommodate one character of output, an infinite loop ensues. When
3760/// converting with a fixed-size output buffer, it generally makes sense to
3761/// make the buffer fairly large (e.g. couple of kilobytes).
3762pub struct Decoder {
3763 encoding: &'static Encoding,
3764 variant: VariantDecoder,
3765 life_cycle: DecoderLifeCycle,
3766}
3767
3768impl Decoder {
3769 fn new(enc: &'static Encoding, decoder: VariantDecoder, sniffing: BomHandling) -> Decoder {
3770 Decoder {
3771 encoding: enc,
3772 variant: decoder,
3773 life_cycle: match sniffing {
3774 BomHandling::Off => DecoderLifeCycle::Converting,
3775 BomHandling::Sniff => DecoderLifeCycle::AtStart,
3776 BomHandling::Remove => {
3777 if enc == UTF_8 {
3778 DecoderLifeCycle::AtUtf8Start
3779 } else if enc == UTF_16BE {
3780 DecoderLifeCycle::AtUtf16BeStart
3781 } else if enc == UTF_16LE {
3782 DecoderLifeCycle::AtUtf16LeStart
3783 } else {
3784 DecoderLifeCycle::Converting
3785 }
3786 }
3787 },
3788 }
3789 }
3790
3791 /// The `Encoding` this `Decoder` is for.
3792 ///
3793 /// BOM sniffing can change the return value of this method during the life
3794 /// of the decoder.
3795 ///
3796 /// Available via the C wrapper.
3797 #[inline]
3798 pub fn encoding(&self) -> &'static Encoding {
3799 self.encoding
3800 }
3801
3802 /// Query the worst-case UTF-8 output size _with replacement_.
3803 ///
3804 /// Returns the size of the output buffer in UTF-8 code units (`u8`)
3805 /// that will not overflow given the current state of the decoder and
3806 /// `byte_length` number of additional input bytes when decoding with
3807 /// errors handled by outputting a REPLACEMENT CHARACTER for each malformed
3808 /// sequence or `None` if `usize` would overflow.
3809 ///
3810 /// Available via the C wrapper.
3811 pub fn max_utf8_buffer_length(&self, byte_length: usize) -> Option<usize> {
3812 // Need to consider a) the decoder morphing due to the BOM and b) a partial
3813 // BOM getting pushed to the underlying decoder.
3814 match self.life_cycle {
3815 DecoderLifeCycle::Converting
3816 | DecoderLifeCycle::AtUtf8Start
3817 | DecoderLifeCycle::AtUtf16LeStart
3818 | DecoderLifeCycle::AtUtf16BeStart => {
3819 return self.variant.max_utf8_buffer_length(byte_length);
3820 }
3821 DecoderLifeCycle::AtStart => {
3822 if let Some(utf8_bom) = checked_add(3, byte_length.checked_mul(3))
3823 && let Some(utf16_bom) = checked_add(
3824 1,
3825 checked_mul(3, checked_div(byte_length.checked_add(1), 2)),
3826 )
3827 {
3828 let utf_bom = core::cmp::max(utf8_bom, utf16_bom);
3829 let encoding = self.encoding();
3830 if encoding == UTF_8 || encoding == UTF_16LE || encoding == UTF_16BE {
3831 // No need to consider the internal state of the underlying decoder,
3832 // because it is at start, because no data has reached it yet.
3833 return Some(utf_bom);
3834 } else if let Some(non_bom) = self.variant.max_utf8_buffer_length(byte_length) {
3835 return Some(core::cmp::max(utf_bom, non_bom));
3836 }
3837 }
3838 }
3839 DecoderLifeCycle::SeenUtf8First | DecoderLifeCycle::SeenUtf8Second => {
3840 // Add two bytes even when only one byte has been seen,
3841 // because the one byte can become a lead byte in multibyte
3842 // decoders, but only after the decoder has been queried
3843 // for max length, so the decoder's own logic for adding
3844 // one for a pending lead cannot work.
3845 if let Some(sum) = byte_length.checked_add(2)
3846 && let Some(utf8_bom) = checked_add(3, sum.checked_mul(3))
3847 {
3848 if self.encoding() == UTF_8 {
3849 // No need to consider the internal state of the underlying decoder,
3850 // because it is at start, because no data has reached it yet.
3851 return Some(utf8_bom);
3852 } else if let Some(non_bom) = self.variant.max_utf8_buffer_length(sum) {
3853 return Some(core::cmp::max(utf8_bom, non_bom));
3854 }
3855 }
3856 }
3857 DecoderLifeCycle::ConvertingWithPendingBB => {
3858 if let Some(sum) = byte_length.checked_add(2) {
3859 return self.variant.max_utf8_buffer_length(sum);
3860 }
3861 }
3862 DecoderLifeCycle::SeenUtf16LeFirst | DecoderLifeCycle::SeenUtf16BeFirst => {
3863 // Add two bytes even when only one byte has been seen,
3864 // because the one byte can become a lead byte in multibyte
3865 // decoders, but only after the decoder has been queried
3866 // for max length, so the decoder's own logic for adding
3867 // one for a pending lead cannot work.
3868 if let Some(sum) = byte_length.checked_add(2)
3869 && let Some(utf16_bom) =
3870 checked_add(1, checked_mul(3, checked_div(sum.checked_add(1), 2)))
3871 {
3872 let encoding = self.encoding();
3873 if encoding == UTF_16LE || encoding == UTF_16BE {
3874 // No need to consider the internal state of the underlying decoder,
3875 // because it is at start, because no data has reached it yet.
3876 return Some(utf16_bom);
3877 } else if let Some(non_bom) = self.variant.max_utf8_buffer_length(sum) {
3878 return Some(core::cmp::max(utf16_bom, non_bom));
3879 }
3880 }
3881 }
3882 DecoderLifeCycle::Finished => panic!("Must not use a decoder that has finished."),
3883 }
3884 None
3885 }
3886
3887 /// Query the worst-case UTF-8 output size _without replacement_.
3888 ///
3889 /// Returns the size of the output buffer in UTF-8 code units (`u8`)
3890 /// that will not overflow given the current state of the decoder and
3891 /// `byte_length` number of additional input bytes when decoding without
3892 /// replacement error handling or `None` if `usize` would overflow.
3893 ///
3894 /// Note that this value may be too small for the `_with_replacement` case.
3895 /// Use `max_utf8_buffer_length()` for that case.
3896 ///
3897 /// Available via the C wrapper.
3898 pub fn max_utf8_buffer_length_without_replacement(&self, byte_length: usize) -> Option<usize> {
3899 // Need to consider a) the decoder morphing due to the BOM and b) a partial
3900 // BOM getting pushed to the underlying decoder.
3901 match self.life_cycle {
3902 DecoderLifeCycle::Converting
3903 | DecoderLifeCycle::AtUtf8Start
3904 | DecoderLifeCycle::AtUtf16LeStart
3905 | DecoderLifeCycle::AtUtf16BeStart => {
3906 return self
3907 .variant
3908 .max_utf8_buffer_length_without_replacement(byte_length);
3909 }
3910 DecoderLifeCycle::AtStart => {
3911 if let Some(utf8_bom) = byte_length.checked_add(3)
3912 && let Some(utf16_bom) = checked_add(
3913 1,
3914 checked_mul(3, checked_div(byte_length.checked_add(1), 2)),
3915 )
3916 {
3917 let utf_bom = core::cmp::max(utf8_bom, utf16_bom);
3918 let encoding = self.encoding();
3919 if encoding == UTF_8 || encoding == UTF_16LE || encoding == UTF_16BE {
3920 // No need to consider the internal state of the underlying decoder,
3921 // because it is at start, because no data has reached it yet.
3922 return Some(utf_bom);
3923 } else if let Some(non_bom) = self
3924 .variant
3925 .max_utf8_buffer_length_without_replacement(byte_length)
3926 {
3927 return Some(core::cmp::max(utf_bom, non_bom));
3928 }
3929 }
3930 }
3931 DecoderLifeCycle::SeenUtf8First | DecoderLifeCycle::SeenUtf8Second => {
3932 // Add two bytes even when only one byte has been seen,
3933 // because the one byte can become a lead byte in multibyte
3934 // decoders, but only after the decoder has been queried
3935 // for max length, so the decoder's own logic for adding
3936 // one for a pending lead cannot work.
3937 if let Some(sum) = byte_length.checked_add(2)
3938 && let Some(utf8_bom) = sum.checked_add(3)
3939 {
3940 if self.encoding() == UTF_8 {
3941 // No need to consider the internal state of the underlying decoder,
3942 // because it is at start, because no data has reached it yet.
3943 return Some(utf8_bom);
3944 } else if let Some(non_bom) =
3945 self.variant.max_utf8_buffer_length_without_replacement(sum)
3946 {
3947 return Some(core::cmp::max(utf8_bom, non_bom));
3948 }
3949 }
3950 }
3951 DecoderLifeCycle::ConvertingWithPendingBB => {
3952 if let Some(sum) = byte_length.checked_add(2) {
3953 return self.variant.max_utf8_buffer_length_without_replacement(sum);
3954 }
3955 }
3956 DecoderLifeCycle::SeenUtf16LeFirst | DecoderLifeCycle::SeenUtf16BeFirst => {
3957 // Add two bytes even when only one byte has been seen,
3958 // because the one byte can become a lead byte in multibyte
3959 // decoders, but only after the decoder has been queried
3960 // for max length, so the decoder's own logic for adding
3961 // one for a pending lead cannot work.
3962 if let Some(sum) = byte_length.checked_add(2)
3963 && let Some(utf16_bom) =
3964 checked_add(1, checked_mul(3, checked_div(sum.checked_add(1), 2)))
3965 {
3966 let encoding = self.encoding();
3967 if encoding == UTF_16LE || encoding == UTF_16BE {
3968 // No need to consider the internal state of the underlying decoder,
3969 // because it is at start, because no data has reached it yet.
3970 return Some(utf16_bom);
3971 } else if let Some(non_bom) =
3972 self.variant.max_utf8_buffer_length_without_replacement(sum)
3973 {
3974 return Some(core::cmp::max(utf16_bom, non_bom));
3975 }
3976 }
3977 }
3978 DecoderLifeCycle::Finished => panic!("Must not use a decoder that has finished."),
3979 }
3980 None
3981 }
3982
3983 /// Incrementally decode a byte stream into UTF-8 with malformed sequences
3984 /// replaced with the REPLACEMENT CHARACTER.
3985 ///
3986 /// See the documentation of the struct for documentation for `decode_*`
3987 /// methods collectively.
3988 ///
3989 /// Available via the C wrapper.
3990 pub fn decode_to_utf8(
3991 &mut self,
3992 src: &[u8],
3993 dst: &mut [u8],
3994 last: bool,
3995 ) -> (CoderResult, usize, usize, bool) {
3996 let mut had_errors = false;
3997 let mut total_read = 0usize;
3998 let mut total_written = 0usize;
3999 loop {
4000 let (result, read, written) = self.decode_to_utf8_without_replacement(
4001 &src[total_read..],
4002 &mut dst[total_written..],
4003 last,
4004 );
4005 total_read += read;
4006 total_written += written;
4007 match result {
4008 DecoderResult::InputEmpty => {
4009 return (
4010 CoderResult::InputEmpty,
4011 total_read,
4012 total_written,
4013 had_errors,
4014 );
4015 }
4016 DecoderResult::OutputFull => {
4017 return (
4018 CoderResult::OutputFull,
4019 total_read,
4020 total_written,
4021 had_errors,
4022 );
4023 }
4024 DecoderResult::Malformed(_, _) => {
4025 had_errors = true;
4026 // There should always be space for the U+FFFD, because
4027 // otherwise we'd have gotten OutputFull already.
4028 // XXX: is the above comment actually true for UTF-8 itself?
4029 // TODO: Consider having fewer bound checks here.
4030 dst[total_written] = 0xEFu8;
4031 total_written += 1;
4032 dst[total_written] = 0xBFu8;
4033 total_written += 1;
4034 dst[total_written] = 0xBDu8;
4035 total_written += 1;
4036 }
4037 }
4038 }
4039 }
4040
4041 /// Incrementally decode a byte stream into UTF-8 with malformed sequences
4042 /// replaced with the REPLACEMENT CHARACTER with type system signaling
4043 /// of UTF-8 validity.
4044 ///
4045 /// This methods calls `decode_to_utf8` and then zeroes enough subsequent
4046 /// bytes to maintain the invariant of `str`.
4047 ///
4048 /// See the documentation of the struct for documentation for `decode_*`
4049 /// methods collectively.
4050 ///
4051 /// Available to Rust only.
4052 pub fn decode_to_str(
4053 &mut self,
4054 src: &[u8],
4055 dst: &mut str,
4056 last: bool,
4057 ) -> (CoderResult, usize, usize, bool) {
4058 // SAFETY: We trust that `decode_to_utf8` writes
4059 // valid UTF-8. To make the part of the slice after what was reported
4060 // as logically written by that funtion, we use knowledge of the internals
4061 // to overwrite trailing garbage that may have been written. Then we also
4062 // overwrite a possible partial UTF-8 byte sequence after that. Then the
4063 // rest must be valid on the assumption that `dst` was valid to begin with.
4064 // In case of a panic, the `ScopeGuard` zeros the whole slice, which ensures
4065 // it's valid UTF-8 in an use-after-panic scenario when unwinding is enabled.
4066 // (Relevant only if there's a panic due to a crate-internal bug. Panics
4067 // arising from misuse of the public API don't need this guard and end up
4068 // zeroing the slice unnecessarily.)
4069 let mut bytes = scopeguard::guard(unsafe { dst.as_bytes_mut() }, |bytes| {
4070 bytes.iter_mut().for_each(|b| *b = 0)
4071 });
4072 let (result, read, written, replaced) = self.decode_to_utf8(src, &mut bytes, last);
4073 let len = bytes.len();
4074 let mut trail = written;
4075 // Non-UTF-8 ASCII-compatible decoders may write up to `MAX_STRIDE_SIZE`
4076 // bytes of trailing garbage. No need to optimize non-ASCII-compatible
4077 // encodings to avoid overwriting here.
4078 if self.encoding != UTF_8 {
4079 let max = core::cmp::min(len, trail + ascii::MAX_STRIDE_SIZE);
4080 while trail < max {
4081 bytes[trail] = 0;
4082 trail += 1;
4083 }
4084 }
4085 while trail < len && ((bytes[trail] & 0xC0) == 0x80) {
4086 bytes[trail] = 0;
4087 trail += 1;
4088 }
4089 // Defuse the zeroing guard.
4090 let _ = scopeguard::ScopeGuard::<&mut [u8], _>::into_inner(bytes);
4091 (result, read, written, replaced)
4092 }
4093
4094 /// Incrementally decode a byte stream into UTF-8 with malformed sequences
4095 /// replaced with the REPLACEMENT CHARACTER using a `String` receiver.
4096 ///
4097 /// Like the others, this method follows the logic that the output buffer is
4098 /// caller-allocated. This method treats the capacity of the `String` as
4099 /// the output limit. That is, this method guarantees not to cause a
4100 /// reallocation of the backing buffer of `String`.
4101 ///
4102 /// The return value is a tuple that contains the `DecoderResult`, the
4103 /// number of bytes read and a boolean indicating whether replacements
4104 /// were done. The number of bytes written is signaled via the length of
4105 /// the `String` changing.
4106 ///
4107 /// See the documentation of the struct for documentation for `decode_*`
4108 /// methods collectively.
4109 ///
4110 /// Available to Rust only and only with the `alloc` feature enabled (enabled
4111 /// by default).
4112 #[cfg(feature = "alloc")]
4113 pub fn decode_to_string(
4114 &mut self,
4115 src: &[u8],
4116 dst: &mut String,
4117 last: bool,
4118 ) -> (CoderResult, usize, bool) {
4119 // SAFETY: Writing to `String` by using it as `Vec` is safe
4120 // iff the result is valid UTF-8 afterwards. We trust
4121 // `decode_to_utf8` below to write valid UTF-8 and
4122 // we trust that we update the length correctly below.
4123 // Furthermore, the length update is the last operation, so
4124 // if an earlier step panics, the logically exposed part of the
4125 // `Vec`/`String` remains unchanged.
4126 let vec = unsafe { dst.as_mut_vec() };
4127 let old_len = vec.len();
4128 let spare_capacity = minimally_init(vec.spare_capacity_mut());
4129 let (result, read, written, replaced) = self.decode_to_utf8(src, spare_capacity, last);
4130 debug_assert!(written <= spare_capacity.len());
4131 let new_len = old_len + written;
4132 assert!(new_len <= vec.capacity());
4133 // SAFETY: We trust that `decode_to_utf8` wrote valid UTF-8
4134 // to `spare_capacity[..written]`. Also, regarding the information
4135 // disclosure risk of `minimally_init`, this also means trusting
4136 // that every byte of `spare_capacity[..written]` got overwritten.
4137 // (We're no worse off than before regarding
4138 // `spare_capacity[written..]`) which remains not logically exposed.)
4139 // We (non-debug )asserted immediately above that `new_len` conforms
4140 // to the invariant that it must not exceed `vec.capacity()`.
4141 unsafe {
4142 vec.set_len(new_len);
4143 }
4144 (result, read, replaced)
4145 }
4146
4147 public_decode_function!(/// Incrementally decode a byte stream into UTF-8
4148 /// _without replacement_.
4149 ///
4150 /// See the documentation of the struct for
4151 /// documentation for `decode_*` methods
4152 /// collectively.
4153 ///
4154 /// Available via the C wrapper.
4155 ,
4156 decode_to_utf8_without_replacement,
4157 decode_to_utf8_raw,
4158 decode_to_utf8_checking_end,
4159 decode_to_utf8_after_one_potential_bom_byte,
4160 decode_to_utf8_after_two_potential_bom_bytes,
4161 decode_to_utf8_checking_end_with_offset,
4162 u8);
4163
4164 /// Incrementally decode a byte stream into UTF-8 with type system signaling
4165 /// of UTF-8 validity.
4166 ///
4167 /// This methods calls `decode_to_utf8_without_replacement` and then zeroes enough subsequent
4168 /// bytes to maintain the invariant of `str`.
4169 ///
4170 /// See the documentation of the struct for documentation for `decode_*`
4171 /// methods collectively.
4172 ///
4173 /// Available to Rust only.
4174 pub fn decode_to_str_without_replacement(
4175 &mut self,
4176 src: &[u8],
4177 dst: &mut str,
4178 last: bool,
4179 ) -> (DecoderResult, usize, usize) {
4180 // SAFETY: We trust that `decode_to_utf8_without_replacement` writes
4181 // valid UTF-8. To make the part of the slice after what was reported
4182 // as logically written by that funtion, we use knowledge of the internals
4183 // to overwrite trailing garbage that may have been written. Then we also
4184 // overwrite a possible partial UTF-8 byte sequence after that. Then the
4185 // rest must be valid on the assumption that `dst` was valid to begin with.
4186 // In case of a panic, the `ScopeGuard` zeros the whole slice, which ensures
4187 // it's valid UTF-8 in an use-after-panic scenario when unwinding is enabled.
4188 // (Relevant only if there's a panic due to a crate-internal bug. Panics
4189 // arising from misuse of the public API don't need this guard and end up
4190 // zeroing the slice unnecessarily.)
4191 let mut bytes = scopeguard::guard(unsafe { dst.as_bytes_mut() }, |bytes| {
4192 bytes.iter_mut().for_each(|b| *b = 0)
4193 });
4194 let (result, read, written) =
4195 self.decode_to_utf8_without_replacement(src, &mut bytes, last);
4196 let len = bytes.len();
4197 let mut trail = written;
4198 // Non-UTF-8 ASCII-compatible decoders may write up to `MAX_STRIDE_SIZE`
4199 // bytes of trailing garbage. No need to optimize non-ASCII-compatible
4200 // encodings to avoid overwriting here.
4201 if self.encoding != UTF_8 {
4202 let max = core::cmp::min(len, trail + ascii::MAX_STRIDE_SIZE);
4203 while trail < max {
4204 bytes[trail] = 0;
4205 trail += 1;
4206 }
4207 }
4208 while trail < len && ((bytes[trail] & 0xC0) == 0x80) {
4209 bytes[trail] = 0;
4210 trail += 1;
4211 }
4212 // Defuse the zeroing guard.
4213 let _ = scopeguard::ScopeGuard::<&mut [u8], _>::into_inner(bytes);
4214 (result, read, written)
4215 }
4216
4217 /// Incrementally decode a byte stream into UTF-8 using a `String` receiver.
4218 ///
4219 /// Like the others, this method follows the logic that the output buffer is
4220 /// caller-allocated. This method treats the capacity of the `String` as
4221 /// the output limit. That is, this method guarantees not to cause a
4222 /// reallocation of the backing buffer of `String`.
4223 ///
4224 /// The return value is a pair that contains the `DecoderResult` and the
4225 /// number of bytes read. The number of bytes written is signaled via
4226 /// the length of the `String` changing.
4227 ///
4228 /// See the documentation of the struct for documentation for `decode_*`
4229 /// methods collectively.
4230 ///
4231 /// Available to Rust only and only with the `alloc` feature enabled (enabled
4232 /// by default).
4233 #[cfg(feature = "alloc")]
4234 pub fn decode_to_string_without_replacement(
4235 &mut self,
4236 src: &[u8],
4237 dst: &mut String,
4238 last: bool,
4239 ) -> (DecoderResult, usize) {
4240 // SAFETY: Writing to `String` by using it as `Vec` is safe
4241 // iff the result is valid UTF-8 afterwards. We trust
4242 // `decode_to_utf8_without_replacement` below to write valid UTF-8 and
4243 // we trust that we update the length correctly below.
4244 // Furthermore, the length update is the last operation, so
4245 // if an earlier step panics, the logically exposed part of the
4246 // `Vec`/`String` remains unchanged.
4247 let vec = unsafe { dst.as_mut_vec() };
4248 let old_len = vec.len();
4249 let spare_capacity = minimally_init(vec.spare_capacity_mut());
4250 let (result, read, written) =
4251 self.decode_to_utf8_without_replacement(src, spare_capacity, last);
4252 debug_assert!(written <= spare_capacity.len());
4253 let new_len = old_len + written;
4254 assert!(new_len <= vec.capacity());
4255 // SAFETY: We trust that `decode_to_utf8_without_replacement` wrote valid UTF-8
4256 // to `spare_capacity[..written]`. Also, regarding the information
4257 // disclosure risk of `minimally_init`, this also means trusting
4258 // that every byte of `spare_capacity[..written]` got overwritten.
4259 // (We're no worse off than before regarding
4260 // `spare_capacity[written..]`) which remains not logically exposed.)
4261 // We (non-debug )asserted immediately above that `new_len` conforms
4262 // to the invariant that it must not exceed `vec.capacity()`.
4263 unsafe {
4264 vec.set_len(new_len);
4265 }
4266 (result, read)
4267 }
4268
4269 /// Query the worst-case UTF-16 output size (with or without replacement).
4270 ///
4271 /// Returns the size of the output buffer in UTF-16 code units (`u16`)
4272 /// that will not overflow given the current state of the decoder and
4273 /// `byte_length` number of additional input bytes or `None` if `usize`
4274 /// would overflow.
4275 ///
4276 /// Since the REPLACEMENT CHARACTER fits into one UTF-16 code unit, the
4277 /// return value of this method applies also in the
4278 /// `_without_replacement` case.
4279 ///
4280 /// Available via the C wrapper.
4281 pub fn max_utf16_buffer_length(&self, byte_length: usize) -> Option<usize> {
4282 // Need to consider a) the decoder morphing due to the BOM and b) a partial
4283 // BOM getting pushed to the underlying decoder.
4284 match self.life_cycle {
4285 DecoderLifeCycle::Converting
4286 | DecoderLifeCycle::AtUtf8Start
4287 | DecoderLifeCycle::AtUtf16LeStart
4288 | DecoderLifeCycle::AtUtf16BeStart => {
4289 return self.variant.max_utf16_buffer_length(byte_length);
4290 }
4291 DecoderLifeCycle::AtStart => {
4292 if let Some(utf8_bom) = byte_length.checked_add(1)
4293 && let Some(utf16_bom) =
4294 checked_add(1, checked_div(byte_length.checked_add(1), 2))
4295 {
4296 let utf_bom = core::cmp::max(utf8_bom, utf16_bom);
4297 let encoding = self.encoding();
4298 if encoding == UTF_8 || encoding == UTF_16LE || encoding == UTF_16BE {
4299 // No need to consider the internal state of the underlying decoder,
4300 // because it is at start, because no data has reached it yet.
4301 return Some(utf_bom);
4302 } else if let Some(non_bom) = self.variant.max_utf16_buffer_length(byte_length)
4303 {
4304 return Some(core::cmp::max(utf_bom, non_bom));
4305 }
4306 }
4307 }
4308 DecoderLifeCycle::SeenUtf8First | DecoderLifeCycle::SeenUtf8Second => {
4309 // Add two bytes even when only one byte has been seen,
4310 // because the one byte can become a lead byte in multibyte
4311 // decoders, but only after the decoder has been queried
4312 // for max length, so the decoder's own logic for adding
4313 // one for a pending lead cannot work.
4314 if let Some(sum) = byte_length.checked_add(2)
4315 && let Some(utf8_bom) = sum.checked_add(1)
4316 {
4317 if self.encoding() == UTF_8 {
4318 // No need to consider the internal state of the underlying decoder,
4319 // because it is at start, because no data has reached it yet.
4320 return Some(utf8_bom);
4321 } else if let Some(non_bom) = self.variant.max_utf16_buffer_length(sum) {
4322 return Some(core::cmp::max(utf8_bom, non_bom));
4323 }
4324 }
4325 }
4326 DecoderLifeCycle::ConvertingWithPendingBB => {
4327 if let Some(sum) = byte_length.checked_add(2) {
4328 return self.variant.max_utf16_buffer_length(sum);
4329 }
4330 }
4331 DecoderLifeCycle::SeenUtf16LeFirst | DecoderLifeCycle::SeenUtf16BeFirst => {
4332 // Add two bytes even when only one byte has been seen,
4333 // because the one byte can become a lead byte in multibyte
4334 // decoders, but only after the decoder has been queried
4335 // for max length, so the decoder's own logic for adding
4336 // one for a pending lead cannot work.
4337 if let Some(sum) = byte_length.checked_add(2)
4338 && let Some(utf16_bom) = checked_add(1, checked_div(sum.checked_add(1), 2))
4339 {
4340 let encoding = self.encoding();
4341 if encoding == UTF_16LE || encoding == UTF_16BE {
4342 // No need to consider the internal state of the underlying decoder,
4343 // because it is at start, because no data has reached it yet.
4344 return Some(utf16_bom);
4345 } else if let Some(non_bom) = self.variant.max_utf16_buffer_length(sum) {
4346 return Some(core::cmp::max(utf16_bom, non_bom));
4347 }
4348 }
4349 }
4350 DecoderLifeCycle::Finished => panic!("Must not use a decoder that has finished."),
4351 }
4352 None
4353 }
4354
4355 /// Incrementally decode a byte stream into UTF-16 with malformed sequences
4356 /// replaced with the REPLACEMENT CHARACTER.
4357 ///
4358 /// See the documentation of the struct for documentation for `decode_*`
4359 /// methods collectively.
4360 ///
4361 /// Available via the C wrapper.
4362 pub fn decode_to_utf16(
4363 &mut self,
4364 src: &[u8],
4365 dst: &mut [u16],
4366 last: bool,
4367 ) -> (CoderResult, usize, usize, bool) {
4368 let mut had_errors = false;
4369 let mut total_read = 0usize;
4370 let mut total_written = 0usize;
4371 loop {
4372 let (result, read, written) = self.decode_to_utf16_without_replacement(
4373 &src[total_read..],
4374 &mut dst[total_written..],
4375 last,
4376 );
4377 total_read += read;
4378 total_written += written;
4379 match result {
4380 DecoderResult::InputEmpty => {
4381 return (
4382 CoderResult::InputEmpty,
4383 total_read,
4384 total_written,
4385 had_errors,
4386 );
4387 }
4388 DecoderResult::OutputFull => {
4389 return (
4390 CoderResult::OutputFull,
4391 total_read,
4392 total_written,
4393 had_errors,
4394 );
4395 }
4396 DecoderResult::Malformed(_, _) => {
4397 had_errors = true;
4398 // There should always be space for the U+FFFD, because
4399 // otherwise we'd have gotten OutputFull already.
4400 dst[total_written] = 0xFFFD;
4401 total_written += 1;
4402 }
4403 }
4404 }
4405 }
4406
4407 public_decode_function!(/// Incrementally decode a byte stream into UTF-16
4408 /// _without replacement_.
4409 ///
4410 /// See the documentation of the struct for
4411 /// documentation for `decode_*` methods
4412 /// collectively.
4413 ///
4414 /// Available via the C wrapper.
4415 ,
4416 decode_to_utf16_without_replacement,
4417 decode_to_utf16_raw,
4418 decode_to_utf16_checking_end,
4419 decode_to_utf16_after_one_potential_bom_byte,
4420 decode_to_utf16_after_two_potential_bom_bytes,
4421 decode_to_utf16_checking_end_with_offset,
4422 u16);
4423
4424 /// Checks for compatibility with storing Unicode scalar values as unsigned
4425 /// bytes taking into account the state of the decoder.
4426 ///
4427 /// Returns `None` if the decoder is not in a neutral state, including waiting
4428 /// for the BOM, or if the encoding is never Latin1-byte-compatible.
4429 ///
4430 /// Otherwise returns the index of the first byte whose unsigned value doesn't
4431 /// directly correspond to the decoded Unicode scalar value, or the length
4432 /// of the input if all bytes in the input decode directly to scalar values
4433 /// corresponding to the unsigned byte values.
4434 ///
4435 /// Does not change the state of the decoder.
4436 ///
4437 /// Do not use this unless you are supporting SpiderMonkey/V8-style string
4438 /// storage optimizations.
4439 ///
4440 /// Available via the C wrapper.
4441 pub fn latin1_byte_compatible_up_to(&self, bytes: &[u8]) -> Option<usize> {
4442 match self.life_cycle {
4443 DecoderLifeCycle::Converting => self.variant.latin1_byte_compatible_up_to(bytes),
4444 DecoderLifeCycle::Finished => panic!("Must not use a decoder that has finished."),
4445 _ => None,
4446 }
4447 }
4448}
4449
4450impl core::fmt::Debug for Decoder {
4451 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
4452 f.debug_struct("Decoder")
4453 .field("encoding", self.encoding)
4454 .field("life_cycle", &self.life_cycle)
4455 .finish_non_exhaustive()
4456 }
4457}
4458
4459/// Result of a (potentially partial) encode operation without replacement.
4460#[must_use]
4461#[derive(Debug, PartialEq, Eq)]
4462pub enum EncoderResult {
4463 /// The input was exhausted.
4464 ///
4465 /// If this result was returned from a call where `last` was `true`, the
4466 /// decoding process has completed. Otherwise, the caller should call a
4467 /// decode method again with more input.
4468 InputEmpty,
4469
4470 /// The encoder cannot produce another unit of output, because the output
4471 /// buffer does not have enough space left.
4472 ///
4473 /// The caller must provide more output space upon the next call and re-push
4474 /// the remaining input to the decoder.
4475 OutputFull,
4476
4477 /// The encoder encountered an unmappable character.
4478 ///
4479 /// The caller must either treat this as a fatal error or must append
4480 /// a placeholder to the output and then re-push the remaining input to the
4481 /// encoder.
4482 Unmappable(char),
4483}
4484
4485impl EncoderResult {
4486 fn unmappable_from_bmp(bmp: u16) -> EncoderResult {
4487 EncoderResult::Unmappable(::core::char::from_u32(u32::from(bmp)).unwrap())
4488 }
4489}
4490
4491/// A converter that encodes a Unicode stream into bytes according to a
4492/// character encoding in a streaming (incremental) manner.
4493///
4494/// The various `encode_*` methods take an input buffer (`src`) and an output
4495/// buffer `dst` both of which are caller-allocated. There are variants for
4496/// both UTF-8 and UTF-16 input buffers.
4497///
4498/// An `encode_*` method encode characters from `src` into bytes characters
4499/// stored into `dst` until one of the following three things happens:
4500///
4501/// 1. An unmappable character is encountered (`*_without_replacement` variants
4502/// only).
4503///
4504/// 2. The output buffer has been filled so near capacity that the decoder
4505/// cannot be sure that processing an additional character of input wouldn't
4506/// cause so much output that the output buffer would overflow.
4507///
4508/// 3. All the input characters have been processed.
4509///
4510/// The `encode_*` method then returns tuple of a status indicating which one
4511/// of the three reasons to return happened, how many input code units (`u8`
4512/// when encoding from UTF-8 and `u16` when encoding from UTF-16) were read,
4513/// how many output bytes were written (except when encoding into `Vec<u8>`,
4514/// whose length change indicates this), and in the case of the variants that
4515/// perform replacement, a boolean indicating whether an unmappable
4516/// character was replaced with a numeric character reference during the call.
4517///
4518/// The number of bytes "written" is what's logically written. Garbage may be
4519/// written in the output buffer beyond the point logically written to.
4520///
4521/// In the case of the methods whose name ends with
4522/// `*_without_replacement`, the status is an [`EncoderResult`][1] enumeration
4523/// (possibilities `Unmappable`, `OutputFull` and `InputEmpty` corresponding to
4524/// the three cases listed above).
4525///
4526/// In the case of methods whose name does not end with
4527/// `*_without_replacement`, unmappable characters are automatically replaced
4528/// with the corresponding numeric character references and unmappable
4529/// characters do not cause the methods to return early.
4530///
4531/// When encoding from UTF-8 without replacement, the methods are guaranteed
4532/// not to return indicating that more output space is needed if the length
4533/// of the output buffer is at least the length returned by
4534/// [`max_buffer_length_from_utf8_without_replacement()`][2]. When encoding from
4535/// UTF-8 with replacement, the length of the output buffer that guarantees the
4536/// methods not to return indicating that more output space is needed in the
4537/// absence of unmappable characters is given by
4538/// [`max_buffer_length_from_utf8_if_no_unmappables()`][3]. When encoding from
4539/// UTF-16 without replacement, the methods are guaranteed not to return
4540/// indicating that more output space is needed if the length of the output
4541/// buffer is at least the length returned by
4542/// [`max_buffer_length_from_utf16_without_replacement()`][4]. When encoding
4543/// from UTF-16 with replacement, the the length of the output buffer that
4544/// guarantees the methods not to return indicating that more output space is
4545/// needed in the absence of unmappable characters is given by
4546/// [`max_buffer_length_from_utf16_if_no_unmappables()`][5].
4547/// When encoding with replacement, applications are not expected to size the
4548/// buffer for the worst case ahead of time but to resize the buffer if there
4549/// are unmappable characters. This is why max length queries are only available
4550/// for the case where there are no unmappable characters.
4551///
4552/// When encoding from UTF-8, each `src` buffer _must_ be valid UTF-8. (When
4553/// calling from Rust, the type system takes care of this.) When encoding from
4554/// UTF-16, unpaired surrogates in the input are treated as U+FFFD REPLACEMENT
4555/// CHARACTERS. Therefore, in order for astral characters not to turn into a
4556/// pair of REPLACEMENT CHARACTERS, the caller must ensure that surrogate pairs
4557/// are not split across input buffer boundaries.
4558///
4559/// After an `encode_*` call returns, the output produced so far, taken as a
4560/// whole from the start of the stream, is guaranteed to consist of a valid
4561/// byte sequence in the target encoding. (I.e. the code unit sequence for a
4562/// character is guaranteed not to be split across output buffers. However, due
4563/// to the stateful nature of ISO-2022-JP, the stream needs to be considered
4564/// from the start for it to be valid. For other encodings, the validity holds
4565/// on a per-output buffer basis.)
4566///
4567/// The boolean argument `last` indicates that the end of the stream is reached
4568/// when all the characters in `src` have been consumed. This argument is needed
4569/// for ISO-2022-JP and is ignored for other encodings.
4570///
4571/// An `Encoder` object can be used to incrementally encode a byte stream.
4572///
4573/// During the processing of a single stream, the caller must call `encode_*`
4574/// zero or more times with `last` set to `false` and then call `encode_*` at
4575/// least once with `last` set to `true`. If `encode_*` returns `InputEmpty`,
4576/// the processing of the stream has ended. Otherwise, the caller must call
4577/// `encode_*` again with `last` set to `true` (or treat an `Unmappable` result
4578/// as a fatal error).
4579///
4580/// Once the stream has ended, the `Encoder` object must not be used anymore.
4581/// That is, you need to create another one to process another stream.
4582///
4583/// When the encoder returns `OutputFull` or the encoder returns `Unmappable`
4584/// and the caller does not wish to treat it as a fatal error, the input buffer
4585/// `src` may not have been completely consumed. In that case, the caller must
4586/// pass the unconsumed contents of `src` to `encode_*` again upon the next
4587/// call.
4588///
4589/// [1]: enum.EncoderResult.html
4590/// [2]: #method.max_buffer_length_from_utf8_without_replacement
4591/// [3]: #method.max_buffer_length_from_utf8_if_no_unmappables
4592/// [4]: #method.max_buffer_length_from_utf16_without_replacement
4593/// [5]: #method.max_buffer_length_from_utf16_if_no_unmappables
4594///
4595/// # Infinite loops
4596///
4597/// When converting with a fixed-size output buffer whose size is too small to
4598/// accommodate one character or (when applicable) one numeric character
4599/// reference of output, an infinite loop ensues. When converting with a
4600/// fixed-size output buffer, it generally makes sense to make the buffer
4601/// fairly large (e.g. couple of kilobytes).
4602pub struct Encoder {
4603 encoding: &'static Encoding,
4604 variant: VariantEncoder,
4605}
4606
4607impl Encoder {
4608 fn new(enc: &'static Encoding, encoder: VariantEncoder) -> Encoder {
4609 Encoder {
4610 encoding: enc,
4611 variant: encoder,
4612 }
4613 }
4614
4615 /// The `Encoding` this `Encoder` is for.
4616 #[inline]
4617 pub fn encoding(&self) -> &'static Encoding {
4618 self.encoding
4619 }
4620
4621 /// Returns `true` if this is an ISO-2022-JP encoder that's not in the
4622 /// ASCII state and `false` otherwise.
4623 #[inline]
4624 pub fn has_pending_state(&self) -> bool {
4625 self.variant.has_pending_state()
4626 }
4627
4628 /// Query the worst-case output size when encoding from UTF-8 with
4629 /// replacement.
4630 ///
4631 /// Returns the size of the output buffer in bytes that will not overflow
4632 /// given the current state of the encoder and `byte_length` number of
4633 /// additional input code units if there are no unmappable characters in
4634 /// the input or `None` if `usize` would overflow.
4635 ///
4636 /// Available via the C wrapper.
4637 pub fn max_buffer_length_from_utf8_if_no_unmappables(
4638 &self,
4639 byte_length: usize,
4640 ) -> Option<usize> {
4641 checked_add(
4642 if self.encoding().can_encode_everything() {
4643 0
4644 } else {
4645 NCR_EXTRA
4646 },
4647 self.max_buffer_length_from_utf8_without_replacement(byte_length),
4648 )
4649 }
4650
4651 /// Query the worst-case output size when encoding from UTF-8 without
4652 /// replacement.
4653 ///
4654 /// Returns the size of the output buffer in bytes that will not overflow
4655 /// given the current state of the encoder and `byte_length` number of
4656 /// additional input code units or `None` if `usize` would overflow.
4657 ///
4658 /// Available via the C wrapper.
4659 pub fn max_buffer_length_from_utf8_without_replacement(
4660 &self,
4661 byte_length: usize,
4662 ) -> Option<usize> {
4663 self.variant
4664 .max_buffer_length_from_utf8_without_replacement(byte_length)
4665 }
4666
4667 /// Incrementally encode into byte stream from UTF-8 with unmappable
4668 /// characters replaced with HTML (decimal) numeric character references.
4669 ///
4670 /// See the documentation of the struct for documentation for `encode_*`
4671 /// methods collectively.
4672 ///
4673 /// Available via the C wrapper.
4674 pub fn encode_from_utf8(
4675 &mut self,
4676 src: &str,
4677 dst: &mut [u8],
4678 last: bool,
4679 ) -> (CoderResult, usize, usize, bool) {
4680 let dst_len = dst.len();
4681 let effective_dst_len = if self.encoding().can_encode_everything() {
4682 dst_len
4683 } else {
4684 if dst_len < NCR_EXTRA {
4685 if src.is_empty() && !(last && self.has_pending_state()) {
4686 return (CoderResult::InputEmpty, 0, 0, false);
4687 }
4688 return (CoderResult::OutputFull, 0, 0, false);
4689 }
4690 dst_len - NCR_EXTRA
4691 };
4692 let mut had_unmappables = false;
4693 let mut total_read = 0usize;
4694 let mut total_written = 0usize;
4695 loop {
4696 let (result, read, written) = self.encode_from_utf8_without_replacement(
4697 &src[total_read..],
4698 &mut dst[total_written..effective_dst_len],
4699 last,
4700 );
4701 total_read += read;
4702 total_written += written;
4703 match result {
4704 EncoderResult::InputEmpty => {
4705 return (
4706 CoderResult::InputEmpty,
4707 total_read,
4708 total_written,
4709 had_unmappables,
4710 );
4711 }
4712 EncoderResult::OutputFull => {
4713 return (
4714 CoderResult::OutputFull,
4715 total_read,
4716 total_written,
4717 had_unmappables,
4718 );
4719 }
4720 EncoderResult::Unmappable(unmappable) => {
4721 had_unmappables = true;
4722 debug_assert!(dst.len() - total_written >= NCR_EXTRA);
4723 debug_assert_ne!(self.encoding(), UTF_16BE);
4724 debug_assert_ne!(self.encoding(), UTF_16LE);
4725 // Additionally, Iso2022JpEncoder is responsible for
4726 // transitioning to ASCII when returning with Unmappable.
4727 total_written += write_ncr(unmappable, &mut dst[total_written..]);
4728 if total_written >= effective_dst_len {
4729 if total_read == src.len() && !(last && self.has_pending_state()) {
4730 return (
4731 CoderResult::InputEmpty,
4732 total_read,
4733 total_written,
4734 had_unmappables,
4735 );
4736 }
4737 return (
4738 CoderResult::OutputFull,
4739 total_read,
4740 total_written,
4741 had_unmappables,
4742 );
4743 }
4744 }
4745 }
4746 }
4747 }
4748
4749 /// Incrementally encode into byte stream from UTF-8 with unmappable
4750 /// characters replaced with HTML (decimal) numeric character references.
4751 ///
4752 /// See the documentation of the struct for documentation for `encode_*`
4753 /// methods collectively.
4754 ///
4755 /// Available to Rust only and only with the `alloc` feature enabled (enabled
4756 /// by default).
4757 #[cfg(feature = "alloc")]
4758 pub fn encode_from_utf8_to_vec(
4759 &mut self,
4760 src: &str,
4761 dst: &mut Vec<u8>,
4762 last: bool,
4763 ) -> (CoderResult, usize, bool) {
4764 let old_len = dst.len();
4765 let spare_capacity = minimally_init(dst.spare_capacity_mut());
4766 let (result, read, written, replaced) = self.encode_from_utf8(src, spare_capacity, last);
4767 debug_assert!(written <= spare_capacity.len());
4768 let new_len = old_len + written;
4769 assert!(new_len <= dst.capacity());
4770 // SAFETY: We trust that `encode_from_utf8` wrote to every byte of
4771 // to `spare_capacity[..written]`. Also, regarding the information
4772 // disclosure risk of `minimally_init`, this also means trusting
4773 // that every byte of `spare_capacity[..written]` got overwritten.
4774 // (We're no worse off than before regarding
4775 // `spare_capacity[written..]`) which remains not logically exposed.)
4776 // We (non-debug )asserted immediately above that `new_len` conforms
4777 // to the invariant that it must not exceed `dst.capacity()`.
4778 unsafe {
4779 dst.set_len(new_len);
4780 }
4781 (result, read, replaced)
4782 }
4783
4784 /// Incrementally encode into byte stream from UTF-8 _without replacement_.
4785 ///
4786 /// See the documentation of the struct for documentation for `encode_*`
4787 /// methods collectively.
4788 ///
4789 /// Available via the C wrapper.
4790 pub fn encode_from_utf8_without_replacement(
4791 &mut self,
4792 src: &str,
4793 dst: &mut [u8],
4794 last: bool,
4795 ) -> (EncoderResult, usize, usize) {
4796 self.variant.encode_from_utf8_raw(src, dst, last)
4797 }
4798
4799 /// Incrementally encode into byte stream from UTF-8 _without replacement_.
4800 ///
4801 /// See the documentation of the struct for documentation for `encode_*`
4802 /// methods collectively.
4803 ///
4804 /// Available to Rust only and only with the `alloc` feature enabled (enabled
4805 /// by default).
4806 #[cfg(feature = "alloc")]
4807 pub fn encode_from_utf8_to_vec_without_replacement(
4808 &mut self,
4809 src: &str,
4810 dst: &mut Vec<u8>,
4811 last: bool,
4812 ) -> (EncoderResult, usize) {
4813 let old_len = dst.len();
4814 let spare_capacity = minimally_init(dst.spare_capacity_mut());
4815 let (result, read, written) =
4816 self.encode_from_utf8_without_replacement(src, spare_capacity, last);
4817 debug_assert!(written <= spare_capacity.len());
4818 let new_len = old_len + written;
4819 assert!(new_len <= dst.capacity());
4820 // SAFETY: We trust that `encode_from_utf8_without_replacement` wrote to every byte of
4821 // to `spare_capacity[..written]`. Also, regarding the information
4822 // disclosure risk of `minimally_init`, this also means trusting
4823 // that every byte of `spare_capacity[..written]` got overwritten.
4824 // (We're no worse off than before regarding
4825 // `spare_capacity[written..]`) which remains not logically exposed.)
4826 // We (non-debug )asserted immediately above that `new_len` conforms
4827 // to the invariant that it must not exceed `dst.capacity()`.
4828 unsafe {
4829 dst.set_len(new_len);
4830 }
4831 (result, read)
4832 }
4833
4834 /// Query the worst-case output size when encoding from UTF-16 with
4835 /// replacement.
4836 ///
4837 /// Returns the size of the output buffer in bytes that will not overflow
4838 /// given the current state of the encoder and `u16_length` number of
4839 /// additional input code units if there are no unmappable characters in
4840 /// the input or `None` if `usize` would overflow.
4841 ///
4842 /// Available via the C wrapper.
4843 pub fn max_buffer_length_from_utf16_if_no_unmappables(
4844 &self,
4845 u16_length: usize,
4846 ) -> Option<usize> {
4847 checked_add(
4848 if self.encoding().can_encode_everything() {
4849 0
4850 } else {
4851 NCR_EXTRA
4852 },
4853 self.max_buffer_length_from_utf16_without_replacement(u16_length),
4854 )
4855 }
4856
4857 /// Query the worst-case output size when encoding from UTF-16 without
4858 /// replacement.
4859 ///
4860 /// Returns the size of the output buffer in bytes that will not overflow
4861 /// given the current state of the encoder and `u16_length` number of
4862 /// additional input code units or `None` if `usize` would overflow.
4863 ///
4864 /// Available via the C wrapper.
4865 pub fn max_buffer_length_from_utf16_without_replacement(
4866 &self,
4867 u16_length: usize,
4868 ) -> Option<usize> {
4869 self.variant
4870 .max_buffer_length_from_utf16_without_replacement(u16_length)
4871 }
4872
4873 /// Incrementally encode into byte stream from UTF-16 with unmappable
4874 /// characters replaced with HTML (decimal) numeric character references.
4875 ///
4876 /// See the documentation of the struct for documentation for `encode_*`
4877 /// methods collectively.
4878 ///
4879 /// Available via the C wrapper.
4880 pub fn encode_from_utf16(
4881 &mut self,
4882 src: &[u16],
4883 dst: &mut [u8],
4884 last: bool,
4885 ) -> (CoderResult, usize, usize, bool) {
4886 let dst_len = dst.len();
4887 let effective_dst_len = if self.encoding().can_encode_everything() {
4888 dst_len
4889 } else {
4890 if dst_len < NCR_EXTRA {
4891 if src.is_empty() && !(last && self.has_pending_state()) {
4892 return (CoderResult::InputEmpty, 0, 0, false);
4893 }
4894 return (CoderResult::OutputFull, 0, 0, false);
4895 }
4896 dst_len - NCR_EXTRA
4897 };
4898 let mut had_unmappables = false;
4899 let mut total_read = 0usize;
4900 let mut total_written = 0usize;
4901 loop {
4902 let (result, read, written) = self.encode_from_utf16_without_replacement(
4903 &src[total_read..],
4904 &mut dst[total_written..effective_dst_len],
4905 last,
4906 );
4907 total_read += read;
4908 total_written += written;
4909 match result {
4910 EncoderResult::InputEmpty => {
4911 return (
4912 CoderResult::InputEmpty,
4913 total_read,
4914 total_written,
4915 had_unmappables,
4916 );
4917 }
4918 EncoderResult::OutputFull => {
4919 return (
4920 CoderResult::OutputFull,
4921 total_read,
4922 total_written,
4923 had_unmappables,
4924 );
4925 }
4926 EncoderResult::Unmappable(unmappable) => {
4927 had_unmappables = true;
4928 debug_assert!(dst.len() - total_written >= NCR_EXTRA);
4929 // There are no UTF-16 encoders and even if there were,
4930 // they'd never have unmappables.
4931 debug_assert_ne!(self.encoding(), UTF_16BE);
4932 debug_assert_ne!(self.encoding(), UTF_16LE);
4933 // Additionally, Iso2022JpEncoder is responsible for
4934 // transitioning to ASCII when returning with Unmappable
4935 // from the jis0208 state. That is, when we encode
4936 // ISO-2022-JP and come here, the encoder is in either the
4937 // ASCII or the Roman state. We are allowed to generate any
4938 // printable ASCII excluding \ and ~.
4939 total_written += write_ncr(unmappable, &mut dst[total_written..]);
4940 if total_written >= effective_dst_len {
4941 if total_read == src.len() && !(last && self.has_pending_state()) {
4942 return (
4943 CoderResult::InputEmpty,
4944 total_read,
4945 total_written,
4946 had_unmappables,
4947 );
4948 }
4949 return (
4950 CoderResult::OutputFull,
4951 total_read,
4952 total_written,
4953 had_unmappables,
4954 );
4955 }
4956 }
4957 }
4958 }
4959 }
4960
4961 /// Incrementally encode into byte stream from UTF-16 _without replacement_.
4962 ///
4963 /// See the documentation of the struct for documentation for `encode_*`
4964 /// methods collectively.
4965 ///
4966 /// Available via the C wrapper.
4967 pub fn encode_from_utf16_without_replacement(
4968 &mut self,
4969 src: &[u16],
4970 dst: &mut [u8],
4971 last: bool,
4972 ) -> (EncoderResult, usize, usize) {
4973 self.variant.encode_from_utf16_raw(src, dst, last)
4974 }
4975}
4976
4977impl core::fmt::Debug for Encoder {
4978 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
4979 f.debug_struct("Encoder")
4980 .field("encoding", self.encoding)
4981 .finish_non_exhaustive()
4982 }
4983}
4984
4985/// Format an unmappable as NCR without heap allocation.
4986fn write_ncr(unmappable: char, dst: &mut [u8]) -> usize {
4987 // len is the number of decimal digits needed to represent unmappable plus
4988 // 3 (the length of "&#" and ";").
4989 let mut number = unmappable as u32;
4990 let len = if number >= 1_000_000u32 {
4991 10usize
4992 } else if number >= 100_000u32 {
4993 9usize
4994 } else if number >= 10_000u32 {
4995 8usize
4996 } else if number >= 1_000u32 {
4997 7usize
4998 } else if number >= 100u32 {
4999 6usize
5000 } else {
5001 // Review the outcome of https://github.com/whatwg/encoding/issues/15
5002 // to see if this case is possible
5003 5usize
5004 };
5005 debug_assert!(number >= 10u32);
5006 debug_assert!(len <= dst.len());
5007 let mut pos = len - 1;
5008 dst[pos] = b';';
5009 pos -= 1;
5010 loop {
5011 let rightmost = number % 10;
5012 dst[pos] = rightmost as u8 + b'0';
5013 pos -= 1;
5014 if number < 10 {
5015 break;
5016 }
5017 number /= 10;
5018 }
5019 dst[1] = b'#';
5020 dst[0] = b'&';
5021 len
5022}
5023
5024#[inline(always)]
5025fn in_range16(i: u16, start: u16, end: u16) -> bool {
5026 i.wrapping_sub(start) < (end - start)
5027}
5028
5029#[inline(always)]
5030fn in_range32(i: u32, start: u32, end: u32) -> bool {
5031 i.wrapping_sub(start) < (end - start)
5032}
5033
5034#[inline(always)]
5035fn in_inclusive_range8(i: u8, start: u8, end: u8) -> bool {
5036 i.wrapping_sub(start) <= (end - start)
5037}
5038
5039#[inline(always)]
5040fn in_inclusive_range16(i: u16, start: u16, end: u16) -> bool {
5041 i.wrapping_sub(start) <= (end - start)
5042}
5043
5044#[inline(always)]
5045fn in_inclusive_range32(i: u32, start: u32, end: u32) -> bool {
5046 i.wrapping_sub(start) <= (end - start)
5047}
5048
5049#[inline(always)]
5050fn in_inclusive_range(i: usize, start: usize, end: usize) -> bool {
5051 i.wrapping_sub(start) <= (end - start)
5052}
5053
5054#[inline(always)]
5055fn checked_add(num: usize, opt: Option<usize>) -> Option<usize> {
5056 if let Some(n) = opt {
5057 n.checked_add(num)
5058 } else {
5059 None
5060 }
5061}
5062
5063#[inline(always)]
5064fn checked_add_opt(one: Option<usize>, other: Option<usize>) -> Option<usize> {
5065 if let Some(n) = one {
5066 checked_add(n, other)
5067 } else {
5068 None
5069 }
5070}
5071
5072#[inline(always)]
5073fn checked_mul(num: usize, opt: Option<usize>) -> Option<usize> {
5074 if let Some(n) = opt {
5075 n.checked_mul(num)
5076 } else {
5077 None
5078 }
5079}
5080
5081#[inline(always)]
5082fn checked_div(opt: Option<usize>, num: usize) -> Option<usize> {
5083 if let Some(n) = opt {
5084 n.checked_div(num)
5085 } else {
5086 None
5087 }
5088}
5089
5090#[cfg(feature = "alloc")]
5091#[inline(always)]
5092fn checked_next_power_of_two(opt: Option<usize>) -> Option<usize> {
5093 if let Some(n) = opt {
5094 n.checked_next_power_of_two()
5095 } else {
5096 None
5097 }
5098}
5099
5100#[cfg(feature = "alloc")]
5101#[inline(always)]
5102fn checked_min(one: Option<usize>, other: Option<usize>) -> Option<usize> {
5103 if let Some(a) = one {
5104 if let Some(b) = other {
5105 Some(::core::cmp::min(a, b))
5106 } else {
5107 Some(a)
5108 }
5109 } else {
5110 other
5111 }
5112}
5113
5114/// Smallest page size accoding to Wikipedia. If the pages are larger,
5115/// the code is still correct but does non-minimal writes.
5116///
5117/// We could confidently multiply this by 4 on aarch64 macOS, but
5118/// there is no point, since not initializing / initializing every
5119/// 4Kth byte / initializing everything makes no perf difference
5120/// at least on M3 Pro, so this whole thing is mainly for other
5121/// systems.
5122#[cfg(feature = "alloc")]
5123const SMALLEST_PAGE_SIZE: usize = 4096;
5124
5125/// Mask for the bits that are the offset within a page.
5126#[cfg(feature = "alloc")]
5127const PAGE_MASK: usize = SMALLEST_PAGE_SIZE - 1;
5128
5129/// When we only care about writing to a slice but `&mut [u8]` grants the
5130/// read capability and reading would be UB, this function does the minimum
5131/// writing to make the slice have arbitrary but fixed-value bytes. This
5132/// maintains correct boundary between `unsafe` and safe in terms of UB
5133/// avoidance resposibility even though we don't actually perform reads.
5134/// The caller must not rely on any byte in the slice that was already
5135/// initialized to retain its value after this function returns.
5136///
5137/// The point of wishing to not to contaminate all places with
5138/// `&mut [MaybeUninit<u8>]` is that `&mut [u8]` interacts better with
5139/// `core::simd` in a way that doesn't require `unsafe` in more places.
5140///
5141/// Note that the caller has to overwrite the exposed arbitrary but fixed-value
5142/// bytes to avoid information disclosure (somewhat analogously to reusing an
5143/// intermediate buffer created without any `unsafe`). This function is not
5144/// `unsafe`, because this function does not let the caller to experience UB.
5145/// Letting `&mut [MaybeUninit<u8>]` show up in more places in the crate internals
5146/// would not change the information disclosure risk in case there's a bug in
5147/// the tracking of how much has been written. Either way, the tracking of how
5148/// much has been written has to actually work, but it has a very good track
5149/// record of working.
5150///
5151/// On M3 Pro, we could just zero-initialize every byte without a notable
5152/// performance penalty, but on Zen 3 and Skylake, zeroing all bytes carries
5153/// a measurable penalty (in some cases only; depending on details of
5154/// subsequent writes!) but zeroing the first byte of every page does not
5155/// carry this perf penalty compared to not initializing anything (at least
5156/// when the slice is reasonable-sized relative to what meaningful data
5157/// end up written into it later).
5158#[cfg(feature = "alloc")]
5159fn minimally_init(buf: &mut [MaybeUninit<u8>]) -> &mut [u8] {
5160 // This loop is only broken out of as a goto forward. This structure
5161 // avoids borrowing for too long via `first_mut`.
5162 #[allow(clippy::never_loop, clippy::while_let_loop)]
5163 loop {
5164 if let Some(b) = buf.first_mut() {
5165 // Initialize one byte of the first memory page spanned
5166 // by the slice to ensure the first page is normally mapped.
5167 *b = MaybeUninit::zeroed();
5168 } else {
5169 // Empty slice. Nothing to initialize.
5170 break;
5171 };
5172 // Compute offset to the first byte of the next page.
5173 let mut i = SMALLEST_PAGE_SIZE - (buf.as_mut_ptr().addr() & PAGE_MASK);
5174 while let Some(b) = buf.get_mut(i) {
5175 // Initialize the first byte of each subsequent page to ensure
5176 // the subsequent pages are normally mapped.
5177 *b = MaybeUninit::zeroed();
5178 i += SMALLEST_PAGE_SIZE;
5179 }
5180 break;
5181 }
5182 let ptr = buf.as_mut_ptr();
5183 // SAFETY: Any bit pattern is valid for `u8`, but each `u8`
5184 // in the slice needs to have a _fixed_ value to be treated as
5185 // initialized. Before each page spanned by the slice has been
5186 // written to, the layer below Rust could map all the pages to
5187 // a read-only default page. In that case, if you read from
5188 // offset A within the page, write to offset B != A, and the read
5189 // offset A again, the two reads from offset A could yield
5190 // different results, which the Rust layer does not allow.
5191 //
5192 // Now that we've written to each page, each page is a
5193 // separately-mapped distinct writable page, so even the other
5194 // bytes have fixed values. We now need to make the Rust layer
5195 // not to be able to assume that they don't have fixed values.
5196 unsafe {
5197 pointer_escapes(ptr);
5198 }
5199 // SAFETY: The pages spanned by the input slice are now normally
5200 // mapped and each byte has a fixed value, so the memory now
5201 // has the characteristics of initialized memory.
5202 unsafe { core::slice::from_raw_parts_mut(buf.as_mut_ptr().cast(), buf.len()) }
5203}
5204
5205cfg_if! {
5206 if #[cfg(all(not(miri), feature = "alloc", any(
5207 target_arch = "x86",
5208 target_arch = "x86_64",
5209 target_arch = "arm",
5210 target_arch = "aarch64",
5211 target_arch = "arm64ec",
5212 target_arch = "riscv32",
5213 target_arch = "riscv64",
5214 target_arch = "loongarch64",
5215 target_arch = "s390x")))] {
5216 #[inline(always)]
5217 unsafe fn pointer_escapes(ptr: *mut MaybeUninit<u8>) {
5218 // SAFETY:
5219 // For the purpose of https://www.ralfj.de/blog/2026/03/13/inline-asm.html
5220 // the safe Rust story for the `asm!` block is:
5221 // The `asm!` block reads every byte in the slice that was
5222 // written by the above code and uses the read values to derive
5223 // bytes that it writes to every byte in the slice that was
5224 // _not_ already written by the above code.
5225 unsafe {
5226 core::arch::asm!("/* {0} */", in(reg) ptr);
5227 }
5228 }
5229 } else if #[cfg(feature = "alloc")] {
5230 #[inline(never)]
5231 unsafe fn pointer_escapes(_ptr: *mut MaybeUninit<u8>) {
5232 // Can't use the `asm!` block with Miri. Skipping the `asm!` block
5233 // in the Miri-enabled case means that we materialize `&mut [u8]`
5234 // to memory whose initialization Miri hasn't seen. That tests
5235 // pass under Miri nonetheless shows that we don't actually read from
5236 // the slice, which is a stronger result that just zeroing the
5237 // whole slice when Miri is enabled and having `cargo miri test`
5238 // pass like that.
5239 //
5240 // Also, `asm!` doesn't work on some targets, so we end up relying
5241 // on all this being unnecessary anyway, because materializing
5242 // `&mut [u8]` to unitialized memory isn't UB after all. See
5243 // https://users.rust-lang.org/t/soundly-turning-mut-maybeuninit-u8-into-mut-u8-with-garbage/140668/32
5244 }
5245 } else {
5246 }
5247}
5248
5249// ############## TESTS ###############
5250
5251#[cfg(all(test, feature = "serde"))]
5252#[derive(Serialize, Deserialize, Debug, PartialEq)]
5253struct Demo {
5254 num: u32,
5255 name: String,
5256 enc: &'static Encoding,
5257}
5258
5259#[cfg(test)]
5260mod test_labels_names;
5261
5262#[cfg(all(test, feature = "alloc"))]
5263mod tests {
5264 use super::*;
5265 use alloc::borrow::Cow;
5266
5267 fn sniff_to_utf16(
5268 initial_encoding: &'static Encoding,
5269 expected_encoding: &'static Encoding,
5270 bytes: &[u8],
5271 expect: &[u16],
5272 breaks: &[usize],
5273 ) {
5274 let mut decoder = initial_encoding.new_decoder();
5275
5276 let mut dest: Vec<u16> =
5277 Vec::with_capacity(decoder.max_utf16_buffer_length(bytes.len()).unwrap());
5278 let capacity = dest.capacity();
5279 dest.resize(capacity, 0u16);
5280
5281 let mut total_written = 0usize;
5282 let mut start = 0usize;
5283 for br in breaks {
5284 let (result, read, written, _) =
5285 decoder.decode_to_utf16(&bytes[start..*br], &mut dest[total_written..], false);
5286 total_written += written;
5287 assert_eq!(read, *br - start);
5288 match result {
5289 CoderResult::InputEmpty => {}
5290 CoderResult::OutputFull => {
5291 unreachable!();
5292 }
5293 }
5294 start = *br;
5295 }
5296 let (result, read, written, _) =
5297 decoder.decode_to_utf16(&bytes[start..], &mut dest[total_written..], true);
5298 total_written += written;
5299 match result {
5300 CoderResult::InputEmpty => {}
5301 CoderResult::OutputFull => {
5302 unreachable!();
5303 }
5304 }
5305 assert_eq!(read, bytes.len() - start);
5306 assert_eq!(total_written, expect.len());
5307 assert_eq!(&dest[..total_written], expect);
5308 assert_eq!(decoder.encoding(), expected_encoding);
5309 }
5310
5311 // Any copyright to the test code below this comment is dedicated to the
5312 // Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
5313
5314 #[test]
5315 fn test_bom_sniffing() {
5316 // ASCII
5317 sniff_to_utf16(
5318 WINDOWS_1252,
5319 WINDOWS_1252,
5320 b"\x61\x62",
5321 &[0x0061u16, 0x0062u16],
5322 &[],
5323 );
5324 // UTF-8
5325 sniff_to_utf16(
5326 WINDOWS_1252,
5327 UTF_8,
5328 b"\xEF\xBB\xBF\x61\x62",
5329 &[0x0061u16, 0x0062u16],
5330 &[],
5331 );
5332 sniff_to_utf16(
5333 WINDOWS_1252,
5334 UTF_8,
5335 b"\xEF\xBB\xBF\x61\x62",
5336 &[0x0061u16, 0x0062u16],
5337 &[1],
5338 );
5339 sniff_to_utf16(
5340 WINDOWS_1252,
5341 UTF_8,
5342 b"\xEF\xBB\xBF\x61\x62",
5343 &[0x0061u16, 0x0062u16],
5344 &[2],
5345 );
5346 sniff_to_utf16(
5347 WINDOWS_1252,
5348 UTF_8,
5349 b"\xEF\xBB\xBF\x61\x62",
5350 &[0x0061u16, 0x0062u16],
5351 &[3],
5352 );
5353 sniff_to_utf16(
5354 WINDOWS_1252,
5355 UTF_8,
5356 b"\xEF\xBB\xBF\x61\x62",
5357 &[0x0061u16, 0x0062u16],
5358 &[4],
5359 );
5360 sniff_to_utf16(
5361 WINDOWS_1252,
5362 UTF_8,
5363 b"\xEF\xBB\xBF\x61\x62",
5364 &[0x0061u16, 0x0062u16],
5365 &[2, 3],
5366 );
5367 sniff_to_utf16(
5368 WINDOWS_1252,
5369 UTF_8,
5370 b"\xEF\xBB\xBF\x61\x62",
5371 &[0x0061u16, 0x0062u16],
5372 &[1, 2],
5373 );
5374 sniff_to_utf16(
5375 WINDOWS_1252,
5376 UTF_8,
5377 b"\xEF\xBB\xBF\x61\x62",
5378 &[0x0061u16, 0x0062u16],
5379 &[1, 3],
5380 );
5381 sniff_to_utf16(
5382 WINDOWS_1252,
5383 UTF_8,
5384 b"\xEF\xBB\xBF\x61\x62",
5385 &[0x0061u16, 0x0062u16],
5386 &[1, 2, 3, 4],
5387 );
5388 sniff_to_utf16(WINDOWS_1252, UTF_8, b"\xEF\xBB\xBF", &[], &[]);
5389 // Not UTF-8
5390 sniff_to_utf16(
5391 WINDOWS_1252,
5392 WINDOWS_1252,
5393 b"\xEF\xBB\x61\x62",
5394 &[0x00EFu16, 0x00BBu16, 0x0061u16, 0x0062u16],
5395 &[],
5396 );
5397 sniff_to_utf16(
5398 WINDOWS_1252,
5399 WINDOWS_1252,
5400 b"\xEF\xBB\x61\x62",
5401 &[0x00EFu16, 0x00BBu16, 0x0061u16, 0x0062u16],
5402 &[1],
5403 );
5404 sniff_to_utf16(
5405 WINDOWS_1252,
5406 WINDOWS_1252,
5407 b"\xEF\x61\x62",
5408 &[0x00EFu16, 0x0061u16, 0x0062u16],
5409 &[],
5410 );
5411 sniff_to_utf16(
5412 WINDOWS_1252,
5413 WINDOWS_1252,
5414 b"\xEF\x61\x62",
5415 &[0x00EFu16, 0x0061u16, 0x0062u16],
5416 &[1],
5417 );
5418 sniff_to_utf16(
5419 WINDOWS_1252,
5420 WINDOWS_1252,
5421 b"\xEF\xBB",
5422 &[0x00EFu16, 0x00BBu16],
5423 &[],
5424 );
5425 sniff_to_utf16(
5426 WINDOWS_1252,
5427 WINDOWS_1252,
5428 b"\xEF\xBB",
5429 &[0x00EFu16, 0x00BBu16],
5430 &[1],
5431 );
5432 sniff_to_utf16(WINDOWS_1252, WINDOWS_1252, b"\xEF", &[0x00EFu16], &[]);
5433 // Not UTF-16
5434 sniff_to_utf16(
5435 WINDOWS_1252,
5436 WINDOWS_1252,
5437 b"\xFE\x61\x62",
5438 &[0x00FEu16, 0x0061u16, 0x0062u16],
5439 &[],
5440 );
5441 sniff_to_utf16(
5442 WINDOWS_1252,
5443 WINDOWS_1252,
5444 b"\xFE\x61\x62",
5445 &[0x00FEu16, 0x0061u16, 0x0062u16],
5446 &[1],
5447 );
5448 sniff_to_utf16(WINDOWS_1252, WINDOWS_1252, b"\xFE", &[0x00FEu16], &[]);
5449 sniff_to_utf16(
5450 WINDOWS_1252,
5451 WINDOWS_1252,
5452 b"\xFF\x61\x62",
5453 &[0x00FFu16, 0x0061u16, 0x0062u16],
5454 &[],
5455 );
5456 sniff_to_utf16(
5457 WINDOWS_1252,
5458 WINDOWS_1252,
5459 b"\xFF\x61\x62",
5460 &[0x00FFu16, 0x0061u16, 0x0062u16],
5461 &[1],
5462 );
5463 sniff_to_utf16(WINDOWS_1252, WINDOWS_1252, b"\xFF", &[0x00FFu16], &[]);
5464 // UTF-16
5465 sniff_to_utf16(WINDOWS_1252, UTF_16BE, b"\xFE\xFF", &[], &[]);
5466 sniff_to_utf16(WINDOWS_1252, UTF_16BE, b"\xFE\xFF", &[], &[1]);
5467 sniff_to_utf16(WINDOWS_1252, UTF_16LE, b"\xFF\xFE", &[], &[]);
5468 sniff_to_utf16(WINDOWS_1252, UTF_16LE, b"\xFF\xFE", &[], &[1]);
5469 }
5470
5471 #[test]
5472 fn test_output_encoding() {
5473 assert_eq!(REPLACEMENT.output_encoding(), UTF_8);
5474 assert_eq!(UTF_16BE.output_encoding(), UTF_8);
5475 assert_eq!(UTF_16LE.output_encoding(), UTF_8);
5476 assert_eq!(UTF_8.output_encoding(), UTF_8);
5477 assert_eq!(WINDOWS_1252.output_encoding(), WINDOWS_1252);
5478 assert_eq!(REPLACEMENT.new_encoder().encoding(), UTF_8);
5479 assert_eq!(UTF_16BE.new_encoder().encoding(), UTF_8);
5480 assert_eq!(UTF_16LE.new_encoder().encoding(), UTF_8);
5481 assert_eq!(UTF_8.new_encoder().encoding(), UTF_8);
5482 assert_eq!(WINDOWS_1252.new_encoder().encoding(), WINDOWS_1252);
5483 }
5484
5485 #[test]
5486 fn test_label_resolution() {
5487 assert_eq!(Encoding::for_label(b"utf-8"), Some(UTF_8));
5488 assert_eq!(Encoding::for_label(b"UTF-8"), Some(UTF_8));
5489 assert_eq!(
5490 Encoding::for_label(b" \t \n \x0C \n utf-8 \r \n \t \x0C "),
5491 Some(UTF_8)
5492 );
5493 assert_eq!(Encoding::for_label(b"utf-8 _"), None);
5494 assert_eq!(Encoding::for_label(b"bogus"), None);
5495 assert_eq!(Encoding::for_label(b"bogusbogusbogusbogus"), None);
5496 }
5497
5498 #[test]
5499 fn test_decode_valid_windows_1257_to_cow() {
5500 let (cow, encoding, had_errors) = WINDOWS_1257.decode(b"abc\x80\xE4");
5501 match cow {
5502 Cow::Borrowed(_) => unreachable!(),
5503 Cow::Owned(s) => {
5504 assert_eq!(s, "abc\u{20AC}\u{00E4}");
5505 }
5506 }
5507 assert_eq!(encoding, WINDOWS_1257);
5508 assert!(!had_errors);
5509 }
5510
5511 #[test]
5512 fn test_decode_invalid_windows_1257_to_cow() {
5513 let (cow, encoding, had_errors) = WINDOWS_1257.decode(b"abc\x80\xA1\xE4");
5514 match cow {
5515 Cow::Borrowed(_) => unreachable!(),
5516 Cow::Owned(s) => {
5517 assert_eq!(s, "abc\u{20AC}\u{FFFD}\u{00E4}");
5518 }
5519 }
5520 assert_eq!(encoding, WINDOWS_1257);
5521 assert!(had_errors);
5522 }
5523
5524 #[test]
5525 fn test_decode_ascii_only_windows_1257_to_cow() {
5526 let (cow, encoding, had_errors) = WINDOWS_1257.decode(b"abc");
5527 match cow {
5528 Cow::Borrowed(s) => {
5529 assert_eq!(s, "abc");
5530 }
5531 Cow::Owned(_) => unreachable!(),
5532 }
5533 assert_eq!(encoding, WINDOWS_1257);
5534 assert!(!had_errors);
5535 }
5536
5537 #[test]
5538 fn test_decode_bomful_valid_utf8_as_windows_1257_to_cow() {
5539 let (cow, encoding, had_errors) = WINDOWS_1257.decode(b"\xEF\xBB\xBF\xE2\x82\xAC\xC3\xA4");
5540 match cow {
5541 Cow::Borrowed(s) => {
5542 assert_eq!(s, "\u{20AC}\u{00E4}");
5543 }
5544 Cow::Owned(_) => unreachable!(),
5545 }
5546 assert_eq!(encoding, UTF_8);
5547 assert!(!had_errors);
5548 }
5549
5550 #[test]
5551 fn test_decode_bomful_invalid_utf8_as_windows_1257_to_cow() {
5552 let (cow, encoding, had_errors) =
5553 WINDOWS_1257.decode(b"\xEF\xBB\xBF\xE2\x82\xAC\x80\xC3\xA4");
5554 match cow {
5555 Cow::Borrowed(_) => unreachable!(),
5556 Cow::Owned(s) => {
5557 assert_eq!(s, "\u{20AC}\u{FFFD}\u{00E4}");
5558 }
5559 }
5560 assert_eq!(encoding, UTF_8);
5561 assert!(had_errors);
5562 }
5563
5564 #[test]
5565 fn test_decode_bomful_valid_utf8_as_utf_8_to_cow() {
5566 let (cow, encoding, had_errors) = UTF_8.decode(b"\xEF\xBB\xBF\xE2\x82\xAC\xC3\xA4");
5567 match cow {
5568 Cow::Borrowed(s) => {
5569 assert_eq!(s, "\u{20AC}\u{00E4}");
5570 }
5571 Cow::Owned(_) => unreachable!(),
5572 }
5573 assert_eq!(encoding, UTF_8);
5574 assert!(!had_errors);
5575 }
5576
5577 #[test]
5578 fn test_decode_bomful_invalid_utf8_as_utf_8_to_cow() {
5579 let (cow, encoding, had_errors) = UTF_8.decode(b"\xEF\xBB\xBF\xE2\x82\xAC\x80\xC3\xA4");
5580 match cow {
5581 Cow::Borrowed(_) => unreachable!(),
5582 Cow::Owned(s) => {
5583 assert_eq!(s, "\u{20AC}\u{FFFD}\u{00E4}");
5584 }
5585 }
5586 assert_eq!(encoding, UTF_8);
5587 assert!(had_errors);
5588 }
5589
5590 #[test]
5591 fn test_decode_bomful_valid_utf8_as_utf_8_to_cow_with_bom_removal() {
5592 let (cow, had_errors) = UTF_8.decode_with_bom_removal(b"\xEF\xBB\xBF\xE2\x82\xAC\xC3\xA4");
5593 match cow {
5594 Cow::Borrowed(s) => {
5595 assert_eq!(s, "\u{20AC}\u{00E4}");
5596 }
5597 Cow::Owned(_) => unreachable!(),
5598 }
5599 assert!(!had_errors);
5600 }
5601
5602 #[test]
5603 fn test_decode_bomful_valid_utf8_as_windows_1257_to_cow_with_bom_removal() {
5604 let (cow, had_errors) =
5605 WINDOWS_1257.decode_with_bom_removal(b"\xEF\xBB\xBF\xE2\x82\xAC\xC3\xA4");
5606 match cow {
5607 Cow::Borrowed(_) => unreachable!(),
5608 Cow::Owned(s) => {
5609 assert_eq!(
5610 s,
5611 "\u{013C}\u{00BB}\u{00E6}\u{0101}\u{201A}\u{00AC}\u{0106}\u{00A4}"
5612 );
5613 }
5614 }
5615 assert!(!had_errors);
5616 }
5617
5618 #[test]
5619 fn test_decode_valid_windows_1257_to_cow_with_bom_removal() {
5620 let (cow, had_errors) = WINDOWS_1257.decode_with_bom_removal(b"abc\x80\xE4");
5621 match cow {
5622 Cow::Borrowed(_) => unreachable!(),
5623 Cow::Owned(s) => {
5624 assert_eq!(s, "abc\u{20AC}\u{00E4}");
5625 }
5626 }
5627 assert!(!had_errors);
5628 }
5629
5630 #[test]
5631 fn test_decode_invalid_windows_1257_to_cow_with_bom_removal() {
5632 let (cow, had_errors) = WINDOWS_1257.decode_with_bom_removal(b"abc\x80\xA1\xE4");
5633 match cow {
5634 Cow::Borrowed(_) => unreachable!(),
5635 Cow::Owned(s) => {
5636 assert_eq!(s, "abc\u{20AC}\u{FFFD}\u{00E4}");
5637 }
5638 }
5639 assert!(had_errors);
5640 }
5641
5642 #[test]
5643 fn test_decode_ascii_only_windows_1257_to_cow_with_bom_removal() {
5644 let (cow, had_errors) = WINDOWS_1257.decode_with_bom_removal(b"abc");
5645 match cow {
5646 Cow::Borrowed(s) => {
5647 assert_eq!(s, "abc");
5648 }
5649 Cow::Owned(_) => unreachable!(),
5650 }
5651 assert!(!had_errors);
5652 }
5653
5654 #[test]
5655 fn test_decode_bomful_valid_utf8_to_cow_without_bom_handling() {
5656 let (cow, had_errors) =
5657 UTF_8.decode_without_bom_handling(b"\xEF\xBB\xBF\xE2\x82\xAC\xC3\xA4");
5658 match cow {
5659 Cow::Borrowed(s) => {
5660 assert_eq!(s, "\u{FEFF}\u{20AC}\u{00E4}");
5661 }
5662 Cow::Owned(_) => unreachable!(),
5663 }
5664 assert!(!had_errors);
5665 }
5666
5667 #[test]
5668 fn test_decode_bomful_invalid_utf8_to_cow_without_bom_handling() {
5669 let (cow, had_errors) =
5670 UTF_8.decode_without_bom_handling(b"\xEF\xBB\xBF\xE2\x82\xAC\x80\xC3\xA4");
5671 match cow {
5672 Cow::Borrowed(_) => unreachable!(),
5673 Cow::Owned(s) => {
5674 assert_eq!(s, "\u{FEFF}\u{20AC}\u{FFFD}\u{00E4}");
5675 }
5676 }
5677 assert!(had_errors);
5678 }
5679
5680 #[test]
5681 fn test_decode_valid_windows_1257_to_cow_without_bom_handling() {
5682 let (cow, had_errors) = WINDOWS_1257.decode_without_bom_handling(b"abc\x80\xE4");
5683 match cow {
5684 Cow::Borrowed(_) => unreachable!(),
5685 Cow::Owned(s) => {
5686 assert_eq!(s, "abc\u{20AC}\u{00E4}");
5687 }
5688 }
5689 assert!(!had_errors);
5690 }
5691
5692 #[test]
5693 fn test_decode_invalid_windows_1257_to_cow_without_bom_handling() {
5694 let (cow, had_errors) = WINDOWS_1257.decode_without_bom_handling(b"abc\x80\xA1\xE4");
5695 match cow {
5696 Cow::Borrowed(_) => unreachable!(),
5697 Cow::Owned(s) => {
5698 assert_eq!(s, "abc\u{20AC}\u{FFFD}\u{00E4}");
5699 }
5700 }
5701 assert!(had_errors);
5702 }
5703
5704 #[test]
5705 fn test_decode_ascii_only_windows_1257_to_cow_without_bom_handling() {
5706 let (cow, had_errors) = WINDOWS_1257.decode_without_bom_handling(b"abc");
5707 match cow {
5708 Cow::Borrowed(s) => {
5709 assert_eq!(s, "abc");
5710 }
5711 Cow::Owned(_) => unreachable!(),
5712 }
5713 assert!(!had_errors);
5714 }
5715
5716 #[test]
5717 fn test_decode_bomful_valid_utf8_to_cow_without_bom_handling_and_without_replacement() {
5718 match UTF_8.decode_without_bom_handling_and_without_replacement(
5719 b"\xEF\xBB\xBF\xE2\x82\xAC\xC3\xA4",
5720 ) {
5721 Some(cow) => match cow {
5722 Cow::Borrowed(s) => {
5723 assert_eq!(s, "\u{FEFF}\u{20AC}\u{00E4}");
5724 }
5725 Cow::Owned(_) => unreachable!(),
5726 },
5727 None => unreachable!(),
5728 }
5729 }
5730
5731 #[test]
5732 fn test_decode_bomful_invalid_utf8_to_cow_without_bom_handling_and_without_replacement() {
5733 assert!(
5734 UTF_8
5735 .decode_without_bom_handling_and_without_replacement(
5736 b"\xEF\xBB\xBF\xE2\x82\xAC\x80\xC3\xA4"
5737 )
5738 .is_none()
5739 );
5740 }
5741
5742 #[test]
5743 fn test_decode_valid_windows_1257_to_cow_without_bom_handling_and_without_replacement() {
5744 match WINDOWS_1257.decode_without_bom_handling_and_without_replacement(b"abc\x80\xE4") {
5745 Some(cow) => match cow {
5746 Cow::Borrowed(_) => unreachable!(),
5747 Cow::Owned(s) => {
5748 assert_eq!(s, "abc\u{20AC}\u{00E4}");
5749 }
5750 },
5751 None => unreachable!(),
5752 }
5753 }
5754
5755 #[test]
5756 fn test_decode_invalid_windows_1257_to_cow_without_bom_handling_and_without_replacement() {
5757 assert!(
5758 WINDOWS_1257
5759 .decode_without_bom_handling_and_without_replacement(b"abc\x80\xA1\xE4")
5760 .is_none()
5761 );
5762 }
5763
5764 #[test]
5765 fn test_decode_ascii_only_windows_1257_to_cow_without_bom_handling_and_without_replacement() {
5766 match WINDOWS_1257.decode_without_bom_handling_and_without_replacement(b"abc") {
5767 Some(cow) => match cow {
5768 Cow::Borrowed(s) => {
5769 assert_eq!(s, "abc");
5770 }
5771 Cow::Owned(_) => unreachable!(),
5772 },
5773 None => unreachable!(),
5774 }
5775 }
5776
5777 #[test]
5778 fn test_encode_ascii_only_windows_1257_to_cow() {
5779 let (cow, encoding, had_errors) = WINDOWS_1257.encode("abc");
5780 match cow {
5781 Cow::Borrowed(s) => {
5782 assert_eq!(s, b"abc");
5783 }
5784 Cow::Owned(_) => unreachable!(),
5785 }
5786 assert_eq!(encoding, WINDOWS_1257);
5787 assert!(!had_errors);
5788 }
5789
5790 #[test]
5791 fn test_encode_valid_windows_1257_to_cow() {
5792 let (cow, encoding, had_errors) = WINDOWS_1257.encode("abc\u{20AC}\u{00E4}");
5793 match cow {
5794 Cow::Borrowed(_) => unreachable!(),
5795 Cow::Owned(s) => {
5796 assert_eq!(s, b"abc\x80\xE4");
5797 }
5798 }
5799 assert_eq!(encoding, WINDOWS_1257);
5800 assert!(!had_errors);
5801 }
5802
5803 #[test]
5804 fn test_utf16_space_with_one_bom_byte() {
5805 let mut decoder = UTF_16LE.new_decoder();
5806 let mut dst = [0u16; 12];
5807 {
5808 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5809 let (result, _, _, _) = decoder.decode_to_utf16(b"\xFF", &mut dst[..needed], false);
5810 assert_eq!(result, CoderResult::InputEmpty);
5811 }
5812 {
5813 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5814 let (result, _, _, _) = decoder.decode_to_utf16(b"\xFF", &mut dst[..needed], true);
5815 assert_eq!(result, CoderResult::InputEmpty);
5816 }
5817 }
5818
5819 #[test]
5820 fn test_utf8_space_with_one_bom_byte() {
5821 let mut decoder = UTF_8.new_decoder();
5822 let mut dst = [0u16; 12];
5823 {
5824 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5825 let (result, _, _, _) = decoder.decode_to_utf16(b"\xFF", &mut dst[..needed], false);
5826 assert_eq!(result, CoderResult::InputEmpty);
5827 }
5828 {
5829 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5830 let (result, _, _, _) = decoder.decode_to_utf16(b"\xFF", &mut dst[..needed], true);
5831 assert_eq!(result, CoderResult::InputEmpty);
5832 }
5833 }
5834
5835 #[test]
5836 fn test_utf16_space_with_two_bom_bytes() {
5837 let mut decoder = UTF_16LE.new_decoder();
5838 let mut dst = [0u16; 12];
5839 {
5840 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5841 let (result, _, _, _) = decoder.decode_to_utf16(b"\xEF", &mut dst[..needed], false);
5842 assert_eq!(result, CoderResult::InputEmpty);
5843 }
5844 {
5845 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5846 let (result, _, _, _) = decoder.decode_to_utf16(b"\xBB", &mut dst[..needed], false);
5847 assert_eq!(result, CoderResult::InputEmpty);
5848 }
5849 {
5850 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5851 let (result, _, _, _) = decoder.decode_to_utf16(b"\xFF", &mut dst[..needed], true);
5852 assert_eq!(result, CoderResult::InputEmpty);
5853 }
5854 }
5855
5856 #[test]
5857 fn test_utf8_space_with_two_bom_bytes() {
5858 let mut decoder = UTF_8.new_decoder();
5859 let mut dst = [0u16; 12];
5860 {
5861 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5862 let (result, _, _, _) = decoder.decode_to_utf16(b"\xEF", &mut dst[..needed], false);
5863 assert_eq!(result, CoderResult::InputEmpty);
5864 }
5865 {
5866 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5867 let (result, _, _, _) = decoder.decode_to_utf16(b"\xBB", &mut dst[..needed], false);
5868 assert_eq!(result, CoderResult::InputEmpty);
5869 }
5870 {
5871 let needed = decoder.max_utf16_buffer_length(1).unwrap();
5872 let (result, _, _, _) = decoder.decode_to_utf16(b"\xFF", &mut dst[..needed], true);
5873 assert_eq!(result, CoderResult::InputEmpty);
5874 }
5875 }
5876
5877 #[test]
5878 fn test_short_buffer_after_non_bom_bytes() {
5879 // EF BB at the end of one buffer turn out not to be a BOM. Decoding
5880 // them as IBM866 or x-user-defined takes more than four bytes of UTF-8,
5881 // so with a four-byte buffer the replay must return OutputFull and
5882 // resume on the next call.
5883 for (encoding, bytes, expect) in [
5884 (IBM866, &b"\xEF\xBBab"[..], "\u{044F}\u{2557}ab"),
5885 (X_USER_DEFINED, &b"\xEF\xBBab"[..], "\u{F7EF}\u{F7BB}ab"),
5886 (X_USER_DEFINED, &b"\xFE\x80"[..], "\u{F7FE}\u{F780}"),
5887 (ISO_2022_JP, &b"\xEF\xBBab"[..], "\u{FFFD}\u{FFFD}ab"),
5888 ] {
5889 for split in 1..bytes.len() {
5890 for size in 4..8 {
5891 let mut decoder = encoding.new_decoder();
5892 let mut out = String::new();
5893 for (src, last) in [(&bytes[..split], false), (&bytes[split..], true)] {
5894 let mut src = src;
5895 loop {
5896 let mut dst = [0u8; 8];
5897 let (result, read, written, _) =
5898 decoder.decode_to_utf8(src, &mut dst[..size], last);
5899 out.push_str(core::str::from_utf8(&dst[..written]).unwrap());
5900 src = &src[read..];
5901 if result == CoderResult::InputEmpty {
5902 break;
5903 }
5904 }
5905 }
5906 assert_eq!(out, expect);
5907 }
5908 }
5909 }
5910 }
5911
5912 #[test]
5913 fn test_utf16_space_with_one_bom_byte_and_a_second_byte_in_same_call() {
5914 let mut decoder = UTF_16LE.new_decoder();
5915 let mut dst = [0u16; 12];
5916 {
5917 let needed = decoder.max_utf16_buffer_length(2).unwrap();
5918 let (result, _, _, _) = decoder.decode_to_utf16(b"\xFF\xFF", &mut dst[..needed], true);
5919 assert_eq!(result, CoderResult::InputEmpty);
5920 }
5921 }
5922
5923 #[test]
5924 fn test_too_short_buffer_with_iso_2022_jp_ascii_from_utf8() {
5925 let mut dst = [0u8; 8];
5926 let mut encoder = ISO_2022_JP.new_encoder();
5927 {
5928 let (result, _, _, _) = encoder.encode_from_utf8("", &mut dst[..], false);
5929 assert_eq!(result, CoderResult::InputEmpty);
5930 }
5931 {
5932 let (result, _, _, _) = encoder.encode_from_utf8("", &mut dst[..], true);
5933 assert_eq!(result, CoderResult::InputEmpty);
5934 }
5935 }
5936
5937 #[test]
5938 fn test_too_short_buffer_with_iso_2022_jp_roman_from_utf8() {
5939 let mut dst = [0u8; 16];
5940 let mut encoder = ISO_2022_JP.new_encoder();
5941 {
5942 let (result, _, _, _) = encoder.encode_from_utf8("\u{A5}", &mut dst[..], false);
5943 assert_eq!(result, CoderResult::InputEmpty);
5944 }
5945 {
5946 let (result, _, _, _) = encoder.encode_from_utf8("", &mut dst[..8], false);
5947 assert_eq!(result, CoderResult::InputEmpty);
5948 }
5949 {
5950 let (result, _, _, _) = encoder.encode_from_utf8("", &mut dst[..8], true);
5951 assert_eq!(result, CoderResult::OutputFull);
5952 }
5953 }
5954
5955 #[test]
5956 fn test_buffer_end_iso_2022_jp_from_utf8() {
5957 let mut dst = [0u8; 18];
5958 {
5959 let mut encoder = ISO_2022_JP.new_encoder();
5960 let (result, _, _, _) =
5961 encoder.encode_from_utf8("\u{A5}\u{1F4A9}", &mut dst[..], false);
5962 assert_eq!(result, CoderResult::InputEmpty);
5963 }
5964 {
5965 let mut encoder = ISO_2022_JP.new_encoder();
5966 let (result, _, _, _) = encoder.encode_from_utf8("\u{A5}\u{1F4A9}", &mut dst[..], true);
5967 assert_eq!(result, CoderResult::OutputFull);
5968 }
5969 {
5970 let mut encoder = ISO_2022_JP.new_encoder();
5971 let (result, _, _, _) = encoder.encode_from_utf8("\u{1F4A9}", &mut dst[..13], false);
5972 assert_eq!(result, CoderResult::InputEmpty);
5973 }
5974 {
5975 let mut encoder = ISO_2022_JP.new_encoder();
5976 let (result, _, _, _) = encoder.encode_from_utf8("\u{1F4A9}", &mut dst[..13], true);
5977 assert_eq!(result, CoderResult::InputEmpty);
5978 }
5979 }
5980
5981 #[test]
5982 fn test_too_short_buffer_with_iso_2022_jp_ascii_from_utf16() {
5983 let mut dst = [0u8; 8];
5984 let mut encoder = ISO_2022_JP.new_encoder();
5985 {
5986 let (result, _, _, _) = encoder.encode_from_utf16(&[0u16; 0], &mut dst[..], false);
5987 assert_eq!(result, CoderResult::InputEmpty);
5988 }
5989 {
5990 let (result, _, _, _) = encoder.encode_from_utf16(&[0u16; 0], &mut dst[..], true);
5991 assert_eq!(result, CoderResult::InputEmpty);
5992 }
5993 }
5994
5995 #[test]
5996 fn test_too_short_buffer_with_iso_2022_jp_roman_from_utf16() {
5997 let mut dst = [0u8; 16];
5998 let mut encoder = ISO_2022_JP.new_encoder();
5999 {
6000 let (result, _, _, _) = encoder.encode_from_utf16(&[0xA5u16], &mut dst[..], false);
6001 assert_eq!(result, CoderResult::InputEmpty);
6002 }
6003 {
6004 let (result, _, _, _) = encoder.encode_from_utf16(&[0u16; 0], &mut dst[..8], false);
6005 assert_eq!(result, CoderResult::InputEmpty);
6006 }
6007 {
6008 let (result, _, _, _) = encoder.encode_from_utf16(&[0u16; 0], &mut dst[..8], true);
6009 assert_eq!(result, CoderResult::OutputFull);
6010 }
6011 }
6012
6013 #[test]
6014 fn test_buffer_end_iso_2022_jp_from_utf16() {
6015 let mut dst = [0u8; 18];
6016 {
6017 let mut encoder = ISO_2022_JP.new_encoder();
6018 let (result, _, _, _) =
6019 encoder.encode_from_utf16(&[0xA5u16, 0xD83Du16, 0xDCA9u16], &mut dst[..], false);
6020 assert_eq!(result, CoderResult::InputEmpty);
6021 }
6022 {
6023 let mut encoder = ISO_2022_JP.new_encoder();
6024 let (result, _, _, _) =
6025 encoder.encode_from_utf16(&[0xA5u16, 0xD83Du16, 0xDCA9u16], &mut dst[..], true);
6026 assert_eq!(result, CoderResult::OutputFull);
6027 }
6028 {
6029 let mut encoder = ISO_2022_JP.new_encoder();
6030 let (result, _, _, _) =
6031 encoder.encode_from_utf16(&[0xD83Du16, 0xDCA9u16], &mut dst[..13], false);
6032 assert_eq!(result, CoderResult::InputEmpty);
6033 }
6034 {
6035 let mut encoder = ISO_2022_JP.new_encoder();
6036 let (result, _, _, _) =
6037 encoder.encode_from_utf16(&[0xD83Du16, 0xDCA9u16], &mut dst[..13], true);
6038 assert_eq!(result, CoderResult::InputEmpty);
6039 }
6040 }
6041
6042 #[test]
6043 fn test_buffer_end_utf16be() {
6044 let mut decoder = UTF_16BE.new_decoder_without_bom_handling();
6045 let mut dest = [0u8; 4];
6046
6047 assert_eq!(
6048 decoder.decode_to_utf8(&[0xD8, 0x00], &mut dest, false),
6049 (CoderResult::InputEmpty, 2, 0, false)
6050 );
6051
6052 let _ = decoder.decode_to_utf8(&[0xD8, 0x00], &mut dest, true);
6053 }
6054
6055 #[test]
6056 fn test_hash() {
6057 let mut encodings = ::alloc::collections::btree_set::BTreeSet::new();
6058 encodings.insert(UTF_8);
6059 encodings.insert(ISO_2022_JP);
6060 assert!(encodings.contains(UTF_8));
6061 assert!(encodings.contains(ISO_2022_JP));
6062 assert!(!encodings.contains(WINDOWS_1252));
6063 encodings.remove(ISO_2022_JP);
6064 assert!(!encodings.contains(ISO_2022_JP));
6065 }
6066
6067 #[test]
6068 fn test_iso_2022_jp_ncr_extra_from_utf16() {
6069 let mut dst = [0u8; 17];
6070 {
6071 let mut encoder = ISO_2022_JP.new_encoder();
6072 let (result, _, _, _) =
6073 encoder.encode_from_utf16(&[0x3041u16, 0xFFFFu16], &mut dst[..], true);
6074 assert_eq!(result, CoderResult::OutputFull);
6075 }
6076 }
6077
6078 #[test]
6079 fn test_iso_2022_jp_ncr_extra_from_utf8() {
6080 let mut dst = [0u8; 17];
6081 {
6082 let mut encoder = ISO_2022_JP.new_encoder();
6083 let (result, _, _, _) =
6084 encoder.encode_from_utf8("\u{3041}\u{FFFF}", &mut dst[..], true);
6085 assert_eq!(result, CoderResult::OutputFull);
6086 }
6087 }
6088
6089 #[test]
6090 fn test_max_length_with_bom_to_utf8() {
6091 let mut output = [0u8; 20];
6092 let mut decoder = REPLACEMENT.new_decoder();
6093 let input = b"\xEF\xBB\xBFA";
6094 {
6095 let needed = decoder
6096 .max_utf8_buffer_length_without_replacement(input.len())
6097 .unwrap();
6098 let (result, read, written) =
6099 decoder.decode_to_utf8_without_replacement(input, &mut output[..needed], true);
6100 assert_eq!(result, DecoderResult::InputEmpty);
6101 assert_eq!(read, input.len());
6102 assert_eq!(written, 1);
6103 assert_eq!(output[0], 0x41);
6104 }
6105 }
6106
6107 #[cfg(feature = "serde")]
6108 #[test]
6109 fn test_serde() {
6110 let demo = Demo {
6111 num: 42,
6112 name: "foo".into(),
6113 enc: UTF_8,
6114 };
6115
6116 let serialized = serde_json::to_string(&demo).unwrap();
6117
6118 let deserialized: Demo = serde_json::from_str(&serialized).unwrap();
6119 assert_eq!(deserialized, demo);
6120
6121 let bincoded = bincode::serialize(&demo).unwrap();
6122 let debincoded: Demo = bincode::deserialize(&bincoded[..]).unwrap();
6123 assert_eq!(debincoded, demo);
6124 }
6125
6126 #[test]
6127 fn test_is_single_byte() {
6128 assert!(!BIG5.is_single_byte());
6129 assert!(!EUC_JP.is_single_byte());
6130 assert!(!EUC_KR.is_single_byte());
6131 assert!(!GB18030.is_single_byte());
6132 assert!(!GBK.is_single_byte());
6133 assert!(!REPLACEMENT.is_single_byte());
6134 assert!(!SHIFT_JIS.is_single_byte());
6135 assert!(!UTF_8.is_single_byte());
6136 assert!(!UTF_16BE.is_single_byte());
6137 assert!(!UTF_16LE.is_single_byte());
6138 assert!(!ISO_2022_JP.is_single_byte());
6139
6140 assert!(IBM866.is_single_byte());
6141 assert!(ISO_8859_2.is_single_byte());
6142 assert!(ISO_8859_3.is_single_byte());
6143 assert!(ISO_8859_4.is_single_byte());
6144 assert!(ISO_8859_5.is_single_byte());
6145 assert!(ISO_8859_6.is_single_byte());
6146 assert!(ISO_8859_7.is_single_byte());
6147 assert!(ISO_8859_8.is_single_byte());
6148 assert!(ISO_8859_10.is_single_byte());
6149 assert!(ISO_8859_13.is_single_byte());
6150 assert!(ISO_8859_14.is_single_byte());
6151 assert!(ISO_8859_15.is_single_byte());
6152 assert!(ISO_8859_16.is_single_byte());
6153 assert!(ISO_8859_8_I.is_single_byte());
6154 assert!(KOI8_R.is_single_byte());
6155 assert!(KOI8_U.is_single_byte());
6156 assert!(MACINTOSH.is_single_byte());
6157 assert!(WINDOWS_874.is_single_byte());
6158 assert!(WINDOWS_1250.is_single_byte());
6159 assert!(WINDOWS_1251.is_single_byte());
6160 assert!(WINDOWS_1252.is_single_byte());
6161 assert!(WINDOWS_1253.is_single_byte());
6162 assert!(WINDOWS_1254.is_single_byte());
6163 assert!(WINDOWS_1255.is_single_byte());
6164 assert!(WINDOWS_1256.is_single_byte());
6165 assert!(WINDOWS_1257.is_single_byte());
6166 assert!(WINDOWS_1258.is_single_byte());
6167 assert!(X_MAC_CYRILLIC.is_single_byte());
6168 assert!(X_USER_DEFINED.is_single_byte());
6169 }
6170
6171 #[test]
6172 fn test_latin1_byte_compatible_up_to() {
6173 let buffer = b"a\x81\xB6\xF6\xF0\x82\xB4";
6174 assert_eq!(
6175 BIG5.new_decoder_without_bom_handling()
6176 .latin1_byte_compatible_up_to(buffer)
6177 .unwrap(),
6178 1
6179 );
6180 assert_eq!(
6181 EUC_JP
6182 .new_decoder_without_bom_handling()
6183 .latin1_byte_compatible_up_to(buffer)
6184 .unwrap(),
6185 1
6186 );
6187 assert_eq!(
6188 EUC_KR
6189 .new_decoder_without_bom_handling()
6190 .latin1_byte_compatible_up_to(buffer)
6191 .unwrap(),
6192 1
6193 );
6194 assert_eq!(
6195 GB18030
6196 .new_decoder_without_bom_handling()
6197 .latin1_byte_compatible_up_to(buffer)
6198 .unwrap(),
6199 1
6200 );
6201 assert_eq!(
6202 GBK.new_decoder_without_bom_handling()
6203 .latin1_byte_compatible_up_to(buffer)
6204 .unwrap(),
6205 1
6206 );
6207 assert!(
6208 REPLACEMENT
6209 .new_decoder_without_bom_handling()
6210 .latin1_byte_compatible_up_to(buffer)
6211 .is_none()
6212 );
6213 assert_eq!(
6214 SHIFT_JIS
6215 .new_decoder_without_bom_handling()
6216 .latin1_byte_compatible_up_to(buffer)
6217 .unwrap(),
6218 1
6219 );
6220 assert_eq!(
6221 UTF_8
6222 .new_decoder_without_bom_handling()
6223 .latin1_byte_compatible_up_to(buffer)
6224 .unwrap(),
6225 1
6226 );
6227 assert!(
6228 UTF_16BE
6229 .new_decoder_without_bom_handling()
6230 .latin1_byte_compatible_up_to(buffer)
6231 .is_none()
6232 );
6233 assert!(
6234 UTF_16LE
6235 .new_decoder_without_bom_handling()
6236 .latin1_byte_compatible_up_to(buffer)
6237 .is_none()
6238 );
6239 assert_eq!(
6240 ISO_2022_JP
6241 .new_decoder_without_bom_handling()
6242 .latin1_byte_compatible_up_to(buffer)
6243 .unwrap(),
6244 1
6245 );
6246
6247 assert_eq!(
6248 IBM866
6249 .new_decoder_without_bom_handling()
6250 .latin1_byte_compatible_up_to(buffer)
6251 .unwrap(),
6252 1
6253 );
6254 assert_eq!(
6255 ISO_8859_2
6256 .new_decoder_without_bom_handling()
6257 .latin1_byte_compatible_up_to(buffer)
6258 .unwrap(),
6259 2
6260 );
6261 assert_eq!(
6262 ISO_8859_3
6263 .new_decoder_without_bom_handling()
6264 .latin1_byte_compatible_up_to(buffer)
6265 .unwrap(),
6266 2
6267 );
6268 assert_eq!(
6269 ISO_8859_4
6270 .new_decoder_without_bom_handling()
6271 .latin1_byte_compatible_up_to(buffer)
6272 .unwrap(),
6273 2
6274 );
6275 assert_eq!(
6276 ISO_8859_5
6277 .new_decoder_without_bom_handling()
6278 .latin1_byte_compatible_up_to(buffer)
6279 .unwrap(),
6280 2
6281 );
6282 assert_eq!(
6283 ISO_8859_6
6284 .new_decoder_without_bom_handling()
6285 .latin1_byte_compatible_up_to(buffer)
6286 .unwrap(),
6287 2
6288 );
6289 assert_eq!(
6290 ISO_8859_7
6291 .new_decoder_without_bom_handling()
6292 .latin1_byte_compatible_up_to(buffer)
6293 .unwrap(),
6294 2
6295 );
6296 assert_eq!(
6297 ISO_8859_8
6298 .new_decoder_without_bom_handling()
6299 .latin1_byte_compatible_up_to(buffer)
6300 .unwrap(),
6301 3
6302 );
6303 assert_eq!(
6304 ISO_8859_10
6305 .new_decoder_without_bom_handling()
6306 .latin1_byte_compatible_up_to(buffer)
6307 .unwrap(),
6308 2
6309 );
6310 assert_eq!(
6311 ISO_8859_13
6312 .new_decoder_without_bom_handling()
6313 .latin1_byte_compatible_up_to(buffer)
6314 .unwrap(),
6315 4
6316 );
6317 assert_eq!(
6318 ISO_8859_14
6319 .new_decoder_without_bom_handling()
6320 .latin1_byte_compatible_up_to(buffer)
6321 .unwrap(),
6322 4
6323 );
6324 assert_eq!(
6325 ISO_8859_15
6326 .new_decoder_without_bom_handling()
6327 .latin1_byte_compatible_up_to(buffer)
6328 .unwrap(),
6329 6
6330 );
6331 assert_eq!(
6332 ISO_8859_16
6333 .new_decoder_without_bom_handling()
6334 .latin1_byte_compatible_up_to(buffer)
6335 .unwrap(),
6336 4
6337 );
6338 assert_eq!(
6339 ISO_8859_8_I
6340 .new_decoder_without_bom_handling()
6341 .latin1_byte_compatible_up_to(buffer)
6342 .unwrap(),
6343 3
6344 );
6345 assert_eq!(
6346 KOI8_R
6347 .new_decoder_without_bom_handling()
6348 .latin1_byte_compatible_up_to(buffer)
6349 .unwrap(),
6350 1
6351 );
6352 assert_eq!(
6353 KOI8_U
6354 .new_decoder_without_bom_handling()
6355 .latin1_byte_compatible_up_to(buffer)
6356 .unwrap(),
6357 1
6358 );
6359 assert_eq!(
6360 MACINTOSH
6361 .new_decoder_without_bom_handling()
6362 .latin1_byte_compatible_up_to(buffer)
6363 .unwrap(),
6364 1
6365 );
6366 assert_eq!(
6367 WINDOWS_874
6368 .new_decoder_without_bom_handling()
6369 .latin1_byte_compatible_up_to(buffer)
6370 .unwrap(),
6371 2
6372 );
6373 assert_eq!(
6374 WINDOWS_1250
6375 .new_decoder_without_bom_handling()
6376 .latin1_byte_compatible_up_to(buffer)
6377 .unwrap(),
6378 4
6379 );
6380 assert_eq!(
6381 WINDOWS_1251
6382 .new_decoder_without_bom_handling()
6383 .latin1_byte_compatible_up_to(buffer)
6384 .unwrap(),
6385 1
6386 );
6387 assert_eq!(
6388 WINDOWS_1252
6389 .new_decoder_without_bom_handling()
6390 .latin1_byte_compatible_up_to(buffer)
6391 .unwrap(),
6392 5
6393 );
6394 assert_eq!(
6395 WINDOWS_1253
6396 .new_decoder_without_bom_handling()
6397 .latin1_byte_compatible_up_to(buffer)
6398 .unwrap(),
6399 3
6400 );
6401 assert_eq!(
6402 WINDOWS_1254
6403 .new_decoder_without_bom_handling()
6404 .latin1_byte_compatible_up_to(buffer)
6405 .unwrap(),
6406 4
6407 );
6408 assert_eq!(
6409 WINDOWS_1255
6410 .new_decoder_without_bom_handling()
6411 .latin1_byte_compatible_up_to(buffer)
6412 .unwrap(),
6413 3
6414 );
6415 assert_eq!(
6416 WINDOWS_1256
6417 .new_decoder_without_bom_handling()
6418 .latin1_byte_compatible_up_to(buffer)
6419 .unwrap(),
6420 1
6421 );
6422 assert_eq!(
6423 WINDOWS_1257
6424 .new_decoder_without_bom_handling()
6425 .latin1_byte_compatible_up_to(buffer)
6426 .unwrap(),
6427 4
6428 );
6429 assert_eq!(
6430 WINDOWS_1258
6431 .new_decoder_without_bom_handling()
6432 .latin1_byte_compatible_up_to(buffer)
6433 .unwrap(),
6434 4
6435 );
6436 assert_eq!(
6437 X_MAC_CYRILLIC
6438 .new_decoder_without_bom_handling()
6439 .latin1_byte_compatible_up_to(buffer)
6440 .unwrap(),
6441 1
6442 );
6443 assert_eq!(
6444 X_USER_DEFINED
6445 .new_decoder_without_bom_handling()
6446 .latin1_byte_compatible_up_to(buffer)
6447 .unwrap(),
6448 1
6449 );
6450
6451 assert!(
6452 UTF_8
6453 .new_decoder()
6454 .latin1_byte_compatible_up_to(buffer)
6455 .is_none()
6456 );
6457
6458 let mut decoder = UTF_8.new_decoder();
6459 let mut output = [0u16; 4];
6460 let _ = decoder.decode_to_utf16(b"\xEF", &mut output, false);
6461 assert!(decoder.latin1_byte_compatible_up_to(buffer).is_none());
6462 let _ = decoder.decode_to_utf16(b"\xBB\xBF", &mut output, false);
6463 assert_eq!(decoder.latin1_byte_compatible_up_to(buffer), Some(1));
6464 let _ = decoder.decode_to_utf16(b"\xEF", &mut output, false);
6465 assert_eq!(decoder.latin1_byte_compatible_up_to(buffer), None);
6466 }
6467
6468 #[test]
6469 fn test_byte_destination_check_space_two() {
6470 let input8 = "abc\u{4E00}";
6471 let input16 = &[0x0061u16, 0x0062, 0x0063, 0x4E00];
6472 let mut out4 = [0u8; 4];
6473 {
6474 let mut encoder = SHIFT_JIS.new_encoder();
6475 let (r, read, written) =
6476 encoder.encode_from_utf16_without_replacement(input16, &mut out4, false);
6477 assert_eq!(r, EncoderResult::OutputFull);
6478 assert_eq!(read, 3);
6479 assert_eq!(written, 3);
6480 }
6481 {
6482 let mut encoder = SHIFT_JIS.new_encoder();
6483 let (r, read, written) =
6484 encoder.encode_from_utf8_without_replacement(input8, &mut out4, false);
6485 assert_eq!(r, EncoderResult::OutputFull);
6486 assert_eq!(read, 3);
6487 assert_eq!(written, 3);
6488 }
6489 }
6490
6491 #[test]
6492 fn test_byte_destination_check_space_four() {
6493 let input8 = "abc\u{FF00}";
6494 let input16 = &[0x0061u16, 0x0062, 0x0063, 0xFF00];
6495 let mut out6 = [0u8; 6];
6496 {
6497 let mut encoder = GB18030.new_encoder();
6498 let (r, read, written) =
6499 encoder.encode_from_utf16_without_replacement(input16, &mut out6, false);
6500 assert_eq!(r, EncoderResult::OutputFull);
6501 assert_eq!(read, 3);
6502 assert_eq!(written, 3);
6503 }
6504 {
6505 let mut encoder = GB18030.new_encoder();
6506 let (r, read, written) =
6507 encoder.encode_from_utf8_without_replacement(input8, &mut out6, false);
6508 assert_eq!(r, EncoderResult::OutputFull);
6509 assert_eq!(read, 3);
6510 assert_eq!(written, 3);
6511 }
6512 }
6513}