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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#![doc = include_str!("../README.md")]

/// Contains all thirteen strings.
pub mod thirteen_strings;

use fnv::FnvHashSet as HashSet;
use num_traits::FromPrimitive;
use once_cell::sync::OnceCell;
use std::fmt::Debug;
use std::ops::Rem;
use thirteen_strings::THIRTEEN_STRINGS;

/// A type that can be compared to thirteen. This trait is implemented for all primitive types and
/// `&str`.
pub trait IsThirteen {
    /// Returns `true` if self is thirteen.
    fn thirteen(&self) -> bool;
}

macro_rules! impl_for_integer {
    ($type:ty) => {
        impl IsThirteen for $type {
            /// Returns `true` if `self == 13`.
            fn thirteen(&self) -> bool {
                *self == 13
            }
        }
    };
}

impl_for_integer!(i8);
impl_for_integer!(i16);
impl_for_integer!(i32);
impl_for_integer!(i64);
impl_for_integer!(i128);
impl_for_integer!(isize);
impl_for_integer!(u8);
impl_for_integer!(u16);
impl_for_integer!(u32);
impl_for_integer!(u64);
impl_for_integer!(u128);
impl_for_integer!(usize);

macro_rules! impl_for_float {
    ($type:ty) => {
        impl IsThirteen for $type {
            /// Returns `true` if `self` is approximately `13`.
            fn thirteen(&self) -> bool {
                (self - 13.0).abs() < <$type>::EPSILON
            }
        }
    };
}

impl_for_float!(f64);
impl_for_float!(f32);

impl IsThirteen for &str {
    /// Returns `true` if:
    /// - `self` equals `"13"` or `"B"`
    /// - `self` is 13 characters long and all characters are equal to each other
    /// - The lowercase version of `self` is included in [`thirteen_strings::THIRTEEN_STRINGS`]
    fn thirteen(&self) -> bool {
        matches!(*self, "13" | "B")
            || (self.len() == 13 && self.bytes().all(|b| matches!(b, b'I' | b'l' | b'1')))
            || is_thirteen_equal_chars(self)
            // The next line could be non-allocating if there is an ascii-only IsThirteen
            || THIRTEEN_STRINGS.contains(self.to_lowercase().as_str())
    }
}

fn is_thirteen_equal_chars(s: &str) -> bool {
    if let Some(first_char) = s.chars().next() {
        if s.chars().count() == 13 {
            s.chars().all(|c| c == first_char)
        } else {
            false
        }
    } else {
        false
    }
}

impl IsThirteen for String {
    fn thirteen(&self) -> bool {
        self.as_str().thirteen()
    }
}

impl IsThirteen for char {
    /// Returns `true` if self matches a thirteen character.
    fn thirteen(&self) -> bool {
        matches!(*self, 'B' | 'ß' | 'β' | '阝')
    }
}

macro_rules! impl_always_false {
    ($type:ty) => {
        impl IsThirteen for $type {
            /// Returns `false`.
            fn thirteen(&self) -> bool {
                false
            }
        }
    };
}

impl_always_false!(bool);
impl_always_false!(());

/// `Roughly` is thirteen if it is in [12.5, 13.5).
#[derive(Debug, Copy, Clone)]
pub struct Roughly(pub f64);

impl IsThirteen for Roughly {
    fn thirteen(&self) -> bool {
        (12.5..13.5).contains(&self.0)
    }
}

/// `Returns` calls its closure and compares the returned value to thirteen.
#[derive(Debug, Clone)]
pub struct Returns<T>(pub T);

impl<F, R> IsThirteen for Returns<F>
where
    F: Fn() -> R,
    R: IsThirteen,
{
    fn thirteen(&self) -> bool {
        self.0().thirteen()
    }
}

/// `DivisibleBy` is thirteen if it is a divisor of 13.
#[derive(Debug, Copy, Clone)]
pub struct DivisibleBy<T>(pub T);

