diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /js/src/vm/MatchPairs.h | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/vm/MatchPairs.h')
-rw-r--r-- | js/src/vm/MatchPairs.h | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/js/src/vm/MatchPairs.h b/js/src/vm/MatchPairs.h new file mode 100644 index 0000000000..7ae9d5257d --- /dev/null +++ b/js/src/vm/MatchPairs.h @@ -0,0 +1,123 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef vm_MatchPairs_h +#define vm_MatchPairs_h + +#include "ds/LifoAlloc.h" +#include "js/AllocPolicy.h" +#include "js/Vector.h" + +/* + * RegExp match results are succinctly represented by pairs of integer + * indices delimiting (start, limit] segments of the input string. + * + * The pair count for a given RegExp match is the capturing parentheses + * count plus one for the "0 capturing paren" whole text match. + */ + +namespace js { + +struct MatchPair final { + int32_t start; + int32_t limit; + + static constexpr int32_t NoMatch = -1; + + MatchPair() : start(NoMatch), limit(NoMatch) {} + + MatchPair(int32_t start, int32_t limit) : start(start), limit(limit) {} + + size_t length() const { + MOZ_ASSERT(!isUndefined()); + return limit - start; + } + bool isUndefined() const { return start < 0; } + + inline bool check() const { + MOZ_ASSERT(limit >= start); + MOZ_ASSERT_IF(start < 0, start == NoMatch); + MOZ_ASSERT_IF(limit < 0, limit == NoMatch); + return true; + } +}; + +// MachPairs is used as base class for VectorMatchPairs but can also be +// stack-allocated (without a Vector) in JIT code. +class MatchPairs { + protected: + /* Length of pairs_. */ + uint32_t pairCount_; + + /* Raw pointer into an allocated MatchPair buffer. */ + MatchPair* pairs_; + + protected: + /* Not used directly: use VectorMatchPairs. */ + MatchPairs() : pairCount_(0), pairs_(nullptr) {} + + protected: + /* Functions used by friend classes. */ + friend class RegExpShared; + friend class RegExpStatics; + + void forgetArray() { pairs_ = nullptr; } + + public: + void checkAgainst(size_t inputLength) { +#ifdef DEBUG + for (size_t i = 0; i < pairCount_; i++) { + const MatchPair& p = (*this)[i]; + MOZ_ASSERT(p.check()); + if (p.isUndefined()) { + continue; + } + MOZ_ASSERT(size_t(p.limit) <= inputLength); + } +#endif + } + + /* Querying functions in the style of RegExpStatics. */ + bool empty() const { return pairCount_ == 0; } + size_t pairCount() const { + MOZ_ASSERT(pairCount_ > 0); + return pairCount_; + } + + static size_t offsetOfPairs() { return offsetof(MatchPairs, pairs_); } + static size_t offsetOfPairCount() { return offsetof(MatchPairs, pairCount_); } + + int32_t* pairsRaw() { return reinterpret_cast<int32_t*>(pairs_); } + + public: + size_t length() const { return pairCount_; } + + const MatchPair& operator[](size_t i) const { + MOZ_ASSERT(i < pairCount_); + return pairs_[i]; + } + MatchPair& operator[](size_t i) { + MOZ_ASSERT(i < pairCount_); + return pairs_[i]; + } +}; + +class VectorMatchPairs : public MatchPairs { + Vector<MatchPair, 10, SystemAllocPolicy> vec_; + + protected: + friend class RegExpShared; + friend class RegExpStatics; + + /* MatchPair buffer allocator: set pairs_ and pairCount_. */ + bool allocOrExpandArray(size_t pairCount); + + bool initArrayFrom(VectorMatchPairs& copyFrom); +}; + +} /* namespace js */ + +#endif /* vm_MatchPairs_h */ |