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 5727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5738 int right = length; | 5738 int right = length; |
5739 if (trimRight) { | 5739 if (trimRight) { |
5740 while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { | 5740 while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { |
5741 right--; | 5741 right--; |
5742 } | 5742 } |
5743 } | 5743 } |
5744 return s->SubString(left, right); | 5744 return s->SubString(left, right); |
5745 } | 5745 } |
5746 | 5746 |
5747 | 5747 |
| 5748 void FindAsciiStringIndices(Vector<const char> subject, |
| 5749 char pattern, |
| 5750 ZoneList<int>* indices, |
| 5751 unsigned int limit) { |
| 5752 ASSERT(limit > 0); |
| 5753 // Collect indices of pattern in subject using memchr. |
| 5754 // Stop after finding at most limit values. |
| 5755 const char* subject_start = reinterpret_cast<const char*>(subject.start()); |
| 5756 const char* subject_end = subject_start + subject.length(); |
| 5757 const char* pos = subject_start; |
| 5758 while (limit > 0) { |
| 5759 pos = reinterpret_cast<const char*>( |
| 5760 memchr(pos, pattern, subject_end - pos)); |
| 5761 if (pos == NULL) return; |
| 5762 indices->Add(pos - subject_start); |
| 5763 pos++; |
| 5764 limit--; |
| 5765 } |
| 5766 } |
| 5767 |
| 5768 |
5748 template <typename SubjectChar, typename PatternChar> | 5769 template <typename SubjectChar, typename PatternChar> |
5749 void FindStringIndices(Isolate* isolate, | 5770 void FindStringIndices(Isolate* isolate, |
5750 Vector<const SubjectChar> subject, | 5771 Vector<const SubjectChar> subject, |
5751 Vector<const PatternChar> pattern, | 5772 Vector<const PatternChar> pattern, |
5752 ZoneList<int>* indices, | 5773 ZoneList<int>* indices, |
5753 unsigned int limit) { | 5774 unsigned int limit) { |
5754 ASSERT(limit > 0); | 5775 ASSERT(limit > 0); |
5755 // Collect indices of pattern in subject, and the end-of-string index. | 5776 // Collect indices of pattern in subject. |
5756 // Stop after finding at most limit values. | 5777 // Stop after finding at most limit values. |
5757 int pattern_length = pattern.length(); | 5778 int pattern_length = pattern.length(); |
5758 int index = 0; | 5779 int index = 0; |
5759 if (sizeof(SubjectChar) == kCharSize && | 5780 StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
5760 sizeof(PatternChar) == kCharSize && | 5781 while (limit > 0) { |
5761 pattern_length == 1) { | 5782 index = search.Search(subject, index); |
5762 // ASCII subject with one char ASCII pattern allows direct use of memchr. | 5783 if (index < 0) return; |
5763 char pattern_first_char = pattern[0]; | 5784 indices->Add(index); |
5764 const char* subject_start = reinterpret_cast<const char*>(subject.start()); | 5785 index += pattern_length; |
5765 const char* subject_end = subject_start + subject.length(); | 5786 limit--; |
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 } | |
5784 } | 5787 } |
5785 } | 5788 } |
5786 | 5789 |
| 5790 |
5787 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { | 5791 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { |
5788 ASSERT(args.length() == 3); | 5792 ASSERT(args.length() == 3); |
5789 HandleScope handle_scope(isolate); | 5793 HandleScope handle_scope(isolate); |
5790 CONVERT_ARG_CHECKED(String, subject, 0); | 5794 CONVERT_ARG_CHECKED(String, subject, 0); |
5791 CONVERT_ARG_CHECKED(String, pattern, 1); | 5795 CONVERT_ARG_CHECKED(String, pattern, 1); |
5792 CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); | 5796 CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
5793 | 5797 |
5794 int subject_length = subject->length(); | 5798 int subject_length = subject->length(); |
5795 int pattern_length = pattern->length(); | 5799 int pattern_length = pattern->length(); |
5796 RUNTIME_ASSERT(pattern_length > 0); | 5800 RUNTIME_ASSERT(pattern_length > 0); |
(...skipping 12 matching lines...) Expand all Loading... |
5809 int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); | 5813 int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); |
5810 ZoneList<int> indices(initial_capacity); | 5814 ZoneList<int> indices(initial_capacity); |
5811 if (!pattern->IsFlat()) FlattenString(pattern); | 5815 if (!pattern->IsFlat()) FlattenString(pattern); |
5812 | 5816 |
5813 // No allocation block. | 5817 // No allocation block. |
5814 { | 5818 { |
5815 AssertNoAllocation nogc; | 5819 AssertNoAllocation nogc; |
5816 if (subject->IsAsciiRepresentation()) { | 5820 if (subject->IsAsciiRepresentation()) { |
5817 Vector<const char> subject_vector = subject->ToAsciiVector(); | 5821 Vector<const char> subject_vector = subject->ToAsciiVector(); |
5818 if (pattern->IsAsciiRepresentation()) { | 5822 if (pattern->IsAsciiRepresentation()) { |
5819 FindStringIndices(isolate, | 5823 Vector<const char> pattern_vector = pattern->ToAsciiVector(); |
5820 subject_vector, | 5824 if (pattern_vector.length() == 1) { |
5821 pattern->ToAsciiVector(), | 5825 FindAsciiStringIndices(subject_vector, |
5822 &indices, | 5826 pattern_vector[0], |
5823 limit); | 5827 &indices, |
| 5828 limit); |
| 5829 } else { |
| 5830 FindStringIndices(isolate, |
| 5831 subject_vector, |
| 5832 pattern_vector, |
| 5833 &indices, |
| 5834 limit); |
| 5835 } |
5824 } else { | 5836 } else { |
5825 FindStringIndices(isolate, | 5837 FindStringIndices(isolate, |
5826 subject_vector, | 5838 subject_vector, |
5827 pattern->ToUC16Vector(), | 5839 pattern->ToUC16Vector(), |
5828 &indices, | 5840 &indices, |
5829 limit); | 5841 limit); |
5830 } | 5842 } |
5831 } else { | 5843 } else { |
5832 Vector<const uc16> subject_vector = subject->ToUC16Vector(); | 5844 Vector<const uc16> subject_vector = subject->ToUC16Vector(); |
5833 if (pattern->IsAsciiRepresentation()) { | 5845 if (pattern->IsAsciiRepresentation()) { |
(...skipping 6732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12566 } else { | 12578 } else { |
12567 // Handle last resort GC and make sure to allow future allocations | 12579 // Handle last resort GC and make sure to allow future allocations |
12568 // to grow the heap without causing GCs (if possible). | 12580 // to grow the heap without causing GCs (if possible). |
12569 isolate->counters()->gc_last_resort_from_js()->Increment(); | 12581 isolate->counters()->gc_last_resort_from_js()->Increment(); |
12570 isolate->heap()->CollectAllGarbage(false); | 12582 isolate->heap()->CollectAllGarbage(false); |
12571 } | 12583 } |
12572 } | 12584 } |
12573 | 12585 |
12574 | 12586 |
12575 } } // namespace v8::internal | 12587 } } // namespace v8::internal |
OLD | NEW |