impl<T, RemOutput> IsThirteen for DivisibleBy<T>
where
    T: Rem<Output = RemOutput> + FromPrimitive + Copy,
    RemOutput: PartialEq + FromPrimitive,
{
    fn thirteen(&self) -> bool {
        self.0 % FromPrimitive::from_u64(13).unwrap() == FromPrimitive::from_u64(0).unwrap()
    }
}

/// `GreaterThan` returns `true` if it is greater than 13.
#[derive(Debug, Copy, Clone)]
pub struct GreaterThan<T>(pub T);

impl<T> IsThirteen for GreaterThan<T>
where
    T: PartialOrd + FromPrimitive,
{
    fn thirteen(&self) -> bool {
        self.0 > FromPrimitive::from_u64(13).unwrap()
    }
}

/// `LessThan` returns `true` if it is greater than 13.
#[derive(Debug, Copy, Clone)]
pub struct LessThan<T>(pub T);

impl<T> IsThirteen for LessThan<T>
where
    T: PartialOrd + FromPrimitive,
{
    fn thirteen(&self) -> bool {
        self.0 < FromPrimitive::from_u64(13).unwrap()
    }
}

/// `Within` has a custom tolerance for equalling thirteen.
#[derive(Debug, Copy, Clone)]
pub struct Within {
    value: f64,
    radius: f64,
}

impl Within {
    /// `radius` is how far `value` can be from 13 to equal 13. That makes sense, right?
    pub fn new(value: f64, radius: f64) -> Self {
        Self { value, radius }
    }
}

impl IsThirteen for Within {
    fn thirteen(&self) -> bool {
        (self.value - 13.0).abs() <= self.radius
    }
}

/// `CanSpell` is thirteen if its set of characters is a superset of those in "thirteen."
#[derive(Debug, Clone)]
pub struct CanSpell {
    letters: HashSet<u8>,
}

impl CanSpell {
    pub fn new(s: &str) -> Self {
        Self {
            letters: s.bytes().map(|b| b.to_ascii_lowercase()).collect(),
        }
    }
}

impl IsThirteen for CanSpell {
    fn thirteen(&self) -> bool {
        [b't', b'h', b'i', b'r', b't', b'e', b'e', b'n']
            .iter()
            .all(|b| self.letters.contains(b))
    }
}

/// `AnagramOf` is thirteen if it is an [anagram](https://en.wikipedia.org/wiki/Anagram) of
/// "thirteen."
#[derive(Debug, Clone)]
pub struct AnagramOf {
    bytes: HashSet<u8>,
}

impl AnagramOf {
    pub fn new(s: &str) -> Self {
        Self {
            bytes: s.bytes().map(|b| b.to_ascii_lowercase()).collect(),
        }
    }
}

const THIRTEEN_STR: &str = "thirteen";
static THIRTEEN_LETTERS: OnceCell<HashSet<u8>> = OnceCell::new();

impl IsThirteen for AnagramOf {
    fn thirteen(&self) -> bool {
        self.bytes == *THIRTEEN_LETTERS.get_or_init(|| THIRTEEN_STR.bytes().collect())
    }
}

/// `Backwards` is thirteen if it equals `"neetRiht"` (reverse spelling of "thirteen").
#[derive(Debug, Clone)]
pub struct Backwards<'s>(pub &'s str);

impl IsThirteen for Backwards<'_> {
    fn thirteen(&self) -> bool {
        self.0 == "neetRiht"
    }
}

/// `AtomicNumber` is thirteen if the string equals `"aluminum"`.
#[derive(Debug, Clone)]
pub struct AtomicNumber<'s>(pub &'s str);

impl IsThirteen for AtomicNumber<'_> {
    fn thirteen(&self) -> bool {
        self.0.eq_ignore_ascii_case("aluminum")
    }
}

#[cfg(test)]
mod lib_test;