Skip to content
Myr

input.rs

This file

Path

src/exec/input.rs

232 lines · 5.8 KiB · Rust

Last changed by

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

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–11why, one step back
9use std::path::MAIN_SEPARATOR;
10use std::borrow::Cow;11

Fix --execJ.Wbefore adoption6b232a8

line 12why, one step back
12use shell_escape::escape;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 13–47why, one step back
13
14/// A builder for efficiently generating input strings.15///16/// After choosing your required specs, the `get()` method will escape special characters found17/// in the input. Allocations will only occur if special characters are found that need to be18/// escaped.19pub struct Input<'a> {20 data: &'a str,21}2223impl<'a> Input<'a> {24 /// Creates a new `Input` structure, which provides access to command-building25 /// primitives, such as `basename()` and `dirname()`.26 pub fn new(data: &'a str) -> Input<'a> {27 Input { data }28 }2930 /// Removes the parent component of the path31 pub fn basename(&'a mut self) -> &'a mut Self {32 let mut index = 0;33 for (id, character) in self.data.char_indices() {34 if character == MAIN_SEPARATOR {35 index = id;36 }37 }3839 if index != 0 {40 self.data = &self.data[index + 1..]41 }4243 self44 }4546 /// Removes the extension from the path47 pub fn remove_extension(&'a mut self) -> &'a mut Self {

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

line 48why, one step back
48 let mut has_dir = false;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 49–53why, one step back
49 let mut dir_index = 0;
50 let mut ext_index = 0;5152 for (id, character) in self.data.char_indices() {53 if character == MAIN_SEPARATOR {

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

line 54why, one step back
54 has_dir = true;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 55–62why, one step back
55 dir_index = id;
56 }57 if character == '.' {58 ext_index = id;59 }60 }6162 // Account for hidden files and directories

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

line 63why, one step back
63 if ext_index != 0 && (!has_dir || dir_index + 2 <= ext_index) {

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 64–71why, one step back
64 self.data = &self.data[0..ext_index];
65 }6667 self68 }6970 /// Removes the basename from the path.71 pub fn dirname(&'a mut self) -> &'a mut Self {

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

line 72why, one step back
72 let mut has_dir = false;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 73–75why, one step back
73 let mut index = 0;
74 for (id, character) in self.data.char_indices() {75 if character == MAIN_SEPARATOR {

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

line 76why, one step back
76 has_dir = true;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 77–80why, one step back
77 index = id;
78 }79 }80

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

line 81why, one step back
81 self.data = if !has_dir {

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

line 82why, one step back
82 "."

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

lines 83–84why, one step back
83 } else if index == 0 {
84 &self.data[..1]

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 85–92why, one step back
85 } else {
86 &self.data[0..index]87 };8889 self90 }9192 pub fn get(&'a self) -> Cow<'a, str> {

Fix --execJ.Wbefore adoption6b232a8

lines 93–94why, one step back
93 escape(Cow::Borrowed(self.data))
94 }

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

line 95why, one step back
95

Fix --execJ.Wbefore adoption6b232a8

lines 96–98why, one step back
96 #[cfg(test)]
97 fn get_private(&'a self) -> Cow<'a, str> {98 Cow::Borrowed(self.data)

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 99–113why, one step back
99 }
100}101102#[cfg(test)]103mod tests {104 use super::{MAIN_SEPARATOR, Input};105106 fn correct(input: &str) -> String {107 let mut sep = String::new();108 sep.push(MAIN_SEPARATOR);109 input.replace('/', &sep)110 }111112 #[test]113 fn path_remove_ext_simple() {

Fix --execJ.Wbefore adoption6b232a8

lines 114–117why, one step back
114 assert_eq!(
115 &Input::new("foo.txt").remove_extension().get_private(),116 "foo"117 );

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 118–122why, one step back
118 }
119120 #[test]121 fn path_remove_ext_dir() {122 assert_eq!(

Fix --execJ.Wbefore adoption6b232a8

lines 123–125why, one step back
123 &Input::new(&correct("dir/foo.txt"))
124 .remove_extension()125 .get_private(),

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 126–131why, one step back
126 &correct("dir/foo")
127 );128 }129130 #[test]131 fn path_hidden() {

