Skip to content
Myr

input.rs

This file

Path

src/exec/input.rs

165 lines · 3.8 KiB · Rust

Last changed by

Add exec tests and cfg(test) compiler guardsJosh Leeb-du Toitbefore adoptionadc467b

History of this file

Take it

Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2

lines 1–8why, one step back
1// Copyright (c) 2017 fd developers
2// 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.8

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 9–10why, one step back
9use std::path::MAIN_SEPARATOR;
10

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 11–18why, one step back
11/// Removes the parent component of the path
12pub 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 back
19

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 20–25why, one step back
20 // 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 path

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 26–27why, one step back
26}
27

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 28–32why, one step back
28/// Removes the extension from the path
29pub 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 back
33

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 34–37why, one step back
34 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 back
38 }

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 39–40why, one step back
39 if character == '.' {
40 ext_index = id;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

line 41why, one step back
41 }

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 42why, one step back
42 }

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

line 43why, one step back
43

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 44–46why, one step back
44 // Account for hidden files and directories
45 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 back
47 }
48

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 49–50why, one step back
49 path
50}

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

line 51why, one step back
51

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 52–59why, one step back
52/// 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 back
60 }
61 }62

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 63–69why, one step back
63 // 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 back
70 }
71}7273#[cfg(test)]74mod tests {

Add exec tests and cfg(test) compiler guardsJosh Leeb-du Toitbefore adoptionadc467b

line 75why, one step back
75 use super::*;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 76–77why, one step back
76
77 fn correct(input: &str) -> String {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 78why, one step back
78 input.replace('/', &MAIN_SEPARATOR.to_string())

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 79–82why, one step back
79 }
8081 #[test]82 fn path_remove_ext_simple() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 83why, one step back
83 assert_eq!(remove_extension("foo.txt"), "foo");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 84–88why, one step back
84 }
8586 #[test]87 fn path_remove_ext_dir() {88 assert_eq!(

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 89–90why, one step back
89 remove_extension(&correct("dir/foo.txt")),
90 correct("dir/foo")

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 91–95why, one step back
91 );
92 }9394 #[test]95 fn path_hidden() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 96why, one step back
96 assert_eq!(remove_extension(".foo"), ".foo")

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 97–100why, one step back
97 }
9899 #[test]100 fn path_remove_ext_utf8() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 101why, one step back
101 assert_eq!(remove_extension("💖.txt"), "💖");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 102–105why, one step back
102 }
103104 #[test]105 fn path_remove_ext_empty() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 106why, one step back
106 assert_eq!(remove_extension(""), "");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 107–110why, one step back
107 }
108109 #[test]110 fn path_basename_simple() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 111why, one step back
111 assert_eq!(basename("foo.txt"), "foo.txt");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 112–114why, one step back
112 }
113114 #[test]

Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7

line 115why, one step back
115 fn path_basename_no_ext() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 116why, one step back
116 assert_eq!(remove_extension(basename("foo.txt")), "foo");

Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7

lines 117–119why, one step back
117 }
118119 #[test]

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

line 120why, one step back
120 fn path_basename_dir() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 121why, one step back
121 assert_eq!(basename(&correct("dir/foo.txt")), "foo.txt");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 122–125why, one step back
122 }
123124 #[test]125 fn path_basename_empty() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 126why, one step back
126 assert_eq!(basename(""), "");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 127–130why, one step back
127 }
128129 #[test]130 fn path_basename_utf8() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 131–132why, one step back
131 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 back
133 }
134135 #[test]136 fn path_dirname_simple() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 137why, one step back
137 assert_eq!(dirname("foo.txt"), ".");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 138–141why, one step back
138 }
139140 #[test]141 fn path_dirname_dir() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 142why, one step back
142 assert_eq!(dirname(&correct("dir/foo.txt")), "dir");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 143–146why, one step back
143 }
144145 #[test]146 fn path_dirname_utf8() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 147–148why, one step back
147 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 back
149 }
150151 #[test]

Fix --execJ.Wbefore adoption6b232a8

line 152why, one step back
152 fn path_dirname_empty() {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 153why, one step back
153 assert_eq!(dirname(""), ".");

Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7

lines 154–158why, one step back
154 }
155156 #[test]157 fn path_dirname_root() {158 #[cfg(windows)]

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 159why, one step back
159 assert_eq!(dirname("C:\\"), "C:");

Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7

line 160why, one step back
160 #[cfg(windows)]

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 161why, one step back
161 assert_eq!(dirname("\\"), "\\");

Fix Input::remove_extension and Input::dirnameJ.Wbefore adoptiondc29ef7

line 162why, one step back
162 #[cfg(not(windows))]

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 163why, one step back
163 assert_eq!(dirname("/"), "/");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 164–165why, one step back
164 }
165}