/// <summary>
        /// Finds a Node or a Key from a BaseToken collection by using the Name field and return the index of the element if found.
        /// </summary>
        /// <param name="tokens">The collection of BaseToken to search through.</param>
        /// <param name="Name">The name of the node that the method needs to find.</param>
        /// <param name="CaseSensitive">Indicates if the name argument and the name of the BaseToken needs to be an exact match in terms of capitalization.</param>
        /// <param name="throwErrorIfNotFound">Throw an exception if the BaseToken could not be found. If false, method will return -1 if element could not be found.</param>
        /// <returns></returns>
        internal static int FindBaseTokenIndex(this IEnumerable <BaseToken> tokens, string Name, bool CaseSensitive = false, bool throwErrorIfNotFound = false)
        {
            int tokensLength = tokens.Count(); //Store the length of our tokens list to a variable

            if (!CaseSensitive)                //If CaseSensitve is set to false, then we convert the Name argument to lower case.
            {
                Name = Name.ToLower();
            }
            for (int i = 0; i < tokensLength; ++i)
            {
                string    tokenName;                          //This will store the name of the BaseToken that we are checking.
                BaseToken currentToken = tokens.ElementAt(i); //Cache the BaseToken that we are working with.
                if (CaseSensitive)                            //If CaseSensitive is set to true, set the tokenName variable to the name of the currentToken without doing any string manipulation.
                {
                    tokenName = currentToken.Name;
                }
                else //If CasenSensitive is set  to false, set the tokenName variable to the lower case equivalent of the currentToken's name.
                {
                    tokenName = currentToken.Name.ToLower();
                }

                if (tokenName == Name)
                {
                    return(i);
                }
            }
            if (throwErrorIfNotFound) //We're done looping through our collection and we haven't found the currentToken.
            {
                throw new TokenNotFoundException(Name + " has not been found in the collection!");
            }
            return(-1); //If throwError is set to false, then return -1 instead.
        }
        /// <summary>
        /// Finds a node in a node collection by using the Name field.
        /// </summary>
        /// <param name="nodes">The collection of nodes to search through.</param>
        /// <param name="Name">The name of the node that the method needs to find.</param>
        /// <param name="CaseSensitive">Indicates if the name argument and the name of the node needs to be an exact match in terms of capitalization.</param>
        /// <param name="throwErrorIfNotFound">Throw an exception if the node could not be found. If false, method will return null instead.</param>
        /// <returns></returns>
        public static VDFNode FindNode(this IEnumerable <VDFNode> nodes, string Name, bool CaseSensitive = false, bool throwErrorIfNotFound = false)
        {
            BaseToken baseToken = nodes.FindBaseToken(Name, CaseSensitive, throwErrorIfNotFound); //Use the baseToken GetBaseToken extension method to search for the node.

            if (baseToken != null)                                                                //If the result isn't null, we return the Node that was found. We must cast it back to a node type as well.
            {
                return((VDFNode)baseToken);
            }
            return(null);
        }
Beispiel #3
0
        /// <summary>
        ///  Finds a Key in a Key collection by using the Name field.
        /// </summary>
        /// <param name="keys">The key collection that contains the key that the method will search for.</param>
        /// <param name="Name">The name of the key that the method will search for.</param>
        /// <param name="CaseSensitive">Indicates if the name argument and the name of the node needs to be an exact match in terms of capitalization.</param>
        /// <param name="throwErrorIfNotFound">Throw an exception if the key could not be found. If false, method will return null instead.</param>
        /// <returns></returns>
        public static VDFKey FindKey(this IEnumerable <VDFKey> keys, string Name, bool CaseSensitive = false, bool throwErrorIfNotFound = false)
        {
            BaseToken baseToken = keys.FindBaseToken(Name, CaseSensitive, throwErrorIfNotFound);

            if (baseToken != null)
            {
                return((VDFKey)baseToken);
            }
            return(null);
        }