OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/objects.h" | 5 #include "src/objects.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <iomanip> | 8 #include <iomanip> |
9 #include <memory> | 9 #include <memory> |
10 #include <sstream> | 10 #include <sstream> |
(...skipping 11499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11510 bool capture_exists; | 11510 bool capture_exists; |
11511 Handle<String> capture; | 11511 Handle<String> capture; |
11512 ASSIGN_RETURN_ON_EXCEPTION( | 11512 ASSIGN_RETURN_ON_EXCEPTION( |
11513 isolate, capture, match->GetCapture(scaled_index, &capture_exists), | 11513 isolate, capture, match->GetCapture(scaled_index, &capture_exists), |
11514 String); | 11514 String); |
11515 if (capture_exists) builder.AppendString(capture); | 11515 if (capture_exists) builder.AppendString(capture); |
11516 continue_from_ix = peek_ix + advance; | 11516 continue_from_ix = peek_ix + advance; |
11517 break; | 11517 break; |
11518 } | 11518 } |
11519 case '<': { // $<name> - named capture | 11519 case '<': { // $<name> - named capture |
| 11520 typedef String::Match::CaptureState CaptureState; |
| 11521 |
11520 if (!match->HasNamedCaptures()) { | 11522 if (!match->HasNamedCaptures()) { |
11521 builder.AppendCharacter('$'); | 11523 builder.AppendCharacter('$'); |
11522 continue_from_ix = peek_ix; | 11524 continue_from_ix = peek_ix; |
11523 break; | 11525 break; |
11524 } | 11526 } |
11525 | 11527 |
11526 Handle<String> bracket_string = | 11528 Handle<String> bracket_string = |
11527 factory->LookupSingleCharacterStringFromCode('>'); | 11529 factory->LookupSingleCharacterStringFromCode('>'); |
11528 const int closing_bracket_ix = | 11530 const int closing_bracket_ix = |
11529 String::IndexOf(isolate, replacement, bracket_string, peek_ix + 1); | 11531 String::IndexOf(isolate, replacement, bracket_string, peek_ix + 1); |
11530 | 11532 |
11531 if (closing_bracket_ix == -1) { | 11533 if (closing_bracket_ix == -1) { |
11532 THROW_NEW_ERROR( | 11534 THROW_NEW_ERROR( |
11533 isolate, | 11535 isolate, |
11534 NewSyntaxError(MessageTemplate::kRegExpInvalidReplaceString, | 11536 NewSyntaxError(MessageTemplate::kRegExpInvalidReplaceString, |
11535 replacement), | 11537 replacement), |
11536 String); | 11538 String); |
11537 } | 11539 } |
11538 | 11540 |
11539 Handle<String> capture_name = | 11541 Handle<String> capture_name = |
11540 factory->NewSubString(replacement, peek_ix + 1, closing_bracket_ix); | 11542 factory->NewSubString(replacement, peek_ix + 1, closing_bracket_ix); |
11541 bool capture_exists; | |
11542 Handle<String> capture; | 11543 Handle<String> capture; |
| 11544 CaptureState capture_state; |
11543 ASSIGN_RETURN_ON_EXCEPTION( | 11545 ASSIGN_RETURN_ON_EXCEPTION( |
11544 isolate, capture, | 11546 isolate, capture, |
11545 match->GetNamedCapture(capture_name, &capture_exists), String); | 11547 match->GetNamedCapture(capture_name, &capture_state), String); |
11546 if (capture_exists) builder.AppendString(capture); | 11548 |
| 11549 switch (capture_state) { |
| 11550 case CaptureState::INVALID: |
| 11551 THROW_NEW_ERROR( |
| 11552 isolate, |
| 11553 NewSyntaxError(MessageTemplate::kRegExpInvalidReplaceString, |
| 11554 replacement), |
| 11555 String); |
| 11556 break; |
| 11557 case CaptureState::UNMATCHED: |
| 11558 break; |
| 11559 case CaptureState::MATCHED: |
| 11560 builder.AppendString(capture); |
| 11561 break; |
| 11562 } |
| 11563 |
11547 continue_from_ix = closing_bracket_ix + 1; | 11564 continue_from_ix = closing_bracket_ix + 1; |
11548 break; | 11565 break; |
11549 } | 11566 } |
11550 default: | 11567 default: |
11551 builder.AppendCharacter('$'); | 11568 builder.AppendCharacter('$'); |
11552 continue_from_ix = peek_ix; | 11569 continue_from_ix = peek_ix; |
11553 break; | 11570 break; |
11554 } | 11571 } |
11555 | 11572 |
11556 // Go the the next $ in the replacement. | 11573 // Go the the next $ in the replacement. |
(...skipping 8863 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
20420 // depend on this. | 20437 // depend on this. |
20421 return DICTIONARY_ELEMENTS; | 20438 return DICTIONARY_ELEMENTS; |
20422 } | 20439 } |
20423 DCHECK_LE(kind, LAST_ELEMENTS_KIND); | 20440 DCHECK_LE(kind, LAST_ELEMENTS_KIND); |
20424 return kind; | 20441 return kind; |
20425 } | 20442 } |
20426 } | 20443 } |
20427 | 20444 |
20428 } // namespace internal | 20445 } // namespace internal |
20429 } // namespace v8 | 20446 } // namespace v8 |
OLD | NEW |