Shop OBEX P1 Docs P2 Docs Learn Events
STRCOMP for array? — Parallax Forums

STRCOMP for array?

T ChapT Chap Posts: 4,223
edited 2011-12-30 09:42 in Propeller 1
Is there a shortcut to search an array for something? Say that a device sends the Prop some data, 512 bytes for example. The Prop stores the data in an array. This is how I currently do it:

' receive the data here from some source that contains  TEST1=1, could be from a web page, GET or POST
PUB ParseInput  | i 
    ser.str(1, string("TEST1"))   'display
    ser.tx(1, CR)
    i := 0
    repeat 512  
      if response[i] == "T" 
       if  response[i+1] == "E"
        if  response[i+2] == "S"
         if  response[i+3] == "T"
          if  response[i+4] == "1"
           if  response[i+5] == "="
            if  response[i+6] == "1"
                  DoSomething
                  Return
      i++


In this simple example, it is easy to see if the serial rx contains a word and value, then do something with the result. This works fine but seems bulky. This example expects a 1 to do anything, other examples could have unknown numbers that follow the =, which would then would need to be converted to an integer and sent to the next method for use.

Comments

  • JonnyMacJonnyMac Posts: 9,198
    edited 2011-12-29 20:47
    If you can stick zeroes at the end of each array (technically, making them z-strings), this will work (I use it in parsing programs). You could always modify the method to manually pass each string length which would allow you to remove the strsize calls (which require terminating zeroes).
    pub instr(str1, str2) | len1, len2, pos, idx
    
    '' Returns position of str2 in str1
    '' -- if str2 not in str1 returns -1
    
      len1 := strsize(str1)
      len2 := strsize(str2)
      pos  := -1                                           
      idx  := 0
    
      if (len1 => len2)                                    
        repeat (len1 - len2 + 1)                           
          if (byte[str1] == 0)                             
            quit
          else
            if (strncmp(str1++, str2, len2) == 0)          
              pos := idx                                   
              quit                                         
            else
              ++idx
    
      return pos
    

    This is part of the attached object which contains methods written or converted by me and many others.
  • T ChapT Chap Posts: 4,223
    edited 2011-12-29 21:35
    That is a nice collection of stuff, thanks for the help Jon! I had looked in the manual for instr, I knew I had seen it somewhere before. Much appreciated.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-12-29 22:43
    < smart a**-mode on >
    pub instr(str1, str2) | len1, len2, pos, idx
    
    '' Returns position of str2 in str1
    '' -- if str2 not in str1 returns -1
    
      len1 := strsize(str1)
      len2 := strsize(str2)
      pos  := -1                                           
      idx  := 0
    
      if (len1 => len2)                                    
        repeat (len1 - len2 + 1)                           
          [color=red]if (byte[str1] == 0)                             
            quit[/color]
          else
            if (strncmp(str1++, str2, len2) == 0)          
              pos := idx                                   
              quit                                         
            else
              ++idx
    
      return pos
    

    It think the red code can only be met in case that you pass an empty string (with strlen = 0 ). So for optimization I'd remove it from the loop:
    pub instr(str1, str2) | len1, len2, pos, idx
    
    '' Returns position of str2 in str1
    '' -- if str2 not in str1 returns -1
    
      len1 := strsize(str1)
      len2 := strsize(str2)
      pos  := -1                                           
      idx  := 0
    
      if (len1 & (len1 => len2) )                                    
        repeat (len1 - len2 + 1)                           
            if (strncmp(str1++, str2, len2) == 0)          
              pos := idx                                   
              quit                                         
            else
              ++idx
    
      return pos
    
    < smart a**-mode off >

    ;o)

    Of course this is untested, as I have to work ;o)
  • JonnyMacJonnyMac Posts: 9,198
    edited 2011-12-30 09:42
    You're right, that code should never execute and can be removed. As ever, I reserve the right to be human and produce imperfect code from time-to-time as I tend to be very conservative from a design standpoint (ie., always testing for things that could break). ;)
Sign In or Register to comment.