Shop OBEX P1 Docs P2 Docs Learn Events
Find a character within a string - help understanding some code — Parallax Forums

Find a character within a string - help understanding some code

Don MDon M Posts: 1,652
edited 2014-04-24 12:12 in Propeller 1
I have this string located in the DAT section:
DAT

Text_Plain    byte "cc9a67d9fb998b18"          
              byte "922e015a35af0780"
              byte "d6945e8473dc3fab"
              byte "b3c1a7d0f0b9341d"
              byte "0000000011,n0200",0    


I am trying to find the index number or position of the comma (,) using Kye's code from the Strings Object "ASCII0_STREngine_1.spin" :
PUB findCharacter(stringToSearch, characterToFind) '' 5 Stack Longs

'' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'' // Searches a string of characters for the first occurence of the specified character.
'' //
'' // Returns the address of that character if found and zero if not found.
'' //
'' // StringToSearch - A pointer to the string of characters to search.
'' // CharacterToFind - The character to find in the string of characters to search.
'' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  repeat strsize(stringToSearch--)
    if(byte[++stringToSearch] == characterToFind)
      return stringToSearch


Here is how I'm using it:
debug.dec(strng.findCharacter(@Text_Plain, ",")) 


It returns the value of 182 which didn't seem correct so I thought I'd put the comma at the very beginning of the string to see what happens. Then it returns the value of 108. So if I subtract 108 from 182 I get 74 which is the correct position of the comma in that string.

So my question is why is it offset by that amount? Am I not understanding / using the method correctly?

I also tried bytemove to copy the string from the DAT section to a buffer in the VAR section and it too had an offset number by some other amount. If I subtract the offset it is correct. So where is this offset coming from?

If I take the comma out it returns 0.

Thanks.
Don

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-04-24 11:09
    Don M wrote: »
    PUB findCharacter(stringToSearch, characterToFind) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Searches a string of characters for the first occurence of the specified character.
    '' //
    '' // [COLOR="#FF0000"]Returns the address of that character if found and zero if not found.[/COLOR]
    '' //
    '' // StringToSearch - A pointer to the string of characters to search.
    '' // CharacterToFind - The character to find in the string of characters to search.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    If you want the e.g. zero-based index of said character within the string you'll have to subtract the string's start address from the result.
  • Don MDon M Posts: 1,652
    edited 2014-04-24 11:12
    kuroneko wrote: »
    If you want the e.g. zero-based index of said character within the string you'll have to subtract the string's start address from the result.

    And how do you find the string's start address?
  • kuronekokuroneko Posts: 3,623
    edited 2014-04-24 11:20
    Don M wrote: »
    And how do you find the string's start address?
    @Text_Plain
  • Don MDon M Posts: 1,652
    edited 2014-04-24 11:51
    kuroneko wrote: »
    @Text_Plain

    Thanks kuroneko! I learned something today.
      i := @Text_Plain
      debug.dec(strng.findCharacter(@Text_Plain, ",") - i) 
    
    

    Or this way works too...
      debug.dec(strng.findCharacter(@Text_Plain, ",") - @Text_Plain) 
    
    
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-04-24 12:12
    This might be little more verbose than absolutely necessary, but I try to write simple code (it's from an object of string-related methods I use for parsing, etc.). It will return a zero-based index value of a character if found in the target string. If the character is not found, the method returns -1 (an illegal index value).
    pub cinstr(p_str, c) | idx, pos
    
    '' Returns position of character c in string at p_str
    '' -- returns -1 if not found
    
      longfill(@idx, -1, 2)                                         ' preset to -1
    
      repeat strsize(p_str)                                         ' loop through string chars
        idx++
        if (byte[p_str][idx] == c)                                  ' if match
          pos := idx                                                ' return position
          quit
          
      return pos
    
Sign In or Register to comment.