From 82ca8a224e4f748858663e19bb31e71ea4549c89 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 15 Sep 2023 10:37:06 +0200 Subject: [PATCH] Correctly handle multi-byte characters in string encoding --- instant-xml/src/impls.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/instant-xml/src/impls.rs b/instant-xml/src/impls.rs index da144dd..c5b97fc 100644 --- a/instant-xml/src/impls.rs +++ b/instant-xml/src/impls.rs @@ -529,7 +529,7 @@ impl ToXml for Option { fn encode(input: &str) -> Result, Error> { let mut result = String::with_capacity(input.len()); let mut last_end = 0; - for (start, c) in input.chars().enumerate() { + for (start, c) in input.char_indices() { let to = match c { '&' => "&", '"' => """, @@ -849,7 +849,7 @@ impl<'xml> FromXml<'xml> for IpAddr { #[cfg(test)] mod tests { - use super::decode; + use super::*; #[test] fn test_decode() { @@ -867,4 +867,10 @@ mod tests { assert!(decode("&foobar;").is_err()); assert!(decode("cbdtéd&ü").is_err()); } + + #[test] + fn encode_unicode() { + let input = "Iñtërnâ&tiônàlizætiøn"; + assert_eq!(encode(input).unwrap(), "Iñtërnâ&tiônàlizætiøn"); + } }