| 1 | //! An iterator that skips values equal to a provided value. |
| 2 | //! |
| 3 | //! Iterators over a contiguous slice, returning all values |
| 4 | //! except for those matching the provided skip value. |
| 5 | //! |
| 6 | //! # Complexity |
| 7 | //! |
| 8 | //! Although superficially quite simple, the level of complexity |
| 9 | //! introduced by digit separators can be quite complex, due |
| 10 | //! the number of permutations during parsing. |
| 11 | //! |
| 12 | //! We can consume any combinations of of \[0,3\] items from the following set: |
| 13 | //! - \[l\]eading digit separators, where digit separators occur before |
| 14 | //! digits. |
| 15 | //! - \[i\]nternal digit separators, where digit separators occur between |
| 16 | //! digits. |
| 17 | //! - \[t\]railing digit separators, where digit separators occur after |
| 18 | //! digits. |
| 19 | //! |
| 20 | //! In addition to those combinations, we can also have: |
| 21 | //! - \[c\]onsecutive digit separators, which allows two digit separators to |
| 22 | //! be adjacent. |
| 23 | //! |
| 24 | //! # Shorthand |
| 25 | //! |
| 26 | //! We will use the term consumer to denote a function that consumes digits, |
| 27 | //! splitting an input buffer at an index, where the leading section contains |
| 28 | //! valid input digits, and the trailing section contains invalid characters. |
| 29 | //! Due to the number of combinations for consumers, we use the following |
| 30 | //! shorthand to denote consumers: |
| 31 | //! - `no`, does not use a digit separator. |
| 32 | //! - `l`, consumes leading digit separators. |
| 33 | //! - `i`, consumes internal digit separators. |
| 34 | //! - `t`, consumes trailing digit separators. |
| 35 | //! - `c`, consumes consecutive digit separators. |
| 36 | //! |
| 37 | //! The `next`/`iter` algorithms are therefore named `next_x`, where `x` |
| 38 | //! represents the shorthand name of the consumer, in sorted order. |
| 39 | //! For example, `next_ilt` means that consumer can skip internal, |
| 40 | //! leading, and trailing digit separators, but not consecutive ones. |
| 41 | |
| 42 | #![cfg(all(feature = "format", any(feature = "parse-floats", feature = "parse-integers")))] |
| 43 | |
| 44 | use core::{mem, ptr}; |
| 45 | |
| 46 | use crate::digit::char_is_digit_const; |
| 47 | use crate::format::NumberFormat; |
| 48 | use crate::format_flags as flags; |
| 49 | use crate::iterator::{DigitsIter, Iter}; |
| 50 | |
| 51 | // IS_ILTC |
| 52 | // ------- |
| 53 | |
| 54 | // NOTE: The compiler optimizes all these methods pretty well: it's as |
| 55 | // efficient or almost as efficient as optimized assembly without unsafe |
| 56 | // code, especially since we have to do bounds checking |
| 57 | // before and the compiler can determine all cases correctly. |
| 58 | |
| 59 | /// Helpers to get the next or previous elements for checks. |
| 60 | /// |
| 61 | /// This has the non-consecutive iterator variants as well |
| 62 | /// as the consecutive ones. The consecutive ones will iteratively |
| 63 | /// process all digits. |
| 64 | macro_rules! indexing { |
| 65 | (@next $self:ident, $index:expr) => { |
| 66 | $index.wrapping_add(1) |
| 67 | }; |
| 68 | |
| 69 | (@nextc $self:ident, $index:expr) => {{ |
| 70 | let mut index = $index; |
| 71 | let slc = $self.byte.slc; |
| 72 | while slc.get(index.wrapping_add(1)).map_or(false, |&x| $self.is_digit_separator(x)) { |
| 73 | index = index.wrapping_add(1); |
| 74 | } |
| 75 | index.wrapping_add(1) |
| 76 | }}; |
| 77 | |
| 78 | (@prev $self:ident, $index:expr) => { |
| 79 | $index.wrapping_sub(1) |
| 80 | }; |
| 81 | |
| 82 | (@prevc $self:ident, $index:expr) => {{ |
| 83 | let mut index = $index; |
| 84 | let slc = $self.byte.slc; |
| 85 | while slc.get(index.wrapping_sub(1)).map_or(false, |&x| $self.is_digit_separator(x)) { |
| 86 | index = index.wrapping_sub(1); |
| 87 | } |
| 88 | index.wrapping_sub(1) |
| 89 | }}; |
| 90 | } |
| 91 | |
| 92 | /// Determine if a single digit separator is internal. |
| 93 | /// |
| 94 | /// # Examples |
| 95 | /// |
| 96 | /// - `1__1_23`- invalid |
| 97 | /// - `1_1__23`- invalid |
| 98 | /// - `1_1_23`- valid |
| 99 | /// - `11_x23`- invalid |
| 100 | /// - `_1123`- invalid |
| 101 | /// - `+_1123`- invalid |
| 102 | /// - `_+1123`- invalid |
| 103 | /// - `1123_`- invalid |
| 104 | /// - `1123_.`- invalid |
| 105 | /// - `112_3.`- valid |
| 106 | /// |
| 107 | /// # Preconditions |
| 108 | /// |
| 109 | /// Assumes `slc[index]` is a digit separator. |
| 110 | macro_rules! is_i { |
| 111 | (@first $self:ident, $index:expr) => {{ |
| 112 | // NOTE: The conditions here then are that: |
| 113 | // - `index - 1` is a digit |
| 114 | // - `index + 1` is a digit |
| 115 | |
| 116 | let prev = indexing!(@prev $self, $index); |
| 117 | let next = indexing!(@next $self, $index); |
| 118 | let slc = $self.byte.slc; |
| 119 | slc.get(prev).map_or(false, |&x| $self.is_digit(x)) && |
| 120 | slc.get(next).map_or(false, |&x| $self.is_digit(x)) |
| 121 | }}; |
| 122 | |
| 123 | (@first $self:ident) => { |
| 124 | is_i!(@first $self, $self.byte.index) |
| 125 | }; |
| 126 | |
| 127 | (@internal $self:ident, $index:expr) => {{ |
| 128 | // NOTE: We must have validated `prev`, so this just checks `next`. |
| 129 | // NOTE: The conditions here then are that: |
| 130 | // - `index + 1` is a digit |
| 131 | |
| 132 | let next = indexing!(@next $self, $index); |
| 133 | let slc = $self.byte.slc; |
| 134 | slc.get(next).map_or(false, |&x| $self.is_digit(x)) |
| 135 | }}; |
| 136 | |
| 137 | (@internal $self:ident) => { |
| 138 | is_i!(@internal $self, $self.byte.index) |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | /// Determine if consecutive digit separators are internal. |
| 143 | /// |
| 144 | /// # Examples |
| 145 | /// |
| 146 | /// - `1__1_23`- valid |
| 147 | /// - `1_1__23`- valid |
| 148 | /// - `1_1_23`- valid |
| 149 | /// - `11_x23`- invalid |
| 150 | /// - `_1123`- invalid |
| 151 | /// - `+_1123`- invalid |
| 152 | /// - `_+1123`- invalid |
| 153 | /// - `1123_`- invalid |
| 154 | /// - `1123_.`- invalid |
| 155 | /// - `112_3.`- valid |
| 156 | /// |
| 157 | /// # Preconditions |
| 158 | /// |
| 159 | /// Assumes `slc[index]` is a digit separator. |
| 160 | macro_rules! is_ic { |
| 161 | (@first $self:ident, $index:expr) => {{ |
| 162 | // NOTE: The conditions here then are that: |
| 163 | // - `index - 1` is a digit after consuming digit separators |
| 164 | // - `index + 1` is a digit after consuming digit separators |
| 165 | |
| 166 | let prev = indexing!(@prevc $self, $index); |
| 167 | let next = indexing!(@nextc $self, $index); |
| 168 | let slc = $self.byte.slc; |
| 169 | slc.get(prev).map_or(false, |&x| $self.is_digit(x)) && |
| 170 | slc.get(next).map_or(false, |&x| $self.is_digit(x)) |
| 171 | }}; |
| 172 | |
| 173 | (@first $self:ident) => { |
| 174 | is_ic!(@first $self, $self.byte.index) |
| 175 | }; |
| 176 | |
| 177 | (@internal $self:ident, $index:expr) => {{ |
| 178 | // NOTE: We must have validated `prev`, so this just checks `next`. |
| 179 | // NOTE: The conditions here then are that: |
| 180 | // - `index + 1` is a digit after consuming digit separators |
| 181 | |
| 182 | let next = indexing!(@nextc $self, $index); |
| 183 | let slc = $self.byte.slc; |
| 184 | slc.get(next).map_or(false, |&x| $self.is_digit(x)) |
| 185 | }}; |
| 186 | |
| 187 | (@internal $self:ident) => { |
| 188 | is_ic!(@internal $self, $self.byte.index) |
| 189 | }; |
| 190 | } |
| 191 | |
| 192 | /// Determine if a single digit separator is leading. |
| 193 | /// |
| 194 | /// # Examples |
| 195 | /// |
| 196 | /// - `__123`- invalid |
| 197 | /// - `+__123`- invalid |
| 198 | /// - `._123`- valid |
| 199 | /// - `_+123`- valid |
| 200 | /// - `_123`- valid |
| 201 | /// - `+_123`- valid |
| 202 | /// |
| 203 | /// Having a subsequent sign character is fine since it might |
| 204 | /// be part of a partial parser. |
| 205 | /// |
| 206 | /// # Preconditions |
| 207 | /// |
| 208 | /// Assumes `slc[index]` is a digit separator. |
| 209 | macro_rules! is_l { |
| 210 | (@first $self:ident, $index:expr) => {{ |
| 211 | // NOTE: The conditions here then are that: |
| 212 | // - `index - 1` is not a digit |
| 213 | // - `index - 1` is not a digit separator |
| 214 | // - `index + 1` is not a digit separator |
| 215 | |
| 216 | let prev = indexing!(@prev $self, $index); |
| 217 | let next = indexing!(@next $self, $index); |
| 218 | let slc = $self.byte.slc; |
| 219 | slc.get(prev).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x)) && |
| 220 | slc.get(next).map_or(true, |&x| !$self.is_digit_separator(x)) |
| 221 | }}; |
| 222 | |
| 223 | (@first $self:ident) => { |
| 224 | is_l!(@first $self, $self.byte.index) |
| 225 | }; |
| 226 | |
| 227 | (@internal $self:ident, $index:expr) => {{ |
| 228 | // NOTE: Previous must have been a digit so this cannot be valid. |
| 229 | false |
| 230 | }}; |
| 231 | |
| 232 | (@internal $self:ident) => { |
| 233 | is_l!(@internal $self, $self.byte.index) |
| 234 | }; |
| 235 | } |
| 236 | |
| 237 | /// Determine if one or more digit separators are leading. |
| 238 | /// |
| 239 | /// # Examples |
| 240 | /// |
| 241 | /// - `__123`- valid |
| 242 | /// - `+__123`- valid |
| 243 | /// - `+__+123`- valid |
| 244 | /// - `+__.123`- valid |
| 245 | /// - `._123`- valid |
| 246 | /// - `_+123`- invalid |
| 247 | /// - `_123`- valid |
| 248 | /// - `+_123`- valid |
| 249 | /// |
| 250 | /// Having a subsequent sign character is fine since it might |
| 251 | /// be part of a partial parser. |
| 252 | /// |
| 253 | /// # Preconditions |
| 254 | /// |
| 255 | /// Assumes `slc[index]` is a digit separator. |
| 256 | macro_rules! is_lc { |
| 257 | (@first $self:ident, $index:expr) => {{ |
| 258 | // NOTE: The conditions here then are that: |
| 259 | // - `index - 1` is not a digit after removing digit separators |
| 260 | |
| 261 | let prev = indexing!(@prevc $self, $index); |
| 262 | let slc = $self.byte.slc; |
| 263 | slc.get(prev).map_or(true, |&x| !$self.is_digit(x)) |
| 264 | }}; |
| 265 | |
| 266 | (@first $self:ident) => { |
| 267 | is_lc!(@first $self, $self.byte.index) |
| 268 | }; |
| 269 | |
| 270 | (@internal $self:ident, $index:expr) => {{ |
| 271 | // NOTE: Previous must have been a digit so this cannot be valid. |
| 272 | false |
| 273 | }}; |
| 274 | |
| 275 | (@internal $self:ident) => { |
| 276 | is_lc!(@internal $self, $self.byte.index) |
| 277 | }; |
| 278 | } |
| 279 | |
| 280 | /// Determine if a single digit separator is trailing. |
| 281 | /// |
| 282 | /// # Examples |
| 283 | /// |
| 284 | /// - `123_`- valid |
| 285 | /// - `123__`- invalid |
| 286 | /// - `123_.`- valid |
| 287 | /// - `123__.`- invalid |
| 288 | /// - `123_1`- invalid |
| 289 | /// - `123__1`- invalid |
| 290 | /// - _: valid |
| 291 | /// - _+: valid |
| 292 | /// - 1_+: valid |
| 293 | /// |
| 294 | /// Having a subsequent sign character is fine since it might |
| 295 | /// be part of a partial parser. |
| 296 | /// |
| 297 | /// # Preconditions |
| 298 | /// |
| 299 | /// Assumes `slc[index]` is a digit separator. |
| 300 | macro_rules! is_t { |
| 301 | (@first $self:ident, $index:expr) => {{ |
| 302 | // NOTE: The conditions here then are that: |
| 303 | // - `index + 1` is not a digit |
| 304 | // - `index + 1` is not a digit separator |
| 305 | // - `index - 1` is not a digit separator |
| 306 | |
| 307 | let prev = indexing!(@prev $self, $index); |
| 308 | let next = indexing!(@next $self, $index); |
| 309 | let slc = $self.byte.slc; |
| 310 | slc.get(next).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x)) && |
| 311 | slc.get(prev).map_or(true, |&x| !$self.is_digit_separator(x)) |
| 312 | }}; |
| 313 | |
| 314 | (@first $self:ident) => { |
| 315 | is_t!(@first $self, $self.byte.index) |
| 316 | }; |
| 317 | |
| 318 | (@internal $self:ident, $index:expr) => {{ |
| 319 | // NOTE: We must have validated `prev`, so this just checks `next`. |
| 320 | // NOTE: The conditions here then are that: |
| 321 | // - `index + 1` is not a digit |
| 322 | // - `index + 1` is not a digit separator |
| 323 | let next = indexing!(@next $self, $index); |
| 324 | let slc = $self.byte.slc; |
| 325 | slc.get(next).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x)) |
| 326 | }}; |
| 327 | |
| 328 | (@internal $self:ident) => { |
| 329 | is_t!(@internal $self, $self.byte.index) |
| 330 | }; |
| 331 | } |
| 332 | |
| 333 | /// Determine if one or more digit separators are trailing. |
| 334 | /// |
| 335 | /// # Examples |
| 336 | /// |
| 337 | /// - `123_`- valid |
| 338 | /// - `123__`- valid |
| 339 | /// - `123_.`- valid |
| 340 | /// - `123__.`- valid |
| 341 | /// - `123_1`- invalid |
| 342 | /// - `123__1`- invalid |
| 343 | /// |
| 344 | /// # Preconditions |
| 345 | /// |
| 346 | /// Assumes `slc[index]` is a digit separator. |
| 347 | macro_rules! is_tc { |
| 348 | (@first $self:ident, $index:expr) => {{ |
| 349 | // NOTE: The conditions here then are that: |
| 350 | // - `index + 1` is not a digit |
| 351 | |
| 352 | let next = indexing!(@nextc $self, $index); |
| 353 | let slc = $self.byte.slc; |
| 354 | slc.get(next).map_or(true, |&x| !$self.is_digit(x)) |
| 355 | }}; |
| 356 | |
| 357 | (@first $self:ident) => { |
| 358 | is_tc!(@first $self, $self.byte.index) |
| 359 | }; |
| 360 | |
| 361 | (@internal $self:ident, $index:expr) => { |
| 362 | // NOTE: This is already optimized for the first case. |
| 363 | is_tc!(@first $self, $index) |
| 364 | }; |
| 365 | |
| 366 | (@internal $self:ident) => { |
| 367 | is_tc!(@internal $self, $self.byte.index) |
| 368 | }; |
| 369 | } |
| 370 | |
| 371 | /// Determine if the digit separator is leading or internal. |
| 372 | /// |
| 373 | /// # Examples |
| 374 | /// |
| 375 | /// - `__123`- invalid |
| 376 | /// - `+__123`- invalid |
| 377 | /// - `._123`- valid |
| 378 | /// - `_+123`- valid |
| 379 | /// - `_123`- valid |
| 380 | /// - `+_123`- valid |
| 381 | /// - `+1_23`- valid |
| 382 | /// - `+1__23`- invalid |
| 383 | /// - `+123_`- invalid |
| 384 | /// - `+123__`- invalid |
| 385 | /// - _: valid |
| 386 | /// - _+: valid |
| 387 | /// - 1_+: invalid |
| 388 | /// |
| 389 | /// # Preconditions |
| 390 | /// |
| 391 | /// Assumes `slc[index]` is a digit separator. |
| 392 | macro_rules! is_il { |
| 393 | (@first $self:ident, $index:expr) => {{ |
| 394 | // NOTE: The conditions here then are that: |
| 395 | // - `index + 1` is a digit |
| 396 | // - `index + 1` is not a digit separator |
| 397 | // - `index - 1` is not a digit separator |
| 398 | // |
| 399 | // # Logic |
| 400 | // |
| 401 | // If the previous character is a digit, then the |
| 402 | // next character must be a digit. If the previous |
| 403 | // character is not a digit, then the subsequent character can |
| 404 | // be anything besides a digit separator |
| 405 | |
| 406 | let prev = indexing!(@prev $self, $index); |
| 407 | let next = indexing!(@next $self, $index); |
| 408 | let slc = $self.byte.slc; |
| 409 | |
| 410 | if slc.get(prev).map_or(false, |&x| $self.is_digit(x)) { |
| 411 | slc.get(next).map_or(false, |&x| $self.is_digit(x)) |
| 412 | } else { |
| 413 | slc.get(prev).map_or(true, |&x| !$self.is_digit_separator(x)) |
| 414 | } |
| 415 | }}; |
| 416 | |
| 417 | (@first $self:ident) => { |
| 418 | is_il!(@first $self, $self.byte.index) |
| 419 | }; |
| 420 | |
| 421 | (@internal $self:ident, $index:expr) => {{ |
| 422 | // NOTE: We must have validated `prev`, so this just checks `next`. |
| 423 | // NOTE: The conditions here then are that: |
| 424 | // - `index + 1` is a digit |
| 425 | |
| 426 | let next = indexing!(@next $self, $index); |
| 427 | let slc = $self.byte.slc; |
| 428 | slc.get(next).map_or(false, |&x| $self.is_digit(x)) |
| 429 | }}; |
| 430 | |
| 431 | (@internal $self:ident) => { |
| 432 | is_il!(@internal $self, $self.byte.index) |
| 433 | }; |
| 434 | } |
| 435 | |
| 436 | /// Determine if consecutive digit separators are leading or internal. |
| 437 | /// |
| 438 | /// # Examples |
| 439 | /// |
| 440 | /// - `__123`- valid |
| 441 | /// - `+__123`- valid |
| 442 | /// - `._123`- valid |
| 443 | /// - `_+123`- valid |
| 444 | /// - `_123`- valid |
| 445 | /// - `+_123`- valid |
| 446 | /// - `+1_23`- valid |
| 447 | /// - `+1__23`- valid |
| 448 | /// - `+123_`- invalid |
| 449 | /// - `+123__`- invalid |
| 450 | /// - _: valid |
| 451 | /// - _+: valid |
| 452 | /// - 1_+: invalid |
| 453 | /// |
| 454 | /// # Preconditions |
| 455 | /// |
| 456 | /// Assumes `slc[index]` is a digit separator. |
| 457 | macro_rules! is_ilc { |
| 458 | (@first $self:ident, $index:expr) => {{ |
| 459 | // NOTE: The conditions here then are that: |
| 460 | // - `index + 1` is a digit after consuming digit separators |
| 461 | // |
| 462 | // # Logic |
| 463 | // |
| 464 | // We also need to consider the case where it's empty, |
| 465 | // that is, the previous one wasn't a digit if we don't |
| 466 | // have a digit. |
| 467 | |
| 468 | let prev = indexing!(@prevc $self, $index); |
| 469 | let next = indexing!(@nextc $self, $index); |
| 470 | let slc = $self.byte.slc; |
| 471 | slc.get(next).map_or(false, |&x| $self.is_digit(x)) || |
| 472 | slc.get(prev).map_or(true, |&x| !$self.is_digit(x)) |
| 473 | }}; |
| 474 | |
| 475 | (@first $self:ident) => { |
| 476 | is_ilc!(@first $self, $self.byte.index) |
| 477 | }; |
| 478 | |
| 479 | (@internal $self:ident, $index:expr) => {{ |
| 480 | // NOTE: The conditions here then are that: |
| 481 | // - `index + 1` is a digit after consuming digit separators |
| 482 | |
| 483 | let next = indexing!(@nextc $self, $index); |
| 484 | let slc = $self.byte.slc; |
| 485 | slc.get(next).map_or(true, |&x| $self.is_digit(x)) |
| 486 | }}; |
| 487 | |
| 488 | (@internal $self:ident) => { |
| 489 | is_ilc!(@internal $self, $self.byte.index) |
| 490 | }; |
| 491 | } |
| 492 | |
| 493 | /// Determine if the digit separator is internal or trailing. |
| 494 | /// |
| 495 | /// # Examples |
| 496 | /// |
| 497 | /// - `__123`- valid |
| 498 | /// - `+__123`- valid |
| 499 | /// - `._123`- valid |
| 500 | /// - `_+123`- valid |
| 501 | /// - `_123`- valid |
| 502 | /// - `+_123`- valid |
| 503 | /// - `+1_23`- valid |
| 504 | /// - `+1__23`- valid |
| 505 | /// - `+123_`- invalid |
| 506 | /// - `+123__`- invalid |
| 507 | /// - _: valid |
| 508 | /// - _+: valid |
| 509 | /// - 1_+: invalid |
| 510 | /// |
| 511 | /// # Preconditions |
| 512 | /// |
| 513 | /// Assumes `slc[index]` is a digit separator. |
| 514 | macro_rules! is_it { |
| 515 | (@first$self:ident, $index:expr) => {{ |
| 516 | // NOTE: The conditions here then are that: |
| 517 | // - `index - 1` is a digit |
| 518 | // - `index - 1` is not a digit separator |
| 519 | // - `index + 1` is not a digit separator |
| 520 | // |
| 521 | // # Logic |
| 522 | // |
| 523 | // If the previous character is not a digit, there cannot |
| 524 | // be a digit for a following character. If the previous |
| 525 | // character is a digit, then the following one must be |
| 526 | // a digit as well. |
| 527 | |
| 528 | let prev = indexing!(@prev $self, $index); |
| 529 | let next = indexing!(@next $self, $index); |
| 530 | let slc = $self.byte.slc; |
| 531 | if slc.get(prev).map_or(false, |&x| $self.is_digit(x)) { |
| 532 | // Have a digit, any character besides a digit separator is valid |
| 533 | slc.get(next).map_or(true, |&x| !$self.is_digit_separator(x)) |
| 534 | } else { |
| 535 | // Not a digit, so we cannot have a digit or a digit separator |
| 536 | slc.get(next).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x)) |
| 537 | } |
| 538 | }}; |
| 539 | |
| 540 | (@first$self:ident) => { |
| 541 | is_it!(@first $self, $self.byte.index) |
| 542 | }; |
| 543 | |
| 544 | (@internal $self:ident, $index:expr) => {{ |
| 545 | // NOTE: We must have validated `prev`, so this just checks `next`. |
| 546 | // NOTE: The conditions here then are that: |
| 547 | // - `index + 1` is not a digit separator |
| 548 | // Since we've previously had a digit, this is guaranteed to |
| 549 | // be internal or trailing. |
| 550 | |
| 551 | let next = indexing!(@next $self, $index); |
| 552 | let slc = $self.byte.slc; |
| 553 | slc.get(next).map_or(true, |&x| !$self.is_digit_separator(x)) |
| 554 | }}; |
| 555 | |
| 556 | (@internal $self:ident) => { |
| 557 | is_it!(@internal $self, $self.byte.index) |
| 558 | }; |
| 559 | } |
| 560 | |
| 561 | /// Determine if consecutive digit separators are internal or trailing. |
| 562 | /// |
| 563 | /// # Examples |
| 564 | /// |
| 565 | /// - `__123`- invalid |
| 566 | /// - `+__123`- invalid |
| 567 | /// - `._123`- invalid |
| 568 | /// - `_+123`- invalid |
| 569 | /// - `_123`- invalid |
| 570 | /// - `+_123`- invalid |
| 571 | /// - `+1_23`- valid |
| 572 | /// - `+1__23`- valid |
| 573 | /// - `+123_`- valid |
| 574 | /// - `+123__`- valid |
| 575 | /// - _: valid |
| 576 | /// - _+: valid |
| 577 | /// - 1_+: valid |
| 578 | /// |
| 579 | /// # Preconditions |
| 580 | /// |
| 581 | /// Assumes `slc[index]` is a digit separator. |
| 582 | macro_rules! is_itc { |
| 583 | (@first $self:ident, $index:expr) => {{ |
| 584 | // NOTE: The conditions here then are that: |
| 585 | // - `index - 1` is not a digit after consuming digit separators |
| 586 | // |
| 587 | // # Logic |
| 588 | // |
| 589 | // We also need to consider the case where it's empty, |
| 590 | // that is, the previous one wasn't a digit if we don't |
| 591 | // have a digit. |
| 592 | |
| 593 | let prev = indexing!(@prevc $self, $index); |
| 594 | let next = indexing!(@nextc $self, $index); |
| 595 | let slc = $self.byte.slc; |
| 596 | slc.get(prev).map_or(false, |&x| !$self.is_digit(x)) || |
| 597 | slc.get(next).map_or(true, |&x| !$self.is_digit(x)) |
| 598 | }}; |
| 599 | |
| 600 | (@first $self:ident) => { |
| 601 | is_itc!(@first $self, $self.byte.index) |
| 602 | }; |
| 603 | |
| 604 | (@internal $self:ident, $index:expr) => { |
| 605 | // NOTE: Previous must have been a digit so this must be valid. |
| 606 | true |
| 607 | }; |
| 608 | |
| 609 | (@internal $self:ident) => { |
| 610 | is_itc!(@internal $self, $self.byte.index) |
| 611 | }; |
| 612 | } |
| 613 | |
| 614 | /// Determine if the digit separator is leading or trailing. |
| 615 | /// |
| 616 | /// # Examples |
| 617 | /// |
| 618 | /// - `__123`- invalid |
| 619 | /// - `+__123`- invalid |
| 620 | /// - `._123`- valid |
| 621 | /// - `_+123`- valid |
| 622 | /// - `_123`- valid |
| 623 | /// - `+_123`- valid |
| 624 | /// - `+1_23`- invalid |
| 625 | /// - `+1__23`- invalid |
| 626 | /// - `+123_`- valid |
| 627 | /// - `+123__`- invalid |
| 628 | /// - _: valid |
| 629 | /// - _+: valid |
| 630 | /// - 1_+: valid |
| 631 | /// |
| 632 | /// # Preconditions |
| 633 | /// |
| 634 | /// Assumes `slc[index]` is a digit separator. |
| 635 | macro_rules! is_lt { |
| 636 | (@first $self:ident, $index:expr) => {{ |
| 637 | // NOTE: The conditions here then are that: |
| 638 | // - not (`index - 1` is a digit and `index + 1` is a digit) |
| 639 | // - `index - 1` is not a digit separator |
| 640 | // - `index + 1` is not a digit separator |
| 641 | |
| 642 | let prev = indexing!(@prev $self, $index); |
| 643 | let next = indexing!(@next $self, $index); |
| 644 | let slc = $self.byte.slc; |
| 645 | let prev_value = slc.get(prev); |
| 646 | let next_value = slc.get(next); |
| 647 | |
| 648 | let is_prev_sep = prev_value.map_or(false, |&x| $self.is_digit_separator(x)); |
| 649 | let is_prev_dig = prev_value.map_or(false, |&x| $self.is_digit(x)); |
| 650 | let is_next_sep = next_value.map_or(false, |&x| $self.is_digit_separator(x)); |
| 651 | let is_next_dig = next_value.map_or(false, |&x| $self.is_digit(x)); |
| 652 | |
| 653 | !is_prev_sep && !is_next_sep && !(is_prev_dig && is_next_dig) |
| 654 | }}; |
| 655 | |
| 656 | (@first $self:ident) => { |
| 657 | is_lt!(@first $self, $self.byte.index) |
| 658 | }; |
| 659 | |
| 660 | (@internal $self:ident, $index:expr) => {{ |
| 661 | // NOTE: We must have validated `prev`, so this just checks `next`. |
| 662 | // NOTE: The conditions here then are that: |
| 663 | // - `index + 1` is not a digit |
| 664 | // - `index + 1` is not a digit separator |
| 665 | |
| 666 | let next = indexing!(@next $self, $index); |
| 667 | let slc = $self.byte.slc; |
| 668 | slc.get(next).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x)) |
| 669 | }}; |
| 670 | |
| 671 | (@internal $self:ident) => { |
| 672 | is_lt!(@internal $self, $self.byte.index) |
| 673 | }; |
| 674 | } |
| 675 | |
| 676 | /// Determine if consecutive digit separators are leading or trailing. |
| 677 | /// |
| 678 | /// # Examples |
| 679 | /// |
| 680 | /// - `__123`- valid |
| 681 | /// - `+__123`- valid |
| 682 | /// - `._123`- valid |
| 683 | /// - `_+123`- valid |
| 684 | /// - `_123`- valid |
| 685 | /// - `+_123`- valid |
| 686 | /// - `+1_23`- invalid |
| 687 | /// - `+1__23`- invalid |
| 688 | /// - `+123_`- valid |
| 689 | /// - `+123__`- valid |
| 690 | /// - _: valid |
| 691 | /// - _+: valid |
| 692 | /// - 1_+: valid |
| 693 | /// |
| 694 | /// # Preconditions |
| 695 | /// |
| 696 | /// Assumes `slc[index]` is a digit separator. |
| 697 | macro_rules! is_ltc { |
| 698 | (@first $self:ident, $index:expr) => {{ |
| 699 | // NOTE: The conditions here then are that (after consuming separators): |
| 700 | // - not (`index - 1` is a digit and `index + 1` is a digit) |
| 701 | |
| 702 | let prev = indexing!(@prevc $self, $index); |
| 703 | let next = indexing!(@nextc $self, $index); |
| 704 | let slc = $self.byte.slc; |
| 705 | !(slc.get(prev).map_or(false, |&x| $self.is_digit(x)) && slc.get(next).map_or(false, |&x| $self.is_digit(x))) |
| 706 | }}; |
| 707 | |
| 708 | (@first $self:ident) => { |
| 709 | is_ltc!(@first $self, $self.byte.index) |
| 710 | }; |
| 711 | |
| 712 | (@internal $self:ident, $index:expr) => {{ |
| 713 | // NOTE: We must have validated `prev`, so this just checks `next`. |
| 714 | // NOTE: The conditions here then are that: |
| 715 | // - `index + 1` is not a digit |
| 716 | |
| 717 | let next = indexing!(@nextc $self, $index); |
| 718 | let slc = $self.byte.slc; |
| 719 | slc.get(next).map_or(true, |&x| !$self.is_digit(x)) |
| 720 | }}; |
| 721 | |
| 722 | (@internal $self:ident) => { |
| 723 | is_ltc!(@internal $self, $self.byte.index) |
| 724 | }; |
| 725 | } |
| 726 | |
| 727 | /// Determine if a single digit separator is internal, leading, or trailing. |
| 728 | /// |
| 729 | /// # Examples |
| 730 | /// |
| 731 | /// - `__123`- invalid |
| 732 | /// - `+__123`- invalid |
| 733 | /// - `._123`- valid |
| 734 | /// - `_+123`- valid |
| 735 | /// - `_123`- valid |
| 736 | /// - `+_123`- valid |
| 737 | /// - `+1_23`- valid |
| 738 | /// - `+1__23`- invalid |
| 739 | /// - `+123_`- valid |
| 740 | /// - `+123__`- invalid |
| 741 | /// - _: valid |
| 742 | /// - _+: valid |
| 743 | /// - 1_+: valid |
| 744 | /// |
| 745 | /// # Preconditions |
| 746 | /// |
| 747 | /// Assumes `slc[index]` is a digit separator. |
| 748 | macro_rules! is_ilt { |
| 749 | (@first $self:ident, $index:expr) => {{ |
| 750 | // NOTE: The conditions here then are that: |
| 751 | // - `index + 1` is not a digit separator |
| 752 | // - `index - 1` is not a digit separator |
| 753 | |
| 754 | let prev = indexing!(@prev $self, $index); |
| 755 | let next = indexing!(@next $self, $index); |
| 756 | let slc = $self.byte.slc; |
| 757 | !slc.get(next).map_or(false, |&x| $self.is_digit_separator(x)) && |
| 758 | !slc.get(prev).map_or(false, |&x| $self.is_digit_separator(x)) |
| 759 | }}; |
| 760 | |
| 761 | (@first $self:ident) => { |
| 762 | is_ilt!(@first $self, $self.byte.index) |
| 763 | }; |
| 764 | |
| 765 | (@internal $self:ident, $index:expr) => {{ |
| 766 | // NOTE: We must have validated `prev`, so this just checks `next`. |
| 767 | // NOTE: The conditions here then are that: |
| 768 | // - `index + 1` is not a digit separator |
| 769 | |
| 770 | let next = indexing!(@next $self, $index); |
| 771 | let slc = $self.byte.slc; |
| 772 | slc.get(next).map_or(true, |&x| !$self.is_digit_separator(x)) |
| 773 | }}; |
| 774 | |
| 775 | (@internal $self:ident) => { |
| 776 | is_ilt!(@internal $self, $self.byte.index) |
| 777 | }; |
| 778 | } |
| 779 | |
| 780 | /// Determine if consecutive digit separators are internal, leading, or |
| 781 | /// trailing. |
| 782 | /// |
| 783 | /// This is always true. |
| 784 | /// |
| 785 | /// # Examples |
| 786 | /// |
| 787 | /// - `__123`- valid |
| 788 | /// - `+__123`- valid |
| 789 | /// - `._123`- valid |
| 790 | /// - `_+123`- valid |
| 791 | /// - `_123`- valid |
| 792 | /// - `+_123`- valid |
| 793 | /// - `+1_23`- valid |
| 794 | /// - `+1__23`- valid |
| 795 | /// - `+123_`- valid |
| 796 | /// - `+123__`- valid |
| 797 | /// - _: valid |
| 798 | /// - _+: valid |
| 799 | /// - 1_+: valid |
| 800 | /// |
| 801 | /// # Preconditions |
| 802 | /// |
| 803 | /// Assumes `slc[index]` is a digit separator. |
| 804 | macro_rules! is_iltc { |
| 805 | (@first $self:ident, $index:expr) => { |
| 806 | true |
| 807 | }; |
| 808 | |
| 809 | (@first $self:ident) => { |
| 810 | is_iltc!(@first $self, $self.byte.index) |
| 811 | }; |
| 812 | |
| 813 | (@internal $self:ident, $index:expr) => { |
| 814 | true |
| 815 | }; |
| 816 | |
| 817 | (@internal $self:ident) => { |
| 818 | is_iltc!(@internal $self, $self.byte.index) |
| 819 | }; |
| 820 | } |
| 821 | |
| 822 | // PEEK |
| 823 | // ---- |
| 824 | |
| 825 | /// Consumes 1 or more digit separators. |
| 826 | /// Peeks the next token that's not a digit separator. |
| 827 | macro_rules! peek_1 { |
| 828 | ($self:ident, $is_skip:ident) => {{ |
| 829 | // This will consume a single, non-consecutive digit separators. |
| 830 | let index = $self.cursor(); |
| 831 | let buffer = $self.get_buffer(); |
| 832 | let value = buffer.get(index)?; |
| 833 | let is_digit_separator = $self.is_digit_separator(*value); |
| 834 | // NOTE: We can do some pretty major optimizations for internal values, |
| 835 | // since we can check the location and don't need to check previous values. |
| 836 | if is_digit_separator { |
| 837 | // NOTE: This cannot iteratively search for the next value, |
| 838 | // or else the consecutive digit separator has no effect (#96). |
| 839 | let is_skip = if $self.current_count() == 0 { |
| 840 | $is_skip!(@first $self) |
| 841 | } else { |
| 842 | $is_skip!(@internal $self) |
| 843 | }; |
| 844 | if is_skip { |
| 845 | // SAFETY: Safe since `index < buffer.len()`, so `index + 1 <= buffer.len()`` |
| 846 | unsafe { $self.set_cursor(index + 1) }; |
| 847 | buffer.get(index + 1) |
| 848 | } else { |
| 849 | Some(value) |
| 850 | } |
| 851 | } else { |
| 852 | // Have 1 of 2 conditions: |
| 853 | // 1. A non-digit separator character. |
| 854 | // 2. A digit separator that is not valid in the context. |
| 855 | Some(value) |
| 856 | } |
| 857 | }}; |
| 858 | } |
| 859 | |
| 860 | /// Consumes 1 or more digit separators. |
| 861 | /// Peeks the next token that's not a digit separator. |
| 862 | macro_rules! peek_n { |
| 863 | ($self:ident, $is_skip:ident) => {{ |
| 864 | // This will consume consecutive digit separators. |
| 865 | let mut index = $self.cursor(); |
| 866 | let buffer = $self.get_buffer(); |
| 867 | let value = buffer.get(index)?; |
| 868 | let is_digit_separator = $self.is_digit_separator(*value); |
| 869 | // NOTE: We can do some pretty major optimizations for internal values, |
| 870 | // since we can check the location and don't need to check previous values. |
| 871 | if is_digit_separator { |
| 872 | let is_skip = if $self.current_count() == 0 { |
| 873 | $is_skip!(@first $self) |
| 874 | } else { |
| 875 | $is_skip!(@internal $self) |
| 876 | }; |
| 877 | if is_skip { |
| 878 | // Have a skippable digit separator: keep incrementing until we find |
| 879 | // a non-digit separator character. Don't need any complex checks |
| 880 | // here, since we've already done them above. |
| 881 | index += 1; |
| 882 | while index < buffer.len() |
| 883 | && buffer.get(index).map_or(false, |&x| $self.is_digit_separator(x)) |
| 884 | { |
| 885 | index += 1; |
| 886 | } |
| 887 | // SAFETY: Safe since `index <= buffer.len()`. |
| 888 | unsafe { $self.set_cursor(index) }; |
| 889 | buffer.get(index) |
| 890 | } else { |
| 891 | Some(value) |
| 892 | } |
| 893 | } else { |
| 894 | // Have 1 of 2 conditions: |
| 895 | // 1. A non-digit separator character. |
| 896 | // 2. A digit separator that is not valid in the context. |
| 897 | Some(value) |
| 898 | } |
| 899 | }}; |
| 900 | } |
| 901 | |
| 902 | /// Consumes no digit separators and peeks the next value. |
| 903 | macro_rules! peek_noskip { |
| 904 | ($self:ident) => { |
| 905 | $self.byte.slc.get($self.byte.index) |
| 906 | }; |
| 907 | } |
| 908 | |
| 909 | /// Consumes at most 1 leading digit separator and peeks the next value. |
| 910 | macro_rules! peek_l { |
| 911 | ($self:ident) => { |
| 912 | peek_1!($self, is_l) |
| 913 | }; |
| 914 | } |
| 915 | |
| 916 | /// Consumes at most 1 internal digit separator and peeks the next value. |
| 917 | macro_rules! peek_i { |
| 918 | ($self:ident) => { |
| 919 | peek_1!($self, is_i) |
| 920 | }; |
| 921 | } |
| 922 | |
| 923 | /// Consumes at most 1 trailing digit separator and peeks the next value. |
| 924 | macro_rules! peek_t { |
| 925 | ($self:ident) => { |
| 926 | peek_1!($self, is_t) |
| 927 | }; |
| 928 | } |
| 929 | |
| 930 | /// Consumes at most 1 internal/leading digit separator and peeks the next |
| 931 | /// value. |
| 932 | macro_rules! peek_il { |
| 933 | ($self:ident) => { |
| 934 | peek_1!($self, is_il) |
| 935 | }; |
| 936 | } |
| 937 | |
| 938 | /// Consumes at most 1 internal/trailing digit separator and peeks the next |
| 939 | /// value. |
| 940 | macro_rules! peek_it { |
| 941 | ($self:ident) => { |
| 942 | peek_1!($self, is_it) |
| 943 | }; |
| 944 | } |
| 945 | |
| 946 | /// Consumes at most 1 leading/trailing digit separator and peeks the next |
| 947 | /// value. |
| 948 | macro_rules! peek_lt { |
| 949 | ($self:ident) => { |
| 950 | peek_1!($self, is_lt) |
| 951 | }; |
| 952 | } |
| 953 | |
| 954 | /// Consumes at most 1 digit separator and peeks the next value. |
| 955 | macro_rules! peek_ilt { |
| 956 | ($self:ident) => { |
| 957 | peek_1!($self, is_ilt) |
| 958 | }; |
| 959 | } |
| 960 | |
| 961 | /// Consumes 1 or more leading digit separators and peeks the next value. |
| 962 | macro_rules! peek_lc { |
| 963 | ($self:ident) => { |
| 964 | peek_n!($self, is_lc) |
| 965 | }; |
| 966 | } |
| 967 | |
| 968 | /// Consumes 1 or more internal digit separators and peeks the next value. |
| 969 | macro_rules! peek_ic { |
| 970 | ($self:ident) => { |
| 971 | peek_n!($self, is_ic) |
| 972 | }; |
| 973 | } |
| 974 | |
| 975 | /// Consumes 1 or more trailing digit separators and peeks the next value. |
| 976 | macro_rules! peek_tc { |
| 977 | ($self:ident) => { |
| 978 | peek_n!($self, is_tc) |
| 979 | }; |
| 980 | } |
| 981 | |
| 982 | /// Consumes 1 or more internal/leading digit separators and peeks the next |
| 983 | /// value. |
| 984 | macro_rules! peek_ilc { |
| 985 | ($self:ident) => { |
| 986 | peek_n!($self, is_ilc) |
| 987 | }; |
| 988 | } |
| 989 | |
| 990 | /// Consumes 1 or more internal/trailing digit separators and peeks the next |
| 991 | /// value. |
| 992 | macro_rules! peek_itc { |
| 993 | ($self:ident) => { |
| 994 | peek_n!($self, is_itc) |
| 995 | }; |
| 996 | } |
| 997 | |
| 998 | /// Consumes 1 or more leading/trailing digit separators and peeks the next |
| 999 | /// value. |
| 1000 | macro_rules! peek_ltc { |
| 1001 | ($self:ident) => { |
| 1002 | peek_n!($self, is_ltc) |
| 1003 | }; |
| 1004 | } |
| 1005 | |
| 1006 | /// Consumes 1 or more digit separators and peeks the next value. |
| 1007 | macro_rules! peek_iltc { |
| 1008 | ($self:ident) => { |
| 1009 | peek_n!($self, is_iltc) |
| 1010 | }; |
| 1011 | } |
| 1012 | |
| 1013 | // AS DIGITS |
| 1014 | // --------- |
| 1015 | |
| 1016 | /// Trait to simplify creation of a `Bytes` object. |
| 1017 | pub trait AsBytes<'a> { |
| 1018 | /// Create `Bytes` from object. |
| 1019 | fn bytes<const FORMAT: u128>(&'a self) -> Bytes<'a, FORMAT>; |
| 1020 | } |
| 1021 | |
| 1022 | impl<'a> AsBytes<'a> for [u8] { |
| 1023 | #[inline(always)] |
| 1024 | fn bytes<const FORMAT: u128>(&'a self) -> Bytes<'a, FORMAT> { |
| 1025 | Bytes::new(self) |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | // DIGITS |
| 1030 | // ------ |
| 1031 | |
| 1032 | /// Slice iterator that skips characters matching a given value. |
| 1033 | /// |
| 1034 | /// This wraps an iterator over a contiguous block of memory, |
| 1035 | /// and only returns values that are not equal to skip. |
| 1036 | /// |
| 1037 | /// The format allows us to dictate the actual behavior of |
| 1038 | /// the iterator: in what contexts does it skip digit separators. |
| 1039 | /// |
| 1040 | /// `FORMAT` is required to tell us what the digit separator is, and where |
| 1041 | /// the digit separators are allowed, as well tell us the radix. |
| 1042 | /// The radix is required to allow us to differentiate digit from |
| 1043 | /// non-digit characters (see [`DigitSeparators`](/docs/DigitSeparators.md) |
| 1044 | /// for a detailed explanation on why). |
| 1045 | #[derive(Clone)] |
| 1046 | pub struct Bytes<'a, const FORMAT: u128> { |
| 1047 | /// The raw slice for the iterator. |
| 1048 | slc: &'a [u8], |
| 1049 | /// Current index of the iterator in the slice. |
| 1050 | index: usize, |
| 1051 | /// The current count of integer digits returned by the iterator. |
| 1052 | /// This is only used if the iterator is not contiguous. |
| 1053 | integer_count: usize, |
| 1054 | /// The current count of fraction digits returned by the iterator. |
| 1055 | /// This is only used if the iterator is not contiguous. |
| 1056 | fraction_count: usize, |
| 1057 | /// The current count of exponent digits returned by the iterator. |
| 1058 | /// This is only used if the iterator is not contiguous. |
| 1059 | exponent_count: usize, |
| 1060 | } |
| 1061 | |
| 1062 | impl<'a, const FORMAT: u128> Bytes<'a, FORMAT> { |
| 1063 | /// Create new byte object. |
| 1064 | #[inline(always)] |
| 1065 | pub const fn new(slc: &'a [u8]) -> Self { |
| 1066 | Self { |
| 1067 | slc, |
| 1068 | index: 0, |
| 1069 | integer_count: 0, |
| 1070 | fraction_count: 0, |
| 1071 | exponent_count: 0, |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | /// Initialize the slice from raw parts. |
| 1076 | /// |
| 1077 | /// # Safety |
| 1078 | /// This is safe if and only if the index is <= `slc.len()`. |
| 1079 | /// For this reason, since it's easy to get wrong, we only |
| 1080 | /// expose it to our `DigitsIterator`s and nothing else. |
| 1081 | /// |
| 1082 | /// This is only ever used for contiguous iterators. However, |
| 1083 | /// it's not guaranteed to only valid for our contiguous |
| 1084 | /// iterators. |
| 1085 | #[inline(always)] |
| 1086 | const unsafe fn from_parts(slc: &'a [u8], index: usize) -> Self { |
| 1087 | debug_assert!(index <= slc.len()); |
| 1088 | Self { |
| 1089 | slc, |
| 1090 | index, |
| 1091 | integer_count: 0, |
| 1092 | fraction_count: 0, |
| 1093 | exponent_count: 0, |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /// Get iterator over integer digits. |
| 1098 | #[inline(always)] |
| 1099 | pub fn integer_iter<'b>(&'b mut self) -> IntegerDigitsIterator<'a, 'b, FORMAT> { |
| 1100 | IntegerDigitsIterator { |
| 1101 | byte: self, |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /// Get iterator over fraction digits. |
| 1106 | #[inline(always)] |
| 1107 | pub fn fraction_iter<'b>(&'b mut self) -> FractionDigitsIterator<'a, 'b, FORMAT> { |
| 1108 | FractionDigitsIterator { |
| 1109 | byte: self, |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | /// Get iterator over exponent digits. |
| 1114 | #[inline(always)] |
| 1115 | pub fn exponent_iter<'b>(&'b mut self) -> ExponentDigitsIterator<'a, 'b, FORMAT> { |
| 1116 | ExponentDigitsIterator { |
| 1117 | byte: self, |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | /// Get iterator over special floating point values. |
| 1122 | #[inline(always)] |
| 1123 | pub fn special_iter<'b>(&'b mut self) -> SpecialDigitsIterator<'a, 'b, FORMAT> { |
| 1124 | SpecialDigitsIterator { |
| 1125 | byte: self, |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | /// Internal implementation that handles if it's contiguous. |
| 1130 | /// |
| 1131 | /// # Safety |
| 1132 | /// |
| 1133 | /// Safe if the buffer has at least `N` elements. |
| 1134 | #[inline(always)] |
| 1135 | unsafe fn step_by_unchecked_impl(&mut self, count: usize, is_contiguous: bool) { |
| 1136 | // NOTE: THIS IS NOT a duplicate calling `step_by_unchecked` from a digits |
| 1137 | // iterator on the byte, since they can have different contiguousness. |
| 1138 | if is_contiguous { |
| 1139 | // Contiguous, can skip most of these checks. |
| 1140 | debug_assert!(self.as_slice().len() >= count); |
| 1141 | } else { |
| 1142 | // Since this isn't contiguous, it only works |
| 1143 | // if the value is in the range `[0, 1]`. |
| 1144 | // We also need to make sure the **current** value |
| 1145 | // isn't a digit separator. |
| 1146 | let format = NumberFormat::<{ FORMAT }> {}; |
| 1147 | debug_assert!(self.as_slice().len() >= count); |
| 1148 | debug_assert!(count == 0 || count == 1); |
| 1149 | debug_assert!( |
| 1150 | count == 0 || self.slc.get(self.index) != Some(&format.digit_separator()) |
| 1151 | ); |
| 1152 | } |
| 1153 | self.index += count; |
| 1154 | } |
| 1155 | |
| 1156 | /// Internal implementation that handles if it's contiguous. |
| 1157 | /// |
| 1158 | /// If it's contiguous or not does not affect the safety guarantees, |
| 1159 | /// however, it can affect correctness. |
| 1160 | /// |
| 1161 | /// # Safety |
| 1162 | /// |
| 1163 | /// Safe if the buffer has at least `size_of::<V>` elements. |
| 1164 | #[inline(always)] |
| 1165 | unsafe fn peek_many_unchecked_impl<V>(&self, is_contiguous: bool) -> V { |
| 1166 | // NOTE: THIS IS NOT a duplicate calling `peek_many_unchecked` from a digits |
| 1167 | // iterator on the byte, since they can have different contiguousness. |
| 1168 | debug_assert!(is_contiguous); |
| 1169 | debug_assert!(self.as_slice().len() >= mem::size_of::<V>()); |
| 1170 | |
| 1171 | let slc = self.as_slice(); |
| 1172 | // SAFETY: safe as long as the slice has at least count elements. |
| 1173 | unsafe { ptr::read_unaligned::<V>(slc.as_ptr() as *const _) } |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | unsafe impl<'a, const FORMAT: u128> Iter<'a> for Bytes<'a, FORMAT> { |
| 1178 | /// If each yielded value is adjacent in memory. |
| 1179 | const IS_CONTIGUOUS: bool = NumberFormat::<{ FORMAT }>::DIGIT_SEPARATOR == 0; |
| 1180 | |
| 1181 | #[inline(always)] |
| 1182 | fn get_buffer(&self) -> &'a [u8] { |
| 1183 | self.slc |
| 1184 | } |
| 1185 | |
| 1186 | /// Get the current index of the iterator in the slice. |
| 1187 | #[inline(always)] |
| 1188 | fn cursor(&self) -> usize { |
| 1189 | self.index |
| 1190 | } |
| 1191 | |
| 1192 | /// Set the current index of the iterator in the slice. |
| 1193 | /// |
| 1194 | /// # Safety |
| 1195 | /// |
| 1196 | /// Safe if `index <= self.buffer_length()`. |
| 1197 | #[inline(always)] |
| 1198 | unsafe fn set_cursor(&mut self, index: usize) { |
| 1199 | debug_assert!(index <= self.buffer_length()); |
| 1200 | self.index = index; |
| 1201 | } |
| 1202 | |
| 1203 | /// Get the current number of digits returned by the iterator. |
| 1204 | /// |
| 1205 | /// For contiguous iterators, this can include the sign character, decimal |
| 1206 | /// point, and the exponent sign (that is, it is always the cursor). For |
| 1207 | /// non-contiguous iterators, this must always be the only the number of |
| 1208 | /// digits returned. |
| 1209 | #[inline(always)] |
| 1210 | fn current_count(&self) -> usize { |
| 1211 | // If the buffer is contiguous, then we don't need to track the |
| 1212 | // number of values: the current index is enough. |
| 1213 | if Self::IS_CONTIGUOUS { |
| 1214 | self.index |
| 1215 | } else { |
| 1216 | self.integer_count + self.fraction_count + self.exponent_count |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | #[inline(always)] |
| 1221 | unsafe fn step_by_unchecked(&mut self, count: usize) { |
| 1222 | // SAFETY: Safe if the buffer has at least `N` elements. |
| 1223 | unsafe { self.step_by_unchecked_impl(count, Self::IS_CONTIGUOUS) } |
| 1224 | } |
| 1225 | |
| 1226 | #[inline(always)] |
| 1227 | unsafe fn peek_many_unchecked<V>(&self) -> V { |
| 1228 | // SAFETY: Safe if the buffer has at least `size_of::<V>` elements. |
| 1229 | unsafe { self.peek_many_unchecked_impl(Self::IS_CONTIGUOUS) } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | // ITERATOR HELPERS |
| 1234 | // ---------------- |
| 1235 | |
| 1236 | /// Create skip iterator definition. |
| 1237 | macro_rules! skip_iterator { |
| 1238 | ($iterator:ident, $doc:literal) => { |
| 1239 | #[doc = $doc] |
| 1240 | pub struct $iterator<'a: 'b, 'b, const FORMAT: u128> { |
| 1241 | /// The internal byte object for the skip iterator. |
| 1242 | byte: &'b mut Bytes<'a, FORMAT>, |
| 1243 | } |
| 1244 | }; |
| 1245 | } |
| 1246 | |
| 1247 | macro_rules! is_sign { |
| 1248 | () => { |
| 1249 | pub const fn is_sign(&self, value: u8) -> bool { |
| 1250 | matches!(value, b'+' | b'-') |
| 1251 | } |
| 1252 | }; |
| 1253 | } |
| 1254 | |
| 1255 | macro_rules! is_digit_separator { |
| 1256 | ($format:ident) => { |
| 1257 | /// Determine if the character is a digit separator. |
| 1258 | pub const fn is_digit_separator(&self, value: u8) -> bool { |
| 1259 | let format = NumberFormat::<{ $format }> {}; |
| 1260 | let digit_separator = format.digit_separator(); |
| 1261 | if digit_separator == 0 { |
| 1262 | // Check at compile time if we have an invalid digit separator. |
| 1263 | // b'\x00', or the NUL character, is this invalid value. |
| 1264 | false |
| 1265 | } else { |
| 1266 | value == digit_separator |
| 1267 | } |
| 1268 | } |
| 1269 | }; |
| 1270 | } |
| 1271 | |
| 1272 | /// Create impl block for skip iterator. |
| 1273 | macro_rules! skip_iterator_impl { |
| 1274 | ($iterator:ident, $radix_cb:ident) => { |
| 1275 | impl<'a: 'b, 'b, const FORMAT: u128> $iterator<'a, 'b, FORMAT> { |
| 1276 | is_sign!(); |
| 1277 | is_digit_separator!(FORMAT); |
| 1278 | |
| 1279 | /// Create a new digits iterator from the bytes underlying item. |
| 1280 | #[inline(always)] |
| 1281 | pub fn new(byte: &'b mut Bytes<'a, FORMAT>) -> Self { |
| 1282 | Self { |
| 1283 | byte, |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | /// Take the first N digits from the iterator. |
| 1288 | /// |
| 1289 | /// This only takes the digits if we have a contiguous iterator. |
| 1290 | /// It takes the digits, validating the bounds, and then advanced |
| 1291 | /// the iterators state. It does not support non-contiguous iterators |
| 1292 | /// since we would lose information on the count. |
| 1293 | #[cfg_attr(not(feature = "compact"), inline(always))] |
| 1294 | #[allow(clippy::assertions_on_constants)] // reason="ensuring safety invariants are valid" |
| 1295 | pub fn take_n(&mut self, n: usize) -> Option<Bytes<'a, FORMAT>> { |
| 1296 | if Self::IS_CONTIGUOUS { |
| 1297 | let end = self.byte.slc.len().min(n + self.cursor()); |
| 1298 | // NOTE: The compiler should be able to optimize this out. |
| 1299 | let slc: &[u8] = &self.byte.slc[..end]; |
| 1300 | |
| 1301 | // SAFETY: Safe since we just ensured the underlying slice has that count |
| 1302 | // elements, so both the underlying slice for this and this **MUST** |
| 1303 | // have at least count elements. We do static checking on the bounds for this. |
| 1304 | unsafe { |
| 1305 | let byte: Bytes<'_, FORMAT> = Bytes::from_parts(slc, self.cursor()); |
| 1306 | unsafe { self.set_cursor(end) }; |
| 1307 | Some(byte) |
| 1308 | } |
| 1309 | } else { |
| 1310 | None |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | }; |
| 1315 | } |
| 1316 | |
| 1317 | /// Create impl Iterator block for skip iterator. |
| 1318 | macro_rules! skip_iterator_iterator_impl { |
| 1319 | ($iterator:ident) => { |
| 1320 | impl<'a: 'b, 'b, const FORMAT: u128> Iterator for $iterator<'a, 'b, FORMAT> { |
| 1321 | type Item = &'a u8; |
| 1322 | |
| 1323 | #[inline(always)] |
| 1324 | fn next(&mut self) -> Option<Self::Item> { |
| 1325 | // Peek will handle everything properly internally. |
| 1326 | let value = self.peek()?; |
| 1327 | // Increment the index so we know not to re-fetch it. |
| 1328 | self.byte.index += 1; |
| 1329 | // NOTE: Only increment the count if it's not contiguous, otherwise, |
| 1330 | // this is an unnecessary performance penalty. We also need |
| 1331 | // to check if it's a digit, which adds on additional cost but |
| 1332 | // there's not much else we can do. Hopefully the previous inlining |
| 1333 | // checks will minimize the performance hit. |
| 1334 | if !Self::IS_CONTIGUOUS && self.is_digit(*value) { |
| 1335 | self.increment_count(); |
| 1336 | } |
| 1337 | Some(value) |
| 1338 | } |
| 1339 | } |
| 1340 | }; |
| 1341 | } |
| 1342 | |
| 1343 | /// Create base methods for the Iter block of a skip iterator. |
| 1344 | macro_rules! skip_iterator_iter_base { |
| 1345 | ($format:ident, $mask:ident, $count:ident) => { |
| 1346 | // It's contiguous if we don't skip over any values. |
| 1347 | // IE, the digit separator flags for the iterator over |
| 1348 | // the digits doesn't skip any values. |
| 1349 | const IS_CONTIGUOUS: bool = $format & flags::$mask == 0; |
| 1350 | |
| 1351 | #[inline(always)] |
| 1352 | fn get_buffer(&self) -> &'a [u8] { |
| 1353 | self.byte.get_buffer() |
| 1354 | } |
| 1355 | |
| 1356 | #[inline(always)] |
| 1357 | fn cursor(&self) -> usize { |
| 1358 | self.byte.cursor() |
| 1359 | } |
| 1360 | |
| 1361 | #[inline(always)] |
| 1362 | unsafe fn set_cursor(&mut self, index: usize) { |
| 1363 | debug_assert!(index <= self.buffer_length()); |
| 1364 | // SAFETY: safe if `index <= self.buffer_length()`. |
| 1365 | unsafe { self.byte.set_cursor(index) }; |
| 1366 | } |
| 1367 | |
| 1368 | /// Get the current number of digits returned by the iterator. |
| 1369 | /// |
| 1370 | /// For contiguous iterators, this can include the sign character, decimal |
| 1371 | /// point, and the exponent sign (that is, it is always the cursor). For |
| 1372 | /// non-contiguous iterators, this must always be the only the number of |
| 1373 | /// digits returned. |
| 1374 | #[inline(always)] |
| 1375 | fn current_count(&self) -> usize { |
| 1376 | if Self::IS_CONTIGUOUS { |
| 1377 | self.byte.current_count() |
| 1378 | } else { |
| 1379 | self.byte.$count |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | #[inline(always)] |
| 1384 | unsafe fn step_by_unchecked(&mut self, count: usize) { |
| 1385 | // SAFETY: Safe if the buffer has at least `N` elements. |
| 1386 | unsafe { self.byte.step_by_unchecked_impl(count, Self::IS_CONTIGUOUS) } |
| 1387 | } |
| 1388 | |
| 1389 | #[inline(always)] |
| 1390 | unsafe fn peek_many_unchecked<V>(&self) -> V { |
| 1391 | // SAFETY: Safe if the buffer has at least `size_of::<V>` elements. |
| 1392 | unsafe { self.byte.peek_many_unchecked_impl(Self::IS_CONTIGUOUS) } |
| 1393 | } |
| 1394 | }; |
| 1395 | } |
| 1396 | |
| 1397 | /// Create base methods for the `DigitsIter` block of a skip iterator. |
| 1398 | macro_rules! skip_iterator_digits_iter_base { |
| 1399 | () => { |
| 1400 | #[inline(always)] |
| 1401 | fn is_consumed(&mut self) -> bool { |
| 1402 | self.peek().is_none() |
| 1403 | } |
| 1404 | }; |
| 1405 | } |
| 1406 | |
| 1407 | /// Create impl `ByteIter` block for skip iterator. |
| 1408 | macro_rules! skip_iterator_bytesiter_impl { |
| 1409 | ($iterator:ident, $mask:ident, $count:ident, $i:ident, $l:ident, $t:ident, $c:ident) => { |
| 1410 | unsafe impl<'a: 'b, 'b, const FORMAT: u128> Iter<'a> for $iterator<'a, 'b, FORMAT> { |
| 1411 | skip_iterator_iter_base!(FORMAT, $mask, $count); |
| 1412 | } |
| 1413 | |
| 1414 | impl<'a: 'b, 'b, const FORMAT: u128> DigitsIter<'a> for $iterator<'a, 'b, FORMAT> { |
| 1415 | skip_iterator_digits_iter_base!(); |
| 1416 | |
| 1417 | /// Increment the number of digits that have been returned by the iterator. |
| 1418 | /// |
| 1419 | /// For contiguous iterators, this is a no-op. For non-contiguous iterators, |
| 1420 | /// this increments the count by 1. |
| 1421 | #[inline(always)] |
| 1422 | fn increment_count(&mut self) { |
| 1423 | self.byte.$count += 1; |
| 1424 | } |
| 1425 | |
| 1426 | /// Peek the next value of the iterator, without consuming it. |
| 1427 | /// |
| 1428 | /// Note that this can modify the internal state, by skipping digits |
| 1429 | /// for iterators that find the first non-zero value, etc. We optimize |
| 1430 | /// this for the case where we have contiguous iterators, since |
| 1431 | /// non-contiguous iterators already have a major performance penalty. |
| 1432 | /// |
| 1433 | /// Checking if the character is a digit in the `next()` implementation |
| 1434 | /// after skipping characters can: |
| 1435 | /// 1. Likely be optimized out due to the use of macros and inlining. |
| 1436 | /// 2. Is a small amount of overhead compared to the branching on |
| 1437 | /// characters, |
| 1438 | #[inline(always)] |
| 1439 | fn peek(&mut self) -> Option<<Self as Iterator>::Item> { |
| 1440 | let format = NumberFormat::<{ FORMAT }> {}; |
| 1441 | const I: u128 = flags::$i; |
| 1442 | const L: u128 = flags::$l; |
| 1443 | const T: u128 = flags::$t; |
| 1444 | const C: u128 = flags::$c; |
| 1445 | const IL: u128 = I | L; |
| 1446 | const IT: u128 = I | T; |
| 1447 | const LT: u128 = L | T; |
| 1448 | const ILT: u128 = I | L | T; |
| 1449 | const IC: u128 = I | C; |
| 1450 | const LC: u128 = L | C; |
| 1451 | const TC: u128 = T | C; |
| 1452 | const ILC: u128 = IL | C; |
| 1453 | const ITC: u128 = IT | C; |
| 1454 | const LTC: u128 = LT | C; |
| 1455 | const ILTC: u128 = ILT | C; |
| 1456 | |
| 1457 | match format.digit_separator_flags() & flags::$mask { |
| 1458 | 0 => peek_noskip!(self), |
| 1459 | I => peek_i!(self), |
| 1460 | L => peek_l!(self), |
| 1461 | T => peek_t!(self), |
| 1462 | IL => peek_il!(self), |
| 1463 | IT => peek_it!(self), |
| 1464 | LT => peek_lt!(self), |
| 1465 | ILT => peek_ilt!(self), |
| 1466 | IC => peek_ic!(self), |
| 1467 | LC => peek_lc!(self), |
| 1468 | TC => peek_tc!(self), |
| 1469 | ILC => peek_ilc!(self), |
| 1470 | ITC => peek_itc!(self), |
| 1471 | LTC => peek_ltc!(self), |
| 1472 | ILTC => peek_iltc!(self), |
| 1473 | _ => unreachable!(), |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | /// Determine if the character is a digit. |
| 1478 | #[inline(always)] |
| 1479 | fn is_digit(&self, value: u8) -> bool { |
| 1480 | let format = NumberFormat::<{ FORMAT }> {}; |
| 1481 | char_is_digit_const(value, format.mantissa_radix()) |
| 1482 | } |
| 1483 | } |
| 1484 | }; |
| 1485 | } |
| 1486 | |
| 1487 | // INTEGER DIGITS ITERATOR |
| 1488 | // ----------------------- |
| 1489 | |
| 1490 | skip_iterator!(IntegerDigitsIterator, "Iterator that skips over digit separators in the integer."); |
| 1491 | skip_iterator_impl!(IntegerDigitsIterator, mantissa_radix); |
| 1492 | skip_iterator_iterator_impl!(IntegerDigitsIterator); |
| 1493 | skip_iterator_bytesiter_impl!( |
| 1494 | IntegerDigitsIterator, |
| 1495 | INTEGER_DIGIT_SEPARATOR_FLAG_MASK, |
| 1496 | integer_count, |
| 1497 | INTEGER_INTERNAL_DIGIT_SEPARATOR, |
| 1498 | INTEGER_LEADING_DIGIT_SEPARATOR, |
| 1499 | INTEGER_TRAILING_DIGIT_SEPARATOR, |
| 1500 | INTEGER_CONSECUTIVE_DIGIT_SEPARATOR |
| 1501 | ); |
| 1502 | |
| 1503 | // FRACTION DIGITS ITERATOR |
| 1504 | // ------------------------ |
| 1505 | |
| 1506 | skip_iterator!( |
| 1507 | FractionDigitsIterator, |
| 1508 | "Iterator that skips over digit separators in the fraction." |
| 1509 | ); |
| 1510 | skip_iterator_impl!(FractionDigitsIterator, mantissa_radix); |
| 1511 | skip_iterator_iterator_impl!(FractionDigitsIterator); |
| 1512 | skip_iterator_bytesiter_impl!( |
| 1513 | FractionDigitsIterator, |
| 1514 | FRACTION_DIGIT_SEPARATOR_FLAG_MASK, |
| 1515 | fraction_count, |
| 1516 | FRACTION_INTERNAL_DIGIT_SEPARATOR, |
| 1517 | FRACTION_LEADING_DIGIT_SEPARATOR, |
| 1518 | FRACTION_TRAILING_DIGIT_SEPARATOR, |
| 1519 | FRACTION_CONSECUTIVE_DIGIT_SEPARATOR |
| 1520 | ); |
| 1521 | |
| 1522 | // EXPONENT DIGITS ITERATOR |
| 1523 | // ------------------------ |
| 1524 | |
| 1525 | skip_iterator!( |
| 1526 | ExponentDigitsIterator, |
| 1527 | "Iterator that skips over digit separators in the exponent." |
| 1528 | ); |
| 1529 | skip_iterator_impl!(ExponentDigitsIterator, exponent_radix); |
| 1530 | skip_iterator_iterator_impl!(ExponentDigitsIterator); |
| 1531 | skip_iterator_bytesiter_impl!( |
| 1532 | ExponentDigitsIterator, |
| 1533 | EXPONENT_DIGIT_SEPARATOR_FLAG_MASK, |
| 1534 | exponent_count, |
| 1535 | EXPONENT_INTERNAL_DIGIT_SEPARATOR, |
| 1536 | EXPONENT_LEADING_DIGIT_SEPARATOR, |
| 1537 | EXPONENT_TRAILING_DIGIT_SEPARATOR, |
| 1538 | EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR |
| 1539 | ); |
| 1540 | |
| 1541 | // SPECIAL DIGITS ITERATOR |
| 1542 | // ----------------------- |
| 1543 | |
| 1544 | skip_iterator!( |
| 1545 | SpecialDigitsIterator, |
| 1546 | "Iterator that skips over digit separators in special floats." |
| 1547 | ); |
| 1548 | skip_iterator_iterator_impl!(SpecialDigitsIterator); |
| 1549 | |
| 1550 | impl<'a: 'b, 'b, const FORMAT: u128> SpecialDigitsIterator<'a, 'b, FORMAT> { |
| 1551 | is_sign!(); |
| 1552 | is_digit_separator!(FORMAT); |
| 1553 | } |
| 1554 | |
| 1555 | unsafe impl<'a: 'b, 'b, const FORMAT: u128> Iter<'a> for SpecialDigitsIterator<'a, 'b, FORMAT> { |
| 1556 | skip_iterator_iter_base!(FORMAT, SPECIAL_DIGIT_SEPARATOR, integer_count); |
| 1557 | } |
| 1558 | |
| 1559 | impl<'a: 'b, 'b, const FORMAT: u128> DigitsIter<'a> for SpecialDigitsIterator<'a, 'b, FORMAT> { |
| 1560 | skip_iterator_digits_iter_base!(); |
| 1561 | |
| 1562 | // Always a no-op. |
| 1563 | #[inline(always)] |
| 1564 | fn increment_count(&mut self) { |
| 1565 | } |
| 1566 | |
| 1567 | /// Peek the next value of the iterator, without consuming it. |
| 1568 | #[inline(always)] |
| 1569 | fn peek(&mut self) -> Option<<Self as Iterator>::Item> { |
| 1570 | let format = NumberFormat::<{ FORMAT }> {}; |
| 1571 | if format.special_digit_separator() { |
| 1572 | peek_iltc!(self) |
| 1573 | } else { |
| 1574 | peek_noskip!(self) |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | /// Determine if the character is a digit. |
| 1579 | #[inline(always)] |
| 1580 | fn is_digit(&self, value: u8) -> bool { |
| 1581 | let format = NumberFormat::<{ FORMAT }> {}; |
| 1582 | char_is_digit_const(value, format.mantissa_radix()) |
| 1583 | } |
| 1584 | } |