TamusJRoyce

Tamus J Royce - 8/22/2018

Tamus is a mispelling of my first name. But it is most interestingly the last syllable of Hippopo.tamus :) J stands for Junkmail. It is also the first letter of my name. Royce is my middle name passed down many generations. Urban Dictionary has the meaning of this one correct...

I want to post my matching function. Regex is too error prone. And I don't need that level of detail. But simply splitting a string and matching each side is really helpful. So I built this. It should probably support -nocase. But I don't have a need for that yet.

    proc isMatch {pattern text {isSubPattern 0}} {
        if {[string equal "*" $pattern]} {
            return 1;
        }
        if {[string equal "~*" $pattern]} {
            return 0;
        }
        if {[string equal "" $pattern]} {
            return [string match "" $text]
        }

        if {[string match "*||*" $pattern]} {
            foreach subpattern [split $pattern "||"] {
                if {[string match "" $subpattern]} {
                    continue
                }
                if {[string match "||" $subpattern]} {
                    continue
                }

                if {[isMatch $subpattern $text 1]} {
                    return 1
                }
            }

            return 0
        }

        if {[string match "*&&*" $pattern]} {
            foreach subpattern [split $pattern "&&"] {
                if {[string match "" $subpattern]} {
                    continue
                }
                if {[string match "&&" $subpattern]} {
                    continue
                }

                if {![isMatch $subpattern $text 1]} {
                    return 0
                }
            }

            return 1
        }

        set matchNotNegated 1
        if {[string first "~" $pattern] == 0} {
            set matchNotNegated 0
            set pattern [string trimleft $pattern "~"]
        }

        if {[string match $pattern $text]} {
            return $matchNotNegated
        }

        if {[catch {
            if {[regexp $pattern $text]} {
                return $matchNotNegated
            }
        } error]} {
        }

        if {$matchNotNegated} {
            return 0
        }

        return 1
    }