Coverage report for lib/src/string_compare.dart

Line coverage: 14 / 14 (100.0%)

All files > lib/src/string_compare.dart

1
library dart_style.src.string_compare;
2
3
/// Returns `true` if [c] represents a whitespace code unit allowed in Dart
4
/// source code.
512
bool _isWhitespace(int c) => (c <= 0x000D && c >= 0x0009) || c == 0x0020;
6
7
/// Returns the index of the next non-whitespace character.
8
///
9
/// Returns `true` if current contains a non-whitespace character.
10
/// Returns `false` if no characters are left.
113
int _moveNextNonWhitespace(String str, int len, int i) {
129
  while (i < len && _isWhitespace(str.codeUnitAt(i))) {
133
    i++;
14
  }
15
  return i;
16
}
17
18
/// Returns `true` if the strings are equal ignoring whitespace characters.
193
bool equalIgnoringWhitespace(String str1, String str2) {
20
  // Benchmarks showed about a 20% regression in formatter performance when
21
  // when we use the simpler to implement solution of stripping all
22
  // whitespace characters and checking string equality. This solution is
23
  // faster due to lower memory usage and poor performance concatting strings
24
  // together one rune at a time.
25
263
  var len1 = str1.length;
273
  var len2 = str2.length;
28
  var i1 = 0;
29
  var i2 = 0;
30
31
  while (true) {
323
    i1 = _moveNextNonWhitespace(str1, len1, i1);
333
    i2 = _moveNextNonWhitespace(str2, len2, i2);
346
    if (i1 >= len1 || i2 >= len2) {
359
      return (i1 >= len1) == (i2 >= len2);
36
    }
37
389
    if (str1[i1] != str2[i2]) return false;
393
    i1++;
403
    i2++;
41
  }
42
}