From 544cb42d7aeaec230e10471820f48e561e3cd33d Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 6 Feb 2014 00:02:59 -0800 Subject: [PATCH] Hoist path::Display on top of from_utf8_lossy() --- src/libstd/path/mod.rs | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 4aa4a3feab1be..ccbb31da24f5c 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -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()) } } } @@ -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::*;