use std::ffi::OsStr;
use std::num::{ParseFloatError, ParseIntError};
use std::str::FromStr;
pub fn is_number<S>(text: S) -> bool where S: AsRef<OsStr> {
let chars = self::to_chars(text);
let mut has_dot = false;
let mut has_negative = false;
let mut tpmode = false;
let mut tpsymbol = None;
let mut tpvec = Vec::with_capacity(5);
for (ix, &ch) in chars.iter().enumerate() {
if tpmode {
tpvec.push(ch);
continue;
}
if ch == '-' && ix == 0 {
has_negative = true;
continue;
}
if ch == '.' {
if has_dot { return false; }
has_dot = true;
continue;
}
if ch == 'f' || ch == 'u' || ch == 'i' {
tpsymbol = Some(ch);
tpvec.push(ch);
tpmode = true;
continue;
}
if self::char_is_digit(ch) { continue; }
return false;
}
if !tpvec.is_empty() {
let tvs: String = tpvec.into_iter().collect();
let tpvstr = &tvs[..];
if !(tpvstr == "i8" || tpvstr == "i16" || tpvstr == "i32" || tpvstr == "i64" || tpvstr == "i128" || tpvstr == "isize" ||
tpvstr == "u8" || tpvstr == "u16" || tpvstr == "u32" || tpvstr == "u64" || tpvstr == "u128" || tpvstr == "usize" ||
tpvstr == "f32" || tpvstr == "f64") {
return false;
}
}
match tpsymbol {
Some('f') => true,
Some('i') => !chars.contains(&'.'),
Some('u') => !chars.contains(&'.') && !chars.contains(&'-'),
Some(_) => false,
None => true
}
}
pub fn is_udigit<S>(text: S) -> bool where S: AsRef<OsStr> {
self::is_digit(text, false)
}
pub fn is_idigit<S>(text: S) -> bool where S: AsRef<OsStr> {
self::is_digit(text, true)
}
pub fn is_digit<S>(text: S, allow_negative: bool) -> bool where S: AsRef<OsStr> {
let chars = self::to_chars(text);
for (ix, &ch) in chars.iter().enumerate() {
if allow_negative && ch == '-' && ix == 0 { continue; }
if self::char_is_digit(ch) { continue; }
return false;
}
true
}
pub fn as_isize<S>(text: S) -> Result<isize, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<isize>()
}
pub fn as_isized<S>(text: S, def: isize) -> isize where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<isize>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_i8<S>(text: S) -> Result<i8, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<i8>()
}
pub fn as_i8d<S>(text: S, def: i8) -> i8 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<i8>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_i16<S>(text: S) -> Result<i16, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<i16>()
}
pub fn as_i16d<S>(text: S, def: i16) -> i16 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<i16>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_i32<S>(text: S) -> Result<i32, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<i32>()
}
pub fn as_i32d<S>(text: S, def: i32) -> i32 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<i32>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_i64<S>(text: S) -> Result<i64, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<i64>()
}
pub fn as_i64d<S>(text: S, def: i64) -> i64 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<i64>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_i128<S>(text: S) -> Result<i128, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<i128>()
}
pub fn as_i128d<S>(text: S, def: i128) -> i128 where S: AsRef<OsStr> {
self::as_isized(text, def as isize) as i128
}
pub fn as_usize<S>(text: S) -> Result<usize, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<usize>()
}
pub fn as_usized<S>(text: S, def: usize) -> usize where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<usize>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_u8<S>(text: S) -> Result<u8, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<u8>()
}
pub fn as_u8d<S>(text: S, def: u8) -> u8 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<u8>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_u16<S>(text: S) -> Result<u16, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<u16>()
}
pub fn as_u16d<S>(text: S, def: u16) -> u16 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<u16>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_u32<S>(text: S) -> Result<u32, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<u32>()
}
pub fn as_u32d<S>(text: S, def: u32) -> u32 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<u32>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_u64<S>(text: S) -> Result<u64, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<u64>()
}
pub fn as_u64d<S>(text: S, def: u64) -> u64 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<u64>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_u128<S>(text: S) -> Result<u128, ParseIntError> where S: AsRef<OsStr> {
self::to_str(text).parse::<u128>()
}
pub fn as_u128d<S>(text: S, def: u128) -> u128 where S: AsRef<OsStr> {
self::as_usized(text, def as usize) as u128
}
pub fn as_f32<S>(text: S) -> Result<f32, ParseFloatError> where S: AsRef<OsStr> {
self::to_str(text).parse::<f32>()
}
pub fn as_f32d<S>(text: S, def: f32) -> f32 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<f32>() {
Ok(n) => n,
Err(_) => def
}
}
pub fn as_f64<S>(text: S) -> Result<f64, ParseFloatError> where S: AsRef<OsStr> {
self::to_str(text).parse::<f64>()
}
pub fn as_f64d<S>(text: S, def: f64) -> f64 where S: AsRef<OsStr> {
let tsr = self::to_str(text);
if tsr.is_empty() {
return def;
}
match tsr.parse::<f64>() {
Ok(n) => n,
Err(_) => def
}
}
fn to_str<S>(text: S) -> String where S: AsRef<OsStr> {
text.as_ref().to_str().unwrap().to_string()
}
fn to_chars<S>(text: S) -> Vec<char> where S: AsRef<OsStr> {
text.as_ref().to_str().unwrap().chars().collect()
}
fn char_is_digit(ch: char) -> bool {
ch == '0' ||
ch == '1' ||
ch == '2' ||
ch == '3' ||
ch == '4' ||
ch == '5' ||
ch == '6' ||
ch == '7' ||
ch == '8' ||
ch == '9'
}