Updated trim functions

feature/19
Daniel Warner 2011-12-22 14:28:31 +00:00
parent 1f2fe033fe
commit 2930cc649b
3 changed files with 19 additions and 28 deletions

View File

@ -2,6 +2,8 @@
#include "Util.h" #include "Util.h"
#include <locale>
namespace namespace
{ {
/* /*

View File

@ -1,39 +1,28 @@
#include "Util.h" #include "Util.h"
#include <algorithm>
#include <locale>
namespace Util namespace Util
{ {
void TrimLeft(std::string& str) void TrimLeft(std::string& s)
{ {
std::string whitespaces(" \t\f\n\r"); s.erase(s.begin(),
std::find_if(s.begin(), s.end(),
if (!str.empty()) { std::not1(std::ptr_fun<int, int>(std::isspace))));
std::string::size_type pos = str.find_first_not_of(whitespaces);
if (pos != std::string::npos)
str.erase(0, pos);
else
str.clear();
}
} }
void TrimRight(std::string& str) void TrimRight(std::string& s)
{ {
std::string whitespaces(" \t\f\n\r"); s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
if (!str.empty()) { s.end());
std::string::size_type pos = str.find_last_not_of(whitespaces);
if (pos != std::string::npos)
str.erase(pos + 1);
else
str.clear();
}
} }
void Trim(std::string& str) void Trim(std::string& s)
{ {
TrimLeft(str); TrimLeft(s);
TrimRight(str); TrimRight(s);
} }
} }

6
Util.h
View File

@ -63,9 +63,9 @@ namespace Util
} }
} }
void TrimLeft(std::string& str); void TrimLeft(std::string& s);
void TrimRight(std::string& str); void TrimRight(std::string& s);
void Trim(std::string& str); void Trim(std::string& s);
} }
#endif #endif