-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use std::fmt::Write
instead of custom StrWrite
trait
#493
Draft
camelid
wants to merge
1
commit into
pulldown-cmark:master
Choose a base branch
from
camelid:fmt-write
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -21,8 +21,8 @@ | |
//! Utility functions for HTML escaping. Only useful when building your own | ||
//! HTML renderer. | ||
|
||
use std::fmt::{Arguments, Write as FmtWrite}; | ||
use std::io::{self, ErrorKind, Write}; | ||
use std::fmt::{self, Arguments}; | ||
use std::io; | ||
use std::str::from_utf8; | ||
|
||
#[rustfmt::skip] | ||
|
@@ -42,67 +42,36 @@ static AMP_ESCAPE: &str = "&"; | |
static SLASH_ESCAPE: &str = "'"; | ||
|
||
/// This wrapper exists because we can't have both a blanket implementation | ||
/// for all types implementing `Write` and types of the for `&mut W` where | ||
/// `W: StrWrite`. Since we need the latter a lot, we choose to wrap | ||
/// for all types implementing `Write` and types of the form `&mut W` where | ||
/// `W: std::fmt::Write`. Since we need the latter a lot, we choose to wrap | ||
/// `Write` types. | ||
pub struct WriteWrapper<W>(pub W); | ||
|
||
/// Trait that allows writing string slices. This is basically an extension | ||
/// of `std::io::Write` in order to include `String`. | ||
pub trait StrWrite { | ||
fn write_str(&mut self, s: &str) -> io::Result<()>; | ||
|
||
fn write_fmt(&mut self, args: Arguments) -> io::Result<()>; | ||
} | ||
|
||
impl<W> StrWrite for WriteWrapper<W> | ||
where | ||
W: Write, | ||
{ | ||
#[inline] | ||
fn write_str(&mut self, s: &str) -> io::Result<()> { | ||
self.0.write_all(s.as_bytes()) | ||
} | ||
|
||
#[inline] | ||
fn write_fmt(&mut self, args: Arguments) -> io::Result<()> { | ||
self.0.write_fmt(args) | ||
} | ||
} | ||
|
||
impl<'w> StrWrite for String { | ||
#[inline] | ||
fn write_str(&mut self, s: &str) -> io::Result<()> { | ||
self.push_str(s); | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
fn write_fmt(&mut self, args: Arguments) -> io::Result<()> { | ||
// FIXME: translate fmt error to io error? | ||
FmtWrite::write_fmt(self, args).map_err(|_| ErrorKind::Other.into()) | ||
} | ||
} | ||
|
||
impl<W> StrWrite for &'_ mut W | ||
impl<W> fmt::Write for WriteWrapper<W> | ||
where | ||
W: StrWrite, | ||
W: io::Write, | ||
{ | ||
#[inline] | ||
fn write_str(&mut self, s: &str) -> io::Result<()> { | ||
(**self).write_str(s) | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
match self.0.write_all(s.as_bytes()) { | ||
Ok(()) => Ok(()), | ||
Err(_) => Err(fmt::Error), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn write_fmt(&mut self, args: Arguments) -> io::Result<()> { | ||
(**self).write_fmt(args) | ||
fn write_fmt(&mut self, args: Arguments) -> fmt::Result { | ||
match self.0.write_fmt(args) { | ||
Ok(()) => Ok(()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
Err(_) => Err(fmt::Error), | ||
} | ||
} | ||
} | ||
|
||
/// Writes an href to the buffer, escaping href unsafe bytes. | ||
pub fn escape_href<W>(mut w: W, s: &str) -> io::Result<()> | ||
pub fn escape_href<W>(mut w: W, s: &str) -> fmt::Result | ||
where | ||
W: StrWrite, | ||
W: fmt::Write, | ||
{ | ||
let bytes = s.as_bytes(); | ||
let mut mark = 0; | ||
|
@@ -152,7 +121,7 @@ static HTML_ESCAPES: [&str; 5] = ["", """, "&", "<", ">"]; | |
|
||
/// Writes the given string to the Write sink, replacing special HTML bytes | ||
/// (<, >, &, ") by escape sequences. | ||
pub fn escape_html<W: StrWrite>(w: W, s: &str) -> io::Result<()> { | ||
pub fn escape_html<W: fmt::Write>(w: W, s: &str) -> fmt::Result { | ||
#[cfg(all(target_arch = "x86_64", feature = "simd"))] | ||
{ | ||
simd::escape_html(w, s) | ||
|
@@ -163,7 +132,7 @@ pub fn escape_html<W: StrWrite>(w: W, s: &str) -> io::Result<()> { | |
} | ||
} | ||
|
||
fn escape_html_scalar<W: StrWrite>(mut w: W, s: &str) -> io::Result<()> { | ||
fn escape_html_scalar<W: fmt::Write>(mut w: W, s: &str) -> fmt::Result { | ||
let bytes = s.as_bytes(); | ||
let mut mark = 0; | ||
let mut i = 0; | ||
|
@@ -190,14 +159,13 @@ fn escape_html_scalar<W: StrWrite>(mut w: W, s: &str) -> io::Result<()> { | |
|
||
#[cfg(all(target_arch = "x86_64", feature = "simd"))] | ||
mod simd { | ||
use super::StrWrite; | ||
use std::arch::x86_64::*; | ||
use std::io; | ||
use std::fmt; | ||
use std::mem::size_of; | ||
|
||
const VECTOR_SIZE: usize = size_of::<__m128i>(); | ||
|
||
pub(super) fn escape_html<W: StrWrite>(mut w: W, s: &str) -> io::Result<()> { | ||
pub(super) fn escape_html<W: fmt::Write>(mut w: W, s: &str) -> fmt::Result { | ||
// The SIMD accelerated code uses the PSHUFB instruction, which is part | ||
// of the SSSE3 instruction set. Further, we can only use this code if | ||
// the buffer is at least one VECTOR_SIZE in length to prevent reading | ||
|
@@ -277,9 +245,9 @@ mod simd { | |
bytes: &[u8], | ||
mut offset: usize, | ||
mut callback: F, | ||
) -> io::Result<()> | ||
) -> fmt::Result | ||
where | ||
F: FnMut(usize) -> io::Result<()>, | ||
F: FnMut(usize) -> fmt::Result, | ||
{ | ||
// The strategy here is to walk the byte buffer in chunks of VECTOR_SIZE (16) | ||
// bytes at a time starting at the given offset. For each chunk, we compute a | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could
.map_err
here.