input.rs
This file
Path
src/exec/input.rs
165 lines · 3.9 KiB · Rust
Last changed by
Re-enable code style checksharkdpbefore adoptiona5f5ad6
History of this fileTake 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 tests {75 use super::{basename, dirname, remove_extension, MAIN_SEPARATOR};7677 fn correct(input: &str) -> String {78 input.replace('/', &MAIN_SEPARATOR.to_string())79 }8081 #[test]82 fn path_remove_ext_simple() {83 assert_eq!(remove_extension("foo.txt"), "foo");84 }8586 #[test]87 fn path_remove_ext_dir() {88 assert_eq!(89 remove_extension(&correct("dir/foo.txt")),90 correct("dir/foo")91 );92 }9394 #[test]95 fn path_hidden() {96 assert_eq!(remove_extension(".foo"), ".foo")97 }9899 #[test]100 fn path_remove_ext_utf8() {101 assert_eq!(remove_extension("💖.txt"), "💖");102 }103104 #[test]105 fn path_remove_ext_empty() {106 assert_eq!(remove_extension(""), "");107 }108109 #[test]110 fn path_basename_simple() {111 assert_eq!(basename("foo.txt"), "foo.txt");112 }113114 #[test]115 fn path_basename_no_ext() {116 assert_eq!(remove_extension(basename("foo.txt")), "foo");117 }118119 #[test]120 fn path_basename_dir() {121 assert_eq!(basename(&correct("dir/foo.txt")), "foo.txt");122 }123124 #[test]125 fn path_basename_empty() {126 assert_eq!(basename(""), "");127 }128129 #[test]130 fn path_basename_utf8() {131 assert_eq!(basename(&correct("💖/foo.txt")), "foo.txt");132 assert_eq!(basename(&correct("dir/💖.txt")), "💖.txt");133 }134135 #[test]136 fn path_dirname_simple() {137 assert_eq!(dirname("foo.txt"), ".");138 }139140 #[test]141 fn path_dirname_dir() {142 assert_eq!(dirname(&correct("dir/foo.txt")), "dir");143 }144145 #[test]146 fn path_dirname_utf8() {147 assert_eq!(dirname(&correct("💖/foo.txt")), "💖");148 assert_eq!(dirname(&correct("dir/💖.txt")), "dir");149 }150151 #[test]152 fn path_dirname_empty() {153 assert_eq!(dirname(""), ".");154 }155156 #[test]157 fn path_dirname_root() {158 #[cfg(windows)]159 assert_eq!(dirname("C:\\"), "C:");160 #[cfg(windows)]161 assert_eq!(dirname("\\"), "\\");162 #[cfg(not(windows))]163 assert_eq!(dirname("/"), "/");164 }165}