Created
March 27, 2021 15:27
-
-
Save lights0123/d0ca064a6ece2eefdd8820a88ac0fa58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn escape_str<W: fmt::Write>(mut f: W, mut s: &str, replacements: &[(u8, &str)]) -> fmt::Result { | |
while !s.is_empty() { | |
let (idx, replacement) = s | |
.as_bytes() | |
.iter() | |
.enumerate() | |
.find_map(|(i, c)| { | |
replacements | |
.iter() | |
.find(|(r, _)| c == r) | |
.map(|(_, replacement)| (i, *replacement)) | |
}) | |
.unwrap_or((s.len(), "")); | |
write!(f, "{}{}", &s[..idx], replacement)?; | |
s = &s[s.len().min(idx + 1)..]; | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment