Advertisement
tomdodd4598

Untitled

Jun 6th, 2021
1,567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.94 KB | None | 0 0
  1. mod helpers;
  2. mod item;
  3.  
  4. use item::Item;
  5.  
  6. use num::bigint::BigInt;
  7. use once_cell::sync::OnceCell;
  8. use regex::Regex;
  9.  
  10. use std::cell::RefCell;
  11. use std::io;
  12. use std::rc::Rc;
  13. use std::str::FromStr;
  14.  
  15. fn trim_newline(s: &mut String) {
  16.     if s.ends_with('\n') {
  17.         s.pop();
  18.         if s.ends_with('\r') {
  19.             s.pop();
  20.         }
  21.     }
  22. }
  23.  
  24. fn is_valid_string(string: &str) -> bool {
  25.     static VALID_REGEX: OnceCell<Regex> = OnceCell::new();
  26.     let regex = VALID_REGEX.get_or_init(
  27.         || Regex::new("^(0|-?[1-9][0-9]*|[A-Za-z][0-9A-Z_a-z]*)$").unwrap()
  28.     );
  29.     regex.is_match(string)
  30. }
  31.  
  32. fn insert_before(item: &Item<String>, other: &Item<String>) -> bool {
  33.     let (val, oth) = (&item.value, &other.value);
  34.     if let Ok(x) = BigInt::from_str(val) {
  35.         if let Ok(y) = BigInt::from_str(oth) {
  36.             return x <= y
  37.         }
  38.     }
  39.     val <= oth
  40. }
  41.  
  42. fn main() {
  43.     let mut begin = true;
  44.     let mut input;
  45.    
  46.     let start: Option<Box<Item<String>>> = None;
  47.    
  48.     loop {
  49.         if !begin {
  50.             println!();
  51.         }
  52.         else {
  53.             begin = false;
  54.         }
  55.        
  56.         println!("Awaiting input...");
  57.         let mut string = String::new();
  58.         io::stdin().read_line(&mut string).expect("Failed to read line!");
  59.         trim_newline(&mut string);
  60.         input = &string[..];
  61.        
  62.         if string.is_empty() {
  63.             println!("is_empty!");
  64.         }
  65.         else if string.starts_with('~') {
  66.             println!("starts_with '~'!");
  67.         }
  68.         else if input.eq("l") {
  69.             println!("eq \"l\"!");
  70.         }
  71.         else if input.eq("i") {
  72.             println!("eq \"i\"!");
  73.         }
  74.         else if input.eq("a") {
  75.             println!("eq \"a\"!");
  76.         }
  77.         else if is_valid_string(input) {
  78.             println!("is_valid_string!");
  79.         }
  80.         else {
  81.             println!("else...");
  82.         }
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement