RegexOptions.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = u16;
  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. 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.
  32. Internal_BrowserExtended = __Regex_Internal_BrowserExtended, // Only for ECMA262, Enable the behaviours defined in section B.1.4. of the ECMA262 spec.
  33. Last = Internal_BrowserExtended,
  34. };
  35. enum class PosixFlags : FlagsUnderlyingType {
  36. Global = (FlagsUnderlyingType)AllFlags::Global,
  37. Insensitive = (FlagsUnderlyingType)AllFlags::Insensitive,
  38. Ungreedy = (FlagsUnderlyingType)AllFlags::Ungreedy,
  39. Unicode = (FlagsUnderlyingType)AllFlags::Unicode,
  40. Extended = (FlagsUnderlyingType)AllFlags::Extended,
  41. Extra = (FlagsUnderlyingType)AllFlags::Extra,
  42. MatchNotBeginOfLine = (FlagsUnderlyingType)AllFlags::MatchNotBeginOfLine,
  43. MatchNotEndOfLine = (FlagsUnderlyingType)AllFlags::MatchNotEndOfLine,
  44. SkipSubExprResults = (FlagsUnderlyingType)AllFlags::SkipSubExprResults,
  45. SkipTrimEmptyMatches = (FlagsUnderlyingType)AllFlags::SkipTrimEmptyMatches,
  46. Multiline = (FlagsUnderlyingType)AllFlags::Multiline,
  47. StringCopyMatches = (FlagsUnderlyingType)AllFlags::StringCopyMatches,
  48. };
  49. enum class ECMAScriptFlags : FlagsUnderlyingType {
  50. Global = (FlagsUnderlyingType)AllFlags::Global | (FlagsUnderlyingType)AllFlags::Internal_Stateful, // Note: ECMAScript "Global" creates a stateful regex.
  51. Insensitive = (FlagsUnderlyingType)AllFlags::Insensitive,
  52. Ungreedy = (FlagsUnderlyingType)AllFlags::Ungreedy,
  53. Unicode = (FlagsUnderlyingType)AllFlags::Unicode,
  54. Extended = (FlagsUnderlyingType)AllFlags::Extended,
  55. Extra = (FlagsUnderlyingType)AllFlags::Extra,
  56. SingleLine = (FlagsUnderlyingType)AllFlags::SingleLine,
  57. Sticky = (FlagsUnderlyingType)AllFlags::Sticky,
  58. Multiline = (FlagsUnderlyingType)AllFlags::Multiline,
  59. StringCopyMatches = (FlagsUnderlyingType)AllFlags::StringCopyMatches,
  60. BrowserExtended = (FlagsUnderlyingType)AllFlags::Internal_BrowserExtended,
  61. };
  62. template<class T>
  63. class RegexOptions {
  64. public:
  65. using FlagsType = T;
  66. RegexOptions() = default;
  67. RegexOptions(T flags)
  68. : m_flags(flags)
  69. {
  70. }
  71. template<class U>
  72. RegexOptions(RegexOptions<U> other)
  73. : m_flags((T) static_cast<FlagsUnderlyingType>(other.value()))
  74. {
  75. }
  76. operator bool() const { return !!*this; }
  77. bool operator!() const { return (FlagsUnderlyingType)m_flags == 0; }
  78. RegexOptions<T> operator|(T flag) const { return RegexOptions<T> { (T)((FlagsUnderlyingType)m_flags | (FlagsUnderlyingType)flag) }; }
  79. RegexOptions<T> operator&(T flag) const { return RegexOptions<T> { (T)((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag) }; }
  80. RegexOptions<T>& operator|=(T flag)
  81. {
  82. m_flags = (T)((FlagsUnderlyingType)m_flags | (FlagsUnderlyingType)flag);
  83. return *this;
  84. }
  85. RegexOptions<T>& operator&=(T flag)
  86. {
  87. m_flags = (T)((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag);
  88. return *this;
  89. }
  90. void reset_flags() { m_flags = (T)0; }
  91. void reset_flag(T flag) { m_flags = (T)((FlagsUnderlyingType)m_flags & ~(FlagsUnderlyingType)flag); }
  92. void set_flag(T flag) { *this |= flag; }
  93. bool has_flag_set(T flag) const { return (FlagsUnderlyingType)flag == ((FlagsUnderlyingType)m_flags & (FlagsUnderlyingType)flag); }
  94. T value() const { return m_flags; }
  95. private:
  96. T m_flags { 0 };
  97. };
  98. template<class T>
  99. inline RegexOptions<T> operator|(T lhs, T rhs)
  100. {
  101. return RegexOptions<T> { lhs } |= rhs;
  102. }
  103. template<class T>
  104. inline RegexOptions<T> operator&(T lhs, T rhs)
  105. {
  106. return RegexOptions<T> { lhs } &= rhs;
  107. }
  108. template<class T>
  109. inline T operator~(T flag)
  110. {
  111. return (T) ~((FlagsUnderlyingType)flag);
  112. }
  113. using AllOptions = RegexOptions<AllFlags>;
  114. using ECMAScriptOptions = RegexOptions<ECMAScriptFlags>;
  115. using PosixOptions = RegexOptions<PosixFlags>;
  116. }
  117. using regex::ECMAScriptFlags;
  118. using regex::ECMAScriptOptions;
  119. using regex::PosixFlags;
  120. using regex::PosixOptions;