auto merge of #8978 : pnkfelix/rust/make-path-api-less-allocation-happy, r=huonw
A [dialogue](https://github.com/mozilla/rust/pull/8909#discussion-diff-6102725) on PR #8909 inspired me to make this change. r? anyone (It is possible that `std::path` itself will soon be replaced with a new implementation that kballard's working on, as mentioned in the dialogue linked above, but this revision is simple enough that I figured I'd offer it up.)
This commit is contained in:
commit
8827b94e5b
@ -307,8 +307,9 @@ pub fn make_test_name(config: &config, testfile: &Path) -> test::TestName {
|
||||
// Try to elide redundant long paths
|
||||
fn shorten(path: &Path) -> ~str {
|
||||
let filename = path.filename();
|
||||
let dir = path.pop().filename();
|
||||
fmt!("%s/%s", dir.unwrap_or_default(~""), filename.unwrap_or_default(~""))
|
||||
let p = path.pop();
|
||||
let dir = p.filename();
|
||||
fmt!("%s/%s", dir.unwrap_or_default(""), filename.unwrap_or_default(""))
|
||||
}
|
||||
|
||||
test::DynTestName(fmt!("[%s] %s",
|
||||
|
@ -880,7 +880,7 @@ fn _arm_push_aux_shared_library(config: &config, testfile: &Path) {
|
||||
let dirs = os::list_dir_path(&Path(tstr));
|
||||
for file in dirs.iter() {
|
||||
|
||||
if (file.filetype() == Some(~".so")) {
|
||||
if (file.filetype() == Some(".so")) {
|
||||
|
||||
let copy_result = procsrv::run("", config.adb_path,
|
||||
[~"push", file.to_str(), config.adb_test_dir.clone()],
|
||||
|
@ -162,7 +162,8 @@ fn cmd_help(args: &[~str]) -> ValidUsage {
|
||||
fn cmd_test(args: &[~str]) -> ValidUsage {
|
||||
match args {
|
||||
[ref filename] => {
|
||||
let test_exec = Path(*filename).filestem().unwrap() + "test~";
|
||||
let p = Path(*filename);
|
||||
let test_exec = p.filestem().unwrap() + "test~";
|
||||
invoke("rustc", &[~"--test", filename.to_owned(),
|
||||
~"-o", test_exec.to_owned()], rustc::main_args);
|
||||
let exit_code = run::process_status(~"./" + test_exec, []);
|
||||
@ -175,7 +176,8 @@ fn cmd_test(args: &[~str]) -> ValidUsage {
|
||||
fn cmd_run(args: &[~str]) -> ValidUsage {
|
||||
match args {
|
||||
[ref filename, ..prog_args] => {
|
||||
let exec = Path(*filename).filestem().unwrap() + "~";
|
||||
let p = Path(*filename);
|
||||
let exec = p.filestem().unwrap() + "~";
|
||||
invoke("rustc", &[filename.to_owned(), ~"-o", exec.to_owned()],
|
||||
rustc::main_args);
|
||||
let exit_code = run::process_status(~"./"+exec, prog_args);
|
||||
|
@ -952,13 +952,13 @@ pub fn link_args(sess: Session,
|
||||
let cstore = sess.cstore;
|
||||
let r = cstore::get_used_crate_files(cstore);
|
||||
for cratepath in r.iter() {
|
||||
if cratepath.filetype() == Some(~".rlib") {
|
||||
if cratepath.filetype() == Some(".rlib") {
|
||||
args.push(cratepath.to_str());
|
||||
loop;
|
||||
}
|
||||
let dir = cratepath.dirname();
|
||||
if dir != ~"" { args.push(~"-L" + dir); }
|
||||
let libarg = unlib(sess.targ_cfg, cratepath.filestem().unwrap());
|
||||
let libarg = unlib(sess.targ_cfg, cratepath.filestem().unwrap().to_owned());
|
||||
args.push(~"-l" + libarg);
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,8 @@ pub fn list_installed_packages(f: &fn(&PkgId) -> bool) -> bool {
|
||||
for p in workspaces.iter() {
|
||||
let binfiles = os::list_dir(&p.push("bin"));
|
||||
for exec in binfiles.iter() {
|
||||
let exec_path = Path(*exec).filestem();
|
||||
let p = Path(*exec);
|
||||
let exec_path = p.filestem();
|
||||
do exec_path.iter().advance |s| {
|
||||
f(&PkgId::new(*s))
|
||||
};
|
||||
@ -49,8 +50,8 @@ pub fn has_library(p: &Path) -> Option<~str> {
|
||||
let files = os::list_dir(p);
|
||||
for q in files.iter() {
|
||||
let as_path = Path(*q);
|
||||
if as_path.filetype() == Some(os::consts::DLL_SUFFIX.to_owned()) {
|
||||
let stuff : ~str = as_path.filestem().expect("has_library: weird path");
|
||||
if as_path.filetype() == Some(os::consts::DLL_SUFFIX) {
|
||||
let stuff : &str = as_path.filestem().expect("has_library: weird path");
|
||||
let mut stuff2 = stuff.split_str_iter(&"-");
|
||||
let stuff3: ~[&str] = stuff2.collect();
|
||||
// argh
|
||||
|
@ -68,7 +68,7 @@ impl PkgId {
|
||||
if path.components.len() < 1 {
|
||||
return cond.raise((path, ~"0-length pkgid"));
|
||||
}
|
||||
let short_name = path.clone().filestem().expect(fmt!("Strange path! %s", s));
|
||||
let short_name = path.filestem().expect(fmt!("Strange path! %s", s));
|
||||
|
||||
let version = match given_version {
|
||||
Some(v) => v,
|
||||
@ -83,8 +83,8 @@ impl PkgId {
|
||||
|
||||
debug!("path = %s", path.to_str());
|
||||
PkgId {
|
||||
path: path,
|
||||
short_name: short_name,
|
||||
path: path.clone(),
|
||||
short_name: short_name.to_owned(),
|
||||
version: version
|
||||
}
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ impl PkgSrc {
|
||||
return Some(local);
|
||||
}
|
||||
|
||||
if (self.id.path.clone()).components().len() < 2 {
|
||||
if self.id.path.components().len() < 2 {
|
||||
// If a non-URL, don't bother trying to fetch
|
||||
return None;
|
||||
}
|
||||
@ -157,7 +157,7 @@ impl PkgSrc {
|
||||
|
||||
/// True if the given path's stem is self's pkg ID's stem
|
||||
fn stem_matches(&self, p: &Path) -> bool {
|
||||
p.filestem().map_default(false, |p| { p == &self.id.short_name })
|
||||
p.filestem().map_default(false, |p| { p == &self.id.short_name.as_slice() })
|
||||
}
|
||||
|
||||
fn push_crate(cs: &mut ~[Crate], prefix: uint, p: &Path) {
|
||||
@ -182,10 +182,10 @@ impl PkgSrc {
|
||||
do os::walk_dir(&dir) |pth| {
|
||||
let maybe_known_crate_set = match pth.filename() {
|
||||
Some(filename) => match filename {
|
||||
~"lib.rs" => Some(&mut self.libs),
|
||||
~"main.rs" => Some(&mut self.mains),
|
||||
~"test.rs" => Some(&mut self.tests),
|
||||
~"bench.rs" => Some(&mut self.benchs),
|
||||
"lib.rs" => Some(&mut self.libs),
|
||||
"main.rs" => Some(&mut self.mains),
|
||||
"test.rs" => Some(&mut self.tests),
|
||||
"bench.rs" => Some(&mut self.benchs),
|
||||
_ => None
|
||||
},
|
||||
_ => None
|
||||
|
@ -234,14 +234,14 @@ pub fn library_in_workspace(path: &Path, short_name: &str, where: Target,
|
||||
Some(j) => {
|
||||
debug!("Maybe %s equals %s", f_name.slice(0, j), lib_prefix);
|
||||
if f_name.slice(0, j) == lib_prefix {
|
||||
result_filename = Some(p_path);
|
||||
result_filename = Some(p_path.clone());
|
||||
}
|
||||
break;
|
||||
}
|
||||
None => break
|
||||
}
|
||||
}
|
||||
_ => { f_name = f_name.slice(0, i).to_owned(); }
|
||||
_ => { f_name = f_name.slice(0, i); }
|
||||
}
|
||||
}
|
||||
None => break
|
||||
|
@ -396,7 +396,7 @@ fn touch_source_file(workspace: &Path, pkgid: &PkgId) {
|
||||
let pkg_src_dir = workspace.push("src").push(pkgid.to_str());
|
||||
let contents = os::list_dir_path(&pkg_src_dir);
|
||||
for p in contents.iter() {
|
||||
if p.filetype() == Some(~".rs") {
|
||||
if p.filetype() == Some(".rs") {
|
||||
// should be able to do this w/o a process
|
||||
if run::process_output("touch", [p.to_str()]).status != 0 {
|
||||
let _ = cond.raise((pkg_src_dir.clone(), ~"Bad path"));
|
||||
@ -413,7 +413,7 @@ fn frob_source_file(workspace: &Path, pkgid: &PkgId) {
|
||||
let contents = os::list_dir_path(&pkg_src_dir);
|
||||
let mut maybe_p = None;
|
||||
for p in contents.iter() {
|
||||
if p.filetype() == Some(~".rs") {
|
||||
if p.filetype() == Some(".rs") {
|
||||
maybe_p = Some(p);
|
||||
break;
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ use option::{None, Option, Some};
|
||||
use str::{OwnedStr, Str, StrSlice, StrVector};
|
||||
use to_str::ToStr;
|
||||
use ascii::{AsciiCast, AsciiStr};
|
||||
use vec::{OwnedVector, ImmutableVector, OwnedCopyableVector};
|
||||
use vec::{Vector, OwnedVector, ImmutableVector, OwnedCopyableVector};
|
||||
|
||||
#[cfg(windows)]
|
||||
pub use Path = self::WindowsPath;
|
||||
@ -65,17 +65,17 @@ pub trait GenericPath {
|
||||
fn dirname(&self) -> ~str;
|
||||
/// Returns the file component of `self`, as a string option.
|
||||
/// Returns None if `self` names a directory.
|
||||
fn filename(&self) -> Option<~str>;
|
||||
fn filename<'a>(&'a self) -> Option<&'a str>;
|
||||
/// Returns the stem of the file component of `self`, as a string option.
|
||||
/// The stem is the slice of a filename starting at 0 and ending just before
|
||||
/// the last '.' in the name.
|
||||
/// Returns None if `self` names a directory.
|
||||
fn filestem(&self) -> Option<~str>;
|
||||
fn filestem<'a>(&'a self) -> Option<&'a str>;
|
||||
/// Returns the type of the file component of `self`, as a string option.
|
||||
/// The file type is the slice of a filename starting just after the last
|
||||
/// '.' in the name and ending at the last index in the filename.
|
||||
/// Returns None if `self` names a directory.
|
||||
fn filetype(&self) -> Option<~str>;
|
||||
fn filetype<'a>(&'a self) -> Option<&'a str>;
|
||||
|
||||
/// Returns a new path consisting of `self` with the parent directory component replaced
|
||||
/// with the given string.
|
||||
@ -163,7 +163,7 @@ pub trait GenericPath {
|
||||
result
|
||||
}
|
||||
|
||||
fn components(self) -> ~[~str];
|
||||
fn components<'a>(&'a self) -> &'a [~str];
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
@ -600,31 +600,31 @@ impl GenericPath for PosixPath {
|
||||
}
|
||||
}
|
||||
|
||||
fn filename(&self) -> Option<~str> {
|
||||
fn filename<'a>(&'a self) -> Option<&'a str> {
|
||||
match self.components.len() {
|
||||
0 => None,
|
||||
n => Some(self.components[n - 1].clone()),
|
||||
n => Some(self.components[n - 1].as_slice()),
|
||||
}
|
||||
}
|
||||
|
||||
fn filestem(&self) -> Option<~str> {
|
||||
fn filestem<'a>(&'a self) -> Option<&'a str> {
|
||||
match self.filename() {
|
||||
None => None,
|
||||
Some(ref f) => {
|
||||
match f.rfind('.') {
|
||||
Some(p) => Some(f.slice_to(p).to_owned()),
|
||||
None => Some((*f).clone()),
|
||||
Some(p) => Some(f.slice_to(p)),
|
||||
None => Some((*f)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn filetype(&self) -> Option<~str> {
|
||||
fn filetype<'a>(&'a self) -> Option<&'a str> {
|
||||
match self.filename() {
|
||||
None => None,
|
||||
Some(ref f) => {
|
||||
match f.rfind('.') {
|
||||
Some(p) if p < f.len() => Some(f.slice_from(p).to_owned()),
|
||||
Some(p) if p < f.len() => Some(f.slice_from(p)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@ -670,7 +670,7 @@ impl GenericPath for PosixPath {
|
||||
fn file_path(&self) -> PosixPath {
|
||||
let cs = match self.filename() {
|
||||
None => ~[],
|
||||
Some(ref f) => ~[(*f).clone()]
|
||||
Some(ref f) => ~[(*f).to_owned()]
|
||||
};
|
||||
PosixPath {
|
||||
is_absolute: false,
|
||||
@ -756,7 +756,7 @@ impl GenericPath for PosixPath {
|
||||
self.is_ancestor_of(&other.pop()))
|
||||
}
|
||||
|
||||
fn components(self) -> ~[~str] { self.components }
|
||||
fn components<'a>(&'a self) -> &'a [~str] { self.components.as_slice() }
|
||||
}
|
||||
|
||||
|
||||
@ -842,31 +842,31 @@ impl GenericPath for WindowsPath {
|
||||
}
|
||||
}
|
||||
|
||||
fn filename(&self) -> Option<~str> {
|
||||
fn filename<'a>(&'a self) -> Option<&'a str> {
|
||||
match self.components.len() {
|
||||
0 => None,
|
||||
n => Some(self.components[n - 1].clone()),
|
||||
n => Some(self.components[n - 1].as_slice()),
|
||||
}
|
||||
}
|
||||
|
||||
fn filestem(&self) -> Option<~str> {
|
||||
fn filestem<'a>(&'a self) -> Option<&'a str> {
|
||||
match self.filename() {
|
||||
None => None,
|
||||
Some(ref f) => {
|
||||
match f.rfind('.') {
|
||||
Some(p) => Some(f.slice_to(p).to_owned()),
|
||||
None => Some((*f).clone()),
|
||||
Some(p) => Some(f.slice_to(p)),
|
||||
None => Some((*f)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn filetype(&self) -> Option<~str> {
|
||||
fn filetype<'a>(&'a self) -> Option<&'a str> {
|
||||
match self.filename() {
|
||||
None => None,
|
||||
Some(ref f) => {
|
||||
match f.rfind('.') {
|
||||
Some(p) if p < f.len() => Some(f.slice_from(p).to_owned()),
|
||||
Some(p) if p < f.len() => Some(f.slice_from(p)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@ -916,7 +916,7 @@ impl GenericPath for WindowsPath {
|
||||
is_absolute: false,
|
||||
components: match self.filename() {
|
||||
None => ~[],
|
||||
Some(ref f) => ~[(*f).clone()],
|
||||
Some(ref f) => ~[(*f).to_owned()],
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1049,7 +1049,7 @@ impl GenericPath for WindowsPath {
|
||||
self.is_ancestor_of(&other.pop()))
|
||||
}
|
||||
|
||||
fn components(self) -> ~[~str] { self.components }
|
||||
fn components<'a>(&'a self) -> &'a [~str] { self.components.as_slice() }
|
||||
}
|
||||
|
||||
pub fn normalize(components: &[~str]) -> ~[~str] {
|
||||
@ -1143,10 +1143,10 @@ mod tests {
|
||||
#[test]
|
||||
fn test_filetype_foo_bar() {
|
||||
let wp = PosixPath("foo.bar");
|
||||
assert_eq!(wp.filetype(), Some(~".bar"));
|
||||
assert_eq!(wp.filetype(), Some(".bar"));
|
||||
|
||||
let wp = WindowsPath("foo.bar");
|
||||
assert_eq!(wp.filetype(), Some(~".bar"));
|
||||
assert_eq!(wp.filetype(), Some(".bar"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user