Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,7 @@ pub trait BaseGenerator {
/// # Errors
/// if the write fails
unsafe fn write_str_simd(&mut self, string: &mut &[u8]) -> io::Result<()> {
write_string_rust(self.get_writer(), string)?;
*string = &string[string.len()..];
Ok(())
write_string_rust(self.get_writer(), string)
}

#[cfg(target_arch = "aarch64")]
Expand Down Expand Up @@ -455,10 +453,14 @@ where
// Legacy code to handle the remainder of the code
for (index, ch) in string.iter().enumerate() {
if ESCAPED[*ch as usize] > 0 {
return write_string_container(writer, string, index);
write_string_container(writer, string, index)?;
*string = &string[string.len()..];
return Ok(());
}
}
writer.write_all(string)
writer.write_all(string)?;
*string = &string[string.len()..];
Ok(())
}

#[cfg(feature = "portable")]
Expand Down Expand Up @@ -886,12 +888,29 @@ mod tests {
let mut string = "Hello, World!".as_bytes();
super::write_string_rust(&mut writer, &mut string).expect("failed to write string");
assert_eq!(writer, "Hello, World!".as_bytes());
assert!(string.is_empty(), "write_string_rust should advance string");
}
#[test]
fn test_write_string_rust2() {
let mut writer = Vec::new();
let mut string = "Hello, \"World!\"".as_bytes();
super::write_string_rust(&mut writer, &mut string).expect("failed to write string");
assert_eq!(writer, "Hello, \\\"World!\\\"".as_bytes());
assert!(string.is_empty(), "write_string_rust should advance string");
}

#[test]
fn test_write_string_long_with_escapes() {
use super::{BaseGenerator, DumpGenerator};
// Long string with escapes past the 32-byte SIMD boundary exercises
// write_string_content -> write_str_simd -> write_string_rust and
// catches the double-write bug if the fallback doesn't advance the string.
let input = "abcdefghijklmnopqrstuvwxyz012345\"escape here\"";
let mut g = DumpGenerator::new();
g.write_string(input).expect("write failed");
assert_eq!(
g.consume(),
"\"abcdefghijklmnopqrstuvwxyz012345\\\"escape here\\\"\""
);
}
}