Line data Source code
1 : // Copyright 2013 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 :
6 : #ifndef V8_REGEXP_JSREGEXP_INL_H_
7 : #define V8_REGEXP_JSREGEXP_INL_H_
8 :
9 : #include "src/allocation.h"
10 : #include "src/objects.h"
11 : #include "src/regexp/jsregexp.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 :
16 :
17 : RegExpImpl::GlobalCache::~GlobalCache() {
18 : // Deallocate the register array if we allocated it in the constructor
19 : // (as opposed to using the existing jsregexp_static_offsets_vector).
20 361004 : if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
21 1764 : DeleteArray(register_array_);
22 : }
23 : }
24 :
25 :
26 : int32_t* RegExpImpl::GlobalCache::FetchNext() {
27 22511591 : current_match_index_++;
28 22511591 : if (current_match_index_ >= num_matches_) {
29 : // Current batch of results exhausted.
30 : // Fail if last batch was not even fully filled.
31 866322 : if (num_matches_ < max_matches_) {
32 150312 : num_matches_ = 0; // Signal failed match.
33 : return NULL;
34 : }
35 :
36 : int32_t* last_match =
37 716010 : ®ister_array_[(current_match_index_ - 1) * registers_per_match_];
38 716010 : int last_end_index = last_match[1];
39 :
40 716010 : if (regexp_->TypeTag() == JSRegExp::ATOM) {
41 : num_matches_ = RegExpImpl::AtomExecRaw(regexp_,
42 : subject_,
43 : last_end_index,
44 : register_array_,
45 140390 : register_array_size_);
46 : } else {
47 575620 : int last_start_index = last_match[0];
48 575620 : if (last_start_index == last_end_index) {
49 : // Zero-length match. Advance by one code point.
50 449 : last_end_index = AdvanceZeroLength(last_end_index);
51 : }
52 575620 : if (last_end_index > subject_->length()) {
53 0 : num_matches_ = 0; // Signal failed match.
54 : return NULL;
55 : }
56 : num_matches_ = RegExpImpl::IrregexpExecRaw(regexp_,
57 : subject_,
58 : last_end_index,
59 : register_array_,
60 575620 : register_array_size_);
61 : }
62 :
63 716010 : if (num_matches_ <= 0) return NULL;
64 505547 : current_match_index_ = 0;
65 505547 : return register_array_;
66 : } else {
67 21645269 : return ®ister_array_[current_match_index_ * registers_per_match_];
68 : }
69 : }
70 :
71 :
72 : int32_t* RegExpImpl::GlobalCache::LastSuccessfulMatch() {
73 153035 : int index = current_match_index_ * registers_per_match_;
74 153035 : if (num_matches_ == 0) {
75 : // After a failed match we shift back by one result.
76 153035 : index -= registers_per_match_;
77 : }
78 153035 : return ®ister_array_[index];
79 : }
80 :
81 :
82 : } // namespace internal
83 : } // namespace v8
84 :
85 : #endif // V8_REGEXP_JSREGEXP_INL_H_
|