public InternalLocaleBuilder SetUnicodeLocaleKeyword(string key, string type) { if (!UnicodeLocaleExtension.IsKey(key)) { throw new FormatException("Ill-formed Unicode locale keyword key: " + key); } CaseInsensitiveString cikey = new CaseInsensitiveString(key); if (type == null) { if (_ukeywords != null) { // null type is used for remove the key _ukeywords.Remove(cikey); } } else { if (type.Length != 0) { // normalize separator to "-" string tp = type.Replace(BaseLocale.Separator, LanguageTag.Separator); // validate StringTokenEnumerator itr = new StringTokenEnumerator(tp, LanguageTag.Separator); while (itr.MoveNext()) { string s = itr.Current; if (!UnicodeLocaleExtension.IsTypeSubtag(s)) { throw new FormatException("Ill-formed Unicode locale keyword type: " + type /*, itr.CurrentStart*/); } } } if (_ukeywords == null) { _ukeywords = new Dictionary <CaseInsensitiveString, string>(4); } _ukeywords[cikey] = type; } return(this); }
/// <summary> /// Private methods parsing Unicode Locale Extension subtags. /// Duplicated attributes/keywords will be ignored. /// The input must be a valid extension subtags (excluding singleton). /// </summary> private void SetUnicodeLocaleExtension(string subtags) { // wipe out existing attributes/keywords if (_uattributes != null) { _uattributes.Clear(); } if (_ukeywords != null) { _ukeywords.Clear(); } StringTokenEnumerator itr = new StringTokenEnumerator(subtags, LanguageTag.Separator); // parse attributes while (itr.MoveNext()) { if (!UnicodeLocaleExtension.IsAttribute(itr.Current)) { break; } if (_uattributes == null) { _uattributes = new HashSet <CaseInsensitiveString>(/*4*/); } _uattributes.Add(new CaseInsensitiveString(itr.Current)); } // parse keywords CaseInsensitiveString key = null; string type; int typeStart = -1; int typeEnd = -1; while (!itr.IsDone) { if (key != null) { if (UnicodeLocaleExtension.IsKey(itr.Current)) { // next keyword - emit previous one Debug.Assert(typeStart == -1 || typeEnd != -1); type = (typeStart == -1) ? "" : subtags.Substring(typeStart, typeEnd - typeStart); // ICU4N: Corrected 2nd parameter if (_ukeywords == null) { _ukeywords = new Dictionary <CaseInsensitiveString, string>(4); } _ukeywords[key] = type; // reset keyword info CaseInsensitiveString tmpKey = new CaseInsensitiveString(itr.Current); key = _ukeywords.ContainsKey(tmpKey) ? null : tmpKey; typeStart = typeEnd = -1; } else { if (typeStart == -1) { typeStart = itr.CurrentStart; } typeEnd = itr.CurrentEnd; } } else if (UnicodeLocaleExtension.IsKey(itr.Current)) { // 1. first keyword or // 2. next keyword, but previous one was duplicate key = new CaseInsensitiveString(itr.Current); if (_ukeywords != null && _ukeywords.ContainsKey(key)) { // duplicate key = null; } } if (!itr.HasNext) { if (key != null) { // last keyword Debug.Assert(typeStart == -1 || typeEnd != -1); type = (typeStart == -1) ? "" : subtags.Substring(typeStart, typeEnd - typeStart); // ICU4N: Corrected 2nd parameter if (_ukeywords == null) { _ukeywords = new Dictionary <CaseInsensitiveString, string>(4); } _ukeywords[key] = type; } break; } itr.MoveNext(); } }
public static bool IsValidUnicodeLocaleKey(string ukey) { return(UnicodeLocaleExtension.IsKey(ukey)); }