skip.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. // Copyright © 2019 Ariadne Devos
  3. /* sHT -- find a byte in a string */
  4. #include <limits.h>
  5. #include <stddef.h>
  6. #include <sHT/bitvec.h>
  7. #include <sHT/index.h>
  8. #include <sHT/lex.h>
  9. #include <sHT/test.h>
  10. size_t
  11. sHT_lex_skip(const unsigned char from[], size_t n, const struct sHT_lex_type *c, void *x)
  12. {
  13. /* TODO: word-at-a-time */
  14. /* This can be assigned to the return register */
  15. size_t i;
  16. /* TODO: variant which always performs one iteration
  17. (less branching, shorter code) */
  18. sHT_index_iterate(i, n) {
  19. /* bounds: @var{sHT_index_iterate} */
  20. unsigned char b = from[i];
  21. if (sHT_bit_test(c->c_allow, b))
  22. continue;
  23. /* A syntax error or the terminator. */
  24. /* 1: terminator, 0: syntax error */
  25. int which = sHT_eq_bool(c->c_stop, b);
  26. /* In any case, pass the number of skipped/parsed bytes,
  27. not the index of the last. */
  28. i++;
  29. return c->cb_skip_done[which](i, x);
  30. }
  31. /* ‘The detection may speculatively be incorrect.’
  32. Any of @var{i} and @var{n} would do, but @var{i} produced
  33. smaller code on x86-64 SystemV (153 < 155). */
  34. return i;
  35. }