memspan 0.1.0

SIMD-accelerated byte-class scanning for lexers and parsers. Backends: AVX-512, AVX2, SSE4.1, NEON, WASM SIMD128. no_std compatible.
Documentation
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
use crate::Needles;

#[cfg(target_arch = "aarch64")]
use crate::utils::neon_available;

#[cfg(target_arch = "aarch64")]
pub(crate) mod neon;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use crate::utils::sse42_available;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub(crate) mod sse42;

#[cfg(target_arch = "x86_64")]
use crate::utils::{avx2_available, avx512bw_available};

#[cfg(target_arch = "x86_64")]
pub(crate) mod avx2;

#[cfg(target_arch = "x86_64")]
pub(crate) mod avx512;

#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
pub(crate) mod simd128;

// ── scalar predicates ────────────────────────────────────────────────────────

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_binary_digit(byte: u8) -> bool {
  byte == b'0' || byte == b'1'
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_octal_digit(byte: u8) -> bool {
  matches!(byte, b'0'..=b'7')
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_digit(byte: u8) -> bool {
  byte.is_ascii_digit()
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_hex_digit(byte: u8) -> bool {
  let lower = byte | 0x20;
  is_digit(byte) || matches!(lower, b'a'..=b'f')
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_alpha(byte: u8) -> bool {
  (byte | 0x20).is_ascii_lowercase()
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_alphanumeric(byte: u8) -> bool {
  is_alpha(byte) || is_digit(byte)
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_ident_start(byte: u8) -> bool {
  is_alpha(byte) || byte == b'_'
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_ident(byte: u8) -> bool {
  is_alphanumeric(byte) || byte == b'_'
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_whitespace(byte: u8) -> bool {
  matches!(byte, b' ' | b'\t' | b'\n' | b'\r')
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn prefix_len_by(input: &[u8], is_match: impl Fn(u8) -> bool) -> usize {
  input
    .iter()
    .position(|&byte| !is_match(byte))
    .unwrap_or(input.len())
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_binary(input: &[u8]) -> usize {
  prefix_len_by(input, is_binary_digit)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_octal_digits(input: &[u8]) -> usize {
  prefix_len_by(input, is_octal_digit)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_digits(input: &[u8]) -> usize {
  prefix_len_by(input, is_digit)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_hex_digits(input: &[u8]) -> usize {
  prefix_len_by(input, is_hex_digit)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_alpha(input: &[u8]) -> usize {
  prefix_len_by(input, is_alpha)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_alphanumeric(input: &[u8]) -> usize {
  prefix_len_by(input, is_alphanumeric)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_ident_start(input: &[u8]) -> usize {
  prefix_len_by(input, is_ident_start)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_ident(input: &[u8]) -> usize {
  prefix_len_by(input, is_ident)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_whitespace(input: &[u8]) -> usize {
  prefix_len_by(input, is_whitespace)
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_lower(byte: u8) -> bool {
  byte.is_ascii_lowercase()
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_upper(byte: u8) -> bool {
  byte.is_ascii_uppercase()
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_ascii_byte(byte: u8) -> bool {
  byte.is_ascii()
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_non_ascii(byte: u8) -> bool {
  !byte.is_ascii()
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_ascii_graphic(byte: u8) -> bool {
  matches!(byte, 0x21..=0x7E)
}

#[cfg_attr(not(tarpaulin), inline(always))]
fn is_ascii_control(byte: u8) -> bool {
  matches!(byte, 0x00..=0x1F | 0x7F)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_lower(input: &[u8]) -> usize {
  prefix_len_by(input, is_lower)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_upper(input: &[u8]) -> usize {
  prefix_len_by(input, is_upper)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_ascii(input: &[u8]) -> usize {
  prefix_len_by(input, is_ascii_byte)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_non_ascii(input: &[u8]) -> usize {
  prefix_len_by(input, is_non_ascii)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_ascii_graphic(input: &[u8]) -> usize {
  prefix_len_by(input, is_ascii_graphic)
}

#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn prefix_len_ascii_control(input: &[u8]) -> usize {
  prefix_len_by(input, is_ascii_control)
}

// ── x86/x86_64 dispatch helpers ──────────────────────────────────────────────

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn dispatch_skip_until_x86<Nd: Needles>(input: &[u8], needles: Nd) -> Option<usize> {
  #[cfg(target_arch = "x86_64")]
  if avx512bw_available() {
    if input.len() >= 64 {
      return unsafe { avx512::skip_until(input, needles) };
    }
    if avx2_available() && input.len() >= 32 {
      return unsafe { avx2::skip_until(input, needles) };
    }
  } else if avx2_available() && input.len() >= 32 {
    return unsafe { avx2::skip_until(input, needles) };
  }
  if sse42_available() {
    return unsafe { sse42::skip_until(input, needles) };
  }
  needles.tail_find(input)
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn dispatch_skip_while_x86<Nd: Needles>(input: &[u8], needles: Nd) -> usize {
  #[cfg(target_arch = "x86_64")]
  if avx512bw_available() {
    if input.len() >= 64 {
      return unsafe { avx512::skip_while(input, needles) };
    }
    if avx2_available() && input.len() >= 32 {
      return unsafe { avx2::skip_while(input, needles) };
    }
  } else if avx2_available() && input.len() >= 32 {
    return unsafe { avx2::skip_while(input, needles) };
  }
  if sse42_available() {
    return unsafe { sse42::skip_while(input, needles) };
  }
  needles.prefix_len(input)
}

fn count_matches_scalar<Nd: Needles>(input: &[u8], needles: Nd) -> usize {
  input
    .iter()
    .filter(|&&b| needles.tail_find(core::slice::from_ref(&b)).is_some())
    .count()
}

fn find_last_scalar<Nd: Needles>(input: &[u8], needles: Nd) -> Option<usize> {
  let mut last = None;
  for (i, &b) in input.iter().enumerate() {
    if needles.tail_find(core::slice::from_ref(&b)).is_some() {
      last = Some(i);
    }
  }
  last
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn dispatch_count_matches_x86<Nd: Needles>(input: &[u8], needles: Nd) -> usize {
  #[cfg(target_arch = "x86_64")]
  {
    if avx512bw_available() {
      return unsafe { avx512::count_matches(input, needles) };
    }
    if avx2_available() {
      return unsafe { avx2::count_matches(input, needles) };
    }
  }
  if sse42_available() {
    return unsafe { sse42::count_matches(input, needles) };
  }
  count_matches_scalar(input, needles)
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn dispatch_find_last_x86<Nd: Needles>(input: &[u8], needles: Nd) -> Option<usize> {
  #[cfg(target_arch = "x86_64")]
  {
    if avx512bw_available() {
      return unsafe { avx512::find_last(input, needles) };
    }
    if avx2_available() {
      return unsafe { avx2::find_last(input, needles) };
    }
  }
  if sse42_available() {
    return unsafe { sse42::find_last(input, needles) };
  }
  find_last_scalar(input, needles)
}

/// Selects the right SIMD tier for a specialized ASCII-class function on x86/x86_64.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
macro_rules! x86_class_dispatch {
  ($input:expr, $scalar:ident, $sse42_fn:path, $avx2_fn:path, $avx512_fn:path) => {{
    #[cfg(target_arch = "x86_64")]
    if avx512bw_available() {
      if $input.len() >= 64 {
        return unsafe { $avx512_fn($input) };
      }
      if avx2_available() && $input.len() >= 32 {
        return unsafe { $avx2_fn($input) };
      }
    } else if avx2_available() && $input.len() >= 32 {
      return unsafe { $avx2_fn($input) };
    }
    if sse42_available() {
      return unsafe { $sse42_fn($input) };
    }
    $scalar($input)
  }};
}

/// Returns the index of the first byte in `input` that matches any of `needles`.
///
/// Dispatches to AVX-512BW / AVX2 / SSE4.2 (x86_64), NEON (aarch64), or
/// WASM SIMD128 (wasm32) depending on what the CPU supports at runtime.
/// Falls back to a scalar loop on unsupported targets or when SIMD is
/// disabled via `memspan_force_scalar`.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_until<Nd>(input: &[u8], needles: Nd) -> Option<usize>
where
  Nd: Needles,
{
  cfg_select! {
    target_arch = "aarch64" => {
      if needles.needle_count() == 0 { return None; }
      if input.len() < 16 { return needles.tail_find(input); }
      if neon_available() { return neon::skip_until(input, needles); }
      needles.tail_find(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      if needles.needle_count() == 0 { return None; }
      if input.len() < 16 { return needles.tail_find(input); }
      dispatch_skip_until_x86(input, needles)
    }
    target_arch = "wasm32" => {
      if needles.needle_count() == 0 { return None; }
      if input.len() < 16 { return needles.tail_find(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_until(input, needles); }
      needles.tail_find(input)
    }
    _ => {
      if needles.needle_count() == 0 { return None; }
      needles.tail_find(input)
    }
  }
}

/// Returns the number of leading bytes in `input` that match any of `needles`.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_while<Nd>(input: &[u8], needles: Nd) -> usize
where
  Nd: Needles,
{
  cfg_select! {
    target_arch = "aarch64" => {
      let count = needles.needle_count();
      if count <= 1 || input.len() < 32 { return needles.prefix_len(input); }
      if neon_available() { return neon::skip_while(input, needles); }
      needles.prefix_len(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      let count = needles.needle_count();
      if count <= 1 || input.len() < 16 { return needles.prefix_len(input); }
      dispatch_skip_while_x86(input, needles)
    }
    target_arch = "wasm32" => {
      let count = needles.needle_count();
      if count <= 1 || input.len() < 16 { return needles.prefix_len(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_while(input, needles); }
      needles.prefix_len(input)
    }
    _ => {
      needles.prefix_len(input)
    }
  }
}

/// Returns the length of the leading ASCII binary-digit prefix (`0` or `1`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_binary(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_binary(input); }
      if neon_available() { return neon::skip_binary(input); }
      prefix_len_binary(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_binary, sse42::skip_binary, avx2::skip_binary, avx512::skip_binary)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_binary(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_binary(input); }
      prefix_len_binary(input)
    }
    _ => { prefix_len_binary(input) }
  }
}

/// Returns the length of the leading ASCII decimal-digit prefix (`0..=9`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_digits(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_digits(input); }
      if neon_available() { return neon::skip_digits(input); }
      prefix_len_digits(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_digits, sse42::skip_digits, avx2::skip_digits, avx512::skip_digits)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_digits(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_digits(input); }
      prefix_len_digits(input)
    }
    _ => { prefix_len_digits(input) }
  }
}

/// Returns the length of the leading ASCII hexadecimal-digit prefix.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_hex_digits(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_hex_digits(input); }
      if neon_available() { return neon::skip_hex_digits(input); }
      prefix_len_hex_digits(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_hex_digits, sse42::skip_hex_digits, avx2::skip_hex_digits, avx512::skip_hex_digits)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_hex_digits(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_hex_digits(input); }
      prefix_len_hex_digits(input)
    }
    _ => { prefix_len_hex_digits(input) }
  }
}

/// Returns the length of the leading ASCII octal-digit prefix (`0..=7`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_octal_digits(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_octal_digits(input); }
      if neon_available() { return neon::skip_octal_digits(input); }
      prefix_len_octal_digits(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_octal_digits, sse42::skip_octal_digits, avx2::skip_octal_digits, avx512::skip_octal_digits)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_octal_digits(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_octal_digits(input); }
      prefix_len_octal_digits(input)
    }
    _ => { prefix_len_octal_digits(input) }
  }
}

/// Returns the length of the leading ASCII whitespace prefix.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_whitespace(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_whitespace(input); }
      if neon_available() { return neon::skip_whitespace(input); }
      prefix_len_whitespace(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_whitespace, sse42::skip_whitespace, avx2::skip_whitespace, avx512::skip_whitespace)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_whitespace(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_whitespace(input); }
      prefix_len_whitespace(input)
    }
    _ => { prefix_len_whitespace(input) }
  }
}

/// Returns the length of the leading ASCII alphabetic prefix.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_alpha(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_alpha(input); }
      if neon_available() { return neon::skip_alpha(input); }
      prefix_len_alpha(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_alpha, sse42::skip_alpha, avx2::skip_alpha, avx512::skip_alpha)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_alpha(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_alpha(input); }
      prefix_len_alpha(input)
    }
    _ => { prefix_len_alpha(input) }
  }
}

/// Returns the length of the leading ASCII alphanumeric prefix.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_alphanumeric(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_alphanumeric(input); }
      if neon_available() { return neon::skip_alphanumeric(input); }
      prefix_len_alphanumeric(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_alphanumeric, sse42::skip_alphanumeric, avx2::skip_alphanumeric, avx512::skip_alphanumeric)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_alphanumeric(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_alphanumeric(input); }
      prefix_len_alphanumeric(input)
    }
    _ => { prefix_len_alphanumeric(input) }
  }
}

/// Returns the length of the leading C-style identifier-start prefix.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_ident_start(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_ident_start(input); }
      if neon_available() { return neon::skip_ident_start(input); }
      prefix_len_ident_start(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_ident_start, sse42::skip_ident_start, avx2::skip_ident_start, avx512::skip_ident_start)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_ident_start(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_ident_start(input); }
      prefix_len_ident_start(input)
    }
    _ => { prefix_len_ident_start(input) }
  }
}

/// Returns the length of the leading C-style identifier-continuation prefix.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_ident(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_ident(input); }
      if neon_available() { return neon::skip_ident(input); }
      prefix_len_ident(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_ident, sse42::skip_ident, avx2::skip_ident, avx512::skip_ident)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_ident(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_ident(input); }
      prefix_len_ident(input)
    }
    _ => { prefix_len_ident(input) }
  }
}

/// Returns the length of the leading ASCII lowercase prefix (`a..=z`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_lower(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_lower(input); }
      if neon_available() { return neon::skip_lower(input); }
      prefix_len_lower(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_lower, sse42::skip_lower, avx2::skip_lower, avx512::skip_lower)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_lower(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_lower(input); }
      prefix_len_lower(input)
    }
    _ => { prefix_len_lower(input) }
  }
}

/// Returns the length of the leading ASCII uppercase prefix (`A..=Z`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_upper(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_upper(input); }
      if neon_available() { return neon::skip_upper(input); }
      prefix_len_upper(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_upper, sse42::skip_upper, avx2::skip_upper, avx512::skip_upper)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_upper(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_upper(input); }
      prefix_len_upper(input)
    }
    _ => { prefix_len_upper(input) }
  }
}

/// Returns the length of the leading ASCII byte prefix (bytes `0x00..=0x7F`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_ascii(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_ascii(input); }
      if neon_available() { return neon::skip_ascii(input); }
      prefix_len_ascii(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_ascii, sse42::skip_ascii, avx2::skip_ascii, avx512::skip_ascii)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_ascii(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_ascii(input); }
      prefix_len_ascii(input)
    }
    _ => { prefix_len_ascii(input) }
  }
}

