| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library services.src.correction.strings; | 5 library services.src.correction.strings; |
| 6 | 6 |
| 7 import 'dart:math'; |
| 8 |
| 7 | 9 |
| 8 /** | 10 /** |
| 9 * "$" | 11 * "$" |
| 10 */ | 12 */ |
| 11 const int CHAR_DOLLAR = 0x24; | 13 const int CHAR_DOLLAR = 0x24; |
| 12 | 14 |
| 13 /** | 15 /** |
| 14 * "." | 16 * "." |
| 15 */ | 17 */ |
| 16 const int CHAR_DOT = 0x2E; | 18 const int CHAR_DOT = 0x2E; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 int count = 0; | 53 int count = 0; |
| 52 int idx = 0; | 54 int idx = 0; |
| 53 while ((idx = str.indexOf(sub, idx)) != -1) { | 55 while ((idx = str.indexOf(sub, idx)) != -1) { |
| 54 count++; | 56 count++; |
| 55 idx += sub.length; | 57 idx += sub.length; |
| 56 } | 58 } |
| 57 return count; | 59 return count; |
| 58 } | 60 } |
| 59 | 61 |
| 60 /** | 62 /** |
| 63 * Returns the number of characters common to the end of [a] and the start |
| 64 * of [b]. |
| 65 */ |
| 66 int findCommonOverlap(String a, String b) { |
| 67 int a_length = a.length; |
| 68 int b_length = b.length; |
| 69 // all empty |
| 70 if (a_length == 0 || b_length == 0) { |
| 71 return 0; |
| 72 } |
| 73 // truncate |
| 74 if (a_length > b_length) { |
| 75 a = a.substring(a_length - b_length); |
| 76 } else if (a_length < b_length) { |
| 77 b = b.substring(0, a_length); |
| 78 } |
| 79 int text_length = min(a_length, b_length); |
| 80 // the worst case |
| 81 if (a == b) { |
| 82 return text_length; |
| 83 } |
| 84 // increase common length one by one |
| 85 int length = 0; |
| 86 while (length < text_length) { |
| 87 if (a.codeUnitAt(text_length - 1 - length) != b.codeUnitAt(length)) { |
| 88 break; |
| 89 } |
| 90 length++; |
| 91 } |
| 92 return length; |
| 93 } |
| 94 |
| 95 /** |
| 96 * Return the number of characters common to the start of [a] and [b]. |
| 97 */ |
| 98 int findCommonPrefix(String a, String b) { |
| 99 int n = min(a.length, b.length); |
| 100 for (int i = 0; i < n; i++) { |
| 101 if (a.codeUnitAt(i) != b.codeUnitAt(i)) { |
| 102 return i; |
| 103 } |
| 104 } |
| 105 return n; |
| 106 } |
| 107 |
| 108 /** |
| 109 * Return the number of characters common to the end of [a] and [b]. |
| 110 */ |
| 111 int findCommonSuffix(String a, String b) { |
| 112 int a_length = a.length; |
| 113 int b_length = b.length; |
| 114 int n = min(a_length, b_length); |
| 115 for (int i = 1; i <= n; i++) { |
| 116 if (a.codeUnitAt(a_length - i) != b.codeUnitAt(b_length - i)) { |
| 117 return i - 1; |
| 118 } |
| 119 } |
| 120 return n; |
| 121 } |
| 122 |
| 123 /** |
| 61 * Checks if [str] is `null`, empty or is whitespace. | 124 * Checks if [str] is `null`, empty or is whitespace. |
| 62 */ | 125 */ |
| 63 bool isBlank(String str) { | 126 bool isBlank(String str) { |
| 64 if (str == null) { | 127 if (str == null) { |
| 65 return true; | 128 return true; |
| 66 } | 129 } |
| 67 if (str.isEmpty) { | 130 if (str.isEmpty) { |
| 68 return true; | 131 return true; |
| 69 } | 132 } |
| 70 return str.codeUnits.every(isSpace); | 133 return str.codeUnits.every(isSpace); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 93 bool isSpace(int c) => c == 0x20 || c == 0x09; | 156 bool isSpace(int c) => c == 0x20 || c == 0x09; |
| 94 | 157 |
| 95 bool isUpperCase(int c) { | 158 bool isUpperCase(int c) { |
| 96 return c >= 0x41 && c <= 0x5A; | 159 return c >= 0x41 && c <= 0x5A; |
| 97 } | 160 } |
| 98 | 161 |
| 99 bool isWhitespace(int c) { | 162 bool isWhitespace(int c) { |
| 100 return isSpace(c) || c == 0x0D || c == 0x0A; | 163 return isSpace(c) || c == 0x0D || c == 0x0A; |
| 101 } | 164 } |
| 102 | 165 |
| 166 |
| 103 String remove(String str, String remove) { | 167 String remove(String str, String remove) { |
| 104 if (isEmpty(str) || isEmpty(remove)) { | 168 if (isEmpty(str) || isEmpty(remove)) { |
| 105 return str; | 169 return str; |
| 106 } | 170 } |
| 107 return str.replaceAll(remove, ''); | 171 return str.replaceAll(remove, ''); |
| 108 } | 172 } |
| 109 | 173 |
| 174 |
| 110 String removeEnd(String str, String remove) { | 175 String removeEnd(String str, String remove) { |
| 111 if (isEmpty(str) || isEmpty(remove)) { | 176 if (isEmpty(str) || isEmpty(remove)) { |
| 112 return str; | 177 return str; |
| 113 } | 178 } |
| 114 if (str.endsWith(remove)) { | 179 if (str.endsWith(remove)) { |
| 115 return str.substring(0, str.length - remove.length); | 180 return str.substring(0, str.length - remove.length); |
| 116 } | 181 } |
| 117 return str; | 182 return str; |
| 118 } | 183 } |
| 119 | 184 |
| 185 |
| 120 String removeStart(String str, String remove) { | 186 String removeStart(String str, String remove) { |
| 121 if (isEmpty(str) || isEmpty(remove)) { | 187 if (isEmpty(str) || isEmpty(remove)) { |
| 122 return str; | 188 return str; |
| 123 } | 189 } |
| 124 if (str.startsWith(remove)) { | 190 if (str.startsWith(remove)) { |
| 125 return str.substring(remove.length); | 191 return str.substring(remove.length); |
| 126 } | 192 } |
| 127 return str; | 193 return str; |
| 128 } | 194 } |
| 129 | 195 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 147 } | 213 } |
| 148 if (isEmpty(separator)) { | 214 if (isEmpty(separator)) { |
| 149 return ''; | 215 return ''; |
| 150 } | 216 } |
| 151 int pos = str.lastIndexOf(separator); | 217 int pos = str.lastIndexOf(separator); |
| 152 if (pos == -1) { | 218 if (pos == -1) { |
| 153 return str; | 219 return str; |
| 154 } | 220 } |
| 155 return str.substring(pos + separator.length); | 221 return str.substring(pos + separator.length); |
| 156 } | 222 } |
| OLD | NEW |