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

Unified Diff: pkg/analysis_server/lib/src/services/correction/strings.dart

Issue 1849843002: Rework computing simple strings difference. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 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: pkg/analysis_server/lib/src/services/correction/strings.dart
diff --git a/pkg/analysis_server/lib/src/services/correction/strings.dart b/pkg/analysis_server/lib/src/services/correction/strings.dart
index 4a0ff769a967ab8224fae5df9343d48600153f0e..1c38cbf60fafd6ed3d3bfe81bb7faa353ff8b2a4 100644
--- a/pkg/analysis_server/lib/src/services/correction/strings.dart
+++ b/pkg/analysis_server/lib/src/services/correction/strings.dart
@@ -41,6 +41,24 @@ int compareStrings(String a, String b) {
return a.compareTo(b);
}
+/**
+ * Return a simple difference between the given [oldStr] and [newStr].
+ */
+SimpleDiff computeSimpleDiff(String oldStr, String newStr) {
+ int prefixLength = findCommonPrefix(oldStr, newStr);
+ int suffixLength = findCommonSuffix(oldStr, newStr);
+ while (prefixLength >= 0) {
+ int oldReplaceLength = oldStr.length - prefixLength - suffixLength;
+ int newReplaceLength = newStr.length - prefixLength - suffixLength;
+ if (oldReplaceLength >= 0 && newReplaceLength >= 0) {
+ return new SimpleDiff(prefixLength, oldReplaceLength,
+ newStr.substring(prefixLength, newStr.length - suffixLength));
+ }
+ prefixLength--;
+ }
+ return new SimpleDiff(0, oldStr.length, newStr);
+}
+
int countLeadingWhitespaces(String str) {
int i = 0;
for (; i < str.length; i++) {
@@ -279,3 +297,15 @@ String substringAfterLast(String str, String separator) {
}
return str.substring(pos + separator.length);
}
+
+/**
+ * Information about a single replacement that should be made to convert the
+ * "old" string to the "new" one.
+ */
+class SimpleDiff {
+ final int offset;
+ final int length;
+ final String replacement;
+
+ SimpleDiff(this.offset, this.length, this.replacement);
+}

Powered by Google App Engine
This is Rietveld 408576698