Shop Learn P1 Docs P2 Docs
String library for Spin2 — Parallax Forums

String library for Spin2

Is there a standard library of string manipulation objects for spin2? I seem to recall using one on the P1 that had objects such as string position (return the location of a string within a string). Has anything like that been written for the P2 yet?

Thanks,
Terry

Comments

  • Cluso99Cluso99 Posts: 18,063

    V2.43 of my P2 OS has Kye's P1 object converted to P2 included.
    https://forums.parallax.com/discussion/173395/clusos-propeller-os-v2-38-includes-sd-driver-and-fat32-object-plus-serial-driver

    I have attached the _StringE.spin2 file here too :)

  • @Cluso99 said:
    V2.43 of my P2 OS has Kye's P1 object converted to P2 included.
    https://forums.parallax.com/discussion/173395/clusos-propeller-os-v2-38-includes-sd-driver-and-fat32-object-plus-serial-driver

    I have attached the _StringE.spin2 file here too :)

    Thank you sir!!

  • RS_JimRS_Jim Posts: 1,610

    OT, but has anyone heard from Nyamekye?
    Jim

  • PublisonPublison Posts: 12,362

    He is running a company in Atlanta:

    https://openmv.io/

  • JonnyMacJonnyMac Posts: 8,231
    edited 2022-01-05 04:12

    I haven't used my own strings library in a while, so seeing your thread gave me an excuse to port it to the P2. I think this implementation is cleaner and has better features -- now I need to back-port those to my P1 version.

  • @JonnyMac said:
    I haven't used my own strings library in a while, so seeing your thread gave me an excuse to port it to the P2. I think this implementation is cleaner and has better features -- now I need to back-port those to my P1 version.

    Thanks Jon! Much appreciated!

  • EDIT: While posting this I facepalmed. I think webbuff[0] needs to be zeroed out before reuse or use bytemove with the strsize + 1 of resp_ptr. Posting anyway, in case someone else finds this useful.

    @JonnyMac I think there is some kind of bug in the strings routine, but I am unsure if it is the proptool or the strings library above. I am using the append_str method to append a string. First time use, the string works as expected. 2nd time use, where the string buffer was used to hold binary data, the string is not appended correctly. All kinds of weird results occur, unless I zero out the useful part of the buffer. I don't know if the string() directive may not be appending the 0 at the end, if the append isn't copying the zero, or what is happening. I suppose the problem could be elsewhere, but zeroing out the buffer fixes the problem.

    pub netjs()
     ' Zero out the first 1000 bytes
       bytefill(@webbuff, 0, 1000)
    
    ' Append the HTTP Headers web buffer
       str.append_str(@webbuff,resp_ptr)
    
    ' set checkbox if DHCP is enabled
      if DHCP == 1
         str.append_str(@webbuff, dhcp_ptr)
    
    ' Set IP Address in form
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('IPADDRESS0').value='"))
      str.append_str(@webbuff,str.dec(IPADDRESS[0],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('IPADDRESS1').value='"))
      str.append_str(@webbuff,str.dec(IPADDRESS[1],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('IPADDRESS2').value='"))
      str.append_str(@webbuff,str.dec(IPADDRESS[2],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('IPADDRESS3').value='"))
      str.append_str(@webbuff,str.dec(IPADDRESS[3],-1))
      str.append_str(@webbuff, string("';"))
    
    ' Set Subnet in form
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('SUBNETMASK0').value='"))
      str.append_str(@webbuff,str.dec(SUBNETMASK[0],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('SUBNETMASK1').value='"))
      str.append_str(@webbuff,str.dec(SUBNETMASK[1],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('SUBNETMASK2').value='"))
      str.append_str(@webbuff,str.dec(SUBNETMASK[2],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('SUBNETMASK3').value='"))
      str.append_str(@webbuff,str.dec(SUBNETMASK[3],-1))
      str.append_str(@webbuff, string("';"))
    
    ' Set Gateway in form
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('GATEWAY0').value='"))
      str.append_str(@webbuff,str.dec(GATEWAY[0],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('GATEWAY1').value='"))
      str.append_str(@webbuff,str.dec(GATEWAY[1],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('GATEWAY2').value='"))
      str.append_str(@webbuff,str.dec(GATEWAY[2],-1))
      str.append_str(@webbuff, string("';"))
      str.append_str(@webbuff, string($0D, $0A, "document.getElementById('GATEWAY3').value='"))
      str.append_str(@webbuff,str.dec(GATEWAY[3],-1))
      str.append_str(@webbuff, string("';"))
    
    
      rdsize := strsize(@webbuff)
    
    
  • JonnyMacJonnyMac Posts: 8,231
    edited 2022-04-13 04:01

    I will have a look.

    </French accent> A few minutes later....</>

    ...unless I zero out the useful part of the buffer
    but zeroing out the buffer fixes the problem.

    Just saw this. Yes; if the front of buffer is not zero when you start appending strings you're going to run into troubles. The reason is that the append_str() method uses strsize() which iterates from the address provided, counting all non-zero characters.

    I ran this short test to prove that append_str() does add the trailing 0 of the appended string.

      bytefill(@testbuf, ".", 15)                                   ' fill with dots
      term.str(@Prompt)
      term.str(@testbuf)
      term.tx(13)
      term.dec(strsize(@testbuf))
      term.tx(13)   
    
      testbuf[0] := 0                                               ' change len to 0
      term.str(@Prompt)  
      term.str(@testbuf)
      term.tx(13)
      term.dec(strsize(@testbuf))
      term.tx(13)
    
      str.append_str(@testbuf, string("Jon"))    
      term.str(@Prompt)  
      term.str(@testbuf)
      term.tx(13)
      term.dec(strsize(@testbuf))
      term.tx(13)
    
      str.append_str(@testbuf, string("nyMac"))    
      term.str(@Prompt)  
      term.str(@testbuf)
      term.tx(13)
      term.dec(strsize(@testbuf))
      term.tx(13)
    
      ' manually print buffer to reveal zeroes and non-printed characters
    
      term.str(@Prompt)   
      repeat x from 0 to 15
        b := testbuf[x]
        if (b == 0)    ' end-of-string marker
          term.tx("0")
        else
          term.tx(b)  
    

    What this shows is that you don't have to clear the whole buffer, just set the first byte to 0 before calling append_str() after the buffer has been used for something else.

Sign In or Register to comment.