Skip to content

Commit db34255

Browse files
committed
nl: implement -d/--section-delimiter
1 parent 6f51596 commit db34255

3 files changed

Lines changed: 125 additions & 11 deletions

File tree

src/uu/nl/src/helper.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
1313
// This vector holds error messages encountered.
1414
let mut errs: Vec<String> = vec![];
1515
settings.renumber = opts.get_flag(options::NO_RENUMBER);
16+
if let Some(delimiter) = opts.get_one::<String>(options::SECTION_DELIMITER) {
17+
// check whether the delimiter is a single ASCII char (1 byte)
18+
// because GNU nl doesn't add a ':' to single non-ASCII chars
19+
settings.section_delimiter = if delimiter.len() == 1 {
20+
format!("{delimiter}:")
21+
} else {
22+
delimiter.to_owned()
23+
};
24+
}
1625
if let Some(val) = opts.get_one::<String>(options::NUMBER_SEPARATOR) {
1726
settings.number_separator = val.to_owned();
1827
}

src/uu/nl/src/nl.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct Settings {
2323
body_numbering: NumberingStyle,
2424
footer_numbering: NumberingStyle,
2525
// The variable corresponding to -d
26-
section_delimiter: [char; 2],
26+
section_delimiter: String,
2727
// The variables corresponding to the options -v, -i, -l, -w.
2828
starting_line_number: i64,
2929
line_increment: i64,
@@ -43,7 +43,7 @@ impl Default for Settings {
4343
header_numbering: NumberingStyle::None,
4444
body_numbering: NumberingStyle::NonEmpty,
4545
footer_numbering: NumberingStyle::None,
46-
section_delimiter: ['\\', ':'],
46+
section_delimiter: String::from("\\:"),
4747
starting_line_number: 1,
4848
line_increment: 1,
4949
join_blank_lines: 1,
@@ -120,6 +120,32 @@ impl NumberFormat {
120120
}
121121
}
122122

123+
enum SectionDelimiter {
124+
Header,
125+
Body,
126+
Footer,
127+
}
128+
129+
impl SectionDelimiter {
130+
// A valid section delimiter contains the pattern one to three times,
131+
// and nothing else.
132+
fn parse(s: &str, pattern: &str) -> Option<Self> {
133+
if s.is_empty() || pattern.is_empty() {
134+
return None;
135+
}
136+
137+
let pattern_count = s.matches(pattern).count();
138+
let is_length_ok = pattern_count * pattern.len() == s.len();
139+
140+
match (pattern_count, is_length_ok) {
141+
(3, true) => Some(Self::Header),
142+
(2, true) => Some(Self::Body),
143+
(1, true) => Some(Self::Footer),
144+
_ => None,
145+
}
146+
}
147+
}
148+
123149
pub mod options {
124150
pub const HELP: &str = "help";
125151
pub const FILE: &str = "file";
@@ -299,14 +325,12 @@ fn nl<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {
299325
consecutive_empty_lines = 0;
300326
};
301327

302-
// FIXME section delimiters are hardcoded and settings.section_delimiter is ignored
303-
// because --section-delimiter is not correctly implemented yet
304-
let _ = settings.section_delimiter; // XXX suppress "field never read" warning
305-
let new_numbering_style = match line.as_str() {
306-
"\\:\\:\\:" => Some(&settings.header_numbering),
307-
"\\:\\:" => Some(&settings.body_numbering),
308-
"\\:" => Some(&settings.footer_numbering),
309-
_ => None,
328+
let new_numbering_style = match SectionDelimiter::parse(&line, &settings.section_delimiter)
329+
{
330+
Some(SectionDelimiter::Header) => Some(&settings.header_numbering),
331+
Some(SectionDelimiter::Body) => Some(&settings.body_numbering),
332+
Some(SectionDelimiter::Footer) => Some(&settings.footer_numbering),
333+
None => None,
310334
};
311335

312336
if let Some(new_style) = new_numbering_style {

tests/by-util/test_nl.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5-
// spell-checker:ignore binvalid finvalid hinvalid iinvalid linvalid ninvalid vinvalid winvalid
5+
//
6+
// spell-checker:ignore binvalid finvalid hinvalid iinvalid linvalid nabcabc nabcabcabc ninvalid vinvalid winvalid
67
use crate::common::util::TestScenario;
78

89
#[test]
@@ -486,3 +487,83 @@ fn test_line_number_overflow() {
486487
.stdout_is(format!("{}\ta\n", i64::MIN))
487488
.stderr_is("nl: line number overflow\n");
488489
}
490+
491+
#[test]
492+
fn test_section_delimiter() {
493+
for arg in ["-dabc", "--section-delimiter=abc"] {
494+
new_ucmd!()
495+
.arg(arg)
496+
.pipe_in("a\nabcabcabc\nb") // header section
497+
.succeeds()
498+
.stdout_is(" 1\ta\n\n b\n");
499+
500+
new_ucmd!()
501+
.arg(arg)
502+
.pipe_in("a\nabcabc\nb") // body section
503+
.succeeds()
504+
.stdout_is(" 1\ta\n\n 1\tb\n");
505+
506+
new_ucmd!()
507+
.arg(arg)
508+
.pipe_in("a\nabc\nb") // footer section
509+
.succeeds()
510+
.stdout_is(" 1\ta\n\n b\n");
511+
}
512+
}
513+
514+
#[test]
515+
fn test_one_char_section_delimiter_expansion() {
516+
for arg in ["-da", "--section-delimiter=a"] {
517+
new_ucmd!()
518+
.arg(arg)
519+
.pipe_in("a\na:a:a:\nb") // header section
520+
.succeeds()
521+
.stdout_is(" 1\ta\n\n b\n");
522+
523+
new_ucmd!()
524+
.arg(arg)
525+
.pipe_in("a\na:a:\nb") // body section
526+
.succeeds()
527+
.stdout_is(" 1\ta\n\n 1\tb\n");
528+
529+
new_ucmd!()
530+
.arg(arg)
531+
.pipe_in("a\na:\nb") // footer section
532+
.succeeds()
533+
.stdout_is(" 1\ta\n\n b\n");
534+
}
535+
}
536+
537+
#[test]
538+
fn test_non_ascii_one_char_section_delimiter() {
539+
for arg in ["-dä", "--section-delimiter=ä"] {
540+
new_ucmd!()
541+
.arg(arg)
542+
.pipe_in("a\näää\nb") // header section
543+
.succeeds()
544+
.stdout_is(" 1\ta\n\n b\n");
545+
546+
new_ucmd!()
547+
.arg(arg)
548+
.pipe_in("a\nää\nb") // body section
549+
.succeeds()
550+
.stdout_is(" 1\ta\n\n 1\tb\n");
551+
552+
new_ucmd!()
553+
.arg(arg)
554+
.pipe_in("a\nä\nb") // footer section
555+
.succeeds()
556+
.stdout_is(" 1\ta\n\n b\n");
557+
}
558+
}
559+
560+
#[test]
561+
fn test_empty_section_delimiter() {
562+
for arg in ["-d ''", "--section-delimiter=''"] {
563+
new_ucmd!()
564+
.arg(arg)
565+
.pipe_in("a\n\nb")
566+
.succeeds()
567+
.stdout_is(" 1\ta\n \n 2\tb\n");
568+
}
569+
}

0 commit comments

Comments
 (0)