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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/source_file.dart
diff --git a/lib/compiler/implementation/source_file.dart b/lib/compiler/implementation/source_file.dart
index 2255620b3ec2e036cff9a707b54456a67d54bfc8..c32225b8f1f5ea724d0860ee18b09b6db7391a01 100644
--- a/lib/compiler/implementation/source_file.dart
+++ b/lib/compiler/implementation/source_file.dart
@@ -39,13 +39,25 @@ class SourceFile {
}
int getLine(int position) {
- // TODO(jimhug): Implement as binary search.
- var starts = lineStarts;
- for (int i=0; i < starts.length; i++) {
- if (starts[i] > position) return i-1;
+ List<int> starts = lineStarts;
+ if (position < 0 || starts.last() <= position) {
+ throw 'bad position #$position in file $filename with '
+ 'length ${text.length}.';
}
- throw 'bad position #$position in file $filename with '
- 'length ${text.length}.';
+ int first = 0;
+ int count = starts.length;
+ while (count > 1) {
+ int step = count ~/ 2;
+ int middle = first + step;
+ int lineStart = starts[middle];
+ if (position < lineStart) {
+ count = step;
+ } else {
+ first = middle;
+ count -= step;
+ }
+ }
+ return first;
}
int getColumn(int line, int position) {
« 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