-
Notifications
You must be signed in to change notification settings - Fork 1
/
20.rs
57 lines (50 loc) · 1.94 KB
/
20.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::io;
// Rust constants work the same way as symbolic constansts in C
const TABSTOP : i32 = 4;
/// A program 'detab' that replaces tabs in the input with the proper number of
/// blanks to space to the next tab stop. Basically, there is a tabstop every Nth
/// position in the input string and all we need to do is expand output with
/// whitespaces until the next tabstop every time a tab character (\t) is found
/// within length of the current tabstop. The concept of a tabstop makes more
/// sense in the context of typewriters and robust text editor (for alignment).
///
/// For more info on tabstops check: https://en.wikipedia.org/wiki/Tab_stop
fn main ()
{
let mut input = String::new();
loop {
match io::stdin().read_line(&mut input) {
Ok (n_bytes) => {
// Break out of main loop if EOF has been reached.
if n_bytes == 0 {
break;
}
// a mutable counter to keep track of where we are inside TABSTOP
let mut idx : i32 = 0;
for _c in input.chars() {
match _c {
'\t' => {
for _ in idx..TABSTOP {
print!(" ");
}
},
_ => {
print!("{}", _c);
idx += 1;
}
}
// Reset tabstop counter when limit is reached
// We don't need to reset when newline is found because 'read_line'
// function will read everything until a '\n' is found
if idx == TABSTOP {
idx = 0;
}
}
input.clear();
},
Err(_) => {
panic!("Unexpected error.");
}
}
}
}