regex.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <stddef.h>
  28. #include <sys/types.h>
  29. __BEGIN_DECLS
  30. typedef ssize_t regoff_t;
  31. struct regex_t {
  32. void* __data;
  33. };
  34. enum __Regex_Error {
  35. __Regex_NoError,
  36. __Regex_InvalidPattern, // Invalid regular expression.
  37. __Regex_InvalidCollationElement, // Invalid collating element referenced.
  38. __Regex_InvalidCharacterClass, // Invalid character class type referenced.
  39. __Regex_InvalidTrailingEscape, // Trailing \ in pattern.
  40. __Regex_InvalidNumber, // Number in \digit invalid or in error.
  41. __Regex_MismatchingBracket, // [ ] imbalance.
  42. __Regex_MismatchingParen, // ( ) imbalance.
  43. __Regex_MismatchingBrace, // { } imbalance.
  44. __Regex_InvalidBraceContent, // Content of {} invalid: not a number, number too large, more than two numbers, first larger than second.
  45. __Regex_InvalidBracketContent, // Content of [] invalid.
  46. __Regex_InvalidRange, // Invalid endpoint in range expression.
  47. __Regex_InvalidRepetitionMarker, // ?, * or + not preceded by valid regular expression.
  48. __Regex_ReachedMaxRecursion, // MaximumRecursion has been reached.
  49. __Regex_EmptySubExpression, // Sub expression has empty content.
  50. __Regex_InvalidCaptureGroup, // Content of capture group is invalid.
  51. __Regex_InvalidNameForCaptureGroup, // Name of capture group is invalid.
  52. };
  53. enum ReError {
  54. REG_NOERR = __Regex_NoError,
  55. REG_BADPAT = __Regex_InvalidPattern, // Invalid regular expression.
  56. REG_ECOLLATE = __Regex_InvalidCollationElement, // Invalid collating element referenced.
  57. REG_ECTYPE = __Regex_InvalidCharacterClass, // Invalid character class type referenced.
  58. REG_EESCAPE = __Regex_InvalidTrailingEscape, // Trailing \ in pattern.
  59. REG_ESUBREG = __Regex_InvalidNumber, // Number in \digit invalid or in error.
  60. REG_EBRACK = __Regex_MismatchingBracket, // [ ] imbalance.
  61. REG_EPAREN = __Regex_MismatchingParen, // \( \) or ( ) imbalance.
  62. REG_EBRACE = __Regex_MismatchingBrace, // \{ \} imbalance.
  63. REG_BADBR = __Regex_InvalidBraceContent, // Content of \{ \} invalid: not a number, number too large, more than two numbers, first larger than second.
  64. REG_ERANGE = __Regex_InvalidRange, // Invalid endpoint in range expression.
  65. REG_BADRPT = __Regex_InvalidRepetitionMarker, // ?, * or + not preceded by valid regular expression.
  66. REG_EMPTY_EXPR = __Regex_EmptySubExpression, // Empty expression
  67. REG_ENOSYS, // The implementation does not support the function.
  68. REG_ESPACE, // Out of memory.
  69. REG_NOMATCH, // regexec() failed to match.
  70. };
  71. struct regmatch_t {
  72. regoff_t rm_so; // byte offset from start of string to start of substring
  73. regoff_t rm_eo; // byte offset from start of string of the first character after the end of substring
  74. regoff_t rm_cnt; // number of matches
  75. };
  76. enum __RegexAllFlags {
  77. __Regex_Global = 1, // All matches (don't return after first match)
  78. __Regex_Insensitive = __Regex_Global << 1, // Case insensitive match (ignores case of [a-zA-Z])
  79. __Regex_Ungreedy = __Regex_Global << 2, // The match becomes lazy by default. Now a ? following a quantifier makes it greedy
  80. __Regex_Unicode = __Regex_Global << 3, // Enable all unicode features and interpret all unicode escape sequences as such
  81. __Regex_Extended = __Regex_Global << 4, // Ignore whitespaces. Spaces and text after a # in the pattern are ignored
  82. __Regex_Extra = __Regex_Global << 5, // Disallow meaningless escapes. A \ followed by a letter with no special meaning is faulted
  83. __Regex_MatchNotBeginOfLine = __Regex_Global << 6, // Pattern is not forced to ^ -> search in whole string!
  84. __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
  85. __Regex_SkipSubExprResults = __Regex_Global << 8, // Do not return sub expressions in the result
  86. __Regex_StringCopyMatches = __Regex_Global << 9, // Do explicitly copy results into new allocated string instead of StringView to original string.
  87. __Regex_SingleLine = __Regex_Global << 10, // Dot matches newline characters
  88. __Regex_Sticky = __Regex_Global << 11, // Force the pattern to only match consecutive matches from where the previous match ended.
  89. __Regex_Multiline = __Regex_Global << 12, // Handle newline characters. Match each line, one by one.
  90. __Regex_SkipTrimEmptyMatches = __Regex_Global << 13, // Do not remove empty capture group results.
  91. __Regex_Internal_Stateful = __Regex_Global << 14, // Internal flag; enables stateful matches.
  92. __Regex_Internal_BrowserExtended = __Regex_Global << 15, // Internal flag; enable browser-specific ECMA262 extensions.
  93. __Regex_Last = __Regex_SkipTrimEmptyMatches
  94. };
  95. // Values for the cflags parameter to the regcomp() function:
  96. #define REG_EXTENDED __Regex_Extended // Use Extended Regular Expressions.
  97. #define REG_ICASE __Regex_Insensitive // Ignore case in match.
  98. #define REG_NOSUB __Regex_SkipSubExprResults // Report only success or fail in regexec().
  99. #define REG_GLOBAL __Regex_Global // Don't stop searching for more match
  100. #define REG_NEWLINE (__Regex_Multiline | REG_GLOBAL) // Change the handling of newline.
  101. // Values for the eflags parameter to the regexec() function:
  102. #define REG_NOTBOL __Regex_MatchNotBeginOfLine // The circumflex character (^), when taken as a special character, will not match the beginning of string.
  103. #define REG_NOTEOL __Regex_MatchNotEndOfLine // The dollar sign ($), when taken as a special character, will not match the end of string.
  104. //static_assert (sizeof(FlagsUnderlyingType) * 8 >= regex::POSIXFlags::Last << 1), "flags type too small")
  105. #define REG_SEARCH __Regex_Last << 1
  106. int regcomp(regex_t*, const char*, int);
  107. int regexec(const regex_t*, const char*, size_t, regmatch_t[], int);
  108. size_t regerror(int, const regex_t*, char*, size_t);
  109. void regfree(regex_t*);
  110. __END_DECLS