RegexOptions.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <AK/Types.h>
  8. #include <stdio.h>
  9. #ifdef __serenity__
  10. # include <bits/regex_defs.h>
  11. #else
  12. # include <LibC/bits/regex_defs.h>
  13. #endif
  14. namespace regex {
  15. using FlagsUnderlyingType = u32;
  16. enum class AllFlags {
  17. Global = __Regex_Global, // All matches (don't return after first match)
  18. Insensitive = __Regex_Insensitive, // Case insensitive match (ignores case of [a-zA-Z])
  19. Ungreedy = __Regex_Ungreedy, // The match becomes lazy by default. Now a ? following a quantifier makes it greedy
  20. Unicode = __Regex_Unicode, // Enable all unicode features and interpret all unicode escape sequences as such
  21. Extended = __Regex_Extended, // Ignore whitespaces. Spaces and text after a # in the pattern are ignored
  22. Extra = __Regex_Extra, // Disallow meaningless escapes. A \ followed by a letter with no special meaning is faulted
  23. MatchNotBeginOfLine = __Regex_MatchNotBeginOfLine, // Pattern is not forced to ^ -> search in whole string!
  24. MatchNotEndOfLine = __Regex_MatchNotEndOfLine, // 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
  25. SkipSubExprResults = __Regex_SkipSubExprResults, // Do not return sub expressions in the result
  26. StringCopyMatches = __Regex_StringCopyMatches, // Do explicitly copy results into new allocated string instead of StringView to original string.
  27. SingleLine = __Regex_SingleLine, // Dot matches newline characters
  28. Sticky = __Regex_Sticky, // Force the pattern to only match consecutive matches from where the previous match ended.
  29. Multiline = __Regex_Multiline, // Handle newline characters. Match each line, one by one.
  30. SkipTrimEmptyMatches = __Regex_SkipTrimEmptyMatches, // Do not remove empty capture group results.
  31. SingleMatch = __Regex_SingleMatch, // Stop after acquiring a single match.
  32. UnicodeSets = __Regex_UnicodeSets, // Only for ECMA262, Allow set operations in character classes.
  33. Internal_Stateful = __Regex_Internal_Stateful, // Make global matches match one result at a time, and further match() calls on the same instance continue where the previous one left off.
  34. Internal_BrowserExtended = __Regex_Internal_BrowserExtended, // Only for ECMA262, Enable the behaviors defined in section B.1.4. of the ECMA262 spec.
  35. Internal_ConsiderNewline = __Regex_Internal_ConsiderNewline, // Only for ECMA262, Allow multiline matches to consider newlines as line boundaries.
  36. Last = Internal_BrowserExtended,
  37. };
  38. enum class PosixFlags : FlagsUnderlyingType {
  39. Global = (FlagsUnderlyingType)AllFlags::Global,
  40. Insensitive = (FlagsUnderlyingType)AllFlags::Insensitive,
  41. Ungreedy = (FlagsUnderlyingType)AllFlags::Ungreedy,
  42. Unicode = (FlagsUnderlyingType)AllFlags::Unicode,
  43. Extended = (FlagsUnderlyingType)AllFlags::Extended,
  44. Extra = (FlagsUnderlyingType)AllFlags::Extra,
  45. MatchNotBeginOfLine = (FlagsUnderlyingType)AllFlags::MatchNotBeginOfLine,
  46. MatchNotEndOfLine = (FlagsUnderlyingType)AllFlags::MatchNotEndOfLine,
  47. SkipSubExprResults = (FlagsUnderlyingType)AllFlags::SkipSubExprResults,
  48. SkipTrimEmptyMatches = (FlagsUnderlyingType)AllFlags::SkipTrimEmptyMatches,
  49. Multiline = (FlagsUnderlyingType)AllFlags::Multiline,
  50. StringCopyMatches = (FlagsUnderlyingType)AllFlags::StringCopyMatches,
  51. };
  52. enum class ECMAScriptFlags : FlagsUnderlyingType {
  53. Global = (FlagsUnderlyingType)AllFlags::Global | (FlagsUnderlyingType)AllFlags::Internal_Stateful, // Note: ECMAScript "Global" creates a stateful regex.
  54. Insensitive = (FlagsUnderlyingType)AllFlags::Insensitive,
  55. Ungreedy = (FlagsUnderlyingType)AllFlags::Ungreedy,
  56. Unicode = (FlagsUnderlyingType)AllFlags::Unicode,
  57. Extended = (FlagsUnderlyingType)AllFlags::Extended,
  58. Extra = (FlagsUnderlyingType)AllFlags::Extra,
  59. SingleLine = (FlagsUnderlyingType)AllFlags::SingleLine,
  60. Sticky = (FlagsUnderlyingType)AllFlags::Sticky,
  61. Multiline = (FlagsUnderlyingType)AllFlags::Multiline,
  62. StringCopyMatches = (FlagsUnderlyingType)AllFlags::StringCopyMatches,
  63. UnicodeSets = (FlagsUnderlyingType)AllFlags::UnicodeSets,
  64. BrowserExtended = (FlagsUnderlyingType)AllFlags::Internal_BrowserExtended,
  65. };
  66. template<class T>
  67. class RegexOptions {
  68. public:
  69. using FlagsType = T;
  70. RegexOptions() = default;
  71. constexpr RegexOptions(T flags)
  72. : m_flags(flags)
  73. {
  74. }
  75. template<class U>
  76. constexpr RegexOptions(RegexOptions<U> other)
  77. : m_flags((T) static_cast<FlagsUnderlyingType>(other.value()))
  78. {
  79. }
  80. operator bool() const { return !!*this; }
  81. bool operator!() const { return (FlagsUnderlyingType)m_flags == 0; }
  82. constexpr RegexOptions<T> operator|(T flag) const { return RegexOptions<T> { (T)((FlagsUnderlyingType)m_flags | (FlagsUnderlyingType)flag) }; }
  83. constexpr RegexOptions<T> operator&(T flag) const { return RegexOptions<T> { (T)((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag) }; }
  84. constexpr RegexOptions<T>& operator|=(T flag)
  85. {
  86. m_flags = (T)((FlagsUnderlyingType)m_flags | (FlagsUnderlyingType)flag);
  87. return *this;
  88. }
  89. constexpr RegexOptions<T>& operator&=(T flag)
  90. {
  91. m_flags = (T)((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag);
  92. return *this;
  93. }
  94. void reset_flags() { m_flags = (T)0; }
  95. void reset_flag(T flag) { m_flags = (T)((FlagsUnderlyingType)m_flags & ~(FlagsUnderlyingType)flag); }
  96. void set_flag(T flag) { *this |= flag; }
  97. bool has_flag_set(T flag) const { return (FlagsUnderlyingType)flag == ((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag); }
  98. T value() const { return m_flags; }
  99. private:
  100. T m_flags { 0 };
  101. };
  102. template<class T>
  103. constexpr RegexOptions<T> operator|(T lhs, T rhs)
  104. {
  105. return RegexOptions<T> { lhs } |= rhs;
  106. }
  107. template<class T>
  108. constexpr RegexOptions<T> operator&(T lhs, T rhs)
  109. {
  110. return RegexOptions<T> { lhs } &= rhs;
  111. }
  112. template<class T>
  113. constexpr T operator~(T flag)
  114. {
  115. return (T) ~((FlagsUnderlyingType)flag);
  116. }
  117. using AllOptions = RegexOptions<AllFlags>;
  118. using ECMAScriptOptions = RegexOptions<ECMAScriptFlags>;
  119. using PosixOptions = RegexOptions<PosixFlags>;
  120. }
  121. using regex::ECMAScriptFlags;
  122. using regex::ECMAScriptOptions;
  123. using regex::PosixFlags;
  124. using regex::PosixOptions;