D
Derek
A common technique for trimming leading and trailing spaces
from std::string is the following:
string s(" blah blah blah ");
const char* ws= " \t\r";
string::size_type not_white;
// trim leading whitespace
not_white = s.find_first_not_of(ws);
s.erase(0, not_white);
// trim trailing space
not_white = s.find_last_not_of(ws);
s.erase(not_white+1); /*** Is this safe? ***/
// ^^^^^^^^^^^
My question is about the underlined not_white+1. What if
the find_last_not_of call returns string::npos, as would
happen if s was a blank string. It seems to work with
all of my compilers, but it seems dubious to increment
string::npos.
from std::string is the following:
string s(" blah blah blah ");
const char* ws= " \t\r";
string::size_type not_white;
// trim leading whitespace
not_white = s.find_first_not_of(ws);
s.erase(0, not_white);
// trim trailing space
not_white = s.find_last_not_of(ws);
s.erase(not_white+1); /*** Is this safe? ***/
// ^^^^^^^^^^^
My question is about the underlined not_white+1. What if
the find_last_not_of call returns string::npos, as would
happen if s was a blank string. It seems to work with
all of my compilers, but it seems dubious to increment
string::npos.