Add the split_first and split_last functions on strings.
base/str.cc: base/str.hh: Add a couple functions that allow you to split a string at the first or last instance of a delimiter. --HG-- extra : convert_revision : 2af22639e1b67ac61577c00475a555841a56f902
This commit is contained in:
parent
89ba024b98
commit
26ef1f56c8
2 changed files with 44 additions and 0 deletions
30
base/str.cc
30
base/str.cc
|
@ -39,6 +39,36 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
bool
|
||||||
|
split_first(const string &s, string &lhs, string &rhs, char c)
|
||||||
|
{
|
||||||
|
string::size_type offset = s.find(c);
|
||||||
|
if (offset == string::npos) {
|
||||||
|
lhs = s;
|
||||||
|
rhs = "";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
lhs = s.substr(0, offset);
|
||||||
|
rhs = s.substr(offset + 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
split_last(const string &s, string &lhs, string &rhs, char c)
|
||||||
|
{
|
||||||
|
string::size_type offset = s.rfind(c);
|
||||||
|
if (offset == string::npos) {
|
||||||
|
lhs = s;
|
||||||
|
rhs = "";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
lhs = s.substr(0, offset);
|
||||||
|
rhs = s.substr(offset + 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
tokenize(vector<string>& v, const string &s, char token, bool ignore)
|
tokenize(vector<string>& v, const string &s, char token, bool ignore)
|
||||||
{
|
{
|
||||||
|
|
14
base/str.hh
14
base/str.hh
|
@ -90,6 +90,20 @@ to_lower(const std::string &s)
|
||||||
return lower;
|
return lower;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Split the string s into lhs and rhs on the first occurence of the
|
||||||
|
// character c.
|
||||||
|
bool
|
||||||
|
split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
|
||||||
|
|
||||||
|
// Split the string s into lhs and rhs on the last occurence of the
|
||||||
|
// character c.
|
||||||
|
bool
|
||||||
|
split_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
|
||||||
|
|
||||||
|
// Tokenize the string <s> splitting on the character <token>, and
|
||||||
|
// place the result in the string vector <vector>. If <ign> is true,
|
||||||
|
// then empty result strings (due to trailing tokens, or consecutive
|
||||||
|
// tokens) are skipped.
|
||||||
void
|
void
|
||||||
tokenize(std::vector<std::string> &vector, const std::string &s,
|
tokenize(std::vector<std::string> &vector, const std::string &s,
|
||||||
char token, bool ign = true);
|
char token, bool ign = true);
|
||||||
|
|
Loading…
Reference in a new issue