Skip to content

Commit 4d61894

Browse files
committed
use const table
1 parent a855a77 commit 4d61894

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

text/wc.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,18 @@ impl CountInfo {
6262
}
6363
}
6464

65-
fn is_space(c: usize) -> bool {
66-
( c > 8 && c < 14) || (c == 32)
67-
}
68-
69-
fn create_table() -> [bool; 256] {
65+
const fn create_table() -> [bool; 256] {
7066
let mut table = [false; 256];
71-
72-
for i in 0..256 {
73-
table[i] = is_space(i)
74-
}
67+
table[9] = true;
68+
table[10] = true;
69+
table[11] = true;
70+
table[12] = true;
71+
table[13] = true;
72+
table[32] = true;
7573
table
7674
}
7775

76+
const BYTE_TABLE: [bool; 256] = create_table();
7877

7978
fn build_display_str(args: &Args, count: &CountInfo, filename: &OsStr) -> String {
8079
let mut output = String::with_capacity(filename.len() + (3 * 10));
@@ -231,14 +230,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
231230
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
232231

233232
let mut exit_code = 0;
234-
let table = create_table();
235233
let mut totals = CountInfo::new();
236234

237235
// input via stdin
238236
if args.files.is_empty() {
239237
let mut count = CountInfo::new();
240238

241-
if let Err(e) = wc_file(&args, chars_mode, &PathBuf::new(), &mut count, &table) {
239+
if let Err(e) = wc_file(&args, chars_mode, &PathBuf::new(), &mut count, &BYTE_TABLE) {
242240
exit_code = 1;
243241
eprintln!("stdin: {}", e);
244242
}
@@ -248,7 +246,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
248246
for filename in &args.files {
249247
let mut count = CountInfo::new();
250248

251-
if let Err(e) = wc_file(&args, chars_mode, filename, &mut count, &table) {
249+
if let Err(e) = wc_file(&args, chars_mode, filename, &mut count, &BYTE_TABLE) {
252250
exit_code = 1;
253251
eprintln!("{}: {}", filename.display(), e);
254252
}

0 commit comments

Comments
 (0)