1 | | library dart_style.src.string_compare;
|
2 | |
|
3 | | /// Returns `true` if [c] represents a whitespace code unit allowed in Dart
|
4 | | /// source code.
|
5 | 12 | 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.
|
11 | 3 | int _moveNextNonWhitespace(String str, int len, int i) {
|
12 | 9 | while (i < len && _isWhitespace(str.codeUnitAt(i))) {
|
13 | 3 | i++;
|
14 | | }
|
15 | | return i;
|
16 | | }
|
17 | |
|
18 | | /// Returns `true` if the strings are equal ignoring whitespace characters.
|
19 | 3 | 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 | |
|
26 | 3 | var len1 = str1.length;
|
27 | 3 | var len2 = str2.length;
|
28 | | var i1 = 0;
|
29 | | var i2 = 0;
|
30 | |
|
31 | | while (true) {
|
32 | 3 | i1 = _moveNextNonWhitespace(str1, len1, i1);
|
33 | 3 | i2 = _moveNextNonWhitespace(str2, len2, i2);
|
34 | 6 | if (i1 >= len1 || i2 >= len2) {
|
35 | 9 | return (i1 >= len1) == (i2 >= len2);
|
36 | | }
|
37 | |
|
38 | 9 | if (str1[i1] != str2[i2]) return false;
|
39 | 3 | i1++;
|
40 | 3 | i2++;
|
41 | | }
|
42 | | }
|