1 #include "daq/slc/base/StringUtil.h"
11 StringList StringUtil::split(
const std::string& str,
const char type,
size_t max)
14 size_t current = 0, found;
15 while ((found = str.find_first_of(type, current)) != std::string::npos) {
16 str_v.push_back(std::string(str, current, found - current));
19 if (str.size() - current > 0) {
20 str_v.push_back(std::string(str, current, str.size() - current));
22 while (max > 0 && str_v.size() < max) {
28 std::string StringUtil::join(StringList str_v,
const std::string& s,
size_t start,
size_t end)
31 for (
size_t i = start; i < str_v.size();) {
34 if ((end > 0 && i == end) || i == str_v.size())
break;
40 std::string StringUtil::replace(
const std::string& source,
41 const std::string& pattern,
42 const std::string& placement)
45 std::string::size_type pos_before = 0;
46 std::string::size_type pos = 0;
47 std::string::size_type len = pattern.size();
48 while ((pos = source.find(pattern, pos)) != std::string::npos) {
49 result.append(source, pos_before, pos - pos_before);
50 result.append(placement);
54 result.append(source, pos_before, source.size() - pos_before);
58 std::string StringUtil::form(
const std::string& str, ...)
61 static __thread
char ss[1024 * 10];
63 vsnprintf(ss,
sizeof(ss), str.c_str(), ap);
68 std::string StringUtil::toupper(
const std::string& str)
71 transform(s.begin(), s.end(), s.begin(), ::toupper);
75 std::string StringUtil::tolower(
const std::string& str)
78 transform(s.begin(), s.end(), s.begin(), ::tolower);
82 int StringUtil::atoi(
const std::string str)
84 return atoi(str.c_str());
87 double StringUtil::atof(
const std::string str)
89 return atof(str.c_str());
92 long long StringUtil::atoll(
const std::string str)
94 return atoll(str.c_str());
97 bool StringUtil::find(
const std::string& s,
const std::string& str)
99 return s.find(str) != std::string::npos;
102 bool StringUtil::isdigit(
const std::string& s)
104 if (s.find(
"0x") == 0)
return true;
105 for (
size_t i = 0; i < s.size(); i++) {
106 if (i == 0 && s.at(0) ==
'-')
continue;
107 if (!::isdigit(s.at(i)))
return false;
109 return s.size() > 0 && s !=
"-";