forked from danielpclark/faster_path
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basename: Improve performance and style
More idiomatic, faster, extracted from danielpclark#66
- Loading branch information
Showing
5 changed files
with
31 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule ruby_spec
added at
8190a7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,31 @@ | ||
use std::path::MAIN_SEPARATOR; | ||
use libc::c_char; | ||
use std::ffi::{CStr,CString}; | ||
use std::str; | ||
|
||
#[allow(dead_code)] | ||
fn rubyish_basename(string: &str, globish_string: &str) -> String { | ||
let result = if globish_string == ".*" { | ||
let base = string.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or(""); | ||
let index = base.rfind("."); | ||
let (first, _) = base.split_at(index.unwrap()); | ||
first | ||
} else { | ||
if &string[string.len()-globish_string.len()..] == globish_string { | ||
&string[0..string.len()-globish_string.len()] | ||
} else { | ||
string | ||
}.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("") | ||
}; | ||
result.to_string() | ||
} | ||
#[no_mangle] | ||
pub extern "C" fn basename(c_pth: *const c_char, c_ext: *const c_char) -> *const c_char { | ||
// TODO: rb_raise on type or encoding errors | ||
// TODO: support objects that respond to `to_path` | ||
if c_pth.is_null() || c_ext.is_null() { | ||
return c_pth; | ||
} | ||
let pth = unsafe { CStr::from_ptr(c_pth) }.to_str().unwrap(); | ||
let ext = unsafe { CStr::from_ptr(c_ext) }.to_str().unwrap(); | ||
|
||
#[test] | ||
fn it_chomps_strings_correctly(){ | ||
assert_eq!(rubyish_basename("","") , ""); | ||
assert_eq!(rubyish_basename("ruby","") , "ruby"); | ||
assert_eq!(rubyish_basename("ruby.rb",".rb") , "ruby"); | ||
assert_eq!(rubyish_basename("ruby.rb",".*") , "ruby"); | ||
assert_eq!(rubyish_basename(".ruby/ruby.rb",".rb") , "ruby"); | ||
assert_eq!(rubyish_basename(".ruby/ruby.rb.swp",".rb") , "ruby.rb.swp"); | ||
assert_eq!(rubyish_basename(".ruby/ruby.rb.swp",".swp") , "ruby.rb"); | ||
assert_eq!(rubyish_basename(".ruby/ruby.rb.swp",".*") , "ruby.rb"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp","") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".*") , "asdf.rb"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", "*") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".*") , "asdf.rb"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".rb") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".swp") , "asdf.rb"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".sw") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".sw*") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".rb.s*") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".s*") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".s**") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".**") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".*") , "asdf.rb"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".*.*") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".rb.swp") , "asdf"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".rb.s*p") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".r*.s*p") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".r*.sw*p") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".r*b.sw*p") , "asdf.rb.swp"); | ||
assert_eq!(rubyish_basename("asdf/asdf.rb.swp", "rb.swp") , "asdf."); | ||
} | ||
let filename = pth.trim_right_matches(MAIN_SEPARATOR).rsplit(MAIN_SEPARATOR).next().unwrap_or(""); | ||
|
||
#[no_mangle] | ||
pub extern fn basename(str_pth: *const c_char, comp_ext: *const c_char) -> *const c_char { | ||
let c_str1 = unsafe { | ||
if str_pth.is_null(){ | ||
return str_pth; | ||
} | ||
CStr::from_ptr(str_pth) | ||
}; | ||
let c_str2 = unsafe { | ||
if comp_ext.is_null() { | ||
return str_pth; | ||
let result = if ext == ".*" { | ||
match filename.rfind('.') { | ||
Some(dot_i) => &filename[0..dot_i], | ||
_ => filename, | ||
} | ||
CStr::from_ptr(comp_ext) | ||
}; | ||
let string = str::from_utf8(c_str1.to_bytes()).unwrap(); | ||
let globish_string = str::from_utf8(c_str2.to_bytes()).unwrap(); | ||
|
||
let result = if globish_string == ".*" { | ||
let base = string.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or(""); | ||
let index = base.rfind("."); | ||
let (first, _) = base.split_at(index.unwrap()); | ||
first | ||
} else { | ||
if &string[string.len()-globish_string.len()..] == globish_string { | ||
&string[0..string.len()-globish_string.len()] | ||
if filename.ends_with(ext) { | ||
&filename[..filename.len() - ext.len()] | ||
} else { | ||
string | ||
}.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("") | ||
filename | ||
} | ||
}; | ||
|
||
CString::new(result).unwrap().into_raw() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters