Bug 1745819 - Require origin permission for content scripts in mv3 r=robwu
[gecko.git] / js / public / RegExpFlags.h
blob4fa36031b0306fbe8ce2e2e57a77d87659a26d10
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Regular expression flags. */
9 #ifndef js_RegExpFlags_h
10 #define js_RegExpFlags_h
12 #include "mozilla/Assertions.h" // MOZ_ASSERT
13 #include "mozilla/Attributes.h" // MOZ_IMPLICIT
15 #include <stdint.h> // uint8_t
17 namespace JS {
19 /**
20 * Regular expression flag values, suitable for initializing a collection of
21 * regular expression flags as defined below in |RegExpFlags|. Flags are listed
22 * in alphabetical order by syntax -- /d, /g, /i, /m, /s, /u, /y.
24 class RegExpFlag {
25 // WARNING TO SPIDERMONKEY HACKERS (embedders must assume these values can
26 // change):
28 // Flag-bit values appear in XDR and structured clone data formats, so none of
29 // these values can be changed (including to assign values in numerically
30 // ascending order) unless you also add a translation layer.
32 public:
33 /**
34 * Add .indices property to the match result, i.e. /d
36 static constexpr uint8_t HasIndices = 0b100'0000;
38 /**
39 * Act globally and find *all* matches (rather than stopping after just the
40 * first one), i.e. /g.
42 static constexpr uint8_t Global = 0b000'0010;
44 /**
45 * Interpret regular expression source text case-insensitively by folding
46 * uppercase letters to lowercase, i.e. /i.
48 static constexpr uint8_t IgnoreCase = 0b000'0001;
50 /** Treat ^ and $ as begin and end of line, i.e. /m. */
51 static constexpr uint8_t Multiline = 0b000'0100;
53 /* Allow . to match newline characters, i.e. /s. */
54 static constexpr uint8_t DotAll = 0b010'0000;
56 /** Use Unicode semantics, i.e. /u. */
57 static constexpr uint8_t Unicode = 0b001'0000;
59 /** Only match starting from <regular expression>.lastIndex, i.e. /y. */
60 static constexpr uint8_t Sticky = 0b000'1000;
62 /** No regular expression flags. */
63 static constexpr uint8_t NoFlags = 0b000'0000;
65 /** All regular expression flags. */
66 static constexpr uint8_t AllFlags = 0b111'1111;
69 /**
70 * A collection of regular expression flags. Individual flag values may be
71 * combined into a collection using bitwise operators.
73 class RegExpFlags {
74 public:
75 using Flag = uint8_t;
77 private:
78 Flag flags_;
80 public:
81 RegExpFlags() = default;
83 MOZ_IMPLICIT RegExpFlags(Flag flags) : flags_(flags) {
84 MOZ_ASSERT((flags & RegExpFlag::AllFlags) == flags,
85 "flags must not contain unrecognized flags");
88 RegExpFlags(const RegExpFlags&) = default;
90 bool operator==(const RegExpFlags& other) const {
91 return flags_ == other.flags_;
94 bool operator!=(const RegExpFlags& other) const { return !(*this == other); }
96 RegExpFlags& operator&=(const RegExpFlags& rhs) {
97 flags_ &= rhs.flags_;
98 return *this;
101 RegExpFlags& operator|=(const RegExpFlags& rhs) {
102 flags_ |= rhs.flags_;
103 return *this;
106 RegExpFlags operator&(Flag flag) const { return RegExpFlags(flags_ & flag); }
108 RegExpFlags operator|(Flag flag) const { return RegExpFlags(flags_ | flag); }
110 RegExpFlags operator^(Flag flag) const { return RegExpFlags(flags_ ^ flag); }
112 RegExpFlags operator~() const {
113 return RegExpFlags(~flags_ & RegExpFlag::AllFlags);
116 bool hasIndices() const { return flags_ & RegExpFlag::HasIndices; }
117 bool global() const { return flags_ & RegExpFlag::Global; }
118 bool ignoreCase() const { return flags_ & RegExpFlag::IgnoreCase; }
119 bool multiline() const { return flags_ & RegExpFlag::Multiline; }
120 bool dotAll() const { return flags_ & RegExpFlag::DotAll; }
121 bool unicode() const { return flags_ & RegExpFlag::Unicode; }
122 bool sticky() const { return flags_ & RegExpFlag::Sticky; }
124 explicit operator bool() const { return flags_ != 0; }
126 Flag value() const { return flags_; }
129 inline RegExpFlags& operator&=(RegExpFlags& flags, RegExpFlags::Flag flag) {
130 flags = flags & flag;
131 return flags;
134 inline RegExpFlags& operator|=(RegExpFlags& flags, RegExpFlags::Flag flag) {
135 flags = flags | flag;
136 return flags;
139 inline RegExpFlags& operator^=(RegExpFlags& flags, RegExpFlags::Flag flag) {
140 flags = flags ^ flag;
141 return flags;
144 inline RegExpFlags operator&(const RegExpFlags& lhs, const RegExpFlags& rhs) {
145 RegExpFlags result = lhs;
146 result &= rhs;
147 return lhs;
150 inline RegExpFlags operator|(const RegExpFlags& lhs, const RegExpFlags& rhs) {
151 RegExpFlags result = lhs;
152 result |= rhs;
153 return result;
156 } // namespace JS
158 #endif // js_RegExpFlags_h