Sha256: 4e1748e70be64f58eeb15111036da4051b200d4d82b1e77ece5ead5ed4c07801

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

pub mod rust {
  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 == "/") {
      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

1 entries across 1 versions & 1 rubygems

Version Path
faster_path-0.1.13 src/chop_basename.rs