forked from google/protobuf.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix and test permissive compare in proto3json (google#329)
- Loading branch information
Showing
5 changed files
with
91 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// Returns true if [a] and [b] are the same ignoring case and all instances of | ||
/// `-` and `_`. | ||
/// | ||
/// This is specialized code for comparing enum names. | ||
/// Works only for ascii strings containing letters and `_` and `-`. | ||
bool permissiveCompare(String a, String b) { | ||
const dash = 45; | ||
const underscore = 95; | ||
|
||
int i = 0; | ||
int j = 0; | ||
|
||
while (true) { | ||
int ca, cb; | ||
do { | ||
ca = i < a.length ? a.codeUnitAt(i++) : -1; | ||
} while (ca == dash || ca == underscore); | ||
do { | ||
cb = j < b.length ? b.codeUnitAt(j++) : -1; | ||
} while (cb == dash || cb == underscore); | ||
if (ca == cb) { | ||
if (ca == -1) return true; // Both at end | ||
continue; | ||
} | ||
if (ca ^ cb != 0x20 || !_isAsciiLetter(ca)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
bool _isAsciiLetter(int char) { | ||
const lowerA = 97; | ||
const lowerZ = 122; | ||
const capitalA = 65; | ||
char |= lowerA ^ capitalA; | ||
return lowerA <= char && char <= lowerZ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:protobuf/src/protobuf/permissive_compare.dart'; | ||
|
||
void main() { | ||
void symmetric(String a, String b, bool expected) { | ||
expect(permissiveCompare(a, b), expected); | ||
expect(permissiveCompare(b, a), expected); | ||
} | ||
|
||
List<String> variationsFromSeed(String seed) { | ||
final result = [ | ||
seed, | ||
seed.toUpperCase(), | ||
'-$seed', | ||
'-${seed.toUpperCase()}', | ||
'_$seed', | ||
'_${seed.toUpperCase()}', | ||
'$seed-', | ||
'${seed}_', | ||
]; | ||
if (2 <= seed.length) { | ||
result.add('${seed.substring(0, 1)}_${seed.substring(1)}'); | ||
result.add('${seed.substring(0, 1)}-${seed.substring(1)}'); | ||
result.add('${seed.substring(0, 1).toUpperCase()}${seed.substring(1)}'); | ||
result.add('${seed.substring(0, 1)}${seed.substring(1).toUpperCase()}'); | ||
} | ||
return result; | ||
} | ||
|
||
test('permissive compare', () { | ||
final seeds = ['', 'a', 'b', 'aa', 'ab', 'bb', 'aaaa']; | ||
for (final a in seeds) { | ||
for (final aVariant in variationsFromSeed(a)) { | ||
for (final b in seeds) { | ||
for (final bVariant in variationsFromSeed(b)) { | ||
symmetric(aVariant, bVariant, a == b); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} |