regex.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <stddef.h>
  8. #include <sys/types.h>
  9. __BEGIN_DECLS
  10. typedef ssize_t regoff_t;
  11. typedef struct {
  12. void* __data;
  13. // Number of capture groups, Dr.POSIX requires this.
  14. size_t re_nsub;
  15. } regex_t;
  16. enum __Regex_Error {
  17. __Regex_NoError,
  18. __Regex_InvalidPattern, // Invalid regular expression.
  19. __Regex_InvalidCollationElement, // Invalid collating element referenced.
  20. __Regex_InvalidCharacterClass, // Invalid character class type referenced.
  21. __Regex_InvalidTrailingEscape, // Trailing \ in pattern.
  22. __Regex_InvalidNumber, // Number in \digit invalid or in error.
  23. __Regex_MismatchingBracket, // [ ] imbalance.
  24. __Regex_MismatchingParen, // ( ) imbalance.
  25. __Regex_MismatchingBrace, // { } imbalance.
  26. __Regex_InvalidBraceContent, // Content of {} invalid: not a number, number too large, more than two numbers, first larger than second.
  27. __Regex_InvalidBracketContent, // Content of [] invalid.
  28. __Regex_InvalidRange, // Invalid endpoint in range expression.
  29. __Regex_InvalidRepetitionMarker, // ?, * or + not preceded by valid regular expression.
  30. __Regex_ReachedMaxRecursion, // MaximumRecursion has been reached.
  31. __Regex_EmptySubExpression, // Sub expression has empty content.
  32. __Regex_InvalidCaptureGroup, // Content of capture group is invalid.
  33. __Regex_InvalidNameForCaptureGroup, // Name of capture group is invalid.
  34. __Regex_InvalidNameForProperty, // Name of property is invalid.
  35. };
  36. enum ReError {
  37. REG_NOERR = __Regex_NoError,
  38. REG_BADPAT = __Regex_InvalidPattern, // Invalid regular expression.
  39. REG_ECOLLATE = __Regex_InvalidCollationElement, // Invalid collating element referenced.
  40. REG_ECTYPE = __Regex_InvalidCharacterClass, // Invalid character class type referenced.
  41. REG_EESCAPE = __Regex_InvalidTrailingEscape, // Trailing \ in pattern.
  42. REG_ESUBREG = __Regex_InvalidNumber, // Number in \digit invalid or in error.
  43. REG_EBRACK = __Regex_MismatchingBracket, // [ ] imbalance.
  44. REG_EPAREN = __Regex_MismatchingParen, // \( \) or ( ) imbalance.
  45. REG_EBRACE = __Regex_MismatchingBrace, // \{ \} imbalance.
  46. REG_BADBR = __Regex_InvalidBraceContent, // Content of \{ \} invalid: not a number, number too large, more than two numbers, first larger than second.
  47. REG_ERANGE = __Regex_InvalidRange, // Invalid endpoint in range expression.
  48. REG_BADRPT = __Regex_InvalidRepetitionMarker, // ?, * or + not preceded by valid regular expression.
  49. REG_EMPTY_EXPR = __Regex_EmptySubExpression, // Empty expression
  50. REG_ENOSYS, // The implementation does not support the function.
  51. REG_ESPACE, // Out of memory.
  52. REG_NOMATCH, // regexec() failed to match.
  53. };
  54. typedef struct {
  55. regoff_t rm_so; // byte offset from start of string to start of substring
  56. regoff_t rm_eo; // byte offset from start of string of the first character after the end of substring
  57. regoff_t rm_cnt; // number of matches
  58. } regmatch_t;
  59. enum __RegexAllFlags {
  60. __Regex_Global = 1, // All matches (don't return after first match)
  61. __Regex_Insensitive = __Regex_Global << 1, // Case insensitive match (ignores case of [a-zA-Z])
  62. __Regex_Ungreedy = __Regex_Global << 2, // The match becomes lazy by default. Now a ? following a quantifier makes it greedy
  63. __Regex_Unicode = __Regex_Global << 3, // Enable all unicode features and interpret all unicode escape sequences as such
  64. __Regex_Extended = __Regex_Global << 4, // Ignore whitespaces. Spaces and text after a # in the pattern are ignored
  65. __Regex_Extra = __Regex_Global << 5, // Disallow meaningless escapes. A \ followed by a letter with no special meaning is faulted
  66. __Regex_MatchNotBeginOfLine = __Regex_Global << 6, // Pattern is not forced to ^ -> search in whole string!
  67. __Regex_MatchNotEndOfLine = __Regex_Global << 7, // Don't Force the dollar sign, $, to always match end of the string, instead of end of the line. This option is ignored if the Multiline-flag is set
  68. __Regex_SkipSubExprResults = __Regex_Global << 8, // Do not return sub expressions in the result
  69. __Regex_StringCopyMatches = __Regex_Global << 9, // Do explicitly copy results into new allocated string instead of StringView to original string.
  70. __Regex_SingleLine = __Regex_Global << 10, // Dot matches newline characters
  71. __Regex_Sticky = __Regex_Global << 11, // Force the pattern to only match consecutive matches from where the previous match ended.
  72. __Regex_Multiline = __Regex_Global << 12, // Handle newline characters. Match each line, one by one.
  73. __Regex_SkipTrimEmptyMatches = __Regex_Global << 13, // Do not remove empty capture group results.
  74. __Regex_Internal_Stateful = __Regex_Global << 14, // Internal flag; enables stateful matches.
  75. __Regex_Internal_BrowserExtended = __Regex_Global << 15, // Internal flag; enable browser-specific ECMA262 extensions.
  76. __Regex_Last = __Regex_SkipTrimEmptyMatches
  77. };
  78. // Values for the cflags parameter to the regcomp() function:
  79. #define REG_EXTENDED __Regex_Extended // Use Extended Regular Expressions.
  80. #define REG_ICASE __Regex_Insensitive // Ignore case in match.
  81. #define REG_NOSUB __Regex_SkipSubExprResults // Report only success or fail in regexec().
  82. #define REG_GLOBAL __Regex_Global // Don't stop searching for more match
  83. #define REG_NEWLINE (__Regex_Multiline | REG_GLOBAL) // Change the handling of newline.
  84. // Values for the eflags parameter to the regexec() function:
  85. #define REG_NOTBOL __Regex_MatchNotBeginOfLine // The circumflex character (^), when taken as a special character, will not match the beginning of string.
  86. #define REG_NOTEOL __Regex_MatchNotEndOfLine // The dollar sign ($), when taken as a special character, will not match the end of string.
  87. //static_assert (sizeof(FlagsUnderlyingType) * 8 >= regex::POSIXFlags::Last << 1), "flags type too small")
  88. #define REG_SEARCH __Regex_Last << 1
  89. int regcomp(regex_t*, const char*, int);
  90. int regexec(const regex_t*, const char*, size_t, regmatch_t[], int);
  91. size_t regerror(int, const regex_t*, char*, size_t);
  92. void regfree(regex_t*);
  93. __END_DECLS