76 #define __STDC_FORMAT_MACROS 102 std::vector<std::string>& tokenvec)
104 const size_t sz = tokenstr.size();
105 for (
size_t begp = 0, endp = 0; begp < sz; begp = endp+1) {
106 begp = tokenstr.find_first_not_of(delim, begp);
107 if (begp == string::npos) {
111 endp = tokenstr.find_first_of(delim, begp);
112 if (endp == string::npos) {
115 string x = tokenstr.substr(begp, endp - begp);
116 tokenvec.push_back(x);
123 std::vector<std::string>& tokenvec)
125 const int delimsz = strlen(delim);
126 const size_t sz = tokenstr.size();
128 for (
size_t begp = 0, endp = 0; begp < sz; begp = endp + delimsz) {
129 endp = tokenstr.find(delim, begp);
130 if (endp == string::npos) {
133 string x = tokenstr.substr(begp, endp - begp);
134 tokenvec.push_back(x);
144 join(
const std::vector<string>& tokenvec,
const char* delim,
145 size_t begIdx,
size_t endIdx)
150 for (
size_t i = begIdx; i < endIdx; ++i) {
151 result += tokenvec[i];
152 if (i + 1 < endIdx) {
165 toLong(
const char* str,
unsigned* endidx)
168 DIAG_Assert((str && str[0] !=
'\0'),
"StrUtil::toLong: empty string!");
172 value = strtol(str, &endptr, 0);
174 *endidx = (endptr - str) /
sizeof(
char);
176 if (errno || (!endidx && endptr && strlen(endptr) > 0)) {
177 string msg =
"[StrUtil::toLong] Cannot convert `" + string(str)
178 +
"' to integral (long) value";
180 msg += string(
" (") + strerror(errno) + string(
")");
192 DIAG_Assert((str && str[0] !=
'\0'),
"StrUtil::toUInt64: empty string!");
196 value = strtoull(str, &endptr, 0);
198 *endidx = (endptr - str) /
sizeof(
char);
200 if (errno || (!endidx && endptr && strlen(endptr) > 0)) {
201 string msg =
"[StrUtil::toUInt64] Cannot convert `" + string(str)
202 +
"' to integral (uint64_t) value";
204 msg += string(
" (") + strerror(errno) + string(
")");
213 toDbl(
const char* str,
unsigned* endidx)
216 DIAG_Assert((str && str[0] !=
'\0'),
"StrUtil::toDbl: empty string!");
220 value = strtod(str, &endptr);
222 *endidx = (endptr - str) /
sizeof(
char);
224 if (errno || (!endidx && endptr && strlen(endptr) > 0)) {
225 string msg =
"[StrUtil::toDbl] Cannot convert `" + string(str)
226 +
"' to real (double) value.";
228 msg += string(
" (") + strerror(errno) + string(
")");
245 const char* format =
NULL;
256 sprintf(buf, format, x);
264 const char* format =
NULL;
283 sprintf(buf, format, x);
291 const char* format =
NULL;
299 format =
"%#" PRIx64;
306 sprintf(buf, format, x);
314 const char* format =
NULL;
322 format =
"%#" PRIx64;
329 sprintf(buf, format, x);
337 sprintf(buf,
"%p", x);
343 toStr(
const double x,
const char* format)
346 sprintf(buf, format, x);
double toDbl(const char *str, unsigned *endidx)
uint64_t toUInt64(const char *str, unsigned *endidx)
string toStr(const int x, int base)
const char * DIAG_Unimplemented
long toLong(const char *str, unsigned *endidx)
void tokenize_str(const std::string &tokenstr, const char *delim, std::vector< std::string > &tokenvec)
string join(const std::vector< string > &tokenvec, const char *delim, size_t begIdx, size_t endIdx)
void tokenize_char(const std::string &tokenstr, const char *delim, std::vector< std::string > &tokenvec)