-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
Write
instead of owned strings (#8)
Writing to a mutable buffer is more performant than returning owned strings, and should be the way to go
- Loading branch information
Showing
16 changed files
with
461 additions
and
277 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::fmt::{Result, Write}; | ||
|
||
/// Simple HTML escaping, so strings can be safely rendered. | ||
/// | ||
/// ```rust | ||
/// # use pretty_assertions::assert_eq; | ||
/// # use render::html_escaping; | ||
/// | ||
/// let mut buf = String::new(); | ||
/// html_escaping::escape_html(r#"<hello world="attribute" />"#, &mut buf).unwrap(); | ||
/// assert_eq!(buf, "<hello world="attribute" />"); | ||
/// ``` | ||
pub fn escape_html<W: Write>(html: &str, writer: &mut W) -> Result { | ||
for c in html.chars() { | ||
match c { | ||
'>' => write!(writer, ">")?, | ||
'<' => write!(writer, "<")?, | ||
'"' => write!(writer, """)?, | ||
'&' => write!(writer, "&")?, | ||
'\'' => write!(writer, "'")?, | ||
c => writer.write_char(c)?, | ||
}; | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.