regex.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "bits/regex_defs.h"
  8. #include <stddef.h>
  9. #include <sys/cdefs.h>
  10. #include <sys/types.h>
  11. __BEGIN_DECLS
  12. typedef ssize_t regoff_t;
  13. typedef struct {
  14. void* __data;
  15. // Number of capture groups, Dr.POSIX requires this.
  16. size_t re_nsub;
  17. } regex_t;
  18. enum ReError {
  19. REG_NOERR = __Regex_NoError,
  20. REG_BADPAT = __Regex_InvalidPattern, // Invalid regular expression.
  21. REG_ECOLLATE = __Regex_InvalidCollationElement, // Invalid collating element referenced.
  22. REG_ECTYPE = __Regex_InvalidCharacterClass, // Invalid character class type referenced.
  23. REG_EESCAPE = __Regex_InvalidTrailingEscape, // Trailing \ in pattern.
  24. REG_ESUBREG = __Regex_InvalidNumber, // Number in \digit invalid or in error.
  25. REG_EBRACK = __Regex_MismatchingBracket, // [ ] imbalance.
  26. REG_EPAREN = __Regex_MismatchingParen, // \( \) or ( ) imbalance.
  27. REG_EBRACE = __Regex_MismatchingBrace, // \{ \} imbalance.
  28. REG_BADBR = __Regex_InvalidBraceContent, // Content of \{ \} invalid: not a number, number too large, more than two numbers, first larger than second.
  29. REG_ERANGE = __Regex_InvalidRange, // Invalid endpoint in range expression.
  30. REG_BADRPT = __Regex_InvalidRepetitionMarker, // ?, * or + not preceded by valid regular expression.
  31. REG_EMPTY_EXPR = __Regex_EmptySubExpression, // Empty expression
  32. REG_ENOSYS, // The implementation does not support the function.
  33. REG_ESPACE, // Out of memory.
  34. REG_NOMATCH, // regexec() failed to match.
  35. };
  36. typedef struct {
  37. regoff_t rm_so; // byte offset from start of string to start of substring
  38. regoff_t rm_eo; // byte offset from start of string of the first character after the end of substring
  39. regoff_t rm_cnt; // number of matches
  40. } regmatch_t;
  41. // Values for the cflags parameter to the regcomp() function:
  42. #define REG_EXTENDED __Regex_Extended // Use Extended Regular Expressions.
  43. #define REG_ICASE __Regex_Insensitive // Ignore case in match.
  44. #define REG_NOSUB __Regex_SkipSubExprResults // Report only success or fail in regexec().
  45. #define REG_GLOBAL __Regex_Global // Don't stop searching for more match
  46. #define REG_NEWLINE (__Regex_Multiline | REG_GLOBAL) // Change the handling of newline.
  47. // Values for the eflags parameter to the regexec() function:
  48. #define REG_NOTBOL __Regex_MatchNotBeginOfLine // The circumflex character (^), when taken as a special character, will not match the beginning of string.
  49. #define REG_NOTEOL __Regex_MatchNotEndOfLine // The dollar sign ($), when taken as a special character, will not match the end of string.
  50. #define REG_SEARCH __Regex_Last << 1
  51. int regcomp(regex_t*, char const*, int);
  52. int regexec(regex_t const*, char const*, size_t, regmatch_t[], int);
  53. size_t regerror(int, regex_t const*, char*, size_t);
  54. void regfree(regex_t*);
  55. __END_DECLS