lib-nested/math/str2int/src/main.rs

61 lines
1.3 KiB
Rust
Raw Normal View History

2021-04-19 05:02:46 +02:00
use std::{
fs::File,
io::{Read, Write},
os::unix::io::FromRawFd
};
fn main() {
nested::magic_header();
eprintln!(" Parse MachineInt from String");
nested::magic_header();
eprintln!("
2021-05-03 19:55:29 +02:00
$1: radix
2021-04-19 05:02:46 +02:00
( )
2021-05-03 19:55:29 +02:00
( PositionalInt 10 BigEndian )
( Sequence ( Digit 10 ) )
( Sequence UTF-8-Char )
( Sequence MachineSyllab )
");
eprintln!("
>0: n
( )
( PositionalInt $radix BigEndian )
( Sequence ( Digit $radix ) )
( Sequence UTF-8-Char )
( Stream UTF-8-Char )
( Stream MachineSyllab )
2021-04-19 05:02:46 +02:00
");
eprintln!("
2021-05-03 19:55:29 +02:00
<1: n
2021-04-19 05:02:46 +02:00
( )
( MachineInt )
( MachineWord )
2021-05-03 19:55:29 +02:00
( Stream MachineSyllab )
2021-04-19 05:02:46 +02:00
");
nested::magic_header();
2021-05-03 19:55:29 +02:00
let mut f0 = unsafe { File::from_raw_fd(0) };
let mut f1 = unsafe { File::from_raw_fd(1) };
let mut args = std::env::args();
args.next().expect("Arg $0 missing!");
let radix_str = args.next().expect("Arg $1 required!");
2021-05-13 16:22:30 +02:00
2021-05-03 19:55:29 +02:00
let radix = u32::from_str_radix(&radix_str, 10).expect("could not parse radix");
if radix > 16 {
panic!("invalid radix! (radix<=16 required)");
}
2021-05-13 16:22:30 +02:00
2021-04-19 05:02:46 +02:00
let mut chars = Vec::new();
2021-05-13 16:22:30 +02:00
f0.read_to_end(&mut chars).expect("");
2021-05-03 19:55:29 +02:00
chars.retain(|c| (*c as char).is_alphanumeric());
2021-05-13 16:22:30 +02:00
f1.write(&u64::from_str_radix(&String::from_utf8_lossy(&chars), radix).unwrap().to_le_bytes()).expect("");
2021-04-19 05:02:46 +02:00
}