Chromium Code Reviews| Index: src/handles.cc |
| =================================================================== |
| --- src/handles.cc (revision 5837) |
| +++ src/handles.cc (working copy) |
| @@ -36,6 +36,7 @@ |
| #include "execution.h" |
| #include "global-handles.h" |
| #include "natives.h" |
| +#include "string-search.h" |
| #include "runtime.h" |
| #include "stub-cache.h" |
| @@ -504,22 +505,21 @@ |
| } |
| -Handle<FixedArray> CalculateLineEnds(Handle<String> src, |
| - bool with_imaginary_last_new_line) { |
| - const int src_len = src->length(); |
| - Handle<String> new_line = Factory::NewStringFromAscii(CStrVector("\n")); |
| +template <typename SourceChar> |
| +Handle<FixedArray> CalculateLineEnds(Vector<const SourceChar> src, |
|
mnaganov (inactive)
2010/11/17 15:32:00
Please mark this private function 'static'.
sandholm
2010/11/17 15:45:23
Done.
|
| + bool with_last_line) { |
| + const int src_len = src.length(); |
| + StringSearch<char, SourceChar> search(CStrVector("\n")); |
| // Pass 1: Identify line count. |
| int line_count = 0; |
| int position = 0; |
| while (position != -1 && position < src_len) { |
| - position = Runtime::StringMatch(src, new_line, position); |
| + position = search.Search(src, position); |
| if (position != -1) { |
| position++; |
| - } |
| - if (position != -1) { |
| line_count++; |
| - } else if (with_imaginary_last_new_line) { |
| + } else if (with_last_line) { |
| // Even if the last line misses a line end, it is counted. |
| line_count++; |
| } |
| @@ -530,10 +530,10 @@ |
| int array_index = 0; |
| position = 0; |
| while (position != -1 && position < src_len) { |
| - position = Runtime::StringMatch(src, new_line, position); |
| + position = search.Search(src, position); |
| if (position != -1) { |
| array->set(array_index++, Smi::FromInt(position++)); |
| - } else if (with_imaginary_last_new_line) { |
| + } else if (with_last_line) { |
| // If the script does not end with a line ending add the final end |
| // position as just past the last line ending. |
| array->set(array_index++, Smi::FromInt(src_len)); |
| @@ -544,7 +544,22 @@ |
| return array; |
| } |
| +Handle<FixedArray> CalculateLineEnds(Handle<String> src, |
| + bool with_last_line) { |
| + // const int src_len = src->length(); |
| + if (!src->IsFlat()) FlattenString(src); |
| + AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| + // Extract flattened substrings of cons strings before determining asciiness. |
| + String* seq_src = *src; |
| + if (seq_src->IsConsString()) seq_src = ConsString::cast(seq_src)->first(); |
|
mnaganov (inactive)
2010/11/17 15:32:00
Should you assert anything about the second part o
sandholm
2010/11/17 15:45:23
Yes. src is flat so if it is a cons string then th
|
| + // dispatch on type of strings |
| + if (seq_src->IsAsciiRepresentation()) { |
| + return CalculateLineEnds(seq_src->ToAsciiVector(), with_last_line); |
| + } |
| + return CalculateLineEnds(seq_src->ToUC16Vector(), with_last_line); |
| +} |
| + |
| // Convert code position into line number. |
| int GetScriptLineNumber(Handle<Script> script, int code_pos) { |
| InitScriptLineEnds(script); |