Skip to content
Myr

input.rs

This file

Path

src/exec/input.rs

113 lines · 3.2 KiB · Rust

Last changed by

Remove outdated license headers (not strictly required by Apache-2.0)sharkdpbefore adoption4274075

History of this file

Take it

1use std::path::MAIN_SEPARATOR;23/// Removes the parent component of the path4pub fn basename(path: &str) -> &str {5    let mut index = 0;6    for (id, character) in path.char_indices() {7        if character == MAIN_SEPARATOR {8            index = id;9        }10    }1112    // FIXME: On Windows, should return what for C:file.txt D:file.txt and \\server\share ?13    if index != 0 {14        return &path[index + 1..];15    }1617    path18}1920/// Removes the extension from the path21pub fn remove_extension(path: &str) -> &str {22    let mut has_dir = false;23    let mut dir_index = 0;24    let mut ext_index = 0;2526    for (id, character) in path.char_indices() {27        if character == MAIN_SEPARATOR {28            has_dir = true;29            dir_index = id;30        }31        if character == '.' {32            ext_index = id;33        }34    }3536    // Account for hidden files and directories37    if ext_index != 0 && (!has_dir || dir_index + 2 <= ext_index) {38        return &path[0..ext_index];39    }4041    path42}4344/// Removes the basename from the path.45pub fn dirname(path: &str) -> &str {46    let mut has_dir = false;47    let mut index = 0;48    for (id, character) in path.char_indices() {49        if character == MAIN_SEPARATOR {50            has_dir = true;51            index = id;52        }53    }5455    // FIXME: On Windows, return what for C:file.txt D:file.txt and \\server\share ?56    if !has_dir {57        "."58    } else if index == 0 {59        &path[..1]60    } else {61        &path[0..index]62    }63}6465#[cfg(test)]66mod path_tests {67    use super::*;6869    fn correct(input: &str) -> String {70        input.replace('/', &MAIN_SEPARATOR.to_string())71    }7273    macro_rules! func_tests {74        ($($name:ident: $func:ident for $input:expr => $output:expr)+) => {75            $(76                #[test]77                fn $name() {78                    assert_eq!($func(&correct($input)), correct($output));79                }80            )+81        }82    }8384    func_tests! {85        remove_ext_simple:  remove_extension  for  "foo.txt"      =>  "foo"86        remove_ext_dir:     remove_extension  for  "dir/foo.txt"  =>  "dir/foo"87        hidden:             remove_extension  for  ".foo"         =>  ".foo"88        remove_ext_utf8:    remove_extension  for  "💖.txt"       =>  "💖"89        remove_ext_empty:   remove_extension  for  ""             =>  ""9091        basename_simple:  basename  for  "foo.txt"      =>  "foo.txt"92        basename_dir:     basename  for  "dir/foo.txt"  =>  "foo.txt"93        basename_empty:   basename  for  ""             =>  ""94        basename_utf8_0:  basename  for  "💖/foo.txt"   =>  "foo.txt"95        basename_utf8_1:  basename  for  "dir/💖.txt"   =>  "💖.txt"9697        dirname_simple:  dirname  for  "foo.txt"      =>  "."98        dirname_dir:     dirname  for  "dir/foo.txt"  =>  "dir"99        dirname_utf8_0:  dirname  for  "💖/foo.txt"   =>  "💖"100        dirname_utf8_1:  dirname  for  "dir/💖.txt"   =>  "dir"101        dirname_empty:   dirname  for  ""             =>  "."102    }103104    #[test]105    fn dirname_root() {106        #[cfg(windows)]107        assert_eq!(dirname("C:\\"), "C:");108        #[cfg(windows)]109        assert_eq!(dirname("\\"), "\\");110        #[cfg(not(windows))]111        assert_eq!(dirname("/"), "/");112    }113}