Sha256: 0e2e98a9db9dccca1a81e2693ad8e3ff878efaec7b083cd8d434fdcf03202945

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

extern crate memchr;

use path_parsing::{find_last_sep_pos, find_last_non_sep_pos, find_last_dot_pos};

pub fn basename<'a>(path: &'a str, ext: &str) -> &'a str {
  let bytes: &[u8] = path.as_bytes();
  let mut left: usize = 0;
  let mut right: usize = bytes.len();
  if let Some(last_slash_pos) = find_last_sep_pos(bytes) {
    if last_slash_pos == right - 1 {
      if let Some(pos) = find_last_non_sep_pos(&bytes[..last_slash_pos]) {
        right = pos + 1;
      } else {
        return "/";
      }
      if let Some(pos) = find_last_sep_pos(&bytes[..right]) {
        left = pos + 1;
      }
    } else {
      left = last_slash_pos + 1;
    }
  }
  let ext_bytes = ext.as_bytes();
  if ext_bytes == b".*" {
    if let Some(dot_pos) = find_last_dot_pos(&bytes[left..right]) {
      right = left + dot_pos;
    }
  } else if bytes[left..right].ends_with(ext_bytes) {
    right -= ext_bytes.len();
  }
  &path[left..right]
}

#[test]
fn absolute() {
  assert_eq!(basename("/a/b///c", ""), "c");
}

#[test]
fn trailing_slashes_absolute() {
  assert_eq!(basename("/a/b///c//////", ""), "c");
}

#[test]
fn relative() {
  assert_eq!(basename("b///c", ""), "c");
}

#[test]
fn trailing_slashes_relative() {
  assert_eq!(basename("b/c//", ""), "c");
}

#[test]
fn root() {
  assert_eq!(basename("//c", ""), "c");
}

#[test]
fn trailing_slashes_root() {
  assert_eq!(basename("//c//", ""), "c");
}

#[test]
fn trailing_slashes_relative_root() {
  assert_eq!(basename("c//", ""), "c");
}

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faster_path-0.3.8 src/basename.rs