Sha256: 7da6909fdb13bc57c3a622bc982003ac0719ba2b973ad5764136e6d805a8c9e2

Contents?: true

Size: 738 Bytes

Versions: 8

Compression:

Stored size: 738 Bytes

Contents

extern crate memchr;
use self::memchr::memrchr;
use path_parsing::{SEP, last_non_sep_i};

pub fn basename<'a>(pth: &'a str, ext: &str) -> &'a str {
  let name_end = (last_non_sep_i(pth) + 1) as usize;
  // Known edge case, all '/'.
  if !pth.is_empty() && name_end == 0 {
    return &pth[..1];
  }
  let name_start = match memrchr(SEP, &pth.as_bytes()[..name_end]) {
    Some(i) => i + 1,
    _ => 0
  };
  let mut name = &pth[name_start..name_end];
  if ext == ".*" {
    if let Some(dot_i) = memrchr('.' as u8, name.as_bytes()) {
      name = &name[..dot_i];
    }
  } else if name.ends_with(ext) {
    name = &name[..name.len() - ext.len()];
  };
  name
}

#[test]
fn edge_case_all_seps() {
  assert_eq!("/", basename("///", ".*"));
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
faster_path-0.3.7 src/basename.rs
faster_path-0.3.6 src/basename.rs
faster_path-0.3.2 src/basename.rs
faster_path-0.3.1 src/basename.rs
faster_path-0.2.6 src/basename.rs
faster_path-0.2.5 src/basename.rs
faster_path-0.2.4 src/basename.rs
faster_path-0.2.3 src/basename.rs