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

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..c891e223e3ff5acdaa3253a9b051719c3153ffaa 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;
+ if (position < 0 || text.length <= position) {
+ throw 'bad position #$position in file $filename with '
+ 'length ${text.length}.';
}
- throw 'bad position #$position in file $filename with '
- 'length ${text.length}.';
+ List<int> starts = lineStarts;
+ int first = 0;
+ int count = starts.length;
+ while (count > 1) {
+ int step = count >> 1;
floitsch 2012/09/10 09:08:20 count ~/ 2
podivilov 2012/09/10 09:45:45 Done.
+ 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