Author Topic: String Parsing issues  (Read 470 times)

Legacy_Werehound

  • Newbie
  • *
  • Posts: 22
  • Karma: +0/-0
String Parsing issues
« on: April 13, 2011, 02:42:50 am »


               
int nFirstSpace = FindSubString(sChat, " ", 0);
    int nSecondSpace = FindSubString(sChat, " ", nFirstSpace + 1);
    int nThirdSpace = FindSubString(sChat, " ", nSecondSpace + 1);
    string sCommand = (GetSubString(sChat, 0, 1) == "!" ? GetSubString(sChat, 1, nFirstSpace - 1) : sChat);
    string sParam1  = GetSubString(sChat, nFirstSpace + 1, (nSecondSpace != -1 ? nSecondSpace: GetStringLength(sChat) - (nFirstSpace + 1)) - 1);
    string sParam2 = GetSubString(sChat, nSecondSpace + 1, (nThirdSpace != -1 ? nThirdSpace: GetStringLength(sChat) - (nSecondSpace + 1)) - 1);
   

If I enter the string "!test apple"
sCommand = 'test' and
sParam1 = 'apple'
sParam2 = '!test' (redundant anyway, don't care)

If I enter the string "!test apple pear"
sCommand = 'test'
sParam1 = 'apple pear'
sParam2 = 'pear'

Anyone help me with a correct (and no doubt more efficient) way of parsing strings with spaces/
               
               

               


                     Modifié par Werehound, 13 avril 2011 - 01:43 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
String Parsing issues
« Reply #1 on: April 13, 2011, 03:17:34 am »


               

 // FILE: x3_inc_string       FUNCTION: StringParse()
//
// This function will parse sSource from left to right until it encounters
// sDelimiter and will return all the characters it encountered before
// the delimiter occurred.
// If bRightToLeft is set to TRUE then it will parse from right to left instead.
string StringParse(string sSource, string sDelimiter = " ", int bRightToLeft = FALSE)


 // FILE: x3_inc_string        FUNCTION: StringRemoveParsed()
//
// This function will take sParsed and remove it from the left side of sSource.
// It will also remove any excess sDelimiter occurrences after sParsed.
// If bRightToLeft is set to TRUE then it will perform this from the right side
// rather than the left.
//
// No check is made that sParsed actually matches sSource; all that matters
// about sParsed is its length.
string StringRemoveParsed(string sSource, string sParsed, string sDelimiter = " ", int bRightToLeft = FALSE) 


Both functions are in FILE: x3_inc_string  

EDIT: (nSecondSpace != -1 ? nSecondSpace: GetStringLength(sChat) - (nFirstSpace + 1)) 

? nSecondSpace:
Should be more like
? nSecondSpace - nFirstSpace :

If not you are taking the length frm the begining of the string.
               
               

               


                     Modifié par Lightfoot8, 13 avril 2011 - 02:47 .
                     
                  


            

Legacy_Werehound

  • Newbie
  • *
  • Posts: 22
  • Karma: +0/-0
String Parsing issues
« Reply #2 on: April 13, 2011, 03:49:28 am »


               Doh... Thanks!

EDIT:
Was I really? I thought starting at nFirstSpace + 1 stopped me from starting at the beginning?
               
               

               


                     Modifié par Werehound, 13 avril 2011 - 02:51 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
String Parsing issues
« Reply #3 on: April 14, 2011, 02:37:45 am »


               

Werehound wrote...

Doh... Thanks!

EDIT:
Was I really? I thought starting at nFirstSpace + 1 stopped me from starting at the beginning?


You are correct you where not starting at the beggining of the string.  

But the length of the sub string is what we are looking at.  Your  length is from the beginning to the second space.  You only want the length to be the number of characters between the first and second spaces.   The third Argument is number of characters  not to character at position.  
               
               

               


                     Modifié par Lightfoot8, 14 avril 2011 - 01:42 .