lib-nested/math/str2int/src/main.rs
2021-05-13 16:22:30 +02:00

60 lines
1.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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!("
$1: radix
( )
( 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 )
");
eprintln!("
<1: n
( )
( MachineInt )
( MachineWord )
( Stream MachineSyllab )
");
nested::magic_header();
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!");
let radix = u32::from_str_radix(&radix_str, 10).expect("could not parse radix");
if radix > 16 {
panic!("invalid radix! (radix<=16 required)");
}
let mut chars = Vec::new();
f0.read_to_end(&mut chars).expect("");
chars.retain(|c| (*c as char).is_alphanumeric());
f1.write(&u64::from_str_radix(&String::from_utf8_lossy(&chars), radix).unwrap().to_le_bytes()).expect("");
}