add a function to remove a character from the first and last position...

...in a string (will be used in the server)
This commit is contained in:
Gunter Labes 2007-10-30 16:46:15 +00:00
parent 705e7375fd
commit 2efb613c5b
2 changed files with 16 additions and 13 deletions

View file

@ -127,14 +127,14 @@ static std::string do_interpolation(const std::string &str, const variable_set&
namespace utils {
bool isnewline(char c)
bool isnewline(const char c)
{
return c == '\r' || c == '\n';
}
// Make sure that we can use Mac, DOS, or Unix style text files on any system
// and they will work, by making sure the definition of whitespace is consistent
bool portable_isspace(char c)
bool portable_isspace(const char c)
{
// returns true only on ASCII spaces
if (static_cast<unsigned char>(c) >= 128)
@ -144,11 +144,12 @@ bool portable_isspace(char c)
// Make sure we regard '\r' and '\n' as a space, since Mac, Unix, and DOS
// all consider these differently.
bool notspace(char c)
bool notspace(const char c)
{
return !portable_isspace(c);
}
//! Remove whitespace from the front and back of the string 'str'.
std::string &strip(std::string &str)
{
// If all the string contains is whitespace,
@ -163,14 +164,12 @@ std::string &strip(std::string &str)
return str;
}
std::string &strip_char(std::string &str, char c)
{
std::string::iterator it = std::remove(str.begin(), str.end(), c);
if (it == str.end())
return str;
str.erase(str.begin(), it);
str.erase(std::find_if(str.rbegin(), str.rend(), notspace).base(), str.end());
//! Removes character 'c' from the first and last position of the string 'str'.
std::string& strip_char(std::string &str, const char c) {
if (*str.begin() == c)
str.erase(str.begin(), str.begin() + 1);
if (*(str.end() - 1) == c)
str.erase(str.end() - 1, str.end());
return str;
}
@ -407,6 +406,7 @@ bool string_bool(const std::string& str,bool def)
return def;
}
//! Test for additional valid username characters.
bool isvalid_char(char c)
{
return ((c == '_') || (c == '-'));

View file

@ -49,8 +49,8 @@ bool portable_isspace(char c);
bool notspace(char c);
enum { REMOVE_EMPTY = 0x01, //!< REMOVE_EMPTY : remove empty elements
STRIP_SPACES = 0x02 //!< STRIP_SPACES : strips leading and trailing blank spaces
};
STRIP_SPACES = 0x02 //!< STRIP_SPACES : strips leading and trailing blank spaces
};
std::vector< std::string > split(std::string const &val, char c = ',', int flags = REMOVE_EMPTY | STRIP_SPACES);
std::vector< std::string > paranthetical_split(std::string const &val, const char separator = 0 , std::string const &left="(", std::string const &right=")",int flags = REMOVE_EMPTY | STRIP_SPACES);
@ -63,7 +63,10 @@ int apply_modifier( const int number, const std::string &amount, const int minim
std::string &escape(std::string &str, const std::string& special_chars);
std::string &escape(std::string &str);
std::string &unescape(std::string &str);
//! Remove whitespace from the front and back of the string 'str'.
std::string &strip(std::string &str);
//! Removes character 'c' from the first and last position of the string 'str'.
std::string& strip_char(std::string &str, const char c);
bool has_value(std::string const &values, std::string const &val);
bool string_bool(const std::string& str,bool def=false);