input.rs
This file
Path
src/exec/input.rs
165 lines · 3.9 KiB · Rust
Last changed by
Remove shell with --execMatthias Reitingerbefore adoption18709b1
History of this fileTake it
Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2
lines 1–8why, one step back1// 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.8Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 9–10why, one step back9use std::path::MAIN_SEPARATOR;10Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 11–18why, one step back11/// 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 }Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 19why, one step back19Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 20–25why, one step back20 // 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 pathEscape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 26–27why, one step back26}27Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 28–32why, one step back28/// 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;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 33why, one step back33Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 34–37why, one step back34 for (id, character) in path.char_indices() {35 if character == MAIN_SEPARATOR {36 has_dir = true;37 dir_index = id;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 38why, one step back38 }Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 39–40why, one step back39 if character == '.' {40 ext_index = id;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 41why, one step back41 }Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 42why, one step back42 }Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 43why, one step back43Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 44–46why, one step back44 // Account for hidden files and directories45 if ext_index != 0 && (!has_dir || dir_index + 2 <= ext_index) {46 return &path[0..ext_index];Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 47–48why, one step back47 }48Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 49–50why, one step back49 path50}Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 51why, one step back51Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 52–59why, one step back52/// 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;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 60–62why, one step back60 }61 }62Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 63–69why, one step back63 // 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]Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 70–74why, one step back70 }71}7273#[cfg(test)]74mod tests {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 75why, one step back75 use super::{MAIN_SEPARATOR, basename, dirname, remove_extension};Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 76–77why, one step back7677 fn correct(input: &str) -> String {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 78why, one step back78 input.replace('/', &MAIN_SEPARATOR.to_string())Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 79–82why, one step back79 }8081 #[test]82 fn path_remove_ext_simple() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 83why, one step back83 assert_eq!(remove_extension("foo.txt"), "foo");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 84–88why, one step back84 }8586 #[test]87 fn path_remove_ext_dir() {88 assert_eq!(Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 89–90why, one step back89 remove_extension(&correct("dir/foo.txt")),90 correct("dir/foo")Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 91–95why, one step back91 );92 }9394 #[test]95 fn path_hidden() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 96why, one step back96 assert_eq!(remove_extension(".foo"), ".foo")Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 97–100why, one step back97 }9899 #[test]100 fn path_remove_ext_utf8() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 101why, one step back101 assert_eq!(remove_extension("💖.txt"), "💖");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 102–105why, one step back102 }103104 #[test]105 fn path_remove_ext_empty() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 106why, one step back106 assert_eq!(remove_extension(""), "");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 107–110why, one step back107 }108109 #[test]110 fn path_basename_simple() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 111why, one step back111 assert_eq!(basename("foo.txt"), "foo.txt");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 112–114why, one step back112 }113114 #[test]Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 115why, one step back115 fn path_basename_no_ext() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 116why, one step back116 assert_eq!(remove_extension(basename("foo.txt")), "foo");Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
lines 117–119why, one step back117 }118119 #[test]Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 120why, one step back120 fn path_basename_dir() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 121why, one step back121 assert_eq!(basename(&correct("dir/foo.txt")), "foo.txt");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 122–125why, one step back122 }123124 #[test]125 fn path_basename_empty() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 126why, one step back126 assert_eq!(basename(""), "");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 127–130why, one step back127 }128129 #[test]130 fn path_basename_utf8() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 131–132why, one step back131 assert_eq!(basename(&correct("💖/foo.txt")), "foo.txt");132 assert_eq!(basename(&correct("dir/💖.txt")), "💖.txt");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 133–136why, one step back133 }134135 #[test]136 fn path_dirname_simple() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 137why, one step back137 assert_eq!(dirname("foo.txt"), ".");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 138–141why, one step back138 }139140 #[test]141 fn path_dirname_dir() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 142why, one step back142 assert_eq!(dirname(&correct("dir/foo.txt")), "dir");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 143–146why, one step back143 }144145 #[test]146 fn path_dirname_utf8() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 147–148why, one step back147 assert_eq!(dirname(&correct("💖/foo.txt")), "💖");148 assert_eq!(dirname(&correct("dir/💖.txt")), "dir");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 149–151why, one step back149 }150151 #[test]Fix --execJ.Wbefore adoption6b232a8
line 152why, one step back152 fn path_dirname_empty() {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 153why, one step back153 assert_eq!(dirname(""), ".");Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
lines 154–158why, one step back154 }155156 #[test]157 fn path_dirname_root() {158 #[cfg(windows)]Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 159why, one step back159 assert_eq!(dirname("C:\\"), "C:");Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 160why, one step back160 #[cfg(windows)]Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 161why, one step back161 assert_eq!(dirname("\\"), "\\");Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7
line 162why, one step back162 #[cfg(not(windows))]Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 163why, one step back163 assert_eq!(dirname("/"), "/");Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
lines 164–165why, one step back164 }165}