Sha256: a885d9d75865ff5b3fdb978b6b27e8467cc8bc8a7a8a3d1b1eca24c49573a1cf

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

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

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

  let mut offset = 0;
  let mut trailing_slashes = input.chars().rev();
  loop {
    match trailing_slashes.next() {
      Some(MAIN_SEPARATOR) => { offset = offset + 1 },
      _                    => { break               },
    }
  }

  let input = &input[0..input.len()-offset];
  let base = input.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("");
  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.to_string(), base.to_string()))
}

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
faster_path-0.2.5 src/chop_basename.rs
faster_path-0.2.4 src/chop_basename.rs
faster_path-0.2.3 src/chop_basename.rs
faster_path-0.2.2 src/chop_basename.rs
faster_path-0.2.1 src/chop_basename.rs
faster_path-0.2.0 src/chop_basename.rs