1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 29 #ifndef V8_JSREGEXP_INL_H_ 30 #define V8_JSREGEXP_INL_H_ 31 32 #include "allocation.h" 33 #include "handles.h" 34 #include "heap.h" 35 #include "jsregexp.h" 36 #include "objects.h" 37 38 namespace v8 { 39 namespace internal { 40 41 42 RegExpImpl::GlobalCache::~GlobalCache() { 43 // Deallocate the register array if we allocated it in the constructor 44 // (as opposed to using the existing jsregexp_static_offsets_vector). 45 if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { 46 DeleteArray(register_array_); 47 } 48 } 49 50 51 int32_t* RegExpImpl::GlobalCache::FetchNext() { 52 current_match_index_++; 53 if (current_match_index_ >= num_matches_) { 54 // Current batch of results exhausted. 55 // Fail if last batch was not even fully filled. 56 if (num_matches_ < max_matches_) { 57 num_matches_ = 0; // Signal failed match. 58 return NULL; 59 } 60 61 int32_t* last_match = 62 ®ister_array_[(current_match_index_ - 1) * registers_per_match_]; 63 int last_end_index = last_match[1]; 64 65 if (regexp_->TypeTag() == JSRegExp::ATOM) { 66 num_matches_ = RegExpImpl::AtomExecRaw(regexp_, 67 subject_, 68 last_end_index, 69 register_array_, 70 register_array_size_); 71 } else { 72 int last_start_index = last_match[0]; 73 if (last_start_index == last_end_index) last_end_index++; 74 if (last_end_index > subject_->length()) { 75 num_matches_ = 0; // Signal failed match. 76 return NULL; 77 } 78 num_matches_ = RegExpImpl::IrregexpExecRaw(regexp_, 79 subject_, 80 last_end_index, 81 register_array_, 82 register_array_size_); 83 } 84 85 if (num_matches_ <= 0) return NULL; 86 current_match_index_ = 0; 87 return register_array_; 88 } else { 89 return ®ister_array_[current_match_index_ * registers_per_match_]; 90 } 91 } 92 93 94 int32_t* RegExpImpl::GlobalCache::LastSuccessfulMatch() { 95 int index = current_match_index_ * registers_per_match_; 96 if (num_matches_ == 0) { 97 // After a failed match we shift back by one result. 98 index -= registers_per_match_; 99 } 100 return ®ister_array_[index]; 101 } 102 103 104 } } // namespace v8::internal 105 106 #endif // V8_JSREGEXP_INL_H_ 107