Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Side by Side Diff: pkg/compiler/lib/src/io/line_column_provider.dart

Issue 830703004: Emit to StreamCodeOutput instead of CodeBuffer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/compiler/lib/src/io/code_output.dart ('k') | pkg/compiler/lib/src/io/source_file.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 dart2js.io.line_column;
6
7 import 'code_output.dart';
8
9 /// Interface for providing line/column information.
10 abstract class LineColumnProvider {
11 /// Returns the line number (0-based) for [offset].
12 int getLine(int offset);
13
14 /// Returns the column number (0-based) for [offset] at the given [line].
15 int getColumn(int line, int offset);
16 }
17
18 /// [CodeOutputListener] that collects line information.
19 class LineColumnCollector extends CodeOutputListener
20 implements LineColumnProvider {
21 int lastLineStart = 0;
22 List<int> lineStarts = <int>[0];
23
24 void _collect(String text) {
25 int offset = lastLineStart;
26 int index = 0;
27 while (index < text.length) {
28 // Unix uses '\n' and Windows uses '\r\n', so this algorithm works for
29 // both platforms.
30 index = text.indexOf('\n', index) + 1;
31 if (index <= 0) break;
32 lastLineStart = offset + index;
33 lineStarts.add(lastLineStart);
34 }
35 }
36
37 @override
38 void onText(String text) {
39 _collect(text);
40 }
41
42 @override
43 int getLine(int offset) {
44 List<int> starts = lineStarts;
45 if (offset < 0 || starts.last <= offset) {
46 throw 'bad position #$offset in buffer with length ${lineStarts.last}.';
47 }
48 int first = 0;
49 int count = starts.length;
50 while (count > 1) {
51 int step = count ~/ 2;
52 int middle = first + step;
53 int lineStart = starts[middle];
54 if (offset < lineStart) {
55 count = step;
56 } else {
57 first = middle;
58 count -= step;
59 }
60 }
61 return first;
62 }
63
64 @override
65 int getColumn(int line, int offset) {
66 return offset - lineStarts[line];
67 }
68
69 @override
70 void onDone(int length) {
71 lineStarts.add(length);
72 }
73 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/io/code_output.dart ('k') | pkg/compiler/lib/src/io/source_file.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698