Shop OBEX P1 Docs P2 Docs Learn Events
String operations? substr, strreplace, etc? — Parallax Forums

String operations? substr, strreplace, etc?

dujoducomdujoducom Posts: 11
edited 2008-10-01 16:10 in Propeller 1
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-25 19:43
    The only built-in string operations are STRCOMP and STRSIZE. The block move (BYTEMOVE) can be used to copy strings, but that's it.

    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.
  • dujoducomdujoducom Posts: 11
    edited 2008-09-25 19:52
    Ah hah! strcomp was definitely one of the issues I was having. I was doing string comparisons with ==, which the compiler didn't take issue with but obviously was part of my issue. Bytemove looks promising as well as the BYTE[noparse][[/noparse]@mydata][noparse][[/noparse]i] type stuff, I'll have to build some string functions with them.

    Thanks once again, will report back with any useful info I stumble across for future newbies [noparse]:)[/noparse]
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-25 20:18
    Here is a link to my dynamic strings library. It includes a heap manager. I haven't done anything with it since, so it could still be a little rough around the edges.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-09-25 23:47
    I have a few methods I use periodically:

    CON
    
      STR_MAX_LENGTH  = 64 ' can probably go larger, haven't tested it though
    
    VAR
    
      byte ostr[noparse][[/noparse]STR_MAX_LENGTH]
    



    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 @ostr
    




    PUB 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 @ostr
    




    PUB substr (strAddr, start, count) | i
    '' returns part of a string for count bytes starting from start byte
      bytefill(@ostr, 0, STR_MAX_LENGTH)
      i~
    
      bytemove(@ostr, strAddr + start, count) 
      return @ostr
    



    I, 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
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-09-26 15:30
    Here is strstr:
    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 false
    



    example (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
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-09-26 16:21
    ...and strpos
    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 -1
    



    returns the byte number in the string that matches search
    returns -1 if search is not found
  • dujoducomdujoducom Posts: 11
    edited 2008-10-01 16:10
    Thanks, these are great!

    Phil- you should definitely post your strings object on the object exchange, it's packed full of string modifying goodness!
Sign In or Register to comment.