ReuvenLax

Code Puzzler

What does the following code snippet do?

bool test(const wstring& first, const wstring& second)
{
    if (first.empty() && second.empty())
        return true;

    if (first.empty())
        return (second[0] == L'*') && test(first, second.substr(1));
    if (second.empty())
        return (first[0] == L'*') && test(first.substr(1), second);

    switch(first[0])    {
        case L'?':
            if (second[0] == L'*')  {
                  return test(first.substr(1), second) || test(first, second.substr(1)); 
            }

            return test(first.substr(1), second.substr(1));
        case L'*':
            return test(first, second.substr(1)) || test(first.substr(1), second);    
        default:
            switch(second[0])   {
                case L'?':
                    return test(first.substr(1), second.substr(1));
                case L'*':
                    return test(first.substr(1), second) || test(first, second.substr(1));    
                default:
                    return (first[0] == second[0]) && test(first.substr(1), second.substr(1));
            }
    }
}

Published Thursday, April 21, 2005 8:07 PM by ReuvenLax

Comments

 

josh said:

Searches with wildcards?

* = Any number of characters
? = One character

Just looked at it real quick, didn't spend too much time :)
April 21, 2005 8:30 PM
 

Reuven said:

Not quite.
April 21, 2005 9:18 PM
 

nat said:

Seems like it will try to check whether pattern A match with B or not or vice and versa.

A=*a, B=asdasda => true
A=a?a, B=ana => true
A=a*a, B=abb => false
April 21, 2005 10:12 PM
 

Reuven said:

Look a little harder. That's not all this function does.
April 21, 2005 10:14 PM
 

TAG said:

It return true if there exists at least one string (including empty one) that will satisfy both patterns. (i.e. thouse patterns are not conflicting)

Patterns as everybody guessed out are:
* - 0 or more characters,
? - one character.
<all others> - same character.
April 22, 2005 12:30 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker