Skip to content

Commit 84165be

Browse files
xtqqczzecakebaker
authored andcommitted
refactor(csplit): use &str slices for patterns
1 parent 5d505ca commit 84165be

2 files changed

Lines changed: 23 additions & 31 deletions

File tree

src/uu/csplit/src/csplit.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// spell-checker:ignore rustdoc
66
#![allow(rustdoc::private_intra_doc_links)]
77

8+
use std::borrow::Borrow;
89
use std::cmp::Ordering;
910
use std::ffi::OsString;
1011
use std::io::{self, BufReader, ErrorKind};
@@ -111,7 +112,7 @@ impl<T: BufRead> Iterator for LinesWithNewlines<T> {
111112
/// - [`CsplitError::MatchNotFound`] if no line matched a regular expression.
112113
/// - [`CsplitError::MatchNotFoundOnRepetition`], like previous but after applying the pattern
113114
/// more than once.
114-
pub fn csplit<T>(options: &CsplitOptions, patterns: &[String], input: T) -> Result<(), CsplitError>
115+
pub fn csplit<T>(options: &CsplitOptions, patterns: &[&str], input: T) -> Result<(), CsplitError>
115116
where
116117
T: BufRead,
117118
{
@@ -627,10 +628,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
627628
let file_name = matches.get_one::<OsString>(options::FILE).unwrap();
628629

629630
// get the patterns to split on
630-
let patterns: Vec<String> = matches
631+
let patterns: Vec<_> = matches
631632
.get_many::<String>(options::PATTERN)
632633
.unwrap()
633-
.map(ToOwned::to_owned)
634+
.map(Borrow::borrow)
634635
.collect();
635636
let options = CsplitOptions::new(&matches)?;
636637
if file_name == "-" {

src/uu/csplit/src/patterns.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ impl Iterator for ExecutePatternIter {
9595
/// due to, e.g.,:
9696
/// - an invalid regular expression;
9797
/// - an invalid number for, e.g., the offset.
98-
pub fn get_patterns(args: &[String]) -> Result<Vec<Pattern>, CsplitError> {
98+
pub fn get_patterns(args: &[&str]) -> Result<Vec<Pattern>, CsplitError> {
9999
let patterns = extract_patterns(args)?;
100100
validate_line_numbers(&patterns)?;
101101
Ok(patterns)
102102
}
103103

104-
fn extract_patterns(args: &[String]) -> Result<Vec<Pattern>, CsplitError> {
104+
fn extract_patterns(args: &[&str]) -> Result<Vec<Pattern>, CsplitError> {
105105
let mut patterns = Vec::with_capacity(args.len());
106106
let to_match_reg =
107107
Regex::new(r"^(/(?P<UPTO>.+)/|%(?P<SKIPTO>.+)%)(?P<OFFSET>[\+-]?[0-9]+)?$").unwrap();
108108
let execute_ntimes_reg = Regex::new(r"^\{(?P<TIMES>[0-9]+)|\*\}$").unwrap();
109-
let mut iter = args.iter().peekable();
109+
let mut iter = args.iter().copied().peekable();
110110

111111
while let Some(arg) = iter.next() {
112112
// get the number of times a pattern is repeated, which is at least once plus whatever is
@@ -185,17 +185,14 @@ mod tests {
185185

186186
#[test]
187187
fn bad_pattern() {
188-
let input = vec!["bad".to_string()];
189-
assert!(get_patterns(input.as_slice()).is_err());
188+
let input = ["bad"];
189+
assert!(get_patterns(&input).is_err());
190190
}
191191

192192
#[test]
193193
fn up_to_line_pattern() {
194-
let input: Vec<String> = vec!["24", "42", "{*}", "50", "{4}"]
195-
.into_iter()
196-
.map(ToOwned::to_owned)
197-
.collect();
198-
let patterns = get_patterns(input.as_slice()).unwrap();
194+
let input = ["24", "42", "{*}", "50", "{4}"];
195+
let patterns = get_patterns(&input).unwrap();
199196
assert_eq!(patterns.len(), 3);
200197
match patterns.first() {
201198
Some(Pattern::UpToLine(24, ExecutePattern::Times(1))) => (),
@@ -214,7 +211,7 @@ mod tests {
214211
#[test]
215212
#[allow(clippy::cognitive_complexity)]
216213
fn up_to_match_pattern() {
217-
let input: Vec<String> = vec![
214+
let input = [
218215
"/test1.*end$/",
219216
"/test2.*end$/",
220217
"{*}",
@@ -223,11 +220,8 @@ mod tests {
223220
"/test4.*end$/3",
224221
"/test5.*end$/+3",
225222
"/test6.*end$/-3",
226-
]
227-
.into_iter()
228-
.map(ToString::to_string)
229-
.collect();
230-
let patterns = get_patterns(input.as_slice()).unwrap();
223+
];
224+
let patterns = get_patterns(&input).unwrap();
231225
assert_eq!(patterns.len(), 6);
232226
match patterns.first() {
233227
Some(Pattern::UpToMatch(reg, 0, ExecutePattern::Times(1))) => {
@@ -276,7 +270,7 @@ mod tests {
276270
#[test]
277271
#[allow(clippy::cognitive_complexity)]
278272
fn skip_to_match_pattern() {
279-
let input: Vec<String> = vec![
273+
let input = [
280274
"%test1.*end$%",
281275
"%test2.*end$%",
282276
"{*}",
@@ -285,11 +279,8 @@ mod tests {
285279
"%test4.*end$%3",
286280
"%test5.*end$%+3",
287281
"%test6.*end$%-3",
288-
]
289-
.into_iter()
290-
.map(ToString::to_string)
291-
.collect();
292-
let patterns = get_patterns(input.as_slice()).unwrap();
282+
];
283+
let patterns = get_patterns(&input).unwrap();
293284
assert_eq!(patterns.len(), 6);
294285
match patterns.first() {
295286
Some(Pattern::SkipToMatch(reg, 0, ExecutePattern::Times(1))) => {
@@ -346,26 +337,26 @@ mod tests {
346337

347338
#[test]
348339
fn line_number_smaller_than_previous() {
349-
let input: Vec<String> = vec!["10".to_string(), "5".to_string()];
350-
match get_patterns(input.as_slice()) {
340+
let input = ["10", "5"];
341+
match get_patterns(&input) {
351342
Err(CsplitError::LineNumberSmallerThanPrevious(5, 10)) => (),
352343
_ => panic!("expected LineNumberSmallerThanPrevious error"),
353344
}
354345
}
355346

356347
#[test]
357348
fn line_number_smaller_than_previous_separate() {
358-
let input: Vec<String> = vec!["10".to_string(), "/20/".to_string(), "5".to_string()];
359-
match get_patterns(input.as_slice()) {
349+
let input = ["10", "/20/", "5"];
350+
match get_patterns(&input) {
360351
Err(CsplitError::LineNumberSmallerThanPrevious(5, 10)) => (),
361352
_ => panic!("expected LineNumberSmallerThanPrevious error"),
362353
}
363354
}
364355

365356
#[test]
366357
fn line_number_zero_separate() {
367-
let input: Vec<String> = vec!["10".to_string(), "/20/".to_string(), "0".to_string()];
368-
match get_patterns(input.as_slice()) {
358+
let input = ["10", "/20/", "0"];
359+
match get_patterns(&input) {
369360
Err(CsplitError::LineNumberIsZero) => (),
370361
_ => panic!("expected LineNumberIsZero error"),
371362
}

0 commit comments

Comments
 (0)