synthphonia/text/parsing/
float.rs

1use std::collections::HashSet;
2
3use chrono::{NaiveDate, Datelike, Month};
4use itertools::Itertools;
5use regex::Regex;
6use crate::galloc::TryAllocForExactSizeIter;
7
8use crate::utils::F64;
9use crate::value::ConstValue;
10use crate::{galloc::AllocForExactSizeIter, expr::{Expr, ops}, impl_basic, impl_op1_opt, new_op1_opt, value::Value};
11
12use super::ParsingOp;
13
14
15impl_basic!(ParseFloat, "float.parse");
16impl crate::forward::enumeration::Enumerator1 for ParseFloat {
17    fn enumerate(&self, this: &'static ops::Op1Enum, exec: &'static crate::forward::executor::Executor, opnt: [usize; 1]) -> Result<(), ()> { Ok(())}
18}
19
20impl_op1_opt!(ParseFloat, "float.parse",
21    Str -> Int { |s1: &&str| -> Option<i64> {
22        todo!()
23    }}
24);
25
26impl ParsingOp for ParseFloat {
27
28    fn parse_into(&self, input: &'static str) -> std::vec::Vec<(&'static str, ConstValue)> {
29        let regex = Regex::new(r"(\-|\+)?[\d,]+(\.[\d,]+([eE](\-|\+)?\d+)?)?".to_string().as_str()).unwrap();
30        let iter = regex.captures_iter(input);
31        let mut result = Vec::new();
32        for m in iter {
33            let a = m.get(0).unwrap().as_str();
34            if let Ok(i) = a.parse::<f64>() {
35                result.push((a, F64::new(i).into()));
36            }
37        }
38        result
39    }
40
41}
42
43pub fn detector(input: &str) -> bool {
44    let regex = Regex::new(r"(\-|\+)?[\d,]+(\.[\d,]+([eE](\-|\+)?\d+)?)".to_string().as_str()).unwrap();
45    regex.is_match(input)
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::text::parsing::{float::detector, ParsingOp};
51
52    use super::ParseFloat;
53
54    #[test]
55    fn test1() {
56        let scan = ParseFloat(1);
57        println!("{:?}", scan.parse_into("123"));
58        println!("{:?}", scan.parse_into("+123.321E3"));
59    }
60    #[test]
61    fn test_detector() {
62        assert!(!detector("123"));
63        assert!(detector("123.0"));
64        assert!(!detector("123E1"));
65    }
66}
67