/// Returns the length of the leading non-ASCII byte prefix (bytes `0x80..=0xFF`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_non_ascii(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_non_ascii(input); }
      if neon_available() { return neon::skip_non_ascii(input); }
      prefix_len_non_ascii(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_non_ascii, sse42::skip_non_ascii, avx2::skip_non_ascii, avx512::skip_non_ascii)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_non_ascii(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_non_ascii(input); }
      prefix_len_non_ascii(input)
    }
    _ => { prefix_len_non_ascii(input) }
  }
}

/// Returns the length of the leading ASCII graphic character prefix (`0x21..=0x7E`,
/// i.e. printable non-space characters).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_ascii_graphic(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_ascii_graphic(input); }
      if neon_available() { return neon::skip_ascii_graphic(input); }
      prefix_len_ascii_graphic(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_ascii_graphic, sse42::skip_ascii_graphic, avx2::skip_ascii_graphic, avx512::skip_ascii_graphic)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_ascii_graphic(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_ascii_graphic(input); }
      prefix_len_ascii_graphic(input)
    }
    _ => { prefix_len_ascii_graphic(input) }
  }
}

/// Returns the length of the leading ASCII control character prefix
/// (`0x00..=0x1F` and `0x7F`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_ascii_control(input: &[u8]) -> usize {
  cfg_select! {
    target_arch = "aarch64" => {
      if input.len() < 32 { return prefix_len_ascii_control(input); }
      if neon_available() { return neon::skip_ascii_control(input); }
      prefix_len_ascii_control(input)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      x86_class_dispatch!(input, prefix_len_ascii_control, sse42::skip_ascii_control, avx2::skip_ascii_control, avx512::skip_ascii_control)
    }
    target_arch = "wasm32" => {
      if input.len() < 16 { return prefix_len_ascii_control(input); }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::skip_ascii_control(input); }
      prefix_len_ascii_control(input)
    }
    _ => { prefix_len_ascii_control(input) }
  }
}

