Locale: empty string equates to null (#17356)

This commit is contained in:
Gary Qian 2020-03-27 04:57:07 -07:00 committed by GitHub
parent 82c7ab09a4
commit 020d7c5c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -475,12 +475,14 @@ class Locale {
return true;
return other is Locale
&& other.languageCode == languageCode
&& other.scriptCode == scriptCode
&& other.countryCode == countryCode;
&& other.scriptCode == scriptCode // scriptCode cannot be ''
&& (other.countryCode == countryCode // Treat '' as equal to null.
|| other.countryCode != null && other.countryCode.isEmpty && countryCode == null
|| countryCode != null && countryCode.isEmpty && other.countryCode == null);
}
@override
int get hashCode => hashValues(languageCode, scriptCode, countryCode);
int get hashCode => hashValues(languageCode, scriptCode, countryCode == '' ? null : countryCode);
static Locale _cachedLocale;
static String _cachedLocaleString;

View File

@ -13,8 +13,6 @@ void main() {
expect(const Locale('en').toLanguageTag(), 'en');
expect(const Locale('en'), const Locale('en', $null));
expect(const Locale('en').hashCode, const Locale('en', $null).hashCode);
expect(const Locale('en'), isNot(const Locale('en', '')));
expect(const Locale('en').hashCode, isNot(const Locale('en', '').hashCode));
expect(const Locale('en', 'US').toLanguageTag(), 'en-US');
expect(const Locale('en', 'US').toString(), 'en_US');
expect(const Locale('iw').toLanguageTag(), 'he');
@ -52,6 +50,16 @@ void main() {
isNot(const Locale.fromSubtags(languageCode: 'en', scriptCode: 'Latn')));
expect(const Locale.fromSubtags(languageCode: 'en').hashCode,
isNot(const Locale.fromSubtags(languageCode: 'en', scriptCode: 'Latn').hashCode));
expect(const Locale('en', ''), const Locale('en'));
expect(const Locale('en'), const Locale('en', ''));
expect(const Locale('en'), const Locale('en'));
expect(const Locale('en', ''), const Locale('en', ''));
expect(const Locale('en', ''), isNot(const Locale('en', 'GB')));
expect(const Locale('en'), isNot(const Locale('en', 'GB')));
expect(const Locale('en', 'GB'), isNot(const Locale('en', '')));
expect(const Locale('en', 'GB'), isNot(const Locale('en')));
});
test('Locale toString does not include separator for \'\'', () {