SPIN -- string wild card code
Has anyone written any code that could accept wild cards to match strings against other strings? I'd hate to work on something like this if it has already be done (and likely done better than what I will be able to do).

Comments
Pattern matching and data extraction in Spin using regular expressions by PhiPi
http://forums.parallax.com/showthread.php?117496-Pattern-matching-and-data-extraction-in-Spin-using-regular-expressions.
-Phil
'Sorry Bobb, I tried not to press Post Reply, but the urge was just too strong!
PUB MatchPattern(source, pattern, matchCase, startIndex) | i, j, sourceLength, patternlength sourceLength := strsize(source) patternlength := strsize(pattern) result := -1 ptrEnd := 0 ptrStart := 0 i := 0 j := 0 repeat i from startIndex to sourceLength ' Try to match the first char in pattern to the ' source if(IsCharMatch(source + i, pattern + j, matchCase) ) 'Found a match 'Store the current pointer position and increment the source pointer by one byte ptrStart := source + i 'Lets see if the pattern lines up with the source repeat j from 0 to patternLength - 1 ifnot(IsCharMatch(source + i + j, pattern + j, matchCase)) j := 0 ptrEnd := -1 ptrStart := -1 quit if(j > 0) ptrEnd := ptrStart + patternLength result := i quit PRI IsCharMatch(sourceChar, patternChar, matchCase) '' Initilize the return value result := false if(matchCase) if(byte[sourceChar] == byte[patternChar]) result := true else if((byte[sourceChar] == byte[patternChar]) or (byte[sourceChar] == byte[patternChar]+32)) result := trueNow that I know about Phil's regex code, I'll probably use that.
PRI IsCharMatch(sourceChar, patternChar, matchCase) '' Initilize the return value result := false if(matchCase) if(byte[sourceChar] == byte[patternChar]) result := true else [COLOR="red"]if((byte[sourceChar] == byte[patternChar]) or (byte[sourceChar] == byte[patternChar]+32))[/COLOR] result := trueHow does this work for byte[sourceChar] == "A" and byte[patternChar] == "a"? Shouldn't this involve | $20?I'm going to put this into my string object(s):
PRI match_pattern (sourceAddr, patternAddr) '' ported code from http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-which-does-wildcard.html '' seems to work good, but it's recursive, so it could use a significant '' amount of STACK space depending on the number wild cards there are REPEAT WHILE (byte[sourceAddr]) ' go through entire string CASE byte[patternAddr] ' check for wild card character "*": REPEAT patternAddr++ ' do this at least once, repeat until no more wild card characters WHILE byte[patternAddr] == "*" IF NOT(byte[patternAddr]) ' return true if end of string is reached RETURN true REPEAT WHILE byte[sourceAddr] IF (match_pattern(sourceAddr++, patternAddr)) ' recurse! RETURN true RETURN false OTHER: IF byte[sourceAddr] <> byte[patternAddr] ' check for a match RETURN false patternAddr++ sourceAddr++ REPEAT WHILE (byte[patternAddr] == "*") ' skip repetitive wild card characters patternAddr++ RETURN NOT(byte[patternAddr])