Expand description
Routines for debug escaping Ruby Strings.
Ruby Strings are conventionally UTF-8 byte sequences. When calling
String#inspect
or Symbol#inspect
, these maybe UTF-8 byte strings are
escaped to have a valid and printable UTF-8 representation.
This crate exposes functions and iterators for encoding arbitrary byte slices as valid, printable UTF-8.
§Ruby debug escapes
Ruby produces debug escapes that look like:
[2.6.3] > "Artichoke Ruby is made with Rust.
Invalid UTF-8: \xFF.
Slash \\ and quote \" are escaped."
=> "Artichoke Ruby is made with Rust.\n\nInvalid UTF-8: \xFF.\n\nSlash \\ and quote \" are escaped."
Ruby escape sequences differ than Rust escape sequences for some characters.
For example 0x0C
:
// Rust
assert_eq!('\x0C'.escape_debug().collect::<String>(), r"\u{c}");
// Ruby
assert_eq!(Literal::from(0x0C).as_str(), r"\f");
§Examples
const EXAMPLE: &[u8] = b"Artichoke Ruby is made with Rust.
Invalid UTF-8: \xFF.
Slash \\ and quote \" are escaped.";
let mut escaped = String::new();
format_debug_escape_into(&mut escaped, EXAMPLE)?;
assert_eq!(
escaped,
r#"Artichoke Ruby is made with Rust.\n\nInvalid UTF-8: \xFF.\n\nSlash \\ and quote \" are escaped."#,
);
§no_std
This crate is no_std
compatible. This crate does not depend on alloc
.
Structs§
- Byte
Sequence TooLong Error - Error that indicates a
InvalidUtf8ByteSequence
could not be constructed because the byte sequence contained more than three bytes. - Invalid
Utf8 Byte Sequence - Iterator of Ruby debug escape sequences for a contiguous invalid UTF-8 byte sequence.
- Literal
- Iterator of Ruby debug escape sequences for a byte.
Functions§
- ascii_
char_ with_ escape - Returns whether the given
char
has an ASCII literal escape code. - format_
debug_ escape_ into - Write a UTF-8 debug representation of a byte slice into the given writer.