Skip to content

Commit 8debf33

Browse files
authored
Clarify Characters equality and Unicode normalization (dart-lang#1031)
Fixes dart-lang#395 - Clarify that `Characters.operator ==` compares the underlying strings without Unicode normalization. - Add a focused test using precomposed and decomposed forms of the same grapheme. - Record the documentation change in the changelog.
1 parent d7bd45c commit 8debf33

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎pkgs/characters/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 1.4.2-wip
22

3+
* Clarify that `Characters` equality does not normalize Unicode strings.
4+
35
## 1.4.1
46

57
* Run `dart format` with the new style.

‎pkgs/characters/lib/src/characters.dart‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,13 @@ abstract class Characters implements Iterable<String> {
288288
@override
289289
int get hashCode;
290290

291-
/// Whether [other] to another [Characters] with the same [string].
291+
/// Whether [other] is a [Characters] with an equal [string].
292+
///
293+
/// The underlying strings are compared directly, without Unicode
294+
/// normalization. For example, `Characters('\u00e9')` and
295+
/// `Characters('e\u0301')` are not equal, even though both contain one
296+
/// grapheme cluster. Normalize the strings before comparing if canonically
297+
/// equivalent text should be considered equal.
292298
@override
293299
bool operator ==(Object other);
294300

‎pkgs/characters/test/characters_test.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ void main([List<String>? args]) {
2626
group('[Random Seed: $seed]', tests);
2727

2828
group('characters', () {
29+
test('equality does not normalize Unicode', () {
30+
var composed = Characters('\u00e9');
31+
var decomposed = Characters('e\u0301');
32+
33+
expect(composed.length, 1);
34+
expect(decomposed.length, 1);
35+
expect(composed, Characters('\u00e9'));
36+
expect(composed, isNot(equals(decomposed)));
37+
});
38+
2939
test('operations', () {
3040
var flag = '\u{1F1E9}\u{1F1F0}'; // Regional Indicators "DK".
3141
var string = 'Hi $flag!';

0 commit comments

Comments
 (0)