src/basename.rs in faster_path-0.1.2 vs src/basename.rs in faster_path-0.1.3

- old
+ new

@@ -1,22 +1,93 @@ -#[no_mangle] -pub extern fn basename(string: *const c_char, comp_ext: *const c_char) -> *const c_char { +#[allow(dead_code)] +fn rubyish_basename(string: &str, globish_string: &str) -> String { + let result = if globish_string.chars().next().unwrap_or("muffins".chars().next().unwrap()).eq(&".".chars().next().unwrap()) { + 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("") + } + } 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() +} + +#[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."); +} + +//#[no_mangle] +pub extern fn basename(str_pth: *const c_char, comp_ext: *const c_char) -> *const c_char { let c_str1 = unsafe { - assert!(!string.is_null()); - CStr::from_ptr(string) + assert!(!str_pth.is_null()); + CStr::from_ptr(str_pth) }; let c_str2 = unsafe { assert!(!comp_ext.is_null()); CStr::from_ptr(comp_ext) }; - let r_str = str::from_utf8(c_str1.to_bytes()).unwrap(); - let r_str_chomp = str::from_utf8(c_str2.to_bytes()).unwrap(); + let string = str::from_utf8(c_str1.to_bytes()).unwrap(); + let globish_string = str::from_utf8(c_str2.to_bytes()).unwrap(); - let r_str = if !r_str_chomp.is_empty() { - chomp_pathish_regex(r_str, r_str_chomp) - } else { r_str.to_string() }; - - let part = Path::new(&r_str[..]).file_name().unwrap_or(OsStr::new("")).to_str(); - - let output = CString::new(format!("{}", part.unwrap())).unwrap(); - output.into_raw() + let result = if globish_string.chars().next().unwrap_or("muffins".chars().next().unwrap()).eq(&".".chars().next().unwrap()) { + 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("") + } + } 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("") + }; + CString::new(result).unwrap().into_raw() }