Change encoding/decoding function naming.
I think this makes it more obvious at-a-glance what they do.
This commit is contained in:
parent
37acbba36a
commit
006b578318
|
@ -8,7 +8,7 @@
|
|||
use core;
|
||||
use {DecodeResult, EncodeError, EncodeResult};
|
||||
|
||||
pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
pub fn encode_from_str<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
// Do the encode.
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
@ -41,7 +41,7 @@ pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'
|
|||
Ok((input_i, &output[..output_i]))
|
||||
}
|
||||
|
||||
pub fn decode_to_utf8<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
pub fn decode_to_str<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
for &byte in input.iter() {
|
||||
|
|
|
@ -13,36 +13,36 @@ mod utils;
|
|||
mod windows1252;
|
||||
|
||||
/// Encodes text from utf8 to a destination encoding.
|
||||
pub fn encode_from_utf8<'a>(
|
||||
pub fn encode_from_str<'a>(
|
||||
output_encoding: Encoding,
|
||||
input: &str,
|
||||
output: &'a mut [u8],
|
||||
) -> EncodeResult<'a> {
|
||||
match output_encoding {
|
||||
Encoding::Utf8 => utf8::encode_from_utf8(input, output),
|
||||
Encoding::Utf16BE => utf16_be::encode_from_utf8(input, output),
|
||||
Encoding::Utf16LE => utf16_le::encode_from_utf8(input, output),
|
||||
Encoding::Utf32BE => utf32_be::encode_from_utf8(input, output),
|
||||
Encoding::Utf32LE => utf32_le::encode_from_utf8(input, output),
|
||||
Encoding::Latin1 => latin1::encode_from_utf8(input, output),
|
||||
Encoding::Windows1252 => windows1252::encode_from_utf8(input, output),
|
||||
Encoding::Utf8 => utf8::encode_from_str(input, output),
|
||||
Encoding::Utf16BE => utf16_be::encode_from_str(input, output),
|
||||
Encoding::Utf16LE => utf16_le::encode_from_str(input, output),
|
||||
Encoding::Utf32BE => utf32_be::encode_from_str(input, output),
|
||||
Encoding::Utf32LE => utf32_le::encode_from_str(input, output),
|
||||
Encoding::Latin1 => latin1::encode_from_str(input, output),
|
||||
Encoding::Windows1252 => windows1252::encode_from_str(input, output),
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes text from a source encoding to utf8.
|
||||
pub fn decode_to_utf8<'a>(
|
||||
pub fn decode_to_str<'a>(
|
||||
input_encoding: Encoding,
|
||||
input: &[u8],
|
||||
output: &'a mut [u8],
|
||||
) -> DecodeResult<'a> {
|
||||
match input_encoding {
|
||||
Encoding::Utf8 => utf8::decode_to_utf8(input, output),
|
||||
Encoding::Utf16BE => utf16_be::decode_to_utf8(input, output),
|
||||
Encoding::Utf16LE => utf16_le::decode_to_utf8(input, output),
|
||||
Encoding::Utf32BE => utf32_be::decode_to_utf8(input, output),
|
||||
Encoding::Utf32LE => utf32_le::decode_to_utf8(input, output),
|
||||
Encoding::Latin1 => latin1::decode_to_utf8(input, output),
|
||||
Encoding::Windows1252 => windows1252::decode_to_utf8(input, output),
|
||||
Encoding::Utf8 => utf8::decode_to_str(input, output),
|
||||
Encoding::Utf16BE => utf16_be::decode_to_str(input, output),
|
||||
Encoding::Utf16LE => utf16_le::decode_to_str(input, output),
|
||||
Encoding::Utf32BE => utf32_be::decode_to_str(input, output),
|
||||
Encoding::Utf32LE => utf32_le::decode_to_str(input, output),
|
||||
Encoding::Latin1 => latin1::decode_to_str(input, output),
|
||||
Encoding::Windows1252 => windows1252::decode_to_str(input, output),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ use core;
|
|||
use utils::{from_big_endian_u16, to_big_endian_u16};
|
||||
use {DecodeError, DecodeResult, EncodeResult};
|
||||
|
||||
pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
pub fn encode_from_str<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
// Do the encode.
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
@ -53,7 +53,7 @@ pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'
|
|||
Ok((input_i, &output[..output_i]))
|
||||
}
|
||||
|
||||
pub fn decode_to_utf8<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
pub fn decode_to_str<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
||||
|
@ -127,7 +127,7 @@ mod tests {
|
|||
fn encode_01() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 1];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(encoded, &[]);
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ mod tests {
|
|||
fn encode_02() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 2];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 3);
|
||||
assert_eq!(encoded, &[0x30, 0x53]);
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ mod tests {
|
|||
fn encode_03() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 3);
|
||||
assert_eq!(encoded, &[0x30, 0x53]);
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ mod tests {
|
|||
fn encode_04() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(encoded, &[]);
|
||||
}
|
||||
|
@ -163,7 +163,7 @@ mod tests {
|
|||
fn encode_05() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(encoded, &[0xD8, 0x3D, 0xDE, 0x3A]);
|
||||
}
|
||||
|
@ -172,7 +172,7 @@ mod tests {
|
|||
fn encode_06() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(encoded, &[0xD8, 0x3D, 0xDE, 0x3A]);
|
||||
}
|
||||
|
@ -183,7 +183,7 @@ mod tests {
|
|||
0x30, 0x53, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 2];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(decoded, "");
|
||||
}
|
||||
|
@ -194,7 +194,7 @@ mod tests {
|
|||
0x30, 0x53, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 2);
|
||||
assert_eq!(decoded, "こ");
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ mod tests {
|
|||
0x30, 0x53, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 5];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 2);
|
||||
assert_eq!(decoded, "こ");
|
||||
}
|
||||
|
@ -214,7 +214,7 @@ mod tests {
|
|||
fn decode_04() {
|
||||
let data = [0xD8, 0x3D, 0xDE, 0x3A, 0xD8, 0x3D, 0xDE, 0x3C]; // "😺😼"
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(decoded, "");
|
||||
}
|
||||
|
@ -223,7 +223,7 @@ mod tests {
|
|||
fn decode_05() {
|
||||
let data = [0xD8, 0x3D, 0xDE, 0x3A, 0xD8, 0x3D, 0xDE, 0x3C]; // "😺😼"
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ mod tests {
|
|||
fn decode_06() {
|
||||
let data = [0xD8, 0x3D, 0xDE, 0x3A, 0xD8, 0x3D, 0xDE, 0x3C]; // "😺😼"
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -241,7 +241,7 @@ mod tests {
|
|||
fn decode_07() {
|
||||
let data = [0xD8, 0x3D, 0xDE, 0x3A, 0xD8, 0x3D]; // "😺😼" with last codepoint chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -250,7 +250,7 @@ mod tests {
|
|||
fn decode_08() {
|
||||
let data = [0xD8, 0x3D, 0xDE, 0x3A, 0xD8, 0x3D, 0xDE]; // "😺😼" with last byte chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -259,7 +259,7 @@ mod tests {
|
|||
fn decode_09() {
|
||||
let data = [0xD8, 0x3D, 0xDE, 0x3A, 0xD8]; // "😺😼" with last 3 bytes chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -270,7 +270,7 @@ mod tests {
|
|||
0xDE, 0x3A, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!" with an error on the first char (end surrogate)
|
||||
let mut buf = [0u8; 2];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -286,7 +286,7 @@ mod tests {
|
|||
0x30, 0x53, 0xDE, 0x3A, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!" with an error on the second char (end surrogate)
|
||||
let mut buf = [0u8; 3];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -302,7 +302,7 @@ mod tests {
|
|||
0x30, 0x53, 0x30, 0x93, 0x30, 0x6B, 0xDE, 0x3A, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!" with an error on the fourth char (end surrogate)
|
||||
let mut buf = [0u8; 64];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -318,7 +318,7 @@ mod tests {
|
|||
0xD8, 0x3D, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!" with an error on the first char (start surrogate)
|
||||
let mut buf = [0u8; 2];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -334,7 +334,7 @@ mod tests {
|
|||
0x30, 0x53, 0xD8, 0x3D, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!" with an error on the second char (start surrogate)
|
||||
let mut buf = [0u8; 3];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -350,7 +350,7 @@ mod tests {
|
|||
0x30, 0x53, 0x30, 0x93, 0x30, 0x6B, 0xD8, 0x3D, 0x30, 0x6F, 0xFF, 0x01,
|
||||
]; // "こんにちは!" with an error on the fourth char (start surrogate)
|
||||
let mut buf = [0u8; 64];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
|
|
@ -8,7 +8,7 @@ use core;
|
|||
use utils::{from_little_endian_u16, to_little_endian_u16};
|
||||
use {DecodeError, DecodeResult, EncodeResult};
|
||||
|
||||
pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
pub fn encode_from_str<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
// Do the encode.
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
@ -53,7 +53,7 @@ pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'
|
|||
Ok((input_i, &output[..output_i]))
|
||||
}
|
||||
|
||||
pub fn decode_to_utf8<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
pub fn decode_to_str<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
||||
|
@ -127,7 +127,7 @@ mod tests {
|
|||
fn encode_01() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 1];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(encoded, &[]);
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ mod tests {
|
|||
fn encode_02() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 2];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 3);
|
||||
assert_eq!(encoded, &[0x53, 0x30]);
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ mod tests {
|
|||
fn encode_03() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 3);
|
||||
assert_eq!(encoded, &[0x53, 0x30]);
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ mod tests {
|
|||
fn encode_04() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(encoded, &[]);
|
||||
}
|
||||
|
@ -163,7 +163,7 @@ mod tests {
|
|||
fn encode_05() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(encoded, &[0x3D, 0xD8, 0x3A, 0xDE]);
|
||||
}
|
||||
|
@ -172,7 +172,7 @@ mod tests {
|
|||
fn encode_06() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(encoded, &[0x3D, 0xD8, 0x3A, 0xDE]);
|
||||
}
|
||||
|
@ -183,7 +183,7 @@ mod tests {
|
|||
0x53, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 2];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(decoded, "");
|
||||
}
|
||||
|
@ -194,7 +194,7 @@ mod tests {
|
|||
0x53, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 2);
|
||||
assert_eq!(decoded, "こ");
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ mod tests {
|
|||
0x53, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 5];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 2);
|
||||
assert_eq!(decoded, "こ");
|
||||
}
|
||||
|
@ -214,7 +214,7 @@ mod tests {
|
|||
fn decode_04() {
|
||||
let data = [0x3D, 0xD8, 0x3A, 0xDE, 0x3D, 0xD8, 0x3C, 0xDE]; // "😺😼"
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(decoded, "");
|
||||
}
|
||||
|
@ -223,7 +223,7 @@ mod tests {
|
|||
fn decode_05() {
|
||||
let data = [0x3D, 0xD8, 0x3A, 0xDE, 0x3D, 0xD8, 0x3C, 0xDE]; // "😺😼"
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ mod tests {
|
|||
fn decode_06() {
|
||||
let data = [0x3D, 0xD8, 0x3A, 0xDE, 0x3D, 0xD8, 0x3C, 0xDE]; // "😺😼"
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -241,7 +241,7 @@ mod tests {
|
|||
fn decode_07() {
|
||||
let data = [0x3D, 0xD8, 0x3A, 0xDE, 0x3D, 0xD8]; // "😺😼" with last codepoint chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -250,7 +250,7 @@ mod tests {
|
|||
fn decode_08() {
|
||||
let data = [0x3D, 0xD8, 0x3A, 0xDE, 0x3D, 0xD8, 0x3C]; // "😺😼" with last byte chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -259,7 +259,7 @@ mod tests {
|
|||
fn decode_09() {
|
||||
let data = [0x3D, 0xD8, 0x3A, 0xDE, 0x3D]; // "😺😼" with last 3 bytes chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -270,7 +270,7 @@ mod tests {
|
|||
0x3A, 0xDE, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!" with an error on the first char (end surrogate)
|
||||
let mut buf = [0u8; 2];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -286,7 +286,7 @@ mod tests {
|
|||
0x53, 0x30, 0x3A, 0xDE, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!" with an error on the second char (end surrogate)
|
||||
let mut buf = [0u8; 3];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -302,7 +302,7 @@ mod tests {
|
|||
0x53, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x3A, 0xDE, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!" with an error on the fourth char (end surrogate)
|
||||
let mut buf = [0u8; 64];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -318,7 +318,7 @@ mod tests {
|
|||
0x3D, 0xD8, 0x93, 0x30, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!" with an error on the first char (start surrogate)
|
||||
let mut buf = [0u8; 2];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -334,7 +334,7 @@ mod tests {
|
|||
0x53, 0x30, 0x3D, 0xD8, 0x6B, 0x30, 0x61, 0x30, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!" with an error on the second char (start surrogate)
|
||||
let mut buf = [0u8; 3];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
@ -350,7 +350,7 @@ mod tests {
|
|||
0x53, 0x30, 0x93, 0x30, 0x6B, 0x30, 0x3D, 0xD8, 0x6F, 0x30, 0x01, 0xFF,
|
||||
]; // "こんにちは!" with an error on the fourth char (start surrogate)
|
||||
let mut buf = [0u8; 64];
|
||||
let error = decode_to_utf8(&data, &mut buf);
|
||||
let error = decode_to_str(&data, &mut buf);
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(DecodeError {
|
||||
|
|
|
@ -8,7 +8,7 @@ use core;
|
|||
use utils::{from_big_endian_u32, to_big_endian_u32};
|
||||
use {DecodeError, DecodeResult, EncodeResult};
|
||||
|
||||
pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
pub fn encode_from_str<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
// Do the encode.
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
@ -38,7 +38,7 @@ pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'
|
|||
Ok((input_i, &output[..output_i]))
|
||||
}
|
||||
|
||||
pub fn decode_to_utf8<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
pub fn decode_to_str<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
||||
|
@ -86,7 +86,7 @@ mod tests {
|
|||
fn encode_01() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(encoded, &[]);
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ mod tests {
|
|||
fn encode_02() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 3);
|
||||
assert_eq!(encoded, &[0x00, 0x00, 0x30, 0x53]);
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ mod tests {
|
|||
fn encode_03() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 3);
|
||||
assert_eq!(encoded, &[0x00, 0x00, 0x30, 0x53]);
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ mod tests {
|
|||
fn encode_04() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(encoded, &[]);
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ mod tests {
|
|||
fn encode_05() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(encoded, &[0x00, 0x01, 0xF6, 0x3A]);
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ mod tests {
|
|||
fn encode_06() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(encoded, &[0x00, 0x01, 0xF6, 0x3A]);
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ mod tests {
|
|||
0x30, 0x61, 0x00, 0x00, 0x30, 0x6F, 0x00, 0x00, 0xFF, 0x01,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 2];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(decoded, "");
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ mod tests {
|
|||
0x30, 0x61, 0x00, 0x00, 0x30, 0x6F, 0x00, 0x00, 0xFF, 0x01,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "こ");
|
||||
}
|
||||
|
@ -167,7 +167,7 @@ mod tests {
|
|||
0x30, 0x61, 0x00, 0x00, 0x30, 0x6F, 0x00, 0x00, 0xFF, 0x01,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 5];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "こ");
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ mod tests {
|
|||
fn decode_04() {
|
||||
let data = [0x00, 0x01, 0xF6, 0x3A, 0x00, 0x01, 0xF6, 0x3C]; // "😺😼"
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(decoded, "");
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ mod tests {
|
|||
fn decode_05() {
|
||||
let data = [0x00, 0x01, 0xF6, 0x3A, 0x00, 0x01, 0xF6, 0x3C]; // "😺😼"
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -194,7 +194,7 @@ mod tests {
|
|||
fn decode_06() {
|
||||
let data = [0x00, 0x01, 0xF6, 0x3A, 0x00, 0x01, 0xF6, 0x3C]; // "😺😼"
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ mod tests {
|
|||
fn decode_07() {
|
||||
let data = [0x00, 0x01, 0xF6, 0x3A, 0x00, 0x01, 0xF6]; // "😺😼" with last byte chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -212,7 +212,7 @@ mod tests {
|
|||
fn decode_08() {
|
||||
let data = [0x00, 0x01, 0xF6, 0x3A, 0x00, 0x01]; // "😺😼" with last 2 bytes chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -221,7 +221,7 @@ mod tests {
|
|||
fn decode_09() {
|
||||
let data = [0x00, 0x01, 0xF6, 0x3A, 0x00]; // "😺😼" with last 3 bytes chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -234,7 +234,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the first char (value out of range)
|
||||
let mut buf = [0u8; 2];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (0, 4),
|
||||
output_bytes_written: 0,
|
||||
|
@ -250,7 +250,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the first char (value in surrogate range)
|
||||
let mut buf = [0u8; 2];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (0, 4),
|
||||
output_bytes_written: 0,
|
||||
|
@ -266,7 +266,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the first char (value in surrogate range)
|
||||
let mut buf = [0u8; 64];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (0, 4),
|
||||
output_bytes_written: 0,
|
||||
|
@ -282,7 +282,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the second char (value out of range)
|
||||
let mut buf = [0u8; 64];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (4, 8),
|
||||
output_bytes_written: 3,
|
||||
|
@ -299,7 +299,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the second char (value in surrogate range)
|
||||
let mut buf = [0u8; 64];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (4, 8),
|
||||
output_bytes_written: 3,
|
||||
|
@ -316,7 +316,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the second char (value in surrogate range)
|
||||
let mut buf = [0u8; 64];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (4, 8),
|
||||
output_bytes_written: 3,
|
||||
|
|
|
@ -8,7 +8,7 @@ use core;
|
|||
use utils::{from_little_endian_u32, to_little_endian_u32};
|
||||
use {DecodeError, DecodeResult, EncodeResult};
|
||||
|
||||
pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
pub fn encode_from_str<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
// Do the encode.
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
@ -38,7 +38,7 @@ pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'
|
|||
Ok((input_i, &output[..output_i]))
|
||||
}
|
||||
|
||||
pub fn decode_to_utf8<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
pub fn decode_to_str<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
||||
|
@ -86,7 +86,7 @@ mod tests {
|
|||
fn encode_01() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(encoded, &[]);
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ mod tests {
|
|||
fn encode_02() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 3);
|
||||
assert_eq!(encoded, &[0x53, 0x30, 0x00, 0x00]);
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ mod tests {
|
|||
fn encode_03() {
|
||||
let text = "こんにちは!";
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 3);
|
||||
assert_eq!(encoded, &[0x53, 0x30, 0x00, 0x00]);
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ mod tests {
|
|||
fn encode_04() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(encoded, &[]);
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ mod tests {
|
|||
fn encode_05() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(encoded, &[0x3A, 0xF6, 0x01, 0x00]);
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ mod tests {
|
|||
fn encode_06() {
|
||||
let text = "😺😼";
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, encoded) = encode_from_utf8(text, &mut buf).unwrap();
|
||||
let (consumed_count, encoded) = encode_from_str(text, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(encoded, &[0x3A, 0xF6, 0x01, 0x00]);
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ mod tests {
|
|||
0x00, 0x00, 0x6F, 0x30, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 2];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(decoded, "");
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ mod tests {
|
|||
0x00, 0x00, 0x6F, 0x30, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "こ");
|
||||
}
|
||||
|
@ -167,7 +167,7 @@ mod tests {
|
|||
0x00, 0x00, 0x6F, 0x30, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00,
|
||||
]; // "こんにちは!"
|
||||
let mut buf = [0u8; 5];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "こ");
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ mod tests {
|
|||
fn decode_04() {
|
||||
let data = [0x3A, 0xF6, 0x01, 0x00, 0x3C, 0xF6, 0x01, 0x00]; // "😺😼"
|
||||
let mut buf = [0u8; 3];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 0);
|
||||
assert_eq!(decoded, "");
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ mod tests {
|
|||
fn decode_05() {
|
||||
let data = [0x3A, 0xF6, 0x01, 0x00, 0x3C, 0xF6, 0x01, 0x00]; // "😺😼"
|
||||
let mut buf = [0u8; 4];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -194,7 +194,7 @@ mod tests {
|
|||
fn decode_06() {
|
||||
let data = [0x3A, 0xF6, 0x01, 0x00, 0x3C, 0xF6, 0x01, 0x00]; // "😺😼"
|
||||
let mut buf = [0u8; 7];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ mod tests {
|
|||
fn decode_07() {
|
||||
let data = [0x3A, 0xF6, 0x01, 0x00, 0x3C, 0xF6, 0x01]; // "😺😼" with last byte chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -212,7 +212,7 @@ mod tests {
|
|||
fn decode_08() {
|
||||
let data = [0x3A, 0xF6, 0x01, 0x00, 0x3C, 0xF6]; // "😺😼" with last 2 bytes chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -221,7 +221,7 @@ mod tests {
|
|||
fn decode_09() {
|
||||
let data = [0x3A, 0xF6, 0x01, 0x00, 0x3C]; // "😺😼" with last 3 bytes chopped off.
|
||||
let mut buf = [0u8; 64];
|
||||
let (consumed_count, decoded) = decode_to_utf8(&data, &mut buf).unwrap();
|
||||
let (consumed_count, decoded) = decode_to_str(&data, &mut buf).unwrap();
|
||||
assert_eq!(consumed_count, 4);
|
||||
assert_eq!(decoded, "😺");
|
||||
}
|
||||
|
@ -234,7 +234,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the first char (value out of range)
|
||||
let mut buf = [0u8; 2];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (0, 4),
|
||||
output_bytes_written: 0,
|
||||
|
@ -250,7 +250,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the first char (value in surrogate range)
|
||||
let mut buf = [0u8; 2];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (0, 4),
|
||||
output_bytes_written: 0,
|
||||
|
@ -266,7 +266,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the first char (value in surrogate range)
|
||||
let mut buf = [0u8; 64];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (0, 4),
|
||||
output_bytes_written: 0,
|
||||
|
@ -282,7 +282,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the second char (value out of range)
|
||||
let mut buf = [0u8; 64];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (4, 8),
|
||||
output_bytes_written: 3,
|
||||
|
@ -299,7 +299,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the second char (value in surrogate range)
|
||||
let mut buf = [0u8; 64];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (4, 8),
|
||||
output_bytes_written: 3,
|
||||
|
@ -316,7 +316,7 @@ mod tests {
|
|||
]; // "こんにちは!" with an error on the second char (value in surrogate range)
|
||||
let mut buf = [0u8; 64];
|
||||
assert_eq!(
|
||||
decode_to_utf8(&data, &mut buf),
|
||||
decode_to_str(&data, &mut buf),
|
||||
Err(DecodeError {
|
||||
error_range: (4, 8),
|
||||
output_bytes_written: 3,
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
//! These functions are essentially redundant, since they're supposedly
|
||||
//! encoding/decoding between utf8 and... utf8. However, `decode_to_utf8()`
|
||||
//! encoding/decoding between utf8 and... utf8. However, `decode_to_str()`
|
||||
//! is still useful for validating unknown input. And they allow a uniform
|
||||
//! API for all encodings.
|
||||
|
||||
use core;
|
||||
use {DecodeError, DecodeResult, EncodeResult};
|
||||
|
||||
pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
pub fn encode_from_str<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
let copy_len = {
|
||||
if output.len() >= input.len() {
|
||||
input.len()
|
||||
|
@ -24,7 +24,7 @@ pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'
|
|||
Ok((copy_len, &output[..copy_len]))
|
||||
}
|
||||
|
||||
pub fn decode_to_utf8<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
pub fn decode_to_str<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
let valid_up_to = match core::str::from_utf8(input) {
|
||||
Ok(text) => text.len(),
|
||||
Err(e) => {
|
||||
|
@ -39,7 +39,7 @@ pub fn decode_to_utf8<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a
|
|||
}
|
||||
};
|
||||
|
||||
let (in_consumed, out_slice) = encode_from_utf8(
|
||||
let (in_consumed, out_slice) = encode_from_str(
|
||||
unsafe { core::str::from_utf8_unchecked(&input[..valid_up_to]) },
|
||||
output,
|
||||
).unwrap();
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
use core;
|
||||
use {DecodeError, DecodeResult, EncodeError, EncodeResult};
|
||||
|
||||
pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
pub fn encode_from_str<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'a> {
|
||||
// Do the encode.
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
|
@ -37,7 +37,7 @@ pub fn encode_from_utf8<'a>(input: &str, output: &'a mut [u8]) -> EncodeResult<'
|
|||
Ok((input_i, &output[..output_i]))
|
||||
}
|
||||
|
||||
pub fn decode_to_utf8<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
pub fn decode_to_str<'a>(input: &[u8], output: &'a mut [u8]) -> DecodeResult<'a> {
|
||||
let mut input_i = 0;
|
||||
let mut output_i = 0;
|
||||
for &byte in input.iter() {
|
||||
|
|
|
@ -4,7 +4,7 @@ extern crate text_encoding;
|
|||
|
||||
use proptest::collection::vec;
|
||||
use proptest::test_runner::Config;
|
||||
use text_encoding::{decode_to_utf8, encode_from_utf8, Encoding};
|
||||
use text_encoding::{decode_to_str, encode_from_str, Encoding};
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(Config::with_cases(512))]
|
||||
|
@ -18,7 +18,7 @@ proptest! {
|
|||
// Encode to utf8
|
||||
let mut tmp = &text[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, encoded)) = encode_from_utf8(Encoding::Utf8, tmp, &mut buf) {
|
||||
if let Ok((n, encoded)) = encode_from_str(Encoding::Utf8, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf8_encoded.extend_from_slice(encoded);
|
||||
} else {
|
||||
|
@ -29,7 +29,7 @@ proptest! {
|
|||
// Decode back from utf8
|
||||
let mut tmp = &utf8_encoded[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, decoded)) = decode_to_utf8(Encoding::Utf8, tmp, &mut buf) {
|
||||
if let Ok((n, decoded)) = decode_to_str(Encoding::Utf8, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf8.extend(decoded.chars());
|
||||
} else {
|
||||
|
@ -51,7 +51,7 @@ proptest! {
|
|||
// Encode to utf16 big endian
|
||||
let mut tmp = &text[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, encoded)) = encode_from_utf8(Encoding::Utf16BE, tmp, &mut buf) {
|
||||
if let Ok((n, encoded)) = encode_from_str(Encoding::Utf16BE, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf16.extend_from_slice(encoded);
|
||||
} else {
|
||||
|
@ -62,7 +62,7 @@ proptest! {
|
|||
// Decode back from utf16 big endian
|
||||
let mut tmp = &utf16[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, decoded)) = decode_to_utf8(Encoding::Utf16BE, tmp, &mut buf) {
|
||||
if let Ok((n, decoded)) = decode_to_str(Encoding::Utf16BE, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf8.extend(decoded.chars());
|
||||
} else {
|
||||
|
@ -82,7 +82,7 @@ proptest! {
|
|||
// Encode to utf16 little endian
|
||||
let mut tmp = &text[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, encoded)) = encode_from_utf8(Encoding::Utf16LE, tmp, &mut buf) {
|
||||
if let Ok((n, encoded)) = encode_from_str(Encoding::Utf16LE, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf16.extend_from_slice(encoded);
|
||||
} else {
|
||||
|
@ -93,7 +93,7 @@ proptest! {
|
|||
// Decode back from utf16 big endian
|
||||
let mut tmp = &utf16[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, decoded)) = decode_to_utf8(Encoding::Utf16LE, tmp, &mut buf) {
|
||||
if let Ok((n, decoded)) = decode_to_str(Encoding::Utf16LE, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf8.extend(decoded.chars());
|
||||
} else {
|
||||
|
@ -113,7 +113,7 @@ proptest! {
|
|||
// Encode to utf32 big endian
|
||||
let mut tmp = &text[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, encoded)) = encode_from_utf8(Encoding::Utf32BE, tmp, &mut buf) {
|
||||
if let Ok((n, encoded)) = encode_from_str(Encoding::Utf32BE, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf32.extend_from_slice(encoded);
|
||||
} else {
|
||||
|
@ -124,7 +124,7 @@ proptest! {
|
|||
// Decode back from utf32 big endian
|
||||
let mut tmp = &utf32[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, decoded)) = decode_to_utf8(Encoding::Utf32BE, tmp, &mut buf) {
|
||||
if let Ok((n, decoded)) = decode_to_str(Encoding::Utf32BE, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf8.extend(decoded.chars());
|
||||
} else {
|
||||
|
@ -144,7 +144,7 @@ proptest! {
|
|||
// Encode to utf32 little endian
|
||||
let mut tmp = &text[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, encoded)) = encode_from_utf8(Encoding::Utf32LE, tmp, &mut buf) {
|
||||
if let Ok((n, encoded)) = encode_from_str(Encoding::Utf32LE, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf32.extend_from_slice(encoded);
|
||||
} else {
|
||||
|
@ -155,7 +155,7 @@ proptest! {
|
|||
// Decode back from utf32 little endian
|
||||
let mut tmp = &utf32[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, decoded)) = decode_to_utf8(Encoding::Utf32LE, tmp, &mut buf) {
|
||||
if let Ok((n, decoded)) = decode_to_str(Encoding::Utf32LE, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf8.extend(decoded.chars());
|
||||
} else {
|
||||
|
@ -175,7 +175,7 @@ proptest! {
|
|||
// Decode from latin1 to utf8
|
||||
let mut tmp = &data[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, decoded)) = decode_to_utf8(Encoding::Latin1, tmp, &mut buf) {
|
||||
if let Ok((n, decoded)) = decode_to_str(Encoding::Latin1, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf8.extend(decoded.chars());
|
||||
} else {
|
||||
|
@ -186,7 +186,7 @@ proptest! {
|
|||
// Encode to from utf8 back to latin1
|
||||
let mut tmp = &utf8[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, encoded)) = encode_from_utf8(Encoding::Latin1, tmp, &mut buf) {
|
||||
if let Ok((n, encoded)) = encode_from_str(Encoding::Latin1, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
latin1.extend_from_slice(encoded);
|
||||
} else {
|
||||
|
@ -213,7 +213,7 @@ proptest! {
|
|||
// Decode from windows-1252 to utf8
|
||||
let mut tmp = &data[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, decoded)) = decode_to_utf8(Encoding::Windows1252, tmp, &mut buf) {
|
||||
if let Ok((n, decoded)) = decode_to_str(Encoding::Windows1252, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
utf8.extend(decoded.chars());
|
||||
} else {
|
||||
|
@ -224,7 +224,7 @@ proptest! {
|
|||
// Encode to from utf8 back to w1252
|
||||
let mut tmp = &utf8[..];
|
||||
while !tmp.is_empty() {
|
||||
if let Ok((n, encoded)) = encode_from_utf8(Encoding::Windows1252, tmp, &mut buf) {
|
||||
if let Ok((n, encoded)) = encode_from_str(Encoding::Windows1252, tmp, &mut buf) {
|
||||
tmp = &tmp[n..];
|
||||
w1252.extend_from_slice(encoded);
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue
Block a user