-
Notifications
You must be signed in to change notification settings - Fork 21
/
parsing.rs
62 lines (51 loc) · 1.67 KB
/
parsing.rs
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
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl Solution {
fn parse_month(m1: u8, m2: u8) -> [u8; 2] {
match m2 {
b'b' => *b"02",
b'c' => *b"12",
b'g' => *b"08",
b'l' => *b"07",
b'n' => match m1 {
b'a' => *b"01",
_ => *b"06",
},
b'p' => *b"09",
b'r' => match m1 {
b'a' => *b"03",
_ => *b"04",
},
b't' => *b"10",
b'v' => *b"11",
_ => *b"05",
}
}
pub fn reformat_date(date: String) -> String {
let mut date = date.into_bytes();
let s = date.as_slice();
let ([y0, y1, y2, y3], [m1, m2], [d0, d1]) = if let [d, _, _, _, _, m1, m2, _, y0, y1, y2, y3] = *s {
([y0, y1, y2, y3], [m1, m2], [b'0', d])
} else {
let [d0, d1, _, _, _, _, m1, m2, _, y0, y1, y2, y3]: [_; 13] = s.try_into().unwrap();
([y0, y1, y2, y3], [m1, m2], [d0, d1])
};
let [m0, m1] = Self::parse_month(m1, m2);
date.clear();
date.extend([y0, y1, y2, y3, b'-', m0, m1, b'-', d0, d1]);
String::from_utf8(date).unwrap()
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn reformat_date(date: String) -> String {
Self::reformat_date(date)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}