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
use crate::RegexRepresentation;
use std::ops::*;
macro_rules! impl_wrapper_ops {
($name: ty, $target: ty) => {
impl Deref for $name {
type Target = $target;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for $name {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl PartialEq<$target> for $name {
fn eq(&self, rhs: &$target) -> bool {
self.0.eq(rhs)
}
}
impl PartialEq<$name> for $target {
fn eq(&self, rhs: &$name) -> bool {
self.eq(&rhs.0)
}
}
};
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FullF32(pub f32);
impl std::str::FromStr for FullF32 {
type Err = <f32 as std::str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let f = if s.to_lowercase().ends_with("nan") {
if s.len() == 4 {
-f32::NAN
} else {
f32::NAN
}
} else {
s.to_lowercase().parse()?
};
Ok(FullF32(f))
}
}
impl RegexRepresentation for FullF32 {
const REGEX: &'static str = r"[-+]?([nN]a[nN]|[iI]nf|(\d+|\d+\.\d*|\d*\.\d+)([eE][-+]?\d+)?)";
}
impl_wrapper_ops!(FullF32, f32);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FullF64(pub f64);
impl std::str::FromStr for FullF64 {
type Err = <f64 as std::str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let f = if s.to_lowercase().ends_with("nan") {
if s.len() == 4 {
-f64::NAN
} else {
f64::NAN
}
} else {
s.to_lowercase().parse()?
};
Ok(FullF64(f))
}
}
impl RegexRepresentation for FullF64 {
const REGEX: &'static str = FullF32::REGEX;
}
impl_wrapper_ops!(FullF64, f64);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct HexNumber(pub usize);
impl std::str::FromStr for HexNumber {
type Err = <usize as std::str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s
.strip_prefix("0x")
.or_else(|| s.strip_prefix("0X"))
.unwrap_or(s);
Ok(HexNumber(usize::from_str_radix(s, 16)?))
}
}
impl RegexRepresentation for HexNumber {
const REGEX: &'static str = r"0[xX][0-9a-fA-F]+|[0-9a-fA-F]+";
}
impl_wrapper_ops!(HexNumber, usize);