mirror of
https://github.com/servo/rust-url
synced 2024-10-18 05:52:20 +00:00
perf(punycode): avoid double allocation in decode_to_string (#894)
* perf(punycode): avoid double allocation in decode_to_string * include decode_to_string in tests
This commit is contained in:
parent
92f356e449
commit
f447500049
|
@ -41,10 +41,12 @@ fn adapt(mut delta: u32, num_points: u32, first_time: bool) -> u32 {
|
|||
|
||||
/// Convert Punycode to an Unicode `String`.
|
||||
///
|
||||
/// This is a convenience wrapper around `decode`.
|
||||
/// Return None on malformed input or overflow.
|
||||
/// Overflow can only happen on inputs that take more than
|
||||
/// 63 encoded bytes, the DNS limit on domain name labels.
|
||||
#[inline]
|
||||
pub fn decode_to_string(input: &str) -> Option<String> {
|
||||
decode(input).map(|chars| chars.into_iter().collect())
|
||||
Some(Decoder::default().decode(input).ok()?.collect())
|
||||
}
|
||||
|
||||
/// Convert Punycode to Unicode.
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
// except according to those terms.
|
||||
|
||||
use crate::test::TestFn;
|
||||
use idna::punycode::{decode, encode_str};
|
||||
use idna::punycode::{decode, decode_to_string, encode_str};
|
||||
use serde_json::map::Map;
|
||||
use serde_json::Value;
|
||||
use std::panic::catch_unwind;
|
||||
|
@ -28,6 +28,17 @@ fn one_test(decoded: &str, encoded: &str) {
|
|||
}
|
||||
}
|
||||
|
||||
match decode_to_string(encoded) {
|
||||
None => panic!("Decoding {} failed.", encoded),
|
||||
Some(result) => assert!(
|
||||
result == decoded,
|
||||
"Incorrect decoding of \"{}\":\n \"{}\"\n!= \"{}\"\n",
|
||||
encoded,
|
||||
result,
|
||||
decoded
|
||||
),
|
||||
}
|
||||
|
||||
match encode_str(decoded) {
|
||||
None => panic!("Encoding {} failed.", decoded),
|
||||
Some(result) => assert!(
|
||||
|
|
Loading…
Reference in a new issue