Ejemplo n.º 1
0
        /// <summary>
        /// Initializes new value of <see cref="LanguageTag"/>
        /// </summary>
        private LanguageTag(
            Language?language,
            Script?script,
            Region?region,
            VariantCollection variants,
            ExtensionSubtagCollection extensions,
            ExtensionSubtag privateUse)
        {
            if (language != null && script != null)
            {
                var suppressScript = language.SuppressScript;
                if (suppressScript != null && suppressScript == script)
                {
                    script = null;
                }
            }

            Language   = language;
            Script     = script;
            Region     = region;
            Variants   = variants;
            Extensions = extensions;
            PrivateUse = privateUse;

            var fields = Field.None;

            if (language != null)
            {
                fields |= Field.Language;
            }
            if (script != null)
            {
                fields |= Field.Script;
            }
            if (region != null)
            {
                fields |= Field.Region;
            }
            if (!variants.IsEmpty)
            {
                fields |= Field.Variants;
            }
            if (!extensions.IsEmpty)
            {
                fields |= Field.Extensions;
            }
            if (!privateUse.IsEmpty)
            {
                if (!privateUse.PrivateUse)
                {
                    throw new ArgumentOutOfRangeException(nameof(privateUse), "must be a private use subtag");
                }
                fields |= Field.PrivateUse;
            }

            Fields = fields;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes new value of <see cref="LanguageTag"/>
        /// </summary>
        private LanguageTag(
            Language?language,
            Script?script,
            Region?region,
            VariantCollection variants,
            ExtensionSubtagCollection extensions,
            PrivateUseSubtags privateUse)
        {
            if (language.HasValue && script.HasValue)
            {
                var suppressScript = language.Value.GetSuppressScript();
                if (suppressScript.HasValue && suppressScript.Value == script.Value)
                {
                    script = null;
                }
            }

            Language   = language;
            Script     = script;
            Region     = region;
            Variants   = variants;
            Extensions = extensions;
            PrivateUse = privateUse;

            var fields = Field.None;

            if (language.HasValue)
            {
                fields |= Field.Language;
            }
            if (script.HasValue)
            {
                fields |= Field.Script;
            }
            if (region.HasValue)
            {
                fields |= Field.Region;
            }
            if (!variants.IsEmpty)
            {
                fields |= Field.Variants;
            }
            if (!extensions.IsEmpty)
            {
                fields |= Field.Extensions;
            }
            if (!privateUse.IsEmpty)
            {
                fields |= Field.PrivateUse;
            }

            Fields = fields;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Tries to parse <see cref="LanguageTag"/> value from its text representation
        /// </summary>
        /// <param name="text">text representation of language tag</param>
        /// <param name="result">parsed value</param>
        /// <returns>true if parsing was successful, false otherwise</returns>
        public static bool TryParse(string text, out LanguageTag result)
        {
            var tokens = new TokenEnumerator(Grandfathered.GetPreferredValue(text) ?? text);

            if (tokens.TokenIs(PrivateUseSubtags.Singleton))
            {
                // This tag starts with private use subtags
                var immediatePrivateUseResult = PrivateUseSubtags.Parse(tokens);

                var error = immediatePrivateUseResult.NothingToParse || immediatePrivateUseResult.ErrorOccured ||
                            tokens.CurrentTokenAvailable;

                if (error)
                {
                    // But they are invalid
                    result = default;
                    return(false);
                }

                result = new LanguageTag(immediatePrivateUseResult.Result);
                return(true);
            }

            var language = tokens.TryParseLanguage();

            if (!language.HasValue)
            {
                // If language tag is not entirely private use subtags, it must contain language
                result = default;
                return(false);
            }

            // These two cannot produce error during parsing
            var script = tokens.TryParseScript();
            var region = tokens.TryParseRegion();

            // These three can
            var variantsResult = VariantCollection.Parse(language, script, tokens);

            if (variantsResult.ErrorOccured)
            {
                result = default;
                return(false);
            }

            var extensionsResult = ExtensionSubtagCollection.Parse(tokens);

            if (extensionsResult.ErrorOccured)
            {
                result = default;
                return(false);
            }

            var privateUseResult = PrivateUseSubtags.Parse(tokens);

            if (privateUseResult.ErrorOccured)
            {
                result = default;
                return(false);
            }

            if (tokens.CurrentTokenAvailable)
            {
                // Something left unparsed
                result = default;
                return(false);
            }

            var variants = variantsResult.NothingToParse ? default : variantsResult.Result;

                           var extensions = extensionsResult.NothingToParse ? default : extensionsResult.Result;

                                            var privateUse = privateUseResult.NothingToParse ? default : privateUseResult.Result;

                                                             result = new LanguageTag(language, script, region, variants, extensions, privateUse);
                                                             return(true);
        }
		/// <summary>
		/// Indicates whether the current object is equal to another object of the same type.
		/// </summary>
		public bool Equals(ExtensionSubtagCollection other)
		{
			return _sortedCollection.IsEquivalentTo(other._sortedCollection);
		}