Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library engine.utilities.string; | |
| 6 | |
| 7 /** | |
| 8 * Compute line starts for the given [content]. | |
| 9 * Lines end with `\r`, `\n` or `\r\n`. | |
| 10 */ | |
| 11 List<int> computeLineStarts(String content) { | |
|
Brian Wilkerson
2015/10/27 20:44:10
Could we add it to StringUtilities so that there's
| |
| 12 List<int> lineStarts = <int>[0]; | |
| 13 int length = content.length; | |
| 14 int unit; | |
| 15 for (int index = 0; index < length; index++) { | |
| 16 unit = content.codeUnitAt(index); | |
| 17 // Special-case \r\n. | |
| 18 if (unit == 0x0D /* \r */) { | |
| 19 // Peek ahead to detect a following \n. | |
| 20 if ((index + 1 < length) && content.codeUnitAt(index + 1) == 0x0A) { | |
| 21 // Line start will get registered at next index at the \n. | |
| 22 } else { | |
| 23 lineStarts.add(index + 1); | |
| 24 } | |
| 25 } | |
| 26 // \n | |
| 27 if (unit == 0x0A) { | |
| 28 lineStarts.add(index + 1); | |
| 29 } | |
| 30 } | |
| 31 return lineStarts; | |
| 32 } | |
| OLD | NEW |