v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
regexp-flags.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_REGEXP_REGEXP_FLAGS_H_
6#define V8_REGEXP_REGEXP_FLAGS_H_
7
8#include <optional>
9#include <ostream>
10
11#include "src/base/flags.h"
12
13namespace v8::internal {
14
15// TODO(jgruber,pthier): Decouple more parts of the codebase from
16// JSRegExp::Flags. Consider removing JSRegExp::Flags.
17
18// Order is important! Sorted in alphabetic order by the flag char. Note this
19// means that flag bits are shuffled. Take care to keep them contiguous when
20// adding/removing flags.
21#define REGEXP_FLAG_LIST(V) \
22 V(has_indices, HasIndices, hasIndices, 'd', 7) \
23 V(global, Global, global, 'g', 0) \
24 V(ignore_case, IgnoreCase, ignoreCase, 'i', 1) \
25 V(linear, Linear, linear, 'l', 6) \
26 V(multiline, Multiline, multiline, 'm', 2) \
27 V(dot_all, DotAll, dotAll, 's', 5) \
28 V(unicode, Unicode, unicode, 'u', 4) \
29 V(unicode_sets, UnicodeSets, unicodeSets, 'v', 8) \
30 V(sticky, Sticky, sticky, 'y', 3)
31
32#define V(Lower, Camel, LowerCamel, Char, Bit) k##Camel = 1 << Bit,
34#undef V
35
36#define V(...) +1
38#undef V
39
40// Assert alpha-sorted chars.
41#define V(Lower, Camel, LowerCamel, Char, Bit) < Char) && (Char
42static_assert((('a' - 1) REGEXP_FLAG_LIST(V) <= 'z'), "alpha-sort chars");
43#undef V
44
45// Assert contiguous indices.
46#define V(Lower, Camel, LowerCamel, Char, Bit) | (1 << Bit)
47static_assert(((1 << kRegExpFlagCount) - 1) == (0 REGEXP_FLAG_LIST(V)),
48 "contiguous bits");
49#undef V
50
53
54#define V(Lower, Camel, ...) \
55 constexpr bool Is##Camel(RegExpFlags f) { \
56 return (f & RegExpFlag::k##Camel) != 0; \
57 }
59#undef V
60
61constexpr bool IsEitherUnicode(RegExpFlags f) {
62 return IsUnicode(f) || IsUnicodeSets(f);
63}
64
65// Whether to rewind the index when it initially points into the middle of a
66// surrogate pair. See also OptionallyStepBackToLeadSurrogate().
68 return IsEitherUnicode(f) && (IsGlobal(f) || IsSticky(f));
69}
70
71// clang-format off
72#define V(Lower, Camel, LowerCamel, Char, Bit) \
73 c == Char ? RegExpFlag::k##Camel :
74constexpr std::optional<RegExpFlag> TryRegExpFlagFromChar(char c) {
75 return REGEXP_FLAG_LIST(V) std::optional<RegExpFlag>{};
76}
77#undef V
78// clang-format on
79
80std::ostream& operator<<(std::ostream& os, RegExpFlags flags);
81
82} // namespace v8::internal
83
84#endif // V8_REGEXP_REGEXP_FLAGS_H_
#define DEFINE_OPERATORS_FOR_FLAGS(Type)
Definition flags.h:100
constexpr std::optional< RegExpFlag > TryRegExpFlagFromChar(char c)
constexpr bool IsEitherUnicode(RegExpFlags f)
constexpr int kRegExpFlagCount
constexpr bool ShouldOptionallyStepBackToLeadSurrogate(RegExpFlags f)
std::ostream & operator<<(std::ostream &os, AtomicMemoryOrder order)
base::Flags< RegExpFlag > RegExpFlags