Skip to content
Myr

input.rs

This file

Path

src/exec/input.rs

121 lines · 3.6 KiB · Rust

Last changed by

macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9

History of this file

Take it

1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::path::MAIN_SEPARATOR;1011/// Removes the parent component of the path12pub fn basename(path: &str) -> &str {13    let mut index = 0;14    for (id, character) in path.char_indices() {15        if character == MAIN_SEPARATOR {16            index = id;17        }18    }1920    // FIXME: On Windows, should return what for C:file.txt D:file.txt and \\server\share ?21    if index != 0 {22        return &path[index + 1..];23    }2425    path26}2728/// Removes the extension from the path29pub fn remove_extension(path: &str) -> &str {30    let mut has_dir = false;31    let mut dir_index = 0;32    let mut ext_index = 0;3334    for (id, character) in path.char_indices() {35        if character == MAIN_SEPARATOR {36            has_dir = true;37            dir_index = id;38        }39        if character == '.' {40            ext_index = id;41        }42    }4344    // Account for hidden files and directories45    if ext_index != 0 && (!has_dir || dir_index + 2 <= ext_index) {46        return &path[0..ext_index];47    }4849    path50}5152/// Removes the basename from the path.53pub fn dirname(path: &str) -> &str {54    let mut has_dir = false;55    let mut index = 0;56    for (id, character) in path.char_indices() {57        if character == MAIN_SEPARATOR {58            has_dir = true;59            index = id;60        }61    }6263    // FIXME: On Windows, return what for C:file.txt D:file.txt and \\server\share ?64    if !has_dir {65        "."66    } else if index == 0 {67        &path[..1]68    } else {69        &path[0..index]70    }71}7273#[cfg(test)]74mod path_tests {75    use super::*;7677    fn correct(input: &str) -> String {78        input.replace('/', &MAIN_SEPARATOR.to_string())79    }8081    macro_rules! func_tests {82        ($($name:ident: $func:ident for $input:expr => $output:expr)+) => {83            $(84                #[test]85                fn $name() {86                    assert_eq!($func(&correct($input)), correct($output));87                }88            )+89        }90    }9192    func_tests! {93        remove_ext_simple:  remove_extension  for  "foo.txt"      =>  "foo"94        remove_ext_dir:     remove_extension  for  "dir/foo.txt"  =>  "dir/foo"95        hidden:             remove_extension  for  ".foo"         =>  ".foo"96        remove_ext_utf8:    remove_extension  for  "💖.txt"       =>  "💖"97        remove_ext_empty:   remove_extension  for  ""             =>  ""9899        basename_simple:  basename  for  "foo.txt"      =>  "foo.txt"100        basename_dir:     basename  for  "dir/foo.txt"  =>  "foo.txt"101        basename_empty:   basename  for  ""             =>  ""102        basename_utf8_0:  basename  for  "💖/foo.txt"   =>  "foo.txt"103        basename_utf8_1:  basename  for  "dir/💖.txt"   =>  "💖.txt"104105        dirname_simple:  dirname  for  "foo.txt"      =>  "."106        dirname_dir:     dirname  for  "dir/foo.txt"  =>  "dir"107        dirname_utf8_0:  dirname  for  "💖/foo.txt"   =>  "💖"108        dirname_utf8_1:  dirname  for  "dir/💖.txt"   =>  "dir"109        dirname_empty:   dirname  for  ""             =>  "."110    }111112    #[test]113    fn dirname_root() {114        #[cfg(windows)]115        assert_eq!(dirname("C:\\"), "C:");116        #[cfg(windows)]117        assert_eq!(dirname("\\"), "\\");118        #[cfg(not(windows))]119        assert_eq!(dirname("/"), "/");120    }121}