Skip to content

Commit

Permalink
Hoist path::Display on top of from_utf8_lossy()
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyball committed Feb 7, 2014
1 parent b0b89a5 commit 544cb42
Showing 1 changed file with 2 additions and 25 deletions.
27 changes: 2 additions & 25 deletions src/libstd/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,10 @@ impl<'a, P: GenericPath> ToStr for Display<'a, P> {
if self.filename {
match self.path.filename() {
None => ~"",
Some(v) => from_utf8_with_replacement(v)
Some(v) => str::from_utf8_lossy(v)
}
} else {
from_utf8_with_replacement(self.path.as_vec())
str::from_utf8_lossy(self.path.as_vec())
}
}
}
Expand Down Expand Up @@ -635,29 +635,6 @@ fn contains_nul(v: &[u8]) -> bool {
v.iter().any(|&x| x == 0)
}

#[inline(always)]
fn from_utf8_with_replacement(mut v: &[u8]) -> ~str {
// FIXME (#9516): Don't decode utf-8 manually here once we have a good way to do it in str
// This is a truly horrifically bad implementation, done as a functionality stopgap until
// we have a proper utf-8 decoder. I don't really want to write one here.
static REPLACEMENT_CHAR: char = '\uFFFD';

let mut s = str::with_capacity(v.len());
while !v.is_empty() {
let w = str::utf8_char_width(v[0]);
if w == 0u {
s.push_char(REPLACEMENT_CHAR);
v = v.slice_from(1);
} else if v.len() < w || !str::is_utf8(v.slice_to(w)) {
s.push_char(REPLACEMENT_CHAR);
v = v.slice_from(1);
} else {
s.push_str(unsafe { ::cast::transmute(v.slice_to(w)) });
v = v.slice_from(w);
}
}
s
}
#[cfg(test)]
mod tests {
use prelude::*;
Expand Down

5 comments on commit 544cb42

@bors
Copy link
Contributor

@bors bors commented on 544cb42 Feb 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at lilyball@544cb42

@bors
Copy link
Contributor

@bors bors commented on 544cb42 Feb 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging kballard/rust/from_utf8_lossy = 544cb42 into auto

@bors
Copy link
Contributor

@bors bors commented on 544cb42 Feb 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kballard/rust/from_utf8_lossy = 544cb42 merged ok, testing candidate = 36f1b38

@bors
Copy link
Contributor

@bors bors commented on 544cb42 Feb 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 544cb42 Feb 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 36f1b38

Please sign in to comment.