String operations? substr, strreplace, etc?
I come from a software background so I'm used to plenty of great string functions, why are they not available on the propeller? Am I just not looking in the right spot? I'm trying to parse a number out of some serial data to convert it to a value for doing some calculations. The·string is formatted something like this:
+0 012345000000
I'm using the + as a delimiter and getting the whole message using a serial buffer (well, using FullDuplexSerialExtnededs str method). Now I'm having issues parsing out only the 5 values·between the 0's (the 12345). In a higher level environment I would just use substring, starting at index 4 with a lenght of 5, but I can't seem to find anything in the manual other than strcomp and·strsize. I searched the forum and object exchange to no avail. Is it something so incredibly obvious that it's glossed over somewhere, or will it be more complicated than I thought?
Post Edited (dujoducom) : 9/25/2008 7:09:43 PM GMT
+0 012345000000
I'm using the + as a delimiter and getting the whole message using a serial buffer (well, using FullDuplexSerialExtnededs str method). Now I'm having issues parsing out only the 5 values·between the 0's (the 12345). In a higher level environment I would just use substring, starting at index 4 with a lenght of 5, but I can't seem to find anything in the manual other than strcomp and·strsize. I searched the forum and object exchange to no avail. Is it something so incredibly obvious that it's glossed over somewhere, or will it be more complicated than I thought?
Post Edited (dujoducom) : 9/25/2008 7:09:43 PM GMT

Comments
The main reason is that things like substr are indeed complex and require some kind of memory manager to provide storage for the result. It's too complex and would need to make too many assumptions to be included in Spin. Someone with the need or desire could certainly write an object that provides a string library that would be used much like the existing floating point library.
Thanks once again, will report back with any useful info I stumble across for future newbies [noparse]:)[/noparse]
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Still some PropSTICK Kit bare PCBs left!
PUB strtolower (strAddr) | i '' sets all characters in string to lowercase bytefill(@ostr, 0, STR_MAX_LENGTH) REPEAT i FROM 0 TO strsize(strAddr) IF (byte[noparse][[/noparse]strAddr + i] => 65 AND byte[noparse][[/noparse]strAddr + i] =< 90) ostr[noparse][[/noparse] i ] += byte[noparse][[/noparse]strAddr + i] + 32 ELSE ostr[noparse][[/noparse] i ] += byte[noparse][[/noparse]strAddr + i] return @ostrPUB strtoupper (strAddr) | i '' sets all characters in string to uppercase bytefill(@ostr, 0, STR_MAX_LENGTH) REPEAT i FROM 0 TO strsize(strAddr) IF (byte[noparse][[/noparse]strAddr + i] => 97 AND byte[noparse][[/noparse]strAddr + i] =< 122) ostr[noparse][[/noparse] i ] += byte[noparse][[/noparse]strAddr + i] - 32 ELSE ostr[noparse][[/noparse] i ] += byte[noparse][[/noparse]strAddr + i] return @ostrI, too, am used to using strings a lot and need to manipulate them as needed (habits taken from PHP and other places).
I'm sure I have some more laying around somewhere. I will post them here if I run across them.
PHP's function strstr should be easy to do as well, str_replace is more work, but should be possible as well as many other string function. A string library would be nice!
Post Edited (Bobb Fwed) : 9/26/2008 3:25:43 PM GMT
PUB strstr (strAddr, searchAddr, offset) | searchsize '' finds first occurrence of search string and returns the remainder of str along with the search bytefill(@ostr, 0, STR_MAX_LENGTH) searchsize := strsize(searchAddr) REPEAT UNTIL (offset + searchsize > STR_MAX_LENGTH) IF (strcomp(substr(strAddr, offset++, searchsize), searchAddr)) RETURN substr(strAddr, offset - 1, STR_MAX_LENGTH) RETURN falseexample (for those not familiar with the PHP function):
str := "test@example.com"
search := "@"
strstr(@str, @search, 0) ---> returns "@example.com"
returns false if search string not found
and offset can be used to "ignore" a certain number of characters off the beginning of str
PUB strpos (strAddr, searchAddr, offset) | searchsize '' returns location of first occurrence of search in str, returns -1 if search is not found searchsize := strsize(searchAddr) REPEAT UNTIL (offset + searchsize > STR_MAX_LENGTH) IF (strcomp(substr(strAddr, offset++, searchsize), searchAddr)) RETURN offset - 1 RETURN -1returns the byte number in the string that matches search
returns -1 if search is not found
Phil- you should definitely post your strings object on the object exchange, it's packed full of string modifying goodness!