From d90befb43007274c03fb1783b3236072120e78e1 Mon Sep 17 00:00:00 2001 From: markt Date: Fri, 14 Dec 2007 20:05:14 +0000 Subject: StringUtil::removeFirst/TrailingWhitespace didn't truncate a string that was only whitespace --- src/FbTk/CommandRegistry.cc | 4 ++++ src/FbTk/StringUtil.cc | 15 ++++----------- src/FbTk/StringUtil.hh | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/FbTk/CommandRegistry.cc b/src/FbTk/CommandRegistry.cc index 1d16c75..fc40920 100644 --- a/src/FbTk/CommandRegistry.cc +++ b/src/FbTk/CommandRegistry.cc @@ -53,6 +53,8 @@ Command *CommandRegistry::parseLine(const string &line, bool trusted) const { // parse args and command string command, args; StringUtil::getFirstWord(line, command, args); + StringUtil::removeFirstWhitespace(args); + StringUtil::removeTrailingWhitespace(args); // now we have parsed command and args command = StringUtil::toLower(command); @@ -63,6 +65,8 @@ BoolCommand *CommandRegistry::parseBoolLine(const string &line, bool trusted) co // parse args and command string command, args; StringUtil::getFirstWord(line, command, args); + StringUtil::removeFirstWhitespace(args); + StringUtil::removeTrailingWhitespace(args); // now we have parsed command and args command = StringUtil::toLower(command); diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index 9b4928b..1bd9b8d 100644 --- a/src/FbTk/StringUtil.cc +++ b/src/FbTk/StringUtil.cc @@ -214,8 +214,7 @@ string basename(const string &filename) { string::size_type removeFirstWhitespace(string &str) { string::size_type first_pos = str.find_first_not_of(" \t"); - if (first_pos != string::npos) - str.erase(0, first_pos); + str.erase(0, first_pos); return first_pos; } @@ -223,13 +222,9 @@ string::size_type removeFirstWhitespace(string &str) { string::size_type removeTrailingWhitespace(string &str) { // strip trailing whitespace string::size_type first_pos = str.find_last_not_of(" \t"); - if (first_pos != string::npos) { - string::size_type last_pos = str.find_first_of(" \t", first_pos); - while (last_pos != string::npos) { - str.erase(last_pos); - last_pos = str.find_first_of(" \t", last_pos); - } - } + string::size_type last_pos = str.find_first_of(" \t", first_pos); + if (last_pos != string::npos) + str.erase(last_pos); return first_pos; } @@ -240,8 +235,6 @@ void getFirstWord(const std::string &in, std::string &word, std::string &rest) { if (second_pos != string::npos) { rest = word.substr(second_pos); word.erase(second_pos); - removeFirstWhitespace(rest); - removeTrailingWhitespace(rest); } } diff --git a/src/FbTk/StringUtil.hh b/src/FbTk/StringUtil.hh index 7f0cf54..d3c131e 100644 --- a/src/FbTk/StringUtil.hh +++ b/src/FbTk/StringUtil.hh @@ -64,7 +64,7 @@ std::string basename(const std::string &basename); std::string::size_type removeFirstWhitespace(std::string &str); std::string::size_type removeTrailingWhitespace(std::string &str); -/// removes the first part of a string and returns the two pieces +/// splits input at first non-leading whitespace and returns both parts void getFirstWord(const std::string &in, std::string &first, std::string &rest); /// Breaks a string into tokens -- cgit v0.11.2