예제 #1
0
        /// <summary>
        /// Set extension/private subtags in a single string representation
        /// </summary>
        public InternalLocaleBuilder SetExtensions(string subtags)
        {
            if (subtags == null || subtags.Length == 0)
            {
                ClearExtensions();
                return(this);
            }
            subtags = subtags.Replace(BaseLocale.Separator, LanguageTag.Separator);
            StringTokenEnumerator itr = new StringTokenEnumerator(subtags, LanguageTag.Separator);

            List <string> extensions = null;
            string        privateuse = null;

            int parsed = 0;
            int start;

            // Move to first element
            itr.MoveNext();

            // Make a list of extension subtags
            while (!itr.IsDone)
            {
                string s = itr.Current;
                if (LanguageTag.IsExtensionSingleton(s))
                {
                    start = itr.CurrentStart;
                    string        singleton = s;
                    StringBuilder sb        = new StringBuilder(singleton);

                    itr.MoveNext();
                    while (!itr.IsDone)
                    {
                        s = itr.Current;
                        if (LanguageTag.IsExtensionSubtag(s))
                        {
                            sb.Append(LanguageTag.Separator).Append(s);
                            parsed = itr.CurrentEnd;
                        }
                        else
                        {
                            break;
                        }
                        itr.MoveNext();
                    }

                    if (parsed < start)
                    {
                        throw new FormatException("Incomplete extension '" + singleton + "'" /*, start*/);
                    }

                    if (extensions == null)
                    {
                        extensions = new List <string>(4);
                    }
                    extensions.Add(sb.ToString());
                }
                else
                {
                    break;
                }
            }
            if (!itr.IsDone)
            {
                string s = itr.Current;
                if (LanguageTag.IsPrivateusePrefix(s))
                {
                    start = itr.CurrentStart;
                    StringBuilder sb = new StringBuilder(s);

                    itr.MoveNext();
                    while (!itr.IsDone)
                    {
                        s = itr.Current;
                        if (!LanguageTag.IsPrivateuseSubtag(s))
                        {
                            break;
                        }
                        sb.Append(LanguageTag.Separator).Append(s);
                        parsed = itr.CurrentEnd;

                        itr.MoveNext();
                    }
                    if (parsed <= start)
                    {
                        throw new FormatException("Incomplete privateuse:" + subtags.Substring(start) /*, start*/);
                    }
                    else
                    {
                        privateuse = sb.ToString();
                    }
                }
            }

            if (!itr.IsDone)
            {
                throw new FormatException("Ill-formed extension subtags:" + subtags.Substring(itr.CurrentStart) /*, itr.CurrentStart*/);
            }

            return(SetExtensions(extensions, privateuse));
        }