1 /// A description of an input to benchmark on. 2 #[derive(Clone, Copy, Debug)] 3 pub struct Input { 4 /// The bytes to search. 5 pub corpus: &'static [u8], 6 /// Distinct bytes that never occur in the input. 7 pub never: &'static [SearchByte], 8 /// Distinct bytes that occur very rarely (<0.1%). 9 pub rare: &'static [SearchByte], 10 /// Distinct bytes that are uncommon (~1%). 11 pub uncommon: &'static [SearchByte], 12 /// Distinct bytes that are common (~5%). 13 pub common: &'static [SearchByte], 14 /// Distinct bytes that are very common (~10%). 15 pub verycommon: &'static [SearchByte], 16 /// Distinct bytes that are super common (>90%). 17 pub supercommon: &'static [SearchByte], 18 } 19 20 pub const HUGE: Input = Input { 21 corpus: crate::data::SHERLOCK_HUGE, 22 never: &[ 23 SearchByte { byte: b'<', count: 0 }, 24 SearchByte { byte: b'>', count: 0 }, 25 SearchByte { byte: b'=', count: 0 }, 26 ], 27 rare: &[ 28 SearchByte { byte: b'z', count: 151 }, 29 SearchByte { byte: b'R', count: 275 }, 30 SearchByte { byte: b'J', count: 120 }, 31 ], 32 uncommon: &[ 33 SearchByte { byte: b'b', count: 6124 }, 34 SearchByte { byte: b'p', count: 6989 }, 35 SearchByte { byte: b'.', count: 6425 }, 36 ], 37 common: &[ 38 SearchByte { byte: b'a', count: 35301 }, 39 SearchByte { byte: b't', count: 39268 }, 40 SearchByte { byte: b'o', count: 34495 }, 41 ], 42 verycommon: &[SearchByte { byte: b' ', count: 97626 }], 43 supercommon: &[], 44 }; 45 46 pub const SMALL: Input = Input { 47 corpus: crate::data::SHERLOCK_SMALL, 48 never: &[ 49 SearchByte { byte: b'<', count: 0 }, 50 SearchByte { byte: b'>', count: 0 }, 51 SearchByte { byte: b'=', count: 0 }, 52 ], 53 rare: &[ 54 SearchByte { byte: b'R', count: 1 }, 55 SearchByte { byte: b'P', count: 1 }, 56 SearchByte { byte: b'T', count: 1 }, 57 ], 58 uncommon: &[ 59 SearchByte { byte: b'b', count: 8 }, 60 SearchByte { byte: b'g', count: 8 }, 61 SearchByte { byte: b'p', count: 8 }, 62 ], 63 common: &[ 64 SearchByte { byte: b'a', count: 44 }, 65 SearchByte { byte: b'h', count: 34 }, 66 SearchByte { byte: b'i', count: 35 }, 67 ], 68 verycommon: &[SearchByte { byte: b' ', count: 106 }], 69 supercommon: &[], 70 }; 71 72 pub const TINY: Input = Input { 73 corpus: crate::data::SHERLOCK_TINY, 74 never: &[ 75 SearchByte { byte: b'<', count: 0 }, 76 SearchByte { byte: b'>', count: 0 }, 77 SearchByte { byte: b'=', count: 0 }, 78 ], 79 rare: &[ 80 SearchByte { byte: b'.', count: 1 }, 81 SearchByte { byte: b'H', count: 1 }, 82 SearchByte { byte: b'M', count: 1 }, 83 ], 84 uncommon: &[ 85 SearchByte { byte: b'l', count: 5 }, 86 SearchByte { byte: b's', count: 5 }, 87 SearchByte { byte: b'e', count: 6 }, 88 ], 89 common: &[SearchByte { byte: b' ', count: 11 }], 90 verycommon: &[], 91 supercommon: &[], 92 }; 93 94 pub const EMPTY: Input = Input { 95 corpus: &[], 96 never: &[ 97 SearchByte { byte: b'a', count: 0 }, 98 SearchByte { byte: b'b', count: 0 }, 99 SearchByte { byte: b'c', count: 0 }, 100 ], 101 rare: &[], 102 uncommon: &[], 103 common: &[], 104 verycommon: &[], 105 supercommon: &[], 106 }; 107 108 impl Input { 109 /// Return all of this input's "never" bytes only if there are at least 110 /// `min` of them. never(&self, min: usize) -> Option<&'static [SearchByte]>111 fn never(&self, min: usize) -> Option<&'static [SearchByte]> { 112 if self.never.len() < min { 113 None 114 } else { 115 Some(self.never) 116 } 117 } 118 never1(&self) -> Option<Search1>119 pub fn never1(&self) -> Option<Search1> { 120 self.never(1).and_then(|bytes| Search1::new(self.corpus, bytes)) 121 } 122 never2(&self) -> Option<Search2>123 pub fn never2(&self) -> Option<Search2> { 124 self.never(2).and_then(|bytes| Search2::new(self.corpus, bytes)) 125 } 126 never3(&self) -> Option<Search3>127 pub fn never3(&self) -> Option<Search3> { 128 self.never(3).and_then(|bytes| Search3::new(self.corpus, bytes)) 129 } 130 131 /// Return all of this input's "rare" bytes only if there are at least 132 /// `min` of them. rare(&self, min: usize) -> Option<&'static [SearchByte]>133 fn rare(&self, min: usize) -> Option<&'static [SearchByte]> { 134 if self.rare.len() < min { 135 None 136 } else { 137 Some(self.rare) 138 } 139 } 140 rare1(&self) -> Option<Search1>141 pub fn rare1(&self) -> Option<Search1> { 142 self.rare(1).and_then(|bytes| Search1::new(self.corpus, bytes)) 143 } 144 rare2(&self) -> Option<Search2>145 pub fn rare2(&self) -> Option<Search2> { 146 self.rare(2).and_then(|bytes| Search2::new(self.corpus, bytes)) 147 } 148 rare3(&self) -> Option<Search3>149 pub fn rare3(&self) -> Option<Search3> { 150 self.rare(3).and_then(|bytes| Search3::new(self.corpus, bytes)) 151 } 152 153 /// Return all of this input's "uncommon" bytes only if there are at least 154 /// `min` of them. uncommon(&self, min: usize) -> Option<&'static [SearchByte]>155 fn uncommon(&self, min: usize) -> Option<&'static [SearchByte]> { 156 if self.uncommon.len() < min { 157 None 158 } else { 159 Some(self.uncommon) 160 } 161 } 162 uncommon1(&self) -> Option<Search1>163 pub fn uncommon1(&self) -> Option<Search1> { 164 self.uncommon(1).and_then(|bytes| Search1::new(self.corpus, bytes)) 165 } 166 uncommon2(&self) -> Option<Search2>167 pub fn uncommon2(&self) -> Option<Search2> { 168 self.uncommon(2).and_then(|bytes| Search2::new(self.corpus, bytes)) 169 } 170 uncommon3(&self) -> Option<Search3>171 pub fn uncommon3(&self) -> Option<Search3> { 172 self.uncommon(3).and_then(|bytes| Search3::new(self.corpus, bytes)) 173 } 174 175 /// Return all of this input's "common" bytes only if there are at least 176 /// `min` of them. common(&self, min: usize) -> Option<&'static [SearchByte]>177 fn common(&self, min: usize) -> Option<&'static [SearchByte]> { 178 if self.common.len() < min { 179 None 180 } else { 181 Some(self.common) 182 } 183 } 184 common1(&self) -> Option<Search1>185 pub fn common1(&self) -> Option<Search1> { 186 self.common(1).and_then(|bytes| Search1::new(self.corpus, bytes)) 187 } 188 common2(&self) -> Option<Search2>189 pub fn common2(&self) -> Option<Search2> { 190 self.common(2).and_then(|bytes| Search2::new(self.corpus, bytes)) 191 } 192 common3(&self) -> Option<Search3>193 pub fn common3(&self) -> Option<Search3> { 194 self.common(3).and_then(|bytes| Search3::new(self.corpus, bytes)) 195 } 196 197 /// Return all of this input's "verycommon" bytes only if there are at 198 /// least `min` of them. verycommon(&self, min: usize) -> Option<&'static [SearchByte]>199 fn verycommon(&self, min: usize) -> Option<&'static [SearchByte]> { 200 if self.verycommon.len() < min { 201 None 202 } else { 203 Some(self.verycommon) 204 } 205 } 206 verycommon1(&self) -> Option<Search1>207 pub fn verycommon1(&self) -> Option<Search1> { 208 self.verycommon(1).and_then(|bytes| Search1::new(self.corpus, bytes)) 209 } 210 verycommon2(&self) -> Option<Search2>211 pub fn verycommon2(&self) -> Option<Search2> { 212 self.verycommon(2).and_then(|bytes| Search2::new(self.corpus, bytes)) 213 } 214 verycommon3(&self) -> Option<Search3>215 pub fn verycommon3(&self) -> Option<Search3> { 216 self.verycommon(3).and_then(|bytes| Search3::new(self.corpus, bytes)) 217 } 218 219 /// Return all of this input's "supercommon" bytes only if there are at 220 /// least `min` of them. supercommon(&self, min: usize) -> Option<&'static [SearchByte]>221 fn supercommon(&self, min: usize) -> Option<&'static [SearchByte]> { 222 if self.supercommon.len() < min { 223 None 224 } else { 225 Some(self.supercommon) 226 } 227 } 228 supercommon1(&self) -> Option<Search1>229 pub fn supercommon1(&self) -> Option<Search1> { 230 self.supercommon(1).and_then(|bytes| Search1::new(self.corpus, bytes)) 231 } 232 supercommon2(&self) -> Option<Search2>233 pub fn supercommon2(&self) -> Option<Search2> { 234 self.supercommon(2).and_then(|bytes| Search2::new(self.corpus, bytes)) 235 } 236 supercommon3(&self) -> Option<Search3>237 pub fn supercommon3(&self) -> Option<Search3> { 238 self.supercommon(3).and_then(|bytes| Search3::new(self.corpus, bytes)) 239 } 240 } 241 242 /// A description of a single byte, along with the number of times it is 243 /// expected to occur for a particular data source. 244 #[derive(Clone, Copy, Debug)] 245 pub struct SearchByte { 246 /// A byte. Any byte. 247 pub byte: u8, 248 /// The number of times it is expected to occur. 249 pub count: usize, 250 } 251 252 /// A description of a search for one particular byte. 253 #[derive(Clone, Copy, Debug)] 254 pub struct Search1 { 255 /// The thing to search. 256 pub corpus: &'static [u8], 257 /// The thing to search for. One byte. 258 pub byte1: SearchByte, 259 } 260 261 impl Search1 { new( corpus: &'static [u8], bytes: &[SearchByte], ) -> Option<Search1>262 pub fn new( 263 corpus: &'static [u8], 264 bytes: &[SearchByte], 265 ) -> Option<Search1> { 266 if bytes.len() < 1 { 267 None 268 } else { 269 Some(Search1 { corpus, byte1: bytes[0] }) 270 } 271 } 272 } 273 274 /// A description of a search for one of two particular bytes. 275 #[derive(Clone, Copy, Debug)] 276 pub struct Search2 { 277 /// The thing to search. 278 pub corpus: &'static [u8], 279 /// One of the things to search for. 280 pub byte1: SearchByte, 281 /// The other thing to search for. 282 pub byte2: SearchByte, 283 } 284 285 impl Search2 { new( corpus: &'static [u8], bytes: &[SearchByte], ) -> Option<Search2>286 pub fn new( 287 corpus: &'static [u8], 288 bytes: &[SearchByte], 289 ) -> Option<Search2> { 290 if bytes.len() < 2 { 291 None 292 } else { 293 Some(Search2 { corpus, byte1: bytes[0], byte2: bytes[1] }) 294 } 295 } 296 } 297 298 /// A description of a search for one of three particular bytes. 299 #[derive(Clone, Copy, Debug)] 300 pub struct Search3 { 301 /// The thing to search. 302 pub corpus: &'static [u8], 303 /// One of the things to search for. 304 pub byte1: SearchByte, 305 /// The other thing to search for. 306 pub byte2: SearchByte, 307 /// Another thing to search for. 308 pub byte3: SearchByte, 309 } 310 311 impl Search3 { new( corpus: &'static [u8], bytes: &[SearchByte], ) -> Option<Search3>312 pub fn new( 313 corpus: &'static [u8], 314 bytes: &[SearchByte], 315 ) -> Option<Search3> { 316 if bytes.len() < 3 { 317 None 318 } else { 319 Some(Search3 { 320 corpus, 321 byte1: bytes[0], 322 byte2: bytes[1], 323 byte3: bytes[2], 324 }) 325 } 326 } 327 } 328