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

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

Issue 626883003: Fix for the prefix/suffix diff computing. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 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 53da60bb35992a1ed1920bee5a24ae8bad4d49e4..b58a9d761d815b6c739deb04441c34e3aa6856f5 100644
--- a/pkg/analysis_server/lib/src/services/correction/strings.dart
+++ b/pkg/analysis_server/lib/src/services/correction/strings.dart
@@ -4,6 +4,8 @@
library services.src.correction.strings;
+import 'dart:math';
+
/**
* "$"
@@ -58,6 +60,67 @@ int countMatches(String str, String sub) {
}
/**
+ * Returns the number of characters common to the end of [a] and the start
+ * of [b].
+ */
+int findCommonOverlap(String a, String b) {
+ int a_length = a.length;
+ int b_length = b.length;
+ // all empty
+ if (a_length == 0 || b_length == 0) {
+ return 0;
+ }
+ // truncate
+ if (a_length > b_length) {
+ a = a.substring(a_length - b_length);
+ } else if (a_length < b_length) {
+ b = b.substring(0, a_length);
+ }
+ int text_length = min(a_length, b_length);
+ // the worst case
+ if (a == b) {
+ return text_length;
+ }
+ // increase common length one by one
+ int length = 0;
+ while (length < text_length) {
+ if (a.codeUnitAt(text_length - 1 - length) != b.codeUnitAt(length)) {
+ break;
+ }
+ length++;
+ }
+ return length;
+}
+
+/**
+ * Return the number of characters common to the start of [a] and [b].
+ */
+int findCommonPrefix(String a, String b) {
+ int n = min(a.length, b.length);
+ for (int i = 0; i < n; i++) {
+ if (a.codeUnitAt(i) != b.codeUnitAt(i)) {
+ return i;
+ }
+ }
+ return n;
+}
+
+/**
+ * Return the number of characters common to the end of [a] and [b].
+ */
+int findCommonSuffix(String a, String b) {
+ int a_length = a.length;
+ int b_length = b.length;
+ int n = min(a_length, b_length);
+ for (int i = 1; i <= n; i++) {
+ if (a.codeUnitAt(a_length - i) != b.codeUnitAt(b_length - i)) {
+ return i - 1;
+ }
+ }
+ return n;
+}
+
+/**
* Checks if [str] is `null`, empty or is whitespace.
*/
bool isBlank(String str) {
@@ -100,6 +163,7 @@ bool isWhitespace(int c) {
return isSpace(c) || c == 0x0D || c == 0x0A;
}
+
String remove(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;
@@ -107,6 +171,7 @@ String remove(String str, String remove) {
return str.replaceAll(remove, '');
}
+
String removeEnd(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;
@@ -117,6 +182,7 @@ String removeEnd(String str, String remove) {
return str;
}
+
String removeStart(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;

Powered by Google App Engine
This is Rietveld 408576698