1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
//! onigiri::validator contains 5 functions for validating `Vec<char>`.
pub fn is_number(vc: &Vec<char>) -> bool {
//! Validate `Vec<char>` whether it is number.
//! ```
//! let test_chars_1 = vec!['1', '2'];
//! let test_chars_2 = vec!['-', '1'];
//! let test_chars_3 = vec!['-', 'a'];
//! assert_eq!(
//! onigiri::validator::is_number(&test_chars_1),
//! true
//! );
//! assert_eq!(
//! onigiri::validator::is_number(&test_chars_2),
//! true
//! );
//! assert_eq!(
//! onigiri::validator::is_number(&test_chars_3),
//! false
//! );
let mut stack: Vec<bool> = vec![];
if is_positive_number(&vc) {
stack.push(true);
} else if is_negative_number(&vc) {
stack.push(true);
} else {
return false;
}
if stack.iter().all(|&r| r == true) {true}
else {false}
}
pub fn is_positive_number(vc: &Vec<char>) -> bool {
//! Validate `Vec<char>` whether it is positive number.
//! ```
//! let test_chars_1 = vec!['1', '2', '3'];
//! let test_chars_2 = vec!['2', '+', '1'];
//! let test_chars_3 = vec!['-', '1', '2'];
//!
//! assert_eq!(
//! onigiri::validator::is_positive_number(&test_chars_1), true);
//! assert_eq!(
//! onigiri::validator::is_positive_number(&test_chars_2), false);
//! assert_eq!(
//! onigiri::validator::is_positive_number(&test_chars_3), false);
//! ```
let mut stack: Vec<bool> = vec![];
for v in vc {
match &v {
'0' ... '9' => stack.push(true),
_ => stack.push(false)
}
}
if stack.iter().all(|&x| x == true) { true }
else { false }
}
pub fn is_negative_number(vc: &Vec<char>) -> bool {
//! Validate `Vec<char>` whether it is negative number.
//! ```
//! let test_chars_1 = vec!['1', '2', '3'];
//! let test_chars_2 = vec!['-', '2', '1'];
//! let test_chars_3 = vec!['2', '-', '1'];
//! assert_eq!(
//! onigiri::validator::is_negative_number(&test_chars_1), false);
//! assert_eq!(
//! onigiri::validator::is_negative_number(&test_chars_2), true);
//! assert_eq!(
//! onigiri::validator::is_negative_number(&test_chars_3), false);
//! ```
let head = &vc[0];
let tail = &vc[1..];
let mut stack: Vec<bool> = vec![];
if head == &'-' {
stack.push(true)
} else {
stack.push(false)
}
let result_tail = is_positive_number(&tail.to_vec());
stack.push(result_tail);
if stack.iter().all(|&x| x == true) { true }
else { false }
}
pub fn is_symbol(vc: &Vec<char>) -> bool {
//! Validate `Vec<char>` whether it is symbol.
//! ```
//! let test_vc = vec!['+'];
//! assert_eq!(
//! onigiri::validator::is_symbol(&test_vc),
//! true
//! );
//! let test_vc_2 = vec!['2'];
//! assert_eq!(
//! onigiri::validator::is_symbol(&test_vc_2),
//! false
//! );
//! let test_vc_3 = vec!['+', '+'];
//! assert_eq!(
//! onigiri::validator::is_symbol(&test_vc_3),
//! false
//! );
//! ```
if vc.len() == 1_usize {
match &vc[0] {
'0' ... '9' => false,
'a' ... 'z' => false,
'A' ... 'Z' => false,
_ => true
}
} else {
false
}
}
pub fn is_calc_operator(vc: &Vec<char>) -> bool {
//! Validate `Vec<char>` whether it is calc_operator.
//! ```
//! let test_vc = vec!['+'];
//! assert_eq!(
//! onigiri::validator::is_calc_operator(&test_vc),
//! true
//! );
//! let test_vc_2 = vec!['2'];
//! assert_eq!(
//! onigiri::validator::is_calc_operator(&test_vc_2),
//! false
//! );
//! let test_vc_3 = vec!['+', '+'];
//! assert_eq!(
//! onigiri::validator::is_calc_operator(&test_vc_3),
//! false
//! );
//! ```
if is_symbol(&vc) {
match &vc[0] {
'+'|'-'|'*'|'/' => true,
_ => false
}
} else {
false
}
}