1
Fork 0

path2: Remove .with_display_str and friends

Rewrite these methods as methods on Display and FilenameDisplay. This
turns

  do path.with_display_str |s| { ... }

into

  do path.display().with_str |s| { ... }
This commit is contained in:
Kevin Ballard 2013-10-06 18:51:49 -07:00
parent d6d9b92683
commit c01a97b7a9
12 changed files with 131 additions and 126 deletions

View file

@ -334,7 +334,7 @@ fn check_error_patterns(props: &TestProps,
testfile: &Path, testfile: &Path,
ProcRes: &ProcRes) { ProcRes: &ProcRes) {
if props.error_patterns.is_empty() { if props.error_patterns.is_empty() {
do testfile.with_display_str |s| { do testfile.display().with_str |s| {
fatal(~"no error pattern specified in " + s); fatal(~"no error pattern specified in " + s);
} }
} }

View file

@ -454,7 +454,7 @@ impl<'self> SourceCollector<'self> {
let w = cur.open_writer(io::CreateOrTruncate); let w = cur.open_writer(io::CreateOrTruncate);
let mut w = BufferedWriter::new(w); let mut w = BufferedWriter::new(w);
let title = cur.with_filename_display_str(|s| format!("{} -- source", s.unwrap())); let title = cur.filename_display().with_str(|s| format!("{} -- source", s));
let page = layout::Page { let page = layout::Page {
title: title, title: title,
ty: "source", ty: "source",

View file

@ -40,7 +40,7 @@ pub fn list_installed_packages(f: &fn(&PkgId) -> bool) -> bool {
let rel_p = lib.path_relative_from(&parent).unwrap(); let rel_p = lib.path_relative_from(&parent).unwrap();
debug2!("Rel: {}", rel_p.display()); debug2!("Rel: {}", rel_p.display());
let rel_path = rel_p.join(basename); let rel_path = rel_p.join(basename);
do rel_path.with_display_str |s| { do rel_path.display().with_str |s| {
debug2!("Rel name: {}", s); debug2!("Rel name: {}", s);
f(&PkgId::new(s)); f(&PkgId::new(s));
} }

View file

@ -88,7 +88,7 @@ impl PkgId {
pub fn hash(&self) -> ~str { pub fn hash(&self) -> ~str {
// FIXME (#9639): hash should take a &[u8] so we can hash the real path // FIXME (#9639): hash should take a &[u8] so we can hash the real path
do self.path.with_display_str |s| { do self.path.display().with_str |s| {
let vers = self.version.to_str(); let vers = self.version.to_str();
format!("{}-{}-{}", s, hash(s + vers), vers) format!("{}-{}-{}", s, hash(s + vers), vers)
} }

View file

@ -114,7 +114,7 @@ impl PkgSrc {
} }
debug2!("Checking dirs: {:?}", to_try.map(|p| p.to_display_str()).connect(":")); debug2!("Checking dirs: {:?}", to_try.map(|p| p.display().to_str()).connect(":"));
let path = to_try.iter().find(|&d| os::path_exists(d)); let path = to_try.iter().find(|&d| os::path_exists(d));

View file

@ -337,7 +337,7 @@ impl CtxMethods for BuildContext {
"list" => { "list" => {
io::println("Installed packages:"); io::println("Installed packages:");
do installed_packages::list_installed_packages |pkg_id| { do installed_packages::list_installed_packages |pkg_id| {
do pkg_id.path.with_display_str |s| { do pkg_id.path.display().with_str |s| {
println(s); println(s);
} }
true true
@ -564,7 +564,7 @@ impl CtxMethods for BuildContext {
&pkg_src.destination_workspace, &pkg_src.destination_workspace,
&id).map(|s| Path::new(*s)); &id).map(|s| Path::new(*s));
debug2!("install: id = {}, about to call discover_outputs, {:?}", debug2!("install: id = {}, about to call discover_outputs, {:?}",
id.to_str(), result.map(|p| p.to_display_str())); id.to_str(), result.map(|p| p.display().to_str()));
installed_files = installed_files + result; installed_files = installed_files + result;
note(format!("Installed package {} to {}", note(format!("Installed package {} to {}",
id.to_str(), id.to_str(),

View file

@ -27,7 +27,7 @@ pub fn each_pkg_parent_workspace(cx: &Context, pkgid: &PkgId, action: &fn(&Path)
fail2!("Package {} not found in any of \ fail2!("Package {} not found in any of \
the following workspaces: {}", the following workspaces: {}",
pkgid.path.display(), pkgid.path.display(),
rust_path().map(|p| p.to_display_str()).to_str()); rust_path().map(|p| p.display().to_str()).to_str());
} }
for ws in workspaces.iter() { for ws in workspaces.iter() {
if action(ws) { if action(ws) {

View file

@ -65,6 +65,7 @@ use ptr;
use result::{Result, Ok, Err}; use result::{Result, Ok, Err};
use str::{StrSlice, OwnedStr}; use str::{StrSlice, OwnedStr};
use str; use str;
use to_str::ToStr;
use uint; use uint;
use vec::{MutableVector, ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector}; use vec::{MutableVector, ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector};
use vec; use vec;
@ -1068,7 +1069,7 @@ pub fn file_reader(path: &Path) -> Result<@Reader, ~str> {
}; };
if f as uint == 0u { if f as uint == 0u {
do path.with_display_str |p| { do path.display().with_str |p| {
Err(~"error opening " + p) Err(~"error opening " + p)
} }
} else { } else {
@ -1753,7 +1754,7 @@ pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> {
if str::is_utf8(bytes) { if str::is_utf8(bytes) {
Ok(str::from_utf8(bytes)) Ok(str::from_utf8(bytes))
} else { } else {
Err(file.to_display_str() + " is not UTF-8") Err(file.display().to_str() + " is not UTF-8")
} }
} }
} }

View file

@ -75,6 +75,7 @@ use iter::Iterator;
use option::{Option, None, Some}; use option::{Option, None, Some};
use str; use str;
use str::{OwnedStr, Str, StrSlice}; use str::{OwnedStr, Str, StrSlice};
use to_str::ToStr;
use vec; use vec;
use vec::{CopyableVector, OwnedCopyableVector, OwnedVector, Vector}; use vec::{CopyableVector, OwnedCopyableVector, OwnedVector, Vector};
use vec::{ImmutableEqVector, ImmutableVector}; use vec::{ImmutableEqVector, ImmutableVector};
@ -190,59 +191,6 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
/// Converts the Path into an owned byte vector /// Converts the Path into an owned byte vector
fn into_vec(self) -> ~[u8]; fn into_vec(self) -> ~[u8];
/// Provides the path as a string
///
/// If the path is not UTF-8, invalid sequences will be replaced with the unicode
/// replacement char. This involves allocation.
#[inline]
fn with_display_str<T>(&self, f: &fn(&str) -> T) -> T {
match self.as_str() {
Some(s) => f(s),
None => {
let s = self.to_display_str();
f(s.as_slice())
}
}
}
/// Returns the path as a string
///
/// If the path is not UTF-8, invalid sequences will be replaced with the unicode
/// replacement char. This involves allocation.
///
/// This is similar to `with_display_str()` except it will always allocate a new ~str.
fn to_display_str(&self) -> ~str {
from_utf8_with_replacement(self.as_vec())
}
/// Provides the filename as a string
///
/// If the filename is not UTF-8, invalid sequences will be replaced with the unicode
/// replacement char. This involves allocation.
#[inline]
fn with_filename_display_str<T>(&self, f: &fn(Option<&str>) -> T) -> T {
match self.filename_str() {
s@Some(_) => f(s),
None => {
let o = self.to_filename_display_str();
f(o.map(|s|s.as_slice()))
}
}
}
/// Returns the filename as a string
///
/// If the filename is not UTF-8, invalid sequences will be replaced with the unicode
/// replacement char. This involves allocation.
///
/// This is similar to `to_filename_display_str` except it will always allocate a new ~str.
fn to_filename_display_str(&self) -> Option<~str> {
match self.filename() {
None => None,
Some(v) => Some(from_utf8_with_replacement(v))
}
}
/// Returns an object that implements `fmt::Default` for printing paths /// Returns an object that implements `fmt::Default` for printing paths
/// ///
/// This will print the equivalent of `to_display_str()` when used with a {} format parameter. /// This will print the equivalent of `to_display_str()` when used with a {} format parameter.
@ -764,16 +712,75 @@ pub struct FilenameDisplay<'self, P> {
impl<'self, P: GenericPath> fmt::Default for Display<'self, P> { impl<'self, P: GenericPath> fmt::Default for Display<'self, P> {
fn fmt(d: &Display<P>, f: &mut fmt::Formatter) { fn fmt(d: &Display<P>, f: &mut fmt::Formatter) {
do d.path.with_display_str |s| { do d.with_str |s| {
f.pad(s) f.pad(s)
} }
} }
} }
impl<'self, P: GenericPath> ToStr for Display<'self, P> {
/// Returns the path as a string
///
/// If the path is not UTF-8, invalid sequences with be replaced with the
/// unicode replacement char. This involves allocation.
fn to_str(&self) -> ~str {
from_utf8_with_replacement(self.path.as_vec())
}
}
impl<'self, P: GenericPath> Display<'self, P> {
/// Provides the path as a string to a closure
///
/// If the path is not UTF-8, invalid sequences will be replaced with the
/// unicode replacement char. This involves allocation.
#[inline]
pub fn with_str<T>(&self, f: &fn(&str) -> T) -> T {
match self.path.as_str() {
Some(s) => f(s),
None => {
let s = self.to_str();
f(s.as_slice())
}
}
}
}
impl<'self, P: GenericPath> fmt::Default for FilenameDisplay<'self, P> { impl<'self, P: GenericPath> fmt::Default for FilenameDisplay<'self, P> {
fn fmt(d: &FilenameDisplay<P>, f: &mut fmt::Formatter) { fn fmt(d: &FilenameDisplay<P>, f: &mut fmt::Formatter) {
do d.path.with_filename_display_str |s| { do d.with_str |s| {
f.pad(s.unwrap_or("")) f.pad(s)
}
}
}
impl<'self, P: GenericPath> ToStr for FilenameDisplay<'self, P> {
/// Returns the filename as a string. If there is no filename, ~"" will be
/// returned.
///
/// If the filename is not UTF-8, invalid sequences will be replaced with
/// the unicode replacement char. This involves allocation.
fn to_str(&self) -> ~str {
match self.path.filename() {
None => ~"",
Some(v) => from_utf8_with_replacement(v)
}
}
}
impl<'self, P: GenericPath> FilenameDisplay<'self, P> {
/// Provides the filename as a string to a closure. If there is no
/// filename, "" will be provided.
///
/// If the filename is not UTF-8, invalid sequences will be replaced with
/// the unicode replacement char. This involves allocation.
#[inline]
pub fn with_str<T>(&self, f: &fn(&str) -> T) -> T {
match self.path.filename_str() {
Some(s) => f(s),
None => {
let s = self.to_str();
f(s.as_slice())
}
} }
} }
} }

View file

@ -745,50 +745,53 @@ mod tests {
#[test] #[test]
fn test_display_str() { fn test_display_str() {
assert_eq!(Path::new("foo").to_display_str(), ~"foo"); macro_rules! t(
assert_eq!(Path::new(b!("foo", 0x80)).to_display_str(), ~"foo\uFFFD"); ($path:expr, $disp:ident, $exp:expr) => (
assert_eq!(Path::new(b!("foo", 0xff, "bar")).to_display_str(), ~"foo\uFFFDbar"); {
assert_eq!(Path::new(b!("foo", 0xff, "/bar")).to_filename_display_str(), Some(~"bar")); let path = Path::new($path);
assert_eq!(Path::new(b!("foo/", 0xff, "bar")).to_filename_display_str(), assert_eq!(path.$disp().to_str(), ~$exp);
Some(~"\uFFFDbar")); }
assert_eq!(Path::new(b!("/")).to_filename_display_str(), None); )
)
t!("foo", display, "foo");
t!(b!("foo", 0x80), display, "foo\uFFFD");
t!(b!("foo", 0xff, "bar"), display, "foo\uFFFDbar");
t!(b!("foo", 0xff, "/bar"), filename_display, "bar");
t!(b!("foo/", 0xff, "bar"), filename_display, "\uFFFDbar");
t!(b!("/"), filename_display, "");
let mut called = false; macro_rules! t(
do Path::new("foo").with_display_str |s| { ($path:expr, $exp:expr) => (
assert_eq!(s, "foo"); {
called = true; let mut called = false;
}; let path = Path::new($path);
assert!(called); do path.display().with_str |s| {
called = false; assert_eq!(s, $exp);
do Path::new(b!("foo", 0x80)).with_display_str |s| { called = true;
assert_eq!(s, "foo\uFFFD"); };
called = true; assert!(called);
}; }
assert!(called); );
called = false; ($path:expr, $exp:expr, filename) => (
do Path::new(b!("foo", 0xff, "bar")).with_display_str |s| { {
assert_eq!(s, "foo\uFFFDbar"); let mut called = false;
called = true; let path = Path::new($path);
}; do path.filename_display().with_str |s| {
assert!(called); assert_eq!(s, $exp);
called = false; called = true;
do Path::new(b!("foo", 0xff, "/bar")).with_filename_display_str |s| {
assert_eq!(s, Some("bar")); };
called = true; assert!(called);
} }
assert!(called); )
called = false; )
do Path::new(b!("foo/", 0xff, "bar")).with_filename_display_str |s| {
assert_eq!(s, Some("\uFFFDbar")); t!("foo", "foo");
called = true; t!(b!("foo", 0x80), "foo\uFFFD");
} t!(b!("foo", 0xff, "bar"), "foo\uFFFDbar");
assert!(called); t!(b!("foo", 0xff, "/bar"), "bar", filename);
called = false; t!(b!("foo/", 0xff, "bar"), "\uFFFDbar", filename);
do Path::new(b!("/")).with_filename_display_str |s| { t!(b!("/"), "", filename);
assert!(s.is_none());
called = true;
}
assert!(called);
} }
#[test] #[test]

View file

@ -350,16 +350,6 @@ impl GenericPath for Path {
self.repr.into_bytes() self.repr.into_bytes()
} }
#[inline]
fn with_display_str<T>(&self, f: &fn(&str) -> T) -> T {
f(self.repr.as_slice())
}
#[inline]
fn to_display_str(&self) -> ~str {
self.repr.clone()
}
#[inline] #[inline]
fn dirname<'a>(&'a self) -> &'a [u8] { fn dirname<'a>(&'a self) -> &'a [u8] {
self.dirname_str().unwrap().as_bytes() self.dirname_str().unwrap().as_bytes()
@ -1462,18 +1452,22 @@ mod tests {
#[test] #[test]
fn test_display_str() { fn test_display_str() {
assert_eq!(Path::new("foo").to_display_str(), ~"foo"); let path = Path::new("foo");
assert_eq!(Path::new(b!("\\")).to_filename_display_str(), None); assert_eq!(path.display().to_str(), ~"foo");
let path = Path::new(b!("\\"));
assert_eq!(path.filename_display().to_str(), ~"");
let mut called = false; let mut called = false;
do Path::new("foo").with_display_str |s| { let path = Path::new("foo");
do path.display().with_str |s| {
assert_eq!(s, "foo"); assert_eq!(s, "foo");
called = true; called = true;
}; };
assert!(called); assert!(called);
called = false; called = false;
do Path::new(b!("\\")).with_filename_display_str |s| { let path = Path::new(b!("\\"));
assert!(s.is_none()); do path.filename_display().with_str |s| {
assert_eq!(s, "");
called = true; called = true;
} }
assert!(called); assert!(called);

View file

@ -4038,12 +4038,12 @@ impl Parser {
let stack = &self.sess.included_mod_stack; let stack = &self.sess.included_mod_stack;
let mut err = ~"circular modules: "; let mut err = ~"circular modules: ";
for p in stack.slice(i, stack.len()).iter() { for p in stack.slice(i, stack.len()).iter() {
do p.with_display_str |s| { do p.display().with_str |s| {
err.push_str(s); err.push_str(s);
} }
err.push_str(" -> "); err.push_str(" -> ");
} }
do path.with_display_str |s| { do path.display().with_str |s| {
err.push_str(s); err.push_str(s);
} }
self.span_fatal(id_sp, err); self.span_fatal(id_sp, err);