]>
git.proxmox.com Git - rustc.git/blob - vendor/encoding_rs/src/testing.rs
1 // Copyright Mozilla Foundation. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
12 pub fn decode(encoding
: &'
static Encoding
, bytes
: &[u8], expect
: &str) {
13 let mut vec
= Vec
::with_capacity(bytes
.len() + 32);
14 let mut string
= String
::with_capacity(expect
.len() + 32);
15 let range
= if cfg
!(miri
) {
24 let c
= 0x40u8 + (j
as u8);
26 string
.push(c
as char);
28 vec
.extend_from_slice(bytes
);
29 string
.push_str(expect
);
30 decode_without_padding_impl(encoding
, &vec
[..], &string
[..], i
);
34 pub fn decode_without_padding(encoding
: &'
static Encoding
, bytes
: &[u8], expect
: &str) {
35 decode_without_padding_impl(encoding
, bytes
, expect
, 0);
38 fn decode_without_padding_impl(
39 encoding
: &'
static Encoding
,
44 decode_to_utf8_impl(encoding
, bytes
, expect
, padding
);
45 decode_to_utf16_impl(encoding
, bytes
, &utf16_from_utf8(expect
)[..], padding
);
46 decode_to_string(encoding
, bytes
, expect
);
49 pub fn encode(encoding
: &'
static Encoding
, str: &str, expect
: &[u8]) {
50 let mut vec
= Vec
::with_capacity(expect
.len() + 32);
51 let mut string
= String
::with_capacity(str.len() + 32);
52 let range
= if cfg
!(miri
) {
61 let c
= 0x40u8 + (j
as u8);
63 string
.push(c
as char);
65 vec
.extend_from_slice(expect
);
67 encode_without_padding(encoding
, &string
[..], &vec
[..]);
71 pub fn encode_without_padding(encoding
: &'
static Encoding
, string
: &str, expect
: &[u8]) {
72 encode_from_utf8(encoding
, string
, expect
);
73 encode_from_utf16(encoding
, &utf16_from_utf8(string
)[..], expect
);
74 encode_to_vec(encoding
, string
, expect
);
77 pub fn decode_to_utf16(encoding
: &'
static Encoding
, bytes
: &[u8], expect
: &[u16]) {
78 decode_to_utf16_impl(encoding
, bytes
, expect
, 0);
81 pub fn decode_to_utf16_impl(
82 encoding
: &'
static Encoding
,
87 for i
in padding
..bytes
.len() {
88 let (head
, tail
) = bytes
.split_at(i
);
89 decode_to_utf16_with_boundary(encoding
, head
, tail
, expect
);
93 pub fn decode_to_utf16_with_boundary(
94 encoding
: &'
static Encoding
,
99 let mut decoder
= encoding
.new_decoder();
100 let mut dest
: Vec
<u16> = Vec
::with_capacity(
102 .max_utf16_buffer_length(head
.len() + tail
.len())
105 let capacity
= dest
.capacity();
106 dest
.resize(capacity
, 0u16);
107 let mut total_read
= 0;
108 let mut total_written
= 0;
110 let (complete
, read
, written
, _
) = decoder
.decode_to_utf16(head
, &mut dest
, false);
112 CoderResult
::InputEmpty
=> {}
113 CoderResult
::OutputFull
=> {
118 total_written
+= written
;
121 let (complete
, read
, written
, _
) =
122 decoder
.decode_to_utf16(tail
, &mut dest
[total_written
..], true);
124 CoderResult
::InputEmpty
=> {}
125 CoderResult
::OutputFull
=> {
130 total_written
+= written
;
132 assert_eq
!(total_read
, head
.len() + tail
.len());
133 assert_eq
!(total_written
, expect
.len());
134 dest
.truncate(total_written
);
135 assert_eq
!(&dest
[..], expect
);
138 pub fn decode_to_utf8(encoding
: &'
static Encoding
, bytes
: &[u8], expect
: &str) {
139 decode_to_utf8_impl(encoding
, bytes
, expect
, 0);
142 pub fn decode_to_utf8_impl(
143 encoding
: &'
static Encoding
,
148 for i
in padding
..bytes
.len() {
149 let (head
, tail
) = bytes
.split_at(i
);
150 decode_to_utf8_with_boundary(encoding
, head
, tail
, expect
);
154 pub fn decode_to_utf8_with_boundary(
155 encoding
: &'
static Encoding
,
160 let mut decoder
= encoding
.new_decoder();
161 let mut dest
: Vec
<u8> = Vec
::with_capacity(
163 .max_utf8_buffer_length(head
.len() + tail
.len())
166 let capacity
= dest
.capacity();
167 dest
.resize(capacity
, 0u8);
168 let mut total_read
= 0;
169 let mut total_written
= 0;
171 let (complete
, read
, written
, _
) = decoder
.decode_to_utf8(head
, &mut dest
, false);
173 CoderResult
::InputEmpty
=> {}
174 CoderResult
::OutputFull
=> {
179 total_written
+= written
;
182 let (complete
, read
, written
, _
) =
183 decoder
.decode_to_utf8(tail
, &mut dest
[total_written
..], true);
185 CoderResult
::InputEmpty
=> {}
186 CoderResult
::OutputFull
=> {
191 total_written
+= written
;
193 assert_eq
!(total_read
, head
.len() + tail
.len());
194 assert_eq
!(total_written
, expect
.len());
195 dest
.truncate(total_written
);
196 assert_eq
!(&dest
[..], expect
.as_bytes());
199 pub fn decode_to_string(encoding
: &'
static Encoding
, bytes
: &[u8], expect
: &str) {
200 let (cow
, _
, _
) = encoding
.decode(bytes
);
201 assert_eq
!(&cow
[..], expect
);
204 pub fn encode_from_utf8(encoding
: &'
static Encoding
, string
: &str, expect
: &[u8]) {
205 let mut encoder
= encoding
.new_encoder();
206 let mut dest
: Vec
<u8> = Vec
::with_capacity(10 * (string
.len() + 1)); // 10 is replacement worst case
207 let capacity
= dest
.capacity();
208 dest
.resize(capacity
, 0u8);
209 let (complete
, read
, written
, _
) = encoder
.encode_from_utf8(string
, &mut dest
, true);
211 CoderResult
::InputEmpty
=> {}
212 CoderResult
::OutputFull
=> {
216 assert_eq
!(read
, string
.len());
217 assert_eq
!(written
, expect
.len());
218 dest
.truncate(written
);
219 assert_eq
!(&dest
[..], expect
);
222 pub fn encode_from_utf16(encoding
: &'
static Encoding
, string
: &[u16], expect
: &[u8]) {
223 let mut encoder
= encoding
.new_encoder();
224 let mut dest
: Vec
<u8> = Vec
::with_capacity(10 * (string
.len() + 1)); // 10 is replacement worst case
225 let capacity
= dest
.capacity();
226 dest
.resize(capacity
, 0u8);
227 let (complete
, read
, written
, _
) = encoder
.encode_from_utf16(string
, &mut dest
, true);
229 CoderResult
::InputEmpty
=> {}
230 CoderResult
::OutputFull
=> {
234 assert_eq
!(read
, string
.len());
235 // assert_eq!(written, expect.len());
236 dest
.truncate(written
);
237 assert_eq
!(&dest
[..], expect
);
240 pub fn encode_to_vec(encoding
: &'
static Encoding
, string
: &str, expect
: &[u8]) {
241 let (cow
, _
, _
) = encoding
.encode(string
);
242 assert_eq
!(&cow
[..], expect
);
245 pub fn utf16_from_utf8(string
: &str) -> Vec
<u16> {
246 let mut decoder
= UTF_8
.new_decoder_without_bom_handling();
247 let mut vec
= Vec
::with_capacity(decoder
.max_utf16_buffer_length(string
.len()).unwrap());
248 let capacity
= vec
.capacity();
249 vec
.resize(capacity
, 0);
251 let (result
, read
, written
) =
252 decoder
.decode_to_utf16_without_replacement(string
.as_bytes(), &mut vec
[..], true);
254 DecoderResult
::InputEmpty
=> {
255 debug_assert_eq
!(read
, string
.len());
256 vec
.resize(written
, 0);
259 DecoderResult
::Malformed(_
, _
) => unreachable
!("Malformed"),
260 DecoderResult
::OutputFull
=> unreachable
!("Output full"),