RegexOptions.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <regex.h>
  11. #else
  12. # include <LibC/regex.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. 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.
  33. Internal_BrowserExtended = __Regex_Internal_BrowserExtended, // Only for ECMA262, Enable the behaviors defined in section B.1.4. of the ECMA262 spec.
  34. Internal_ConsiderNewline = __Regex_Internal_ConsiderNewline, // Only for ECMA262, Allow multiline matches to consider newlines as line boundaries.
  35. Last = Internal_BrowserExtended,
  36. };
  37. enum class PosixFlags : FlagsUnderlyingType {
  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. StringCopyMatches = (FlagsUnderlyingType)AllFlags::StringCopyMatches,
  50. };
  51. enum class ECMAScriptFlags : FlagsUnderlyingType {
  52. Global = (FlagsUnderlyingType)AllFlags::Global | (FlagsUnderlyingType)AllFlags::Internal_Stateful, // Note: ECMAScript "Global" creates a stateful regex.
  53. Insensitive = (FlagsUnderlyingType)AllFlags::Insensitive,
  54. Ungreedy = (FlagsUnderlyingType)AllFlags::Ungreedy,
  55. Unicode = (FlagsUnderlyingType)AllFlags::Unicode,
  56. Extended = (FlagsUnderlyingType)AllFlags::Extended,
  57. Extra = (FlagsUnderlyingType)AllFlags::Extra,
  58. SingleLine = (FlagsUnderlyingType)AllFlags::SingleLine,
  59. Sticky = (FlagsUnderlyingType)AllFlags::Sticky,
  60. Multiline = (FlagsUnderlyingType)AllFlags::Multiline,
  61. StringCopyMatches = (FlagsUnderlyingType)AllFlags::StringCopyMatches,
  62. BrowserExtended = (FlagsUnderlyingType)AllFlags::Internal_BrowserExtended,
  63. };
  64. template<class T>
  65. class RegexOptions {
  66. public:
  67. using FlagsType = T;
  68. RegexOptions() = default;
  69. constexpr RegexOptions(T flags)
  70. : m_flags(flags)
  71. {
  72. }
  73. template<class U>
  74. constexpr RegexOptions(RegexOptions<U> other)
  75. : m_flags((T) static_cast<FlagsUnderlyingType>(other.value()))
  76. {
  77. }
  78. operator bool() const { return !!*this; }
  79. bool operator!() const { return (FlagsUnderlyingType)m_flags == 0; }
  80. constexpr RegexOptions<T> operator|(T flag) const { return RegexOptions<T> { (T)((FlagsUnderlyingType)m_flags | (FlagsUnderlyingType)flag) }; }
  81. constexpr RegexOptions<T> operator&(T flag) const { return RegexOptions<T> { (T)((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag) }; }
  82. constexpr RegexOptions<T>& operator|=(T flag)
  83. {
  84. m_flags = (T)((FlagsUnderlyingType)m_flags | (FlagsUnderlyingType)flag);
  85. return *this;
  86. }
  87. constexpr RegexOptions<T>& operator&=(T flag)
  88. {
  89. m_flags = (T)((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag);
  90. return *this;
  91. }
  92. void reset_flags() { m_flags = (T)0; }
  93. void reset_flag(T flag) { m_flags = (T)((FlagsUnderlyingType)m_flags & ~(FlagsUnderlyingType)flag); }
  94. void set_flag(T flag) { *this |= flag; }
  95. bool has_flag_set(T flag) const { return (FlagsUnderlyingType)flag == ((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag); }
  96. T value() const { return m_flags; }
  97. private:
  98. T m_flags { 0 };
  99. };
  100. template<class T>
  101. constexpr RegexOptions<T> operator|(T lhs, T rhs)
  102. {
  103. return RegexOptions<T> { lhs } |= rhs;
  104. }
  105. template<class T>
  106. constexpr RegexOptions<T> operator&(T lhs, T rhs)
  107. {
  108. return RegexOptions<T> { lhs } &= rhs;
  109. }
  110. template<class T>
  111. constexpr T operator~(T flag)
  112. {
  113. return (T) ~((FlagsUnderlyingType)flag);
  114. }
  115. using AllOptions = RegexOptions<AllFlags>;
  116. using ECMAScriptOptions = RegexOptions<ECMAScriptFlags>;
  117. using PosixOptions = RegexOptions<PosixFlags>;
  118. }
  119. using regex::ECMAScriptFlags;
  120. using regex::ECMAScriptOptions;
  121. using regex::PosixFlags;
  122. using regex::PosixOptions;