Thread: String search not returning correctly?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    String search not returning correctly?

    Well it was bound to happen eventualy, after writing and rewriting my program I have something solid. But I have a minor problem ~ My parser is not returning correctly. I have made it so it will ignore 'strings' (It's a mock compiler of sorts). However, it doesn't seem to want to ignore them, it still returns if a delimeter is inside a string.


    What should happen (psuedocode):
    Code:
    Function call:
     Function searches a string for instances of character 1:
      Character 1 is found ~ Function searches for character two OR special character
       Special Character is found:
        Flag Special exclusion until the character is found again.
        or
        Unflag special exclusion until the character is found again.
    
       Character 2 is found AND Special Exclusion is not flagged:
        Return string
       Character 2 is found AND Special Exclusion is flagged:
        Ignore and continue normally
    Hopefully that explained it, now for the actual code: -- Excuse my poor formatting, it's know it's an awful sin unto man -,-.

    Code:
    std::string GetStringBetweenTokensSpecial(std::string st, char T1, char T2, char SRC, int* a = false, int* b = false) {
     int x = 0;
     std::string buffer;
     BOOL SpecialRule = false;
    
     while(x <= st.length()) {
    
      if(st[x] == T1 && SpecialRule == false) {
       if(a)
        *a = x;
       x++;
    
       while(x <= st.length()) {
    
        if(st[x] == SRC) {
         if(!SpecialRule)
          SpecialRule = true;
         else
          SpecialRule = false;
         }
    
        if(st[x] == T2 && SpecialRule == false) {
         if(b)
          *b = x;
         return buffer;
        }
    
        buffer += st[x++];
       }
    
      }
    
      x++;
     }
     return "";
    }
    The call:

    Code:
    #define StringSyntax    '"' ~ //This is  ' " ' hard to read that without spaces.
    #define Spacer		','
    
      buffer = GetStringBetweenTokensSpecial(buffer, Spacer, Spacer, StringSyntax, &a, &b);
    I have no idea what's wrong with this, I've rewritten this four times already, it just refuses to work -,-.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Uh...what's with the return ""; at the end of the function?

    When I replaced that, I got this...

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    std::string GetStringBetweenTokensSpecial(std::string st, char T1, char T2, char SRC, int* a = false, int* b = false) {
     int x = 0;
     std::string buffer;
     bool SpecialRule = false;
    
     while(x <= st.length()) {
    
      if(st[x] == T1 && SpecialRule == false) {
       if(a)
        *a = x;
       x++;
    
       while(x <= st.length()) {
    
        if(st[x] == SRC) {
         if(!SpecialRule)
          SpecialRule = true;
         else
          SpecialRule = false;
         }
    
        if(st[x] == T2 && SpecialRule == false) {
         if(b)
          *b = x;
         return buffer;
        }
    
        buffer += st[x++];
       }
    
      }
    
      x++;
     }
     return buffer;
    }
    
    int main() {
    #define StringSyntax    '"' //This is  ' " ' hard to read that without spaces.
    #define Spacer		','
      int a, b;
      string buffer = "here, is a \"string, without\", dollar signs";
    
      buffer = GetStringBetweenTokensSpecial(buffer, Spacer, Spacer, StringSyntax, &a, &b);
      cout << buffer << endl;
    }
    With this output:
    Code:
     is a "string, without"
    What is wrong with that output? It's hard to know from your description. Try to provide example output with your examples in the future. A small working program along with it would be even better.

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I return "" at the end because if the delimeters are not found it should not return any data to me (It would confused my parser otherwise)

    What was happening was not that it wasn't returning, but was returning false data. For example:

    Data("1,2;");

    Was being parsed as

    Data,"1,2",;

    Which was returning Data,"1 instead of Data,"1,2" (If you're wondering why I replaced the brackets, it's because a previous function does this for me)

    If that makes sence.

    It still seems to be returning incorrectly, but I can't see why. I'm going to look at some other lines of code and see if they're problematic, because I just dont see an issue with this no matter how long I look at it -,-. I'll get back to you, and thanks for the response wh.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Blackroot View Post
    Data,"1,2",;

    Which was returning Data,"1 instead of Data,"1,2" (If you're wondering why I replaced the brackets, it's because a previous function does this for me)

    If that makes sence.

    It still seems to be returning incorrectly, but I can't see why.
    I can't see it either, because with this main driver:
    Code:
    int main() {
    #define StringSyntax    '"' //This is  ' " ' hard to read that without spaces.
    #define Spacer		','
      int a, b;
      string buffer = "Data,\"1,2\",;";
    
      buffer = GetStringBetweenTokensSpecial(buffer, Spacer, Spacer, StringSyntax, &a, &b);
      cout << buffer << endl;
    }
    I'm getting this output:
    Code:
    "1,2"
    Which you said was right, yes? The only thing I changed from the function you posted was lowercase bool instead of BOOL (which I don't understand).

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also, the following loop can cause out-of-bounds access:

    Code:
    while(x <= st.length())
    What looks strange is that you are a) replacing brackets with commas, b) apparently doing so even before you have handled string literals. If so, how would you be able to tell for example a) what foo(x, bar(y), z) means and b) what happens to foo("bar(x)")?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Quote Originally Posted by anon View Post
    Also, the following loop can cause out-of-bounds access:

    Code:
    while(x <= st.length())
    What looks strange is that you are a) replacing brackets with commas, b) apparently doing so even before you have handled string literals. If so, how would you be able to tell for example a) what foo(x, bar(y), z) means and b) what happens to foo("bar(x)")?
    -Thanks for pointing that out, I always forget length returns the full size including terminator.

    ~And I'm not sure I fully got your second question. What should happen in case a is:

    foo(x, bar(y), z);
    turns into
    ,x,bar,y,z,

    I do this because it's far easier to parse through and I am not adding support for function-in-function calls. (This is in fact a string-replacer sort of like the Replace function except with the ability to take arguments)

    In case b it should do something like
    foo("bar(x)");
    turning into
    "bar(x)"

    This parses fine and correctly, however what I was having problems with is this:
    foo("bar(x);");
    which would incorrectly be parsed as:
    foo("bar(x)

    The end-of-line semicolin was being intepreted in the string, which was incorrect. However, I finnaly got the thing working, I'm not sure what the problem was but a couple of altered lines seems to have it working. Still, looking around in the code I posted I can't for the life of me see why.

    Anyways thanks for pointing out I was looping to far, I happen to have done that to every parser-type call -,-.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM