RegexOptions.h 7.1 KB

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