How to check if the first char in a string is # (start of comment)

I've already followed this convention:

std::string line;
while(std::getline(in,line)) 
{
    if(line.size() && line[0] =='#')
      continue;
    /* parse text*/
}

      

The obvious disadvantage is that the comment may not start at the first character, in the case of a leading space.

What's a good way to handle such a thing?

+2


source to share


7 replies


Simple extension: you can use line.find_first_not_of ("") to get the first non-whitespace and then check if it is '#'. This also applies to the zero length case. Something like this snippet:

  found= line.find_first_not_of(" \t");

  if( found != string::npos)
  {
    if( line[found] == '#')
      continue;
  }

      



Additional Information

+11


source


Make sure to check the string length before testing the null character:



if (line.length() > 0 && line[0] == '#')

      

+2


source


Use the โ†’ operator. It ignores spaces.

std::string line;
while(std::getline(in,line))
{
    std::stringstream linestr(line);
    char              firstNoWhiteSpaceChar;

    linestr >> firstNoWhiteSpaceChar;
    if ((!linestr) || (firstNoWhiteSpaceChar == '#')) 
    {
        // If line contains only white space then linestr will become invalid.
        // As the equivalent of EOF is set. This is the same as a comment so
        // we can ignore the line like a comment.
        continue;
    }

    // Do Stuff with line.
}

      

+2


source


From the sound of things, your file format indicates that everything from the "#" to the end of the line is a comment. If this happens, you can find the start of the comment with:

// Warning: untested code.
int pos = line.find('#');

      

Then you probably want to ignore the rest of the line, most easily manageable by removing it:

if (pos != std::string::npos)
    line.erase(pos, -1);

      

This should deal with things like:

tax = rate * price    # figure tax on item

      

Of course, this assumes that "#" always signals the start of a comment - if you allow "#" within character strings, or for some other purpose, you need to take that into account (but it's hard to guess what this will be since you told us very little about the file format.)

+1


source


Use flux agent to skip spaces std::ws

:

inline std::istream& get_line(std::istream& in, std::string& line)
{
    in >> std::ws;
    std::getline(in,line);
    return in;
}

std::string line;
while(get_line(in,line)) 
{
    if(!line.empty() && line[0] =='#')
        continue;
    /* parse text*/
}

      

+1


source


You may like the Boost String Library , in particular trim_left

and starts_with

.

0


source


The analysis is complex and complex.

In general, I would not recommend trying to figure it out without a state machine. For example, what if '#' is part of a multiline ("" ... "" "in python)?

There are libraries that can make parsing easier (well, they should be, but understanding them can be tricky if you don't have prior information), for example in C ++, Spirit can only be recommended .

Some pointers have already been suggested to help you use the string methods, although they are only related to detecting if the first significant character is "#".

Unless you're "afraid" of multi-line (that is, if you're trying to parse, you don't have such a function), you still have to manage "simple" lines, which can be done by counting, counting:

print "My \"#\" is: ", phoneNumber # This is my phone number

      

If you parse this string badly, you get an error ... (for example)

If you can't use a library, a state machine is the way to go, writing a parser, it's pretty funny overall, it gives you an idea of โ€‹โ€‹why the notation was designed in a certain way.

0


source







All Articles