-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve speed of fmt::Debug
for str
and char
#28662
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1310,11 +1310,21 @@ impl Display for bool { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl Debug for str { | ||
fn fmt(&self, f: &mut Formatter) -> Result { | ||
try!(write!(f, "\"")); | ||
for c in self.chars().flat_map(|c| c.escape_default()) { | ||
try!(f.write_char(c)) | ||
try!(f.write_char('"')); | ||
let mut from = 0; | ||
for (i, c) in self.char_indices() { | ||
let esc = c.escape_default(); | ||
// If char needs escaping, flush backlog so far and write, else skip | ||
if esc.size_hint().0 != 1 { | ||
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. It may be best to check the upper bound here against 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. I'm not super happy about this unofficial communication through the size hint. A method on the iterator itself would be much cleaner, only problem is that it needs to be some degree of public (but unstable). 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. @alexcrichton Do you mean As @bluss I would prefer if we could add method to iterator (well, to Or maybe just revert to 025ca11 to avoid unnecessary construction of iterator? 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. @semmaz ah right, indeed I do mean that! I also think that a check against And yeah the point of leveraging 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. Any way of checking the size_hint is as good as any other IMO. I just prefer an explicit method, maybe a static method on A static method is a good API level since it ties the behavior tightly to the 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. @alexcrichton Updated. I'm fine if it merges as it is right now. Although I really think that having less taxing way to query if char needs escape would be useful. Question is, should those methods be in scope of this PR? |
||
try!(f.write_str(&self[from..i])); | ||
for c in esc { | ||
try!(f.write_char(c)); | ||
} | ||
from = i + c.len_utf8(); | ||
} | ||
} | ||
write!(f, "\"") | ||
try!(f.write_str(&self[from..])); | ||
f.write_char('"') | ||
} | ||
} | ||
|
||
|
@@ -1328,12 +1338,11 @@ impl Display for str { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl Debug for char { | ||
fn fmt(&self, f: &mut Formatter) -> Result { | ||
use char::CharExt; | ||
try!(write!(f, "'")); | ||
try!(f.write_char('\'')); | ||
for c in self.escape_default() { | ||
try!(f.write_char(c)) | ||
} | ||
write!(f, "'") | ||
f.write_char('\'') | ||
} | ||
} | ||
|
||
|
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.
Would it be better to have
EscapeDefaultState::Done
here?It would make it easier to understand why
(0, Some(0))
is the correct return value and in the unlikely case that the enum is extended it would promptly point out the problem.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.
Yep, thanks for pointing out.