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

Unified Diff: lib/src/utils.dart

Issue 1041163005: Support multiple occurrences of text in context (Closed) Base URL: git@github.com:dart-lang/source_span.git@master
Patch Set: Created 5 years, 9 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
Index: lib/src/utils.dart
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index 4a8eb551e5ca09a67c078a4e34a44719eb27bc64..d905188429474ee20bf627ed2dee696de7a88a47 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -37,3 +37,19 @@ int binarySearch(List list, bool matches(item)) {
return max;
}
+/// Finds a line in [context] containing [text] at the specified [column].
+///
+/// Returns the index in [context] where that line begins, or -1 if none exists.
nweiz 2015/03/31 19:22:10 Nit: I really like using "null" as a signal for "n
Siggi Cherem (dart-lang) 2015/03/31 19:51:21 Done.
+int findLineStart(String context, String text, int column) {
+ var isEmpty = text == '';
+ var index = context.indexOf(text);
+ while (index != -1) {
+ var lineStart = context.lastIndexOf('\n', index) + 1;
+ var textColumn = index - lineStart;
+ if (column == textColumn || (isEmpty && column == textColumn + 1)) {
+ return lineStart;
+ }
+ index = context.indexOf(text, index + 1);
+ }
+ return -1;
+}

Powered by Google App Engine
This is Rietveld 408576698