STRCOMP for array?
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:
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.
' 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
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.
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)