Skip to content

Make path api less allocation happy #8978

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down
6 changes: 4 additions & 2 deletions src/librust/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,13 +948,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);
}

Expand Down
7 changes: 4 additions & 3 deletions src/librustpkg/installed_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
};
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/librustpkg/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustpkg/package_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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;
}
Expand Down Expand Up @@ -156,7 +156,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) {
Expand All @@ -181,10 +181,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
Expand Down
4 changes: 2 additions & 2 deletions src/librustpkg/path_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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;
}
Expand Down
50 changes: 25 additions & 25 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -163,7 +163,7 @@ pub trait GenericPath {
result
}

fn components(self) -> ~[~str];
fn components<'a>(&'a self) -> &'a [~str];
}

#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() }
}


Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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()],
}
}
}
Expand Down Expand Up @@ -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] {
Expand Down Expand Up @@ -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]
Expand Down