From 02f8587f675b65c780a9c0e60c61d2cddd08017f Mon Sep 17 00:00:00 2001 From: Jeremy Soller <jackpot51@gmail.com> Date: Thu, 3 Dec 2015 12:18:07 -0700 Subject: [PATCH] Fix env::args to match libstd --- main.rs | 27 ++++++++++++++++++++----- to_num.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 to_num.rs diff --git a/main.rs b/main.rs index 1d9aa8dc..b7d032e2 100644 --- a/main.rs +++ b/main.rs @@ -8,7 +8,10 @@ use std::io::{Read, Write}; use std::env; use std::time::Duration; use std::process; -use std::to_num::ToNum; + +use self::to_num::ToNum; + +pub mod to_num; macro_rules! readln { () => ({ @@ -72,7 +75,7 @@ impl<'a> Command<'a> { main: Box::new(|args: &Vec<String>| { match args.get(1) { Some(path) => { - if ! env::set_current_dir(&path) { + if env::set_current_dir(&path).is_err() { println!("Bad path: {}", path); } } @@ -618,9 +621,16 @@ impl<'a> Application<'a> { /// Run the application pub fn main(&mut self) { println!("Type help for a command list"); - if let Some(arg) = env::args().get(1) { + for arg in env::args().skip(1) { + let cwd = match env::current_dir() { + Ok(path) => format!("{}", &path), + Err(_) => "?".to_string() + }; + let command = "run ".to_string() + arg; - println!("user@redox:{}# {}", &env::current_dir().unwrap_or("?".to_string()), command); + + println!("user@redox:{}# {}", cwd, command); + self.on_command(&command); } @@ -632,7 +642,14 @@ impl<'a> Application<'a> { print!("- "); } } - print!("user@redox:{}# ", &env::current_dir().unwrap_or("?".to_string())); + + let cwd = match env::current_dir() { + Ok(path) => format!("{}", &path), + Err(_) => "?".to_string() + }; + + print!("user@redox:{}# ", cwd); + if let Some(command_original) = readln!() { let command = command_original.trim(); if command == "exit" { diff --git a/to_num.rs b/to_num.rs new file mode 100644 index 00000000..98ef9250 --- /dev/null +++ b/to_num.rs @@ -0,0 +1,59 @@ +//! Types convertable to integers + +/// Parse the string to a integer using a given radix +pub trait ToNum { + fn to_num_radix(&self, radix: usize) -> usize; + fn to_num_radix_signed(&self, radix: usize) -> isize; + fn to_num(&self) -> usize; + fn to_num_signed(&self) -> isize; +} + +impl ToNum for str { + fn to_num_radix(&self, radix: usize) -> usize { + if radix == 0 { + return 0; + } + + let mut num = 0; + for c in self.chars() { + let digit; + if c >= '0' && c <= '9' { + digit = c as usize - '0' as usize + } else if c >= 'A' && c <= 'Z' { + digit = c as usize - 'A' as usize + 10 + } else if c >= 'a' && c <= 'z' { + digit = c as usize - 'a' as usize + 10 + } else { + break; + } + + if digit >= radix { + break; + } + + num *= radix; + num += digit; + } + + num + } + + /// Parse the string as a signed integer using a given radix + fn to_num_radix_signed(&self, radix: usize) -> isize { + if self.starts_with('-') { + -(self[1 ..].to_num_radix(radix) as isize) + } else { + self.to_num_radix(radix) as isize + } + } + + /// Parse it as a unsigned integer in base 10 + fn to_num(&self) -> usize { + self.to_num_radix(10) + } + + /// Parse it as a signed integer in base 10 + fn to_num_signed(&self) -> isize { + self.to_num_radix_signed(10) + } +} -- GitLab