System.__DTString.MatchSpecifiedWords C# (CSharp) Method

MatchSpecifiedWords() private method

private MatchSpecifiedWords ( String target, bool checkWordBoundary, int &matchLength ) : bool
target String
checkWordBoundary bool
matchLength int
return bool
        internal bool MatchSpecifiedWords(String target, bool checkWordBoundary, ref int matchLength) {
            int valueRemaining = Value.Length - Index;
            matchLength = target.Length;

            if (matchLength > valueRemaining || m_info.Compare(Value, Index, matchLength, target, 0, matchLength, CompareOptions.IgnoreCase) !=0) {
                // Check word by word
                int targetPosition = 0;                 // Where we are in the target string
                int thisPosition = Index;         // Where we are in this string
                int wsIndex = target.IndexOfAny(WhiteSpaceChecks, targetPosition);
                if (wsIndex == -1) {
                    return false;
                }
                do {
                    int segmentLength = wsIndex - targetPosition;
                    if (thisPosition >= Value.Length - segmentLength) { // Subtraction to prevent overflow.
                        return false;
                    }
                    if (segmentLength == 0) {
                        // If segmentLength == 0, it means that we have leading space in the target string.
                        // In that case, skip the leading spaces in the target and this string.
                        matchLength--;
                    } else {
                        // Make sure we also have whitespace in the input string
                        if (!Char.IsWhiteSpace(Value[thisPosition + segmentLength])) {
                            return false;
                        }
                        if (m_info.Compare(Value, thisPosition, segmentLength, target, targetPosition, segmentLength, CompareOptions.IgnoreCase) !=0) {
                            return false;
                        }
                        // Advance the input string
                        thisPosition = thisPosition + segmentLength + 1;
                    }
                    // Advance our target string
                    targetPosition = wsIndex + 1;


                    // Skip past multiple whitespace
                    while (thisPosition < Value.Length && Char.IsWhiteSpace(Value[thisPosition])) {
                        thisPosition++;
                        matchLength++;
                    }
                } while ((wsIndex = target.IndexOfAny(WhiteSpaceChecks, targetPosition)) >= 0);
                // now check the last segment;
                if (targetPosition < target.Length) {
                    int segmentLength = target.Length - targetPosition;
                    if (thisPosition > Value.Length - segmentLength) {
                        return false;
                    }
                    if (m_info.Compare(Value, thisPosition, segmentLength, target, targetPosition, segmentLength, CompareOptions.IgnoreCase) !=0) {
                        return false;
                    }
                }
            }

            if (checkWordBoundary) {
                int nextCharIndex = Index + matchLength;
                if (nextCharIndex < Value.Length) {
                    if (Char.IsLetter(Value[nextCharIndex])) {
                        return (false);
                    }
                }
            }
            return (true);
        }

Usage Example

        [System.Security.SecurityCritical]  // auto-generated
        internal bool Tokenize(TokenType TokenMask, out TokenType tokenType, out int tokenValue, ref __DTString str) {
            tokenType = TokenType.UnknownToken;
            tokenValue = 0;

            TokenHashValue value;
            Contract.Assert(str.Index < str.Value.Length, "DateTimeFormatInfo.Tokenize(): start < value.Length");

            char ch = str.m_current;
            bool isLetter = Char.IsLetter(ch);
            if (isLetter) {
                ch = Char.ToLower(ch, this.Culture);
                if (IsHebrewChar(ch) && TokenMask == TokenType.RegularTokenMask) {
                    bool badFormat;
                    if (TryParseHebrewNumber(ref str, out badFormat, out tokenValue)) {
                        if (badFormat) {
                            tokenType = TokenType.UnknownToken;
                            return (false);
                        }
                        // This is a Hebrew number.
                        // Do nothing here.  TryParseHebrewNumber() will update token accordingly.
                        tokenType = TokenType.HebrewNumber;
                        return (true);
                    }
                }
            }


            int hashcode = ch % TOKEN_HASH_SIZE;
            int hashProbe = 1 + ch % SECOND_PRIME;
            int remaining = str.len - str.Index;
            int i = 0;

            TokenHashValue[] hashTable = m_dtfiTokenHash;
            if (hashTable == null) {
                hashTable = CreateTokenHashTable();
            }
            do {
                value = hashTable[hashcode];
                if (value == null) {
                    // Not found.
                    break;
                }
                // Check this value has the right category (regular token or separator token) that we are looking for.
                if (((int)value.tokenType & (int)TokenMask) > 0 && value.tokenString.Length <= remaining) {
                    if (String.Compare(str.Value, str.Index, value.tokenString, 0, value.tokenString.Length, this.Culture, CompareOptions.IgnoreCase)==0) {
                        if (isLetter) {
                            // If this token starts with a letter, make sure that we won't allow partial match.  So you can't tokenize "MarchWed" separately.
                            int nextCharIndex;
                            if ((nextCharIndex = str.Index + value.tokenString.Length) < str.len) {
                                // Check word boundary.  The next character should NOT be a letter.
                                char nextCh = str.Value[nextCharIndex];
                                if (Char.IsLetter(nextCh)) {
                                    return (false);
                                }
                            }
                        }
                        tokenType = value.tokenType & TokenMask;
                        tokenValue = value.tokenValue;
                        str.Advance(value.tokenString.Length);
                        return (true);
                    }  else if (value.tokenType == TokenType.MonthToken && HasSpacesInMonthNames) {
                        // For month token, we will match the month names which have spaces.
                        int matchStrLen = 0;
                        if (str.MatchSpecifiedWords(value.tokenString, true, ref matchStrLen)) {
                            tokenType = value.tokenType & TokenMask;
                            tokenValue = value.tokenValue;
                            str.Advance(matchStrLen);
                            return (true);
                        }
                    }  else if (value.tokenType == TokenType.DayOfWeekToken && HasSpacesInDayNames) {
                        // For month token, we will match the month names which have spaces.
                        int matchStrLen = 0;
                        if (str.MatchSpecifiedWords(value.tokenString, true, ref matchStrLen)) {
                            tokenType = value.tokenType & TokenMask;
                            tokenValue = value.tokenValue;
                            str.Advance(matchStrLen);
                            return (true);
                        }
                    }
                }
                i++;
                hashcode += hashProbe;
                if (hashcode >= TOKEN_HASH_SIZE) hashcode -= TOKEN_HASH_SIZE;
            }while (i < TOKEN_HASH_SIZE);

            return (false);
        }
All Usage Examples Of System.__DTString::MatchSpecifiedWords