Sha256: 80627c32527420fd3b555980c71097d5632ddd505764c11a4e63e60670a0eb66

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

use std::path::MAIN_SEPARATOR;
use std::str;

pub fn chop_basename<'a>(input: &'a str) -> Option<(&'a str, &'a str)> {
  if input.is_empty() {
    return None;
  }

  let input = input.trim_right_matches(MAIN_SEPARATOR);
  let end = input.rsplitn(2, MAIN_SEPARATOR).nth(0).unwrap().len();
  let base = &input[input.len()-end..input.len()];
  let directory = &input[0..input.len()-base.len()];

  if directory.is_empty() && (base.is_empty() || base.chars().next().unwrap() == MAIN_SEPARATOR) {
    return None
  };

  Some((directory, base))
}

#[test]
fn it_chops_the_basename_and_dirname() {
  assert_eq!(chop_basename(""),           None );
  assert_eq!(chop_basename("/"),          None );
  assert_eq!(chop_basename("."),          Some(("", ".")) );
  assert_eq!(chop_basename("asdf/asdf"),  Some(("asdf/",     "asdf")) );
  assert_eq!(chop_basename("asdf.txt"),   Some(("",      "asdf.txt")) );
  assert_eq!(chop_basename("asdf/"),      Some(("",          "asdf")) );
  assert_eq!(chop_basename("/asdf/"),     Some(("/",         "asdf")) );
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
faster_path-0.3.7 src/chop_basename.rs
faster_path-0.3.6 src/chop_basename.rs
faster_path-0.3.2 src/chop_basename.rs
faster_path-0.3.1 src/chop_basename.rs
faster_path-0.2.6 src/chop_basename.rs