File size: 2,838 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use loda_rust_core::oeis::OeisId;
use std::fmt;
use regex::Regex;
use lazy_static::lazy_static;

pub struct NameRow {
    oeis_id: OeisId,
    name: String,
}

impl NameRow {
    pub fn parse(line: &String) -> Option<Self> {
        parse_name_row(line)
    }

    pub fn new(oeis_id: OeisId, name: String) -> Self {
        Self {
            oeis_id: oeis_id,
            name: name,
        }
    }

    pub fn oeis_id(&self) -> OeisId {
        self.oeis_id
    }

    pub fn name(&self) -> &String {
        &self.name
    }
}

impl fmt::Display for NameRow {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {}", self.oeis_id.a_number(), self.name)
    }
}

lazy_static! {
    /// Extract sequence number and sequence name.
    /// 
    /// With an input like this `A123456 sequence name`.
    /// then sequence number is `123456`.
    /// and sequence name is `sequence name`.
    static ref EXTRACT_SEQUENCE_NUMBER_AND_NAME: Regex = Regex::new(
        "^A(\\d+) (.+)$"
    ).unwrap();
}

fn parse_name_row(line: &String) -> Option<NameRow> {
    if !line.starts_with("A") {
        return None;            
    }

    let re = &EXTRACT_SEQUENCE_NUMBER_AND_NAME;
    let captures = match re.captures(&line) {
        Some(value) => value,
        None => {
            debug!("Unable to extract sequence number and name");
            return None;
        }
    };
    let capture1: &str = captures.get(1).map_or("", |m| m.as_str());
    let sequence_number_string: String = capture1.to_string();
    let sequence_number: u32 = match sequence_number_string.parse() {
        Ok(value) => value,
        _ => {
            debug!("Unable to parse sequence number as u32");
            return None;
        }
    };
    let oeis_id: OeisId = OeisId::from(sequence_number);

    let capture2: &str = captures.get(2).map_or("", |m| m.as_str());
    let name: String = capture2.to_string();
    Some(NameRow::new(oeis_id, name))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(input: &str) -> String {
        match NameRow::parse(&input.to_string()) {
            Some(value) => return value.to_string(),
            None => return "NONE".to_string()
        }
    }

    #[test]
    fn test_10000_parse() {
        assert_eq!(parse(""), "NONE");
        assert_eq!(parse("# comment"), "NONE");
        assert_eq!(parse("Ajunk"), "NONE");
        assert_eq!(parse("A junk"), "NONE");
        assert_eq!(parse("A000001 Number of groups of order n."), "A000001 Number of groups of order n.");
        assert_eq!(parse("A000007 The characteristic function of {0}: a(n) = 0^n."), "A000007 The characteristic function of {0}: a(n) = 0^n.");
        assert_eq!(parse("A999999 x"), "A999999 x");
        assert_eq!(parse("A999999 "), "NONE");
        assert_eq!(parse("A999999"), "NONE");
    }
}