OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 5736 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5747 | 5747 |
5748 template <typename SubjectChar, typename PatternChar> | 5748 template <typename SubjectChar, typename PatternChar> |
5749 void FindStringIndices(Isolate* isolate, | 5749 void FindStringIndices(Isolate* isolate, |
5750 Vector<const SubjectChar> subject, | 5750 Vector<const SubjectChar> subject, |
5751 Vector<const PatternChar> pattern, | 5751 Vector<const PatternChar> pattern, |
5752 ZoneList<int>* indices, | 5752 ZoneList<int>* indices, |
5753 unsigned int limit) { | 5753 unsigned int limit) { |
5754 ASSERT(limit > 0); | 5754 ASSERT(limit > 0); |
5755 // Collect indices of pattern in subject, and the end-of-string index. | 5755 // Collect indices of pattern in subject, and the end-of-string index. |
5756 // Stop after finding at most limit values. | 5756 // Stop after finding at most limit values. |
5757 StringSearch<PatternChar, SubjectChar> search(isolate, pattern); | |
5758 int pattern_length = pattern.length(); | 5757 int pattern_length = pattern.length(); |
5759 int index = 0; | 5758 int index = 0; |
5760 while (limit > 0) { | 5759 if (sizeof(SubjectChar) == kCharSize && |
5761 index = search.Search(subject, index); | 5760 sizeof(PatternChar) == kCharSize && |
5762 if (index < 0) return; | 5761 pattern_length == 1) { |
5763 indices->Add(index); | 5762 // ASCII subject with one char ASCII pattern allows direct use of memchr. |
5764 index += pattern_length; | 5763 char pattern_first_char = pattern[0]; |
5765 limit--; | 5764 const char* subject_start = reinterpret_cast<const char*>(subject.start()); |
| 5765 const char* subject_end = subject_start + subject.length(); |
| 5766 const char* pos = subject_start; |
| 5767 while (limit > 0) { |
| 5768 pos = reinterpret_cast<const char*>( |
| 5769 memchr(pos, pattern_first_char, subject_end - pos)); |
| 5770 if (pos == NULL) return; |
| 5771 indices->Add(pos - subject_start); |
| 5772 pos++; |
| 5773 limit--; |
| 5774 } |
| 5775 } else { |
| 5776 StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
| 5777 while (limit > 0) { |
| 5778 index = search.Search(subject, index); |
| 5779 if (index < 0) return; |
| 5780 indices->Add(index); |
| 5781 index += pattern_length; |
| 5782 limit--; |
| 5783 } |
5766 } | 5784 } |
5767 } | 5785 } |
5768 | 5786 |
5769 | |
5770 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { | 5787 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { |
5771 ASSERT(args.length() == 3); | 5788 ASSERT(args.length() == 3); |
5772 HandleScope handle_scope(isolate); | 5789 HandleScope handle_scope(isolate); |
5773 CONVERT_ARG_CHECKED(String, subject, 0); | 5790 CONVERT_ARG_CHECKED(String, subject, 0); |
5774 CONVERT_ARG_CHECKED(String, pattern, 1); | 5791 CONVERT_ARG_CHECKED(String, pattern, 1); |
5775 CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); | 5792 CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
5776 | 5793 |
5777 int subject_length = subject->length(); | 5794 int subject_length = subject->length(); |
5778 int pattern_length = pattern->length(); | 5795 int pattern_length = pattern->length(); |
5779 RUNTIME_ASSERT(pattern_length > 0); | 5796 RUNTIME_ASSERT(pattern_length > 0); |
(...skipping 6777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12557 } else { | 12574 } else { |
12558 // Handle last resort GC and make sure to allow future allocations | 12575 // Handle last resort GC and make sure to allow future allocations |
12559 // to grow the heap without causing GCs (if possible). | 12576 // to grow the heap without causing GCs (if possible). |
12560 isolate->counters()->gc_last_resort_from_js()->Increment(); | 12577 isolate->counters()->gc_last_resort_from_js()->Increment(); |
12561 isolate->heap()->CollectAllGarbage(false); | 12578 isolate->heap()->CollectAllGarbage(false); |
12562 } | 12579 } |
12563 } | 12580 } |
12564 | 12581 |
12565 | 12582 |
12566 } } // namespace v8::internal | 12583 } } // namespace v8::internal |
OLD | NEW |