Skip to content

Commit

Permalink
PR review fixes: stop using a SplayTreeSet. Add _fromComponents.
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovdm committed Oct 11, 2018
1 parent 2b30a87 commit 761c842
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions lib/ui/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,25 +177,26 @@ class Locale {
this._variants = null,
this._extensions = null;

/// Creates a new Locale object with the specified parts.
///
/// This is for internal use only. All fields must already be normalized, must
/// already be canonicalized. This method does not modify parameters in any
/// way or do any syntax checking.
///
/// * language, script and region must be in canonical form.
/// * Iterating over variants must provide variants in alphabetical order. We
/// force this by taking a SplayTreeSet as parameter input, which
/// automatically sorts its items. We then convert to a LinkedHashSet for
/// faster access and iteration.
/// * The extensions map must contain only valid key/value pairs. "u" and "t"
/// keys must be present, with an empty string as value, if there are any
/// subtags for those singletons.
// Creates a new Locale object with the specified parts.
//
// This is for internal use only. All fields must already be normalized, must
// already be canonicalized. This method does not modify parameters in any
// way or do any syntax checking.
//
// * language, script and region must be in normalized form.
// * variants must already be sorted, with each subtag in normalized form.
// * Iterating over variants must provide variants in alphabetical order. We
// force this by taking a SplayTreeSet as parameter input, which
// automatically sorts its items. We then convert to a LinkedHashSet for
// faster access and iteration.
// * The extensions map must contain only valid key/value pairs. "u" and "t"
// keys must be present, with an empty string as value, if there are any
// subtags for those singletons.
Locale._internal(
String language, {
String script,
String region,
collection.SplayTreeSet<String> variants,
List<String> variants,
collection.SplayTreeMap<String, String> extensions,
}) : assert(language != null),
assert(language.length >= 2 && language.length <= 8),
Expand All @@ -208,6 +209,29 @@ class Locale {
_variants = List<String>.unmodifiable(variants ?? const <String>[]),
_extensions = collection.LinkedHashMap<String, String>.from(extensions ?? const <String, String>{});

// TODO: unit-test this, pick the right name, make it public, update this
// documentation.
//
// TODO: might we prefer constructing with a single string as "variants"
// rather than a list of strings?
//
// TODO: this constructor modifies the list passed to variants.Is this the
// right design? "We mutate what you give us!" feels bad.
//
// Extensions are not yet supported by this constructor, it would require
// first finalizing the design of the extensions map we wish to expose.
factory Locale._fromComponents({
String language,
String script,
String region,
List<String> variants,
}) {
if (variants != null) {
variants.sort();
}
return Locale._internal(language, script: script, region: region, variants: variants);
}

/// Parses [Unicode CLDR Locale
/// Identifiers](https://www.unicode.org/reports/tr35/#Identifiers).
///
Expand All @@ -224,8 +248,7 @@ class Locale {

final List<String> localeSubtags = localeId.split(_reSep);
String language, script, region;
final collection.SplayTreeSet<String> variants =
collection.SplayTreeSet<String>();
final List<String> variants = <String>[];
// Using a SplayTreeMap for its automatic key sorting.
final collection.SplayTreeMap<String, String> extensions =
collection.SplayTreeMap<String, String>();
Expand Down Expand Up @@ -274,6 +297,7 @@ class Locale {
'${problems.join("; ")}.');
}

variants.sort();
return Locale._internal(
language,
script: script,
Expand Down

0 comments on commit 761c842

Please sign in to comment.