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

Side by Side Diff: lib/compiler/implementation/source_file.dart

Issue 10919130: Reimplement SourceFile.getLine using binary search. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #library('source_file'); 5 #library('source_file');
6 6
7 #import('dart:math'); 7 #import('dart:math');
8 8
9 #import('colors.dart', prefix: 'colors'); 9 #import('colors.dart', prefix: 'colors');
10 10
(...skipping 21 matching lines...) Expand all
32 if (index <= 0) break; 32 if (index <= 0) break;
33 starts.add(index); 33 starts.add(index);
34 } 34 }
35 starts.add(text.length + 1); 35 starts.add(text.length + 1);
36 _lineStarts = starts; 36 _lineStarts = starts;
37 } 37 }
38 return _lineStarts; 38 return _lineStarts;
39 } 39 }
40 40
41 int getLine(int position) { 41 int getLine(int position) {
42 // TODO(jimhug): Implement as binary search. 42 if (position < 0 || text.length <= position) {
43 var starts = lineStarts; 43 throw 'bad position #$position in file $filename with '
44 for (int i=0; i < starts.length; i++) { 44 'length ${text.length}.';
45 if (starts[i] > position) return i-1;
46 } 45 }
47 throw 'bad position #$position in file $filename with ' 46 List<int> starts = lineStarts;
48 'length ${text.length}.'; 47 int first = 0;
48 int count = starts.length;
49 while (count > 1) {
50 int step = count >> 1;
floitsch 2012/09/10 09:08:20 count ~/ 2
podivilov 2012/09/10 09:45:45 Done.
51 int middle = first + step;
52 int lineStart = starts[middle];
53 if (position < lineStart) {
54 count = step;
55 } else {
56 first = middle;
57 count -= step;
58 }
59 }
60 return first;
49 } 61 }
50 62
51 int getColumn(int line, int position) { 63 int getColumn(int line, int position) {
52 return position - lineStarts[line]; 64 return position - lineStarts[line];
53 } 65 }
54 66
55 /** 67 /**
56 * Create a pretty string representation from a character position 68 * Create a pretty string representation from a character position
57 * in the file. 69 * in the file.
58 */ 70 */
(...skipping 25 matching lines...) Expand all
84 } 96 }
85 97
86 for (; i < toColumn; i++) { 98 for (; i < toColumn; i++) {
87 buf.add(color('^')); 99 buf.add(color('^'));
88 } 100 }
89 } 101 }
90 102
91 return buf.toString(); 103 return buf.toString();
92 } 104 }
93 } 105 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698