2012-06-29 00:23:19 +08:00
|
|
|
#include "StringUtils.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
bool StripString(string& src)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2012-06-29 00:23:19 +08:00
|
|
|
size_t pos = 0;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
if (src.empty())
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
return false;
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
while ((pos < src.length()) && (' ' == src[pos])) pos++;
|
|
|
|
src.erase(0, pos);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
pos = 0;
|
|
|
|
while ((pos < src.length()) && ('\t' == src[pos])) pos++;
|
|
|
|
src.erase(0, pos);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
pos = src.length() - 1;
|
|
|
|
while (pos && (' ' == src[pos])) pos--;
|
|
|
|
src.erase(pos+1);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
pos = src.length() - 1;
|
|
|
|
while (pos && ('\t' == src[pos])) pos--;
|
|
|
|
src.erase(pos+1);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseString(const string& src, string& key, string& value)
|
|
|
|
{
|
|
|
|
if (src.empty())
|
2012-10-24 16:47:43 +08:00
|
|
|
return false;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-12-25 18:04:44 +08:00
|
|
|
// find separator ":"
|
|
|
|
size_t separator_pos = src.find(":");
|
|
|
|
if (string::npos != separator_pos)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-12-25 18:04:44 +08:00
|
|
|
key = src.substr(0, separator_pos);
|
2012-10-17 07:18:30 +08:00
|
|
|
StripString(key);
|
2013-12-25 18:04:44 +08:00
|
|
|
value = src.substr(separator_pos+1);
|
2012-10-17 07:18:30 +08:00
|
|
|
StripString(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 18:04:44 +08:00
|
|
|
set<string> SplitString(const string& src, const char separator)
|
2012-06-29 00:23:19 +08:00
|
|
|
{
|
|
|
|
set<string> result;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
if (!src.empty())
|
|
|
|
{
|
2013-12-25 18:04:44 +08:00
|
|
|
size_t separator_pos;
|
2012-10-24 16:47:43 +08:00
|
|
|
size_t prev_pos = 0;
|
|
|
|
do
|
|
|
|
{
|
2013-12-25 18:04:44 +08:00
|
|
|
separator_pos = src.find(separator, prev_pos);
|
|
|
|
result.insert(src.substr(prev_pos, separator_pos - prev_pos));
|
|
|
|
prev_pos = separator_pos + 1;
|
2012-10-24 16:47:43 +08:00
|
|
|
}
|
2013-12-25 18:04:44 +08:00
|
|
|
while (string::npos != separator_pos);
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-12-25 18:04:44 +08:00
|
|
|
vector<string> SplitStringVector(const string& src, const char separator)
|
2012-06-29 00:23:19 +08:00
|
|
|
{
|
|
|
|
vector<string> result;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
if (!src.empty())
|
|
|
|
{
|
2013-12-25 18:04:44 +08:00
|
|
|
size_t separator_pos;
|
2012-10-24 16:47:43 +08:00
|
|
|
size_t prev_pos = 0;
|
|
|
|
do
|
|
|
|
{
|
2013-12-25 18:04:44 +08:00
|
|
|
separator_pos = src.find(separator, prev_pos);
|
|
|
|
string tmp = src.substr(prev_pos, separator_pos - prev_pos);
|
2012-10-24 16:47:43 +08:00
|
|
|
result.push_back(tmp);
|
2013-12-25 18:04:44 +08:00
|
|
|
prev_pos = separator_pos + 1;
|
2012-10-24 16:47:43 +08:00
|
|
|
}
|
2013-12-25 18:04:44 +08:00
|
|
|
while (string::npos != separator_pos);
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
return result;
|
2013-08-21 21:26:54 +08:00
|
|
|
}
|