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 <locale>
namespace
{
/*

View File

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

6
Util.h
View File

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