예제 #1
0
        internal static PrivateUseSubtags Parse(LanguageTag.TokenEnumerator tokens)
        {
            var result = new PrivateUseSubtags();

            if (!tokens.TokenIs(Singleton))
            {
                throw new FormatException("unexpected subtag '" + tokens.Token + "'");
            }

            var subtags = new List <string>();

            while (tokens.NextTokenAvailable)             // get all subtags
            {
                tokens.ToNextToken();
                subtags.Add(ValidateSubtag(tokens.Token));
            }

            if (subtags.Count == 0)
            {
                throw new FormatException("private use subtags not contain elements");
            }

            result._subtags = subtags.ToArray();
            return(result);
        }
예제 #2
0
        internal static Region?TryParseRegion(this LanguageTag.TokenEnumerator tokens)
        {
            var region = tokens.Token.TryParseFromRegion();

            if (region != null)
            {
                tokens.ToNextToken();
            }

            return(region);
        }
예제 #3
0
        internal static Script?TryParseScript(this LanguageTag.TokenEnumerator tokens)
        {
            var script = tokens.Token.TryParseFromScript();

            if (script != null)
            {
                tokens.ToNextToken();
            }

            return(script);
        }
예제 #4
0
        internal static ExtensionSubtag?TryParse(LanguageTag.TokenEnumerator tokens)
        {
            if (!tokens.CurrentTokenAvailable)             // get singletone
            {
                return(null);
            }

            if (tokens.Token.Length != 1)
            {
                return(null);
            }

            if (tokens.TokenIs(PrivateUseSubtags.Singleton))
            {
                return(null);
            }

            char singleton = ValidateSingleton(tokens.Token[0]);

            if (!tokens.NextTokenAvailable)
            {
                throw new FormatException("extension subtag '" + singleton + "' not contain elements");
            }

            var sequence = new List <string>();

            tokens.ToNextToken();

            sequence.Add(ValidateElement(tokens.Token));
            tokens.ToNextToken();             // get remaining elements

            while (tokens.CurrentTokenAvailable)
            {
                if (tokens.Token.Length == 1)                 // next extension subtag or private use
                {
                    break;
                }

                sequence.Add(ValidateElement(tokens.Token));
                tokens.ToNextToken();
            }

            var result = new ExtensionSubtag();

            result.Singleton = singleton;
            result._sequence = sequence.ToArray();

            return(result);
        }
예제 #5
0
        internal static Variant?TryParseVariant(this LanguageTag.TokenEnumerator tokens)
        {
            if (!tokens.CurrentTokenAvailable)
            {
                return(null);
            }

            var variant = tokens.Token.TryParseFromVariant();

            if (variant != null)
            {
                tokens.ToNextToken();
            }

            return(variant);
        }
예제 #6
0
        internal static ExtensionSubtagCollection TryParse(LanguageTag.TokenEnumerator tokens)
        {
            var result = new ExtensionSubtagCollection();
            var subtag = ExtensionSubtag.TryParse(tokens);

            if (subtag.HasValue)
            {
                result._sortedList = new List <ExtensionSubtag>();
                do
                {
                    result.Append(subtag.Value);
                    subtag = ExtensionSubtag.TryParse(tokens);
                }while (subtag.HasValue);
            }

            return(result);
        }
예제 #7
0
        public static Language ParseFromLanguage(this string text)
        {
            try
            {
                var tokens = new LanguageTag.TokenEnumerator(text);
                var result = ParseLanguage(tokens);
                if (tokens.CurrentTokenAvailable)
                {
                    throw new FormatException("unexpected language '" + tokens.Token + "'");
                }

                return(result);
            }
            catch (FormatException ex)
            {
                throw new FormatException("unexpected language '" + text + "'", ex);
            }
        }
예제 #8
0
        internal static VariantCollection TryParse(Language?lang, Script?script, LanguageTag.TokenEnumerator tokens)
        {
            var variant = tokens.TryParseVariant();

            if (!variant.HasValue)
            {
                return(new VariantCollection());
            }

            var builder = new VariantCollection.Builder(lang, script);

            do
            {
                builder.Append(variant.Value);
                variant = tokens.TryParseVariant();
            }while (variant.HasValue);

            return(builder.ToCollection());
        }
예제 #9
0
        internal static Language ParseLanguage(this LanguageTag.TokenEnumerator tokens)
        {
            if (!tokens.NextTokenAvailable)
            {
                var result = tokens.Token.TryParseFromLanguage();
                if (!result.HasValue)
                {
                    throw new FormatException("unexpected language '" + tokens.Token + "'");
                }

                tokens.ToNextToken();
                return(result.Value);
            }

            var lang = tokens.Token.TryParseFromLanguage();

            if (lang == null)
            {
                throw new FormatException("unexpected language '" + tokens.Token + "'");
            }

            tokens.ToNextToken();

            if (!lang.Value.ExtLanguageAvailable())
            {
                return(lang.Value);
            }

            var extLang = tokens.Token.TryParseFromExtLanguage(lang.Value);

            if (extLang == null)
            {
                return(lang.Value);
            }

            tokens.ToNextToken();
            return(extLang.Value);
        }