charclass.c 868 B

123456789101112131415161718192021222324252627282930313233
  1. #include "re1.5.h"
  2. int _re1_5_classmatch(const char *pc, const char *sp)
  3. {
  4. // pc points to "cnt" byte after opcode
  5. int is_positive = (pc[-1] == Class);
  6. int cnt = *pc++;
  7. while (cnt--) {
  8. if (*sp >= *pc && *sp <= pc[1]) return is_positive;
  9. pc += 2;
  10. }
  11. return !is_positive;
  12. }
  13. int _re1_5_namedclassmatch(const char *pc, const char *sp)
  14. {
  15. // pc points to name of class
  16. int off = (*pc >> 5) & 1;
  17. if ((*pc | 0x20) == 'd') {
  18. if (!(*sp >= '0' && *sp <= '9')) {
  19. off ^= 1;
  20. }
  21. } else if ((*pc | 0x20) == 's') {
  22. if (!(*sp == ' ' || (*sp >= '\t' && *sp <= '\r'))) {
  23. off ^= 1;
  24. }
  25. } else { // w
  26. if (!((*sp >= 'A' && *sp <= 'Z') || (*sp >= 'a' && *sp <= 'z') || (*sp >= '0' && *sp <= '9') || *sp == '_')) {
  27. off ^= 1;
  28. }
  29. }
  30. return off;
  31. }