faster_path 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,19 @@
1
+ use std::borrow::Cow;
1
2
  use dirname::dirname;
2
- use basename::basename;
3
- use std::path::MAIN_SEPARATOR as SEP;
3
+ use path_parsing::{SEP, contains_sep};
4
+ use std::path::MAIN_SEPARATOR;
4
5
 
5
- pub fn prepend_prefix(prefix: &str, relpath: &str) -> String {
6
- let mut prefix: String = prefix.to_string();
6
+ pub fn prepend_prefix<'a>(prefix: &'a str, relpath: &str) -> Cow<'a, str> {
7
7
  if relpath.is_empty() {
8
- dirname(&prefix).to_string()
9
- } else if prefix.contains(&SEP.to_string()[..]) {
10
- prefix = dirname(&prefix).to_string();
11
- if basename(&format!("{}{}", &prefix, "a"), "") != "a" {
12
- prefix = format!("{}{}", prefix, &SEP);
8
+ dirname(prefix).into()
9
+ } else if contains_sep(prefix.as_bytes()) {
10
+ let prefix_dirname = dirname(prefix);
11
+ match prefix_dirname.as_bytes().last() {
12
+ None => relpath.to_string().into(),
13
+ Some(&SEP) => format!("{}{}", prefix_dirname, relpath).into(),
14
+ _ => format!("{}{}{}", prefix_dirname, MAIN_SEPARATOR, relpath).into()
13
15
  }
14
- format!("{}{}", prefix, relpath)
15
16
  } else {
16
- format!("{}{}", prefix, relpath)
17
+ format!("{}{}", prefix, relpath).into()
17
18
  }
18
19
  }
@@ -1,9 +1,8 @@
1
- use helpers::is_same_path;
1
+ use helpers::{is_same_path, to_str};
2
2
  use path_parsing::SEP_STR;
3
+ use cleanpath_aggressive::cleanpath_aggressive;
3
4
  extern crate array_tool;
4
- use self::array_tool::vec::{Shift, Join};
5
5
  use self::array_tool::iter::ZipOpt;
6
- use pathname;
7
6
  use ruru;
8
7
  use chop_basename;
9
8
  use pathname::Pathname;
@@ -12,36 +11,38 @@ use ruru::{Exception as Exc, AnyException as Exception};
12
11
  type MaybeString = Result<ruru::RString, ruru::result::Error>;
13
12
 
14
13
  pub fn relative_path_from(itself: MaybeString, base_directory: MaybeString) -> Result<Pathname, Exception> {
15
- let dest_directory = pathname::pn_cleanpath_aggressive(itself).to_string();
16
- let base_directory = pathname::pn_cleanpath_aggressive(base_directory).to_string();
14
+ let dest_directory = cleanpath_aggressive(to_str(&itself));
15
+ let base_directory = cleanpath_aggressive(to_str(&base_directory));
17
16
 
18
- let mut dest_prefix = dest_directory.clone();
19
- let mut dest_names: Vec<String> = vec![];
17
+ let mut dest_prefix = dest_directory.as_ref();
18
+ let mut dest_names: Vec<&str> = vec![];
20
19
  loop {
21
20
  match chop_basename::chop_basename(&dest_prefix.clone()) {
22
21
  Some((ref dest, ref basename)) => {
23
- dest_prefix = dest.to_string();
22
+ dest_prefix = dest;
24
23
  if basename != &"." {
25
- dest_names.unshift(basename.to_string())
24
+ dest_names.push(basename)
26
25
  }
27
26
  },
28
27
  None => break,
29
28
  }
30
29
  }
30
+ dest_names.reverse();
31
31
 
32
- let mut base_prefix = base_directory.clone();
33
- let mut base_names: Vec<String> = vec![];
32
+ let mut base_prefix = base_directory.as_ref();
33
+ let mut base_names: Vec<&str> = vec![];
34
34
  loop {
35
- match chop_basename::chop_basename(&base_prefix.clone()) {
35
+ match chop_basename::chop_basename(&base_prefix) {
36
36
  Some((ref base, ref basename)) => {
37
- base_prefix = base.to_string();
37
+ base_prefix = base;
38
38
  if basename != &"." {
39
- base_names.unshift(basename.to_string())
39
+ base_names.push(basename)
40
40
  }
41
41
  },
42
42
  None => break,
43
43
  }
44
44
  }
45
+ base_names.reverse();
45
46
 
46
47
  if !is_same_path(&dest_prefix, &base_prefix) {
47
48
  return Err(
@@ -85,6 +86,7 @@ pub fn relative_path_from(itself: MaybeString, base_directory: MaybeString) -> R
85
86
  if base_names.is_empty() && dest_names.is_empty() {
86
87
  Ok(Pathname::new("."))
87
88
  } else {
88
- Ok(Pathname::new(&base_names.iter().chain(dest_names.iter()).collect::<Vec<&String>>().join(&SEP_STR)))
89
+ Ok(Pathname::new(&base_names.iter().chain(dest_names.iter()).map(String::as_str).
90
+ collect::<Vec<&str>>().join(&SEP_STR)))
89
91
  }
90
92
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faster_path
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-07 00:00:00.000000000 Z
11
+ date: 2018-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -168,6 +168,7 @@ files:
168
168
  - src/extname.rs
169
169
  - src/helpers.rs
170
170
  - src/lib.rs
171
+ - src/memrnchr.rs
171
172
  - src/path_parsing.rs
172
173
  - src/pathname.rs
173
174
  - src/pathname_sys.rs