Skip to content

Commit 3434d10

Browse files
wc: compute is_utf8 in settings instead of one per file
Signed-off-by: Saumya Shah <saumyabshah90@gmail.com>
1 parent 7cf4678 commit 3434d10

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎src/uu/wc/src/wc.rs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct Settings<'a> {
5050
show_bytes: bool,
5151
show_chars: bool,
5252
count_chars_as_bytes: bool,
53+
is_utf8: bool,
5354
show_lines: bool,
5455
show_words: bool,
5556
show_max_line_length: bool,
@@ -65,6 +66,7 @@ impl Default for Settings<'_> {
6566
show_bytes: true,
6667
show_chars: false,
6768
count_chars_as_bytes: false,
69+
is_utf8: locale_encoding() == Encoding::Utf8,
6870
show_lines: true,
6971
show_words: true,
7072
show_max_line_length: false,
@@ -87,12 +89,14 @@ impl<'a> Settings<'a> {
8789
.unwrap_or_default();
8890

8991
let show_chars = matches.get_flag(options::CHAR);
92+
let show_words = matches.get_flag(options::WORDS);
9093
let settings = Self {
9194
show_bytes: matches.get_flag(options::BYTES),
9295
show_chars,
9396
count_chars_as_bytes: show_chars && locale_encoding() == Encoding::SingleByte,
97+
is_utf8: show_words && locale_encoding() == Encoding::Utf8,
9498
show_lines: matches.get_flag(options::LINES),
95-
show_words: matches.get_flag(options::WORDS),
99+
show_words,
96100
show_max_line_length: matches.get_flag(options::MAX_LINE_LENGTH),
97101
debug: matches.get_flag(options::DEBUG),
98102
files0_from,
@@ -545,83 +549,95 @@ fn word_count_from_reader<T: WordCountable>(
545549
word_count_from_reader_specialized::<_, false, false, false, true>(
546550
reader,
547551
settings.count_chars_as_bytes,
552+
settings.is_utf8,
548553
)
549554
}
550555
// show_max_line_length
551556
(_, false, false, true, false) => {
552557
word_count_from_reader_specialized::<_, false, false, true, false>(
553558
reader,
554559
settings.count_chars_as_bytes,
560+
settings.is_utf8,
555561
)
556562
}
557563
// show_max_line_length, show_words
558564
(_, false, false, true, true) => {
559565
word_count_from_reader_specialized::<_, false, false, true, true>(
560566
reader,
561567
settings.count_chars_as_bytes,
568+
settings.is_utf8,
562569
)
563570
}
564571
// show_lines, show_words
565572
(_, false, true, false, true) => {
566573
word_count_from_reader_specialized::<_, false, true, false, true>(
567574
reader,
568575
settings.count_chars_as_bytes,
576+
settings.is_utf8,
569577
)
570578
}
571579
// show_lines, show_max_line_length
572580
(_, false, true, true, false) => {
573581
word_count_from_reader_specialized::<_, false, true, true, false>(
574582
reader,
575583
settings.count_chars_as_bytes,
584+
settings.is_utf8,
576585
)
577586
}
578587
// show_lines, show_max_line_length, show_words
579588
(_, false, true, true, true) => {
580589
word_count_from_reader_specialized::<_, false, true, true, true>(
581590
reader,
582591
settings.count_chars_as_bytes,
592+
settings.is_utf8,
583593
)
584594
}
585595
// show_chars, show_words
586596
(_, true, false, false, true) => {
587597
word_count_from_reader_specialized::<_, true, false, false, true>(
588598
reader,
589599
settings.count_chars_as_bytes,
600+
settings.is_utf8,
590601
)
591602
}
592603
// show_chars, show_max_line_length
593604
(_, true, false, true, false) => {
594605
word_count_from_reader_specialized::<_, true, false, true, false>(
595606
reader,
596607
settings.count_chars_as_bytes,
608+
settings.is_utf8,
597609
)
598610
}
599611
// show_chars, show_max_line_length, show_words
600612
(_, true, false, true, true) => {
601613
word_count_from_reader_specialized::<_, true, false, true, true>(
602614
reader,
603615
settings.count_chars_as_bytes,
616+
settings.is_utf8,
604617
)
605618
}
606619
// show_chars, show_lines, show_words
607620
(_, true, true, false, true) => {
608621
word_count_from_reader_specialized::<_, true, true, false, true>(
609622
reader,
610623
settings.count_chars_as_bytes,
624+
settings.is_utf8,
611625
)
612626
}
613627
// show_chars, show_lines, show_max_line_length
614628
(_, true, true, true, false) => {
615629
word_count_from_reader_specialized::<_, true, true, true, false>(
616630
reader,
617631
settings.count_chars_as_bytes,
632+
settings.is_utf8,
618633
)
619634
}
620635
// show_chars, show_lines, show_max_line_length, show_words
621636
(_, true, true, true, true) => {
622637
word_count_from_reader_specialized::<_, true, true, true, true>(
623638
reader,
624639
settings.count_chars_as_bytes,
640+
settings.is_utf8,
625641
)
626642
}
627643
}
@@ -718,13 +734,13 @@ fn word_count_from_reader_specialized<
718734
>(
719735
reader: T,
720736
count_chars_as_bytes: bool,
737+
is_utf8: bool,
721738
) -> (WordCount, Option<io::Error>) {
722739
let mut total = WordCount::default();
723740
let mut reader = BufReadDecoder::new(reader.buffered());
724741
let mut in_word = false;
725742
let mut current_len = 0;
726743
let is_posixly_correct = *IS_POSIXLY_CORRECT;
727-
let is_utf8 = SHOW_WORDS && locale_encoding() == Encoding::Utf8;
728744
while let Some(chunk) = reader.next_strict() {
729745
match chunk {
730746
Ok(text) => {

0 commit comments

Comments
 (0)