/// Returns the number of bytes in `input` that match any of `needles`.
///
/// Unlike [`skip_until`] this never returns early — every byte is examined and
/// matching bytes are counted via SIMD popcount (`count_ones` on the bitmask).
/// Useful for counting newlines to build line-number tables, counting delimiter
/// occurrences, etc.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn count_matches<Nd>(input: &[u8], needles: Nd) -> usize
where
  Nd: Needles,
{
  cfg_select! {
    target_arch = "aarch64" => {
      if neon_available() { return neon::count_matches(input, needles); }
      count_matches_scalar(input, needles)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      dispatch_count_matches_x86(input, needles)
    }
    target_arch = "wasm32" => {
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::count_matches(input, needles); }
      count_matches_scalar(input, needles)
    }
    _ => { count_matches_scalar(input, needles) }
  }
}

/// Returns the index of the **last** byte in `input` that matches any of
/// `needles`, or `None` if no byte matches.
///
/// Scans the entire input front-to-back, accumulating the rightmost match
/// position using SIMD bitmask `leading_zeros` to find the last set bit in
/// each chunk. The SIMD backends are the same as those used by [`skip_until`].
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn find_last<Nd>(input: &[u8], needles: Nd) -> Option<usize>
where
  Nd: Needles,
{
  cfg_select! {
    target_arch = "aarch64" => {
      if needles.needle_count() == 0 { return None; }
      if neon_available() { return neon::find_last(input, needles); }
      find_last_scalar(input, needles)
    }
    any(target_arch = "x86", target_arch = "x86_64") => {
      if needles.needle_count() == 0 { return None; }
      dispatch_find_last_x86(input, needles)
    }
    target_arch = "wasm32" => {
      if needles.needle_count() == 0 { return None; }
      #[cfg(target_feature = "simd128")]
      if crate::utils::simd128_available() { return simd128::find_last(input, needles); }
      find_last_scalar(input, needles)
    }
    _ => {
      if needles.needle_count() == 0 { return None; }
      find_last_scalar(input, needles)
    }
  }
}

/// Returns the number of bytes before the first `\n`, or `input.len()` if
/// there is no newline. Equivalent to `skip_until(input, b'\n').unwrap_or(input.len())`.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn skip_until_newline(input: &[u8]) -> usize {
  skip_until(input, b'\n').unwrap_or(input.len())
}

/// Returns `true` if any byte in `input` matches any of `needles`.
///
/// This is `skip_until(input, needles).is_some()` with a cleaner call-site name.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn contains_any<Nd: Needles>(input: &[u8], needles: Nd) -> bool {
  skip_until(input, needles).is_some()
}