GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


strip proceeding / trailing whitespace by luke
1
 
static const string scWhiteSpace = " \t\r\n\v\f";
/* WARNING:
The standard white-space characters are the following:
space, tab, carriage-return, newline, vertical tab,
and form-feed in the C locale.

This is currently irrespective of the current locale.
In the C locale or in a locale where character type
information is not defined, characters are classified
according to the rules of the U.S. ASCII 7-bit coded
character set. 
*/

void stripLeadingWhiteSpace(string& token)
{
    string::size_type firstNonWhiteSpace = token.find_first_not_of(scWhiteSpace);
    if(firstNonWhiteSpace != string::npos)
    {
        token = token.substr(firstNonWhiteSpace);
    }
}

void stripTrailingWhiteSpace(string& token)
{
    string::size_type lastNonWhiteSpace = token.find_last_not_of(scWhiteSpace);
    if(lastNonWhiteSpace != string::npos)
    {
        token = token.substr(0, lastNonWhiteSpace);
    }
}