const_str/__ctfe/parse.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 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
use core::marker::PhantomData;
pub struct Parse<T, U>(T, PhantomData<fn(T) -> U>);
impl<T, U> Parse<T, U> {
pub const fn new(t: T) -> Self {
Self(t, PhantomData)
}
}
impl Parse<&str, bool> {
pub const fn const_eval(&self) -> bool {
if crate::str::equal(self.0, "true") {
return true;
}
if crate::str::equal(self.0, "false") {
return false;
}
panic!("parse error")
}
}
impl Parse<&str, &str> {
pub const fn const_eval(&self) -> &str {
self.0
}
}
impl Parse<&str, char> {
pub const fn const_eval(&self) -> char {
let s = self.0.as_bytes();
if let Some((ch, count)) = crate::utf8::next_char(s) {
if count == s.len() {
return ch;
}
}
panic!("parse error")
}
}
trait IsSignedInteger {
const OUTPUT: bool;
}
macro_rules! mark_signed_integer {
($s: expr, $($ty:ty),+) => {
$(
impl IsSignedInteger for $ty {
const OUTPUT: bool = $s;
}
)+
}
}
mark_signed_integer!(true, i8, i16, i32, i64, i128, isize);
mark_signed_integer!(false, u8, u16, u32, u64, u128, usize);
macro_rules! impl_integer_parse {
($($ty: ty),+) => {$(
impl Parse<&str, $ty> {
pub const fn const_eval(&self) -> $ty {
let s = self.0.as_bytes();
let is_signed = <$ty as IsSignedInteger>::OUTPUT;
let (is_positive, digits) = match s {
[] => panic!("parse error"),
[x, xs @ ..] => match x {
b'+' => (true, xs),
b'-' if is_signed => (false, xs),
_ => (true, s),
},
};
let mut ans: $ty = 0;
let mut i = 0;
while i < digits.len() {
let x = crate::ascii::num_from_dec_digit(digits[i]);
match ans.checked_mul(10) {
Some(val) => {
let val = if is_positive {
val.checked_add(x as _)
} else {
val.checked_sub(x as _)
};
match val {
Some(val) => ans = val,
None => panic!("parse error"),
}
}
None => panic!("parse error"),
};
i += 1;
}
ans
}
}
)+};
}
impl_integer_parse!(u8, u16, u32, u64, u128, usize);
impl_integer_parse!(i8, i16, i32, i64, i128, isize);
/// Parse a value from a string slice.
///
/// The output type must be one of
///
/// + [`&str`](str)
/// + [`char`]
/// + [`bool`]
/// + [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`]
/// + [`i8`], [`i16`], [`i32`], [`i64`], [`i128`], [`isize`]
///
/// This macro is [const-fn compatible](./index.html#const-fn-compatible).
///
/// # Examples
///
/// ```
/// const S1: &str = "true";
/// const X1: bool = const_str::parse!(S1, bool);
/// assert_eq!(X1, true);
///
/// const S2: &str = "42";
/// const X2: u32 = const_str::parse!(S2, u32);
/// assert_eq!(X2, 42);
///
/// const S3: &str = "-1";
/// const X3: i8 = const_str::parse!(S3, i8);
/// assert_eq!(X3, -1);
/// ```
#[macro_export]
macro_rules! parse {
($s: expr, $ty: ty) => {{
$crate::__ctfe::Parse::<_, $ty>::new($s).const_eval()
}};
}
#[cfg(test)]
mod tests {
#[test]
fn test_parse() {
macro_rules! test_parse {
($s: expr, $ty: tt) => {{
const OUTPUT: $ty = $crate::parse!($s, $ty);
let ans: $ty = $s.parse().unwrap();
assert_eq!(OUTPUT, ans)
}};
}
test_parse!("true", bool);
test_parse!("false", bool);
test_parse!("啊", char);
test_parse!("0", u8);
test_parse!("-1", i8);
test_parse!("+42000", u32);
test_parse!("-42000", i32);
}
}