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
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 -- /g, /i, /m, /u, /y.
25 // WARNING TO SPIDERMONKEY HACKERS (embedders must assume these values can
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.
34 * Act globally and find *all* matches (rather than stopping after just the
35 * first one), i.e. /g.
37 static constexpr uint8_t Global
= 0b0'0010;
40 * Interpret regular expression source text case-insensitively by folding
41 * uppercase letters to lowercase, i.e. /i.
43 static constexpr uint8_t IgnoreCase
= 0b0'0001;
45 /** Treat ^ and $ as begin and end of line, i.e. /m. */
46 static constexpr uint8_t Multiline
= 0b0'0100;
48 /** Use Unicode semantics, i.e. /u. */
49 static constexpr uint8_t Unicode
= 0b1'0000;
51 /** Only match starting from <regular expression>.lastIndex, i.e. /y. */
52 static constexpr uint8_t Sticky
= 0b0'1000;
54 /** No regular expression flags. */
55 static constexpr uint8_t NoFlags
= 0b0'0000;
57 /** All regular expression flags. */
58 static constexpr uint8_t AllFlags
= 0b1'1111;
62 * A collection of regular expression flags. Individual flag values may be
63 * combined into a collection using bitwise operators.
73 RegExpFlags() = default;
75 MOZ_IMPLICIT
RegExpFlags(Flag flags
) : flags_(flags
) {
76 MOZ_ASSERT((flags
& RegExpFlag::AllFlags
) == flags
,
77 "flags must not contain unrecognized flags");
80 RegExpFlags(const RegExpFlags
&) = default;
82 bool operator==(const RegExpFlags
& other
) const {
83 return flags_
== other
.flags_
;
86 bool operator!=(const RegExpFlags
& other
) const { return !(*this == other
); }
88 RegExpFlags
operator&(Flag flag
) const { return RegExpFlags(flags_
& flag
); }
90 RegExpFlags
operator|(Flag flag
) const { return RegExpFlags(flags_
| flag
); }
92 RegExpFlags
operator^(Flag flag
) const { return RegExpFlags(flags_
^ flag
); }
94 RegExpFlags
operator~() const { return RegExpFlags(~flags_
); }
96 bool global() const { return flags_
& RegExpFlag::Global
; }
97 bool ignoreCase() const { return flags_
& RegExpFlag::IgnoreCase
; }
98 bool multiline() const { return flags_
& RegExpFlag::Multiline
; }
99 bool unicode() const { return flags_
& RegExpFlag::Unicode
; }
100 bool sticky() const { return flags_
& RegExpFlag::Sticky
; }
102 explicit operator bool() const { return flags_
!= 0; }
104 Flag
value() const { return flags_
; }
107 inline RegExpFlags
& operator&=(RegExpFlags
& flags
, RegExpFlags::Flag flag
) {
108 flags
= flags
& flag
;
112 inline RegExpFlags
& operator|=(RegExpFlags
& flags
, RegExpFlags::Flag flag
) {
113 flags
= flags
| flag
;
117 inline RegExpFlags
& operator^=(RegExpFlags
& flags
, RegExpFlags::Flag flag
) {
118 flags
= flags
^ flag
;
124 #endif // js_RegExpFlags_h