Shop OBEX P1 Docs P2 Docs Learn Events
SPIN -- string wild card code — Parallax Forums

SPIN -- string wild card code

Bobb FwedBobb Fwed Posts: 1,119
edited 2011-02-01 10:25 in Propeller 1
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

  • kuronekokuroneko Posts: 3,623
    edited 2011-01-31 16:16
    Does this help [thread=117496]Pattern matching and data extraction in Spin using regular expressions[/thread]? Courtesy of Phil Pilgrim.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-01-31 16:21
    kuroneko wrote: »
    Does this help [thread=117496]Pattern matching and data extraction in Spin using regular expressions[/thread]? Courtesy of Phil Pilgrim.
    More complicated than I want, 628 longs of RAM seems a bit excessive for just wild card functionality, but it is a start. Thanks.
  • WBA ConsultingWBA Consulting Posts: 2,935
    edited 2011-01-31 16:51
    Have you seen this thread? It came to mind, but not sure if it can be tweaked for what you need.

    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.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-01-31 16:53
    Have you seen this thread? It came to mind, but not sure if it can be tweaked for what you need.

    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.
    Nope, I've never seen that before, ever. :-P
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-01-31 17:01
    How about this one?

    -Phil

    'Sorry Bobb, I tried not to press Post Reply, but the urge was just too strong! :)
  • Mike GMike G Posts: 2,702
    edited 2011-01-31 17:19
    I use this in my Spinneret HttpRequest object. I'm sure you can modify the code to handle wild card characters.
    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 := true
    

    Now that I know about Phil's regex code, I'll probably use that.
  • localrogerlocalroger Posts: 3,452
    edited 2011-01-31 17:28
    I wrote a version of the DIR command in PropCMD which recognized pre*.p* wildcards. I'm not sure if the version in the Obex has that, I was working on a much more elaborate version before I realized that what I really wanted to do simply was not going to fit in Hub RAM and drifted off into expanded memory bufferland. I'll see what's what tomorrow at work but if you haven't ever done so you might want to grab the version of PropCMD I posted and see what's there.
  • kuronekokuroneko Posts: 3,623
    edited 2011-01-31 17:29
    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 := true
    
    How does this work for byte[sourceChar] == "A" and byte[patternChar] == "a"? Shouldn't this involve | $20?
  • WBA ConsultingWBA Consulting Posts: 2,935
    edited 2011-01-31 22:31
    Wow, apparently it took my post a half hour to show up. When I responded, your thread had just popped up and didn't have any responses. Oh well, got a laugh, so it wasn't completely a waste!
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-02-01 10:19
    Well, it's not perfect, but I have found a solution. It works exactly as expected (unlimited wild cards), but it's recursive, so there is less control over memory usage, and running this on a pattern with lots of wild cards could use a lot of STACK space (8 longs per call). I'm going to see if I can put an option to remove case sensitivity,

    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])
    
  • Mike GMike G Posts: 2,702
    edited 2011-02-01 10:25
    @kuroneko, yes it should.
Sign In or Register to comment.