Fix --execJ.Wbefore adoption6b232a8

line 132why, one step back
132 assert_eq!(&Input::new(".foo").remove_extension().get_private(), ".foo")

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 133–136why, one step back
133 }
134135 #[test]136 fn path_remove_ext_utf8() {

Fix --execJ.Wbefore adoption6b232a8

lines 137–140why, one step back
137 assert_eq!(
138 &Input::new("💖.txt").remove_extension().get_private(),139 "💖"140 );

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 141–144why, one step back
141 }
142143 #[test]144 fn path_remove_ext_empty() {

Fix --execJ.Wbefore adoption6b232a8

line 145why, one step back
145 assert_eq!(&Input::new("").remove_extension().get_private(), "");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 146–149why, one step back
146 }
147148 #[test]149 fn path_basename_simple() {

Fix --execJ.Wbefore adoption6b232a8

line 150why, one step back
150 assert_eq!(&Input::new("foo.txt").basename().get_private(), "foo.txt");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 151–153why, one step back
151 }
152153 #[test]

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

lines 154–164why, one step back
154 fn path_basename_no_ext() {
155 assert_eq!(156 &Input::new("foo.txt")157 .basename()158 .remove_extension()159 .get_private(),160 "foo"161 );162 }163164 #[test]

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 165–166why, one step back
165 fn path_basename_dir() {
166 assert_eq!(

Fix --execJ.Wbefore adoption6b232a8

line 167why, one step back
167 &Input::new(&correct("dir/foo.txt")).basename().get_private(),

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 168–173why, one step back
168 "foo.txt"
169 );170 }171172 #[test]173 fn path_basename_empty() {

Fix --execJ.Wbefore adoption6b232a8

line 174why, one step back
174 assert_eq!(&Input::new("").basename().get_private(), "");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 175–179why, one step back
175 }
176177 #[test]178 fn path_basename_utf8() {179 assert_eq!(

Fix --execJ.Wbefore adoption6b232a8

lines 180–182why, one step back
180 &Input::new(&correct("💖/foo.txt"))
181 .basename()182 .get_private(),

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 183–185why, one step back
183 "foo.txt"
184 );185 assert_eq!(

Fix --execJ.Wbefore adoption6b232a8

lines 186–188why, one step back
186 &Input::new(&correct("dir/💖.txt"))
187 .basename()188 .get_private(),

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 189–194why, one step back
189 "💖.txt"
190 );191 }192193 #[test]194 fn path_dirname_simple() {

Fix --execJ.Wbefore adoption6b232a8

line 195why, one step back
195 assert_eq!(&Input::new("foo.txt").dirname().get_private(), ".");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 196–199why, one step back
196 }
197198 #[test]199 fn path_dirname_dir() {

Fix --execJ.Wbefore adoption6b232a8

lines 200–203why, one step back
200 assert_eq!(
201 &Input::new(&correct("dir/foo.txt")).dirname().get_private(),202 "dir"203 );

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 204–208why, one step back
204 }
205206 #[test]207 fn path_dirname_utf8() {208 assert_eq!(

Fix --execJ.Wbefore adoption6b232a8

line 209why, one step back
209 &Input::new(&correct("💖/foo.txt")).dirname().get_private(),

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 210–212why, one step back
210 "💖"
211 );212 assert_eq!(

Fix --execJ.Wbefore adoption6b232a8

lines 213–214why, one step back
213 &Input::new(&correct("dir/💖.txt")).dirname().get_private(),
214 "dir"

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 215–218why, one step back
215 );
216 }217218 #[test]

Fix --execJ.Wbefore adoption6b232a8

lines 219–220why, one step back
219 fn path_dirname_empty() {
220 assert_eq!(&Input::new("").dirname().get_private(), ".");

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

lines 221–230why, one step back
221 }
222223 #[test]224 fn path_dirname_root() {225 #[cfg(windows)]226 assert_eq!(&Input::new("C:\\").dirname().get_private(), "C:");227 #[cfg(windows)]228 assert_eq!(&Input::new("\\").dirname().get_private(), "\\");229 #[cfg(not(windows))]230 assert_eq!(&Input::new("/").dirname().get_private(), "/");

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

lines 231–232why, one step back
231 }
232}