Example #1
0
        // Match and compare...
        // Find, but with a mask (using ? and *)
        public bool Matches(ktString Mask)
        {
            /* ** <note>
               For now we just cross our fingers and hopes that this function (/method?)
               actually works. We haven't runned this code yet.
                (BTW this goes with most of the whole class...)</note>
            ***/
            bool Match = false;
            bool GoOn = true;

            int Pos;
            ktTripple Joker = 0; // 1 == * and 2 == ?

            ktString MatchTmp;
            ktString Str = new ktString(GetValue());

            // Don't stop until we say so
            while (GoOn)
            {
                Joker = 0;

                // If there's no more "Joker"
                if ((Pos = ktRegEx.Find(Mask.GetValue(), "\\*|\\?")) < 0)
                {
                    // If (the rest of) the mask matches the string
                    if (Mask == Str)
                    {
                        // Stop looping
                        GoOn = false;
                        // And "OK it"
                        Match = true;
                    }
                    else
                    {
                        // Stop looping
                        GoOn = false;
                        // Didn' match...
                        Match = false;
                    }
                }
                else
                {
                    // Find a "Joker" and get everything infront of it
                    MatchTmp = Mask.SubStr(0, Pos);
                    // Remove what we got from the mask
                    Mask.Remove(0, MatchTmp.Length());

                    // Chech the "Joker"
                    //  If the first char is * in the "new mask"
                    //   then indicate that
                    if (Mask.First() == '*')
                        Joker = 1;
                    //  Or if it's ?...
                    else
                        Joker = 2;

                    // Remove the "Joker"
                    Mask.RemoveFirst();

                    // If this part of the mask doesn't match... (simply not a match)
                    if ((!MatchTmp.IsEmpty()) && (MatchTmp != Str.SubStr(0, MatchTmp.Length())))
                    {
                        // Stop looping
                        GoOn = false;
                        Match = false;
                    }
                    else
                    {
                        // As we now that this part of the mask matches the string
                        //  Remove that part from the string
                        Str.Remove(0, MatchTmp.Length());

                        // If the "Joker" is *
                        if (Joker == 1)
                        {
                            // Get the position of the next "Joker"
                            Pos = ktRegEx.Find(Mask.GetValue(), "\\*|\\?");

                            // If we didn't find a new "Joker"
                            if (Pos < 0)
                            {
                                // get the length of the rest of the mask ("find the end")
                                Pos = Mask.Length();
                                // If Pos is 0
                                if (Pos == 0)
                                    // No more mask...
                                    Pos = -1;
                            }

                            // If Pos is less than 0
                            //  This (should) means that the * was the last thing in
                            //   the mask and should therefor match the rest of the string
                            if (Pos < 0)
                            {
                                // Stop looping
                                GoOn = false;
                                // It's a match...
                                Match = true;
                            }
                            else
                            {
                                // Get the next part of the mask
                                MatchTmp = Mask.SubStr(0, Pos);
                                // Remove that part of the mask...
                                Mask.Remove(0, Pos);

                                // If the "submask" matches the corresponding "substring"
                                if ((Pos = Str.Find(MatchTmp)) >= 0)
                                {
                                    // The substring matched...
                                    Match = true;
                                    // Remove the matched "substring"
                                    Str.Remove(0, Pos + MatchTmp.Length());

                                    // If the mask now is empty
                                    if (Mask.IsEmpty())
                                    {
                                        // Stop looping
                                        GoOn = false;
                                        // If the string isn't empty
                                        if (!Str.IsEmpty())
                                            // As the mask is empty but not the
                                            //  the string, it means that it's
                                            //  an missmatch...
                                            Match = false;
                                    }
                                    // It wasn't a match (the match failed...
                                }
                                else
                                {
                                    // Stop looping
                                    GoOn = false;
                                    Match = false;
                                }
                            }
                            // If the "Joker" is ?
                        }
                        else
                        {
                            // Just remove the first char
                            Str.RemoveFirst();

                            // As the mask has matched so far...
                            Match = true;
                        }
                    }

                }

                // If the mask is empty (and we can go on)
                //   Means that the mask matched the string
                if (((Str == "") || (Mask == "")) && (GoOn))
                {
                    // Stop looping
                    GoOn = false;
                    // And "OK it"
                    Match = true;
                }
            }

            return Match;
        }
Example #2
0
 // Properties....
 // Length of the string
 /*public int Length
 {
     get
     {
         return Length();
     }
 }*/
 //// Private...
 // Check if Ch is an alpha character (or one of the "added ones")
 private bool IsAlpha(char Ch, ktString AddedChars)
 {
     // Check if the char is valid
     switch (Ch)
     {
         case 'a':
         case 'A':
         case 'b':
         case 'B':
         case 'c':
         case 'C':
         case 'd':
         case 'D':
         case 'e':
         case 'E':
         case 'f':
         case 'F':
         case 'g':
         case 'G':
         case 'h':
         case 'H':
         case 'i':
         case 'I':
         case 'j':
         case 'J':
         case 'k':
         case 'K':
         case 'l':
         case 'L':
         case 'm':
         case 'M':
         case 'n':
         case 'N':
         case 'o':
         case 'O':
         case 'p':
         case 'P':
         case 'q':
         case 'Q':
         case 'r':
         case 'R':
         case 's':
         case 'S':
         case 't':
         case 'T':
         case 'u':
         case 'U':
         case 'v':
         case 'V':
         case 'w':
         case 'W':
         case 'x':
         case 'X':
         case 'y':
         case 'Y':
         case 'z':
         case 'Z':
             {
                 return true;
             }
         // Not a-z or A-Z
         default:
             {
                 if (AddedChars != "")
                 {
                     ktString s_chr = " ";
                     s_chr[0] = Ch;
                     if (AddedChars.Find(s_chr) >= 0)
                     {
                         return true;
                     }
                 }
                 return false;
             }
     }
 }