Exemplo n.º 1
0
        /// <summary>
        /// Set a list of BCP47 extensions and private use subtags.
        /// BCP47 extensions are already validated and well-formed, but may contain duplicates.
        /// </summary>
        private InternalLocaleBuilder SetExtensions(IList <string> bcpExtensions, string privateuse)
        {
            ClearExtensions();

            if (bcpExtensions != null && bcpExtensions.Count > 0)
            {
                HashSet <CaseInsensitiveChar> processedExtensions = new HashSet <CaseInsensitiveChar>(/*bcpExtensions.Count*/);
                foreach (string bcpExt in bcpExtensions)
                {
                    CaseInsensitiveChar key = new CaseInsensitiveChar(bcpExt[0]);
                    // ignore duplicates
                    if (!processedExtensions.Contains(key))
                    {
                        // each extension string contains singleton, e.g. "a-abc-def"
                        if (UnicodeLocaleExtension.IsSingletonChar(key.Value))
                        {
                            SetUnicodeLocaleExtension(bcpExt.Substring(2));
                        }
                        else
                        {
                            if (_extensions == null)
                            {
                                _extensions = new Dictionary <CaseInsensitiveChar, string>(4);
                            }
                            _extensions[key] = bcpExt.Substring(2);
                        }
                    }
                }
            }
            if (privateuse != null && privateuse.Length > 0)
            {
                // privateuse string contains prefix, e.g. "x-abc-def"
                if (_extensions == null)
                {
                    _extensions = new Dictionary <CaseInsensitiveChar, string>(1);
                }
                _extensions[new CaseInsensitiveChar(privateuse[0])] = privateuse.Substring(2);
            }

            return(this);
        }
Exemplo n.º 2
0
        public InternalLocaleBuilder SetExtension(char singleton, string value)
        {
            // validate key
            bool isBcpPrivateuse = LanguageTag.IsPrivateusePrefixChar(singleton);

            if (!isBcpPrivateuse && !LanguageTag.IsExtensionSingletonChar(singleton))
            {
                throw new FormatException("Ill-formed extension key: " + singleton);
            }

            bool remove             = (value == null || value.Length == 0);
            CaseInsensitiveChar key = new CaseInsensitiveChar(singleton);

            if (remove)
            {
                if (UnicodeLocaleExtension.IsSingletonChar(key.Value))
                {
                    // clear entire Unicode locale extension
                    if (_uattributes != null)
                    {
                        _uattributes.Clear();
                    }
                    if (_ukeywords != null)
                    {
                        _ukeywords.Clear();
                    }
                }
                else
                {
                    if (_extensions != null && _extensions.ContainsKey(key))
                    {
                        _extensions.Remove(key);
                    }
                }
            }
            else
            {
                // validate value
                string val = value.Replace(BaseLocale.Separator, LanguageTag.Separator);
                StringTokenEnumerator itr = new StringTokenEnumerator(val, LanguageTag.Separator);
                while (itr.MoveNext())
                {
                    string s = itr.Current;
                    bool   validSubtag;
                    if (isBcpPrivateuse)
                    {
                        validSubtag = LanguageTag.IsPrivateuseSubtag(s);
                    }
                    else
                    {
                        validSubtag = LanguageTag.IsExtensionSubtag(s);
                    }
                    if (!validSubtag)
                    {
                        throw new FormatException("Ill-formed extension value: " + s /*, itr.CurrentStart*/);
                    }
                }

                if (UnicodeLocaleExtension.IsSingletonChar(key.Value))
                {
                    SetUnicodeLocaleExtension(val);
                }
                else
                {
                    if (_extensions == null)
                    {
                        _extensions = new Dictionary <CaseInsensitiveChar, string>(4);
                    }
                    _extensions[key] = val;
                }
            }
            return(this);
        }