mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Refactoring
This commit is contained in:
parent
2bdc925fbc
commit
0bb66b167e
1 changed files with 35 additions and 75 deletions
110
echo/echo.rs
110
echo/echo.rs
|
@ -41,6 +41,29 @@ fn isodigit(c: u8) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn convert_str(string: &str, index: uint, base: uint) -> (char, int) {
|
||||||
|
let (max_digits, is_legal_digit) = match base {
|
||||||
|
8u => (3, isodigit),
|
||||||
|
16u => (2, isxdigit),
|
||||||
|
_ => fail!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut bytes: ~[u8] = ~[];
|
||||||
|
for offset in range(0, max_digits) {
|
||||||
|
let c = string[index + offset as uint];
|
||||||
|
if is_legal_digit(c) {
|
||||||
|
bytes.push(c as u8);
|
||||||
|
} else {
|
||||||
|
if bytes.len() > 0 {
|
||||||
|
return (to_char(bytes, base), offset);
|
||||||
|
} else {
|
||||||
|
return (' ', offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (to_char(bytes, base), max_digits)
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = os::args();
|
let args = os::args();
|
||||||
let program = args[0].clone();
|
let program = args[0].clone();
|
||||||
|
@ -120,90 +143,27 @@ fn main() {
|
||||||
't' => print_char('\t'),
|
't' => print_char('\t'),
|
||||||
'v' => print_char('\x0B'),
|
'v' => print_char('\x0B'),
|
||||||
'x' => {
|
'x' => {
|
||||||
if index == string.len() - 1 {
|
let (c, num_char_used) = convert_str(string, index + 1, 16u);
|
||||||
|
if num_char_used == 0 {
|
||||||
print_char('\\');
|
print_char('\\');
|
||||||
print_char('x');
|
print_char('x');
|
||||||
} else if index == string.len() - 2 {
|
|
||||||
let next_char = string[index + 1];
|
|
||||||
if isxdigit(next_char) {
|
|
||||||
print_char(to_char([next_char as u8], 16u));
|
|
||||||
iter.next();
|
|
||||||
} else {
|
|
||||||
print_char('\\');
|
|
||||||
print_char('x');
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let next_char = string[index + 1];
|
print_char(c);
|
||||||
let next_next_char = string[index + 2];
|
for _ in range(0, num_char_used) {
|
||||||
match (isxdigit(next_char), isxdigit(next_next_char)) {
|
iter.next(); // consume used characters
|
||||||
(true, true) => {
|
}
|
||||||
print_char(to_char([next_char as u8, next_next_char as u8], 16u));
|
|
||||||
iter.next(); iter.next();
|
|
||||||
}
|
|
||||||
(true, false) => {
|
|
||||||
print_char(to_char([next_char as u8], 16u));
|
|
||||||
iter.next();
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
print_char('\\');
|
|
||||||
print_char('x');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'0' => {
|
'0' => {
|
||||||
if index == string.len() - 1 {
|
let (c, num_char_used) = convert_str(string, index + 1, 8u);
|
||||||
|
if num_char_used == 0 {
|
||||||
print_char('\\');
|
print_char('\\');
|
||||||
print_char('0');
|
print_char('0');
|
||||||
} else if index == string.len() - 2 {
|
|
||||||
let next_char = string[index + 1];
|
|
||||||
if isodigit(next_char) {
|
|
||||||
print_char(to_char([next_char as u8], 8u));
|
|
||||||
iter.next();
|
|
||||||
} else {
|
|
||||||
print_char('\\');
|
|
||||||
print_char('0');
|
|
||||||
}
|
|
||||||
} else if index == string.len() - 3 {
|
|
||||||
let next_char = string[index + 1];
|
|
||||||
let next_next_char = string[index + 2];
|
|
||||||
match (isodigit(next_char), isodigit(next_next_char)) {
|
|
||||||
(true, true) => {
|
|
||||||
print_char(to_char([next_char as u8, next_next_char as u8], 8u));
|
|
||||||
iter.next(); iter.next();
|
|
||||||
}
|
|
||||||
(true, false) => {
|
|
||||||
print_char(to_char([next_char as u8], 8u));
|
|
||||||
iter.next();
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
print_char('\\');
|
|
||||||
print_char('x');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
let next_char = string[index + 1];
|
print_char(c);
|
||||||
let next_next_char = string[index + 2];
|
for _ in range(0, num_char_used) {
|
||||||
let next_next_next_char = string[index + 3];
|
iter.next(); // consume used characters
|
||||||
match (isodigit(next_char), isodigit(next_next_char),
|
}
|
||||||
isodigit(next_next_next_char)) {
|
|
||||||
(true, true, true) => {
|
|
||||||
print_char(to_char([next_char as u8, next_next_char as u8, next_next_next_char as u8], 8u));
|
|
||||||
iter.next(); iter.next(); iter.next();
|
|
||||||
}
|
|
||||||
(true, true, false) => {
|
|
||||||
print_char(to_char([next_char as u8, next_next_char as u8], 8u));
|
|
||||||
iter.next(); iter.next();
|
|
||||||
}
|
|
||||||
(true, false, false) => {
|
|
||||||
print_char(to_char([next_char as u8], 8u));
|
|
||||||
iter.next();
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
print_char('\\');
|
|
||||||
print_char('x');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue