19 #if defined(_MSC_VER) && _MSC_VER >= 1800
22 #define AJA_BASE_USECPP_11 1
23 #elif defined(__clang__)
25 #if __cplusplus >= 201402L
26 #if __has_include(<codecvt>)
27 #define AJA_BASE_USECPP_11 1
29 #elif __cplusplus >= 201103L
32 #if defined(_LIBCPP_VERSION)
33 #define AJA_BASE_USECPP_11 1
36 #elif defined(__GNUC__)
40 #define AJA_BASE_USECPP_11 1
42 #elif __cplusplus >= 201103L
44 #define AJA_BASE_USECPP_11 1
48 #if defined(AJA_BASE_USECPP_11)
52 #if defined(AJA_WINDOWS)
61 bool starts_with(
const std::string &str,
const std::string &needle)
63 return (str.substr(0, needle.size()) == needle);
68 return str[0] == needle;
71 bool starts_with(
const std::wstring& wstr,
const std::wstring& needle)
73 return (wstr.substr(0, needle.size()) == needle);
75 bool starts_with(
const std::wstring& wstr,
const wchar_t needle)
77 if (wstr.length() > 0)
78 return wstr[0] == needle;
83 bool ends_with(
const std::string& str,
const std::string& needle)
85 if (needle.length() < str.length()) {
86 size_t idx = str.length()-needle.length();
87 return str.rfind(needle, idx) == idx;
91 bool ends_with(
const std::string& str,
const char needle)
93 size_t idx = str.length() - 1;
94 return str.rfind(needle, idx) == idx;
96 bool ends_with(
const std::wstring& wstr,
const std::wstring& needle)
98 if (needle.length() < wstr.length()) {
99 size_t idx = wstr.length() - needle.length();
100 return wstr.rfind(needle, idx) == idx;
104 bool ends_with(
const std::wstring& wstr,
const wchar_t needle)
106 size_t idx = wstr.length() - 1;
107 return wstr.rfind(needle, idx) == idx;
110 std::string&
replace(std::string& str,
const std::string& from,
const std::string& to)
114 for (
size_t pos = 0; (pos = str.find(from, pos)) != std::string::npos; pos += to.size())
116 str.replace(pos, from.size(), to);
122 int stoi (
const std::string & str, std::size_t * idx,
int base)
127 long stol (
const std::string & str, std::size_t * idx,
int base)
130 long retVal = ::strtol(str.c_str(), &pEnd, base);
133 *idx = pEnd - str.c_str();
143 unsigned long stoul (
const std::string & str, std::size_t * idx,
int base)
146 unsigned long retVal = ::strtoul(str.c_str(), &pEnd, base);
149 *idx = pEnd - str.c_str();
154 unsigned long long stoull (
const std::string & str, std::size_t * idx,
int base)
156 return (
unsigned long long)
aja::stoul(str, idx, base);
159 float stof (
const std::string & str, std::size_t * idx)
164 double stod (
const std::string & str, std::size_t * idx)
167 double retVal = ::strtod(str.c_str(), &pEnd);
170 *idx = pEnd - str.c_str();
175 long double stold (
const std::string & str, std::size_t * idx)
182 return val ?
"true" :
"false";
187 std::ostringstream oss; oss << val;
193 std::ostringstream oss; oss << val;
199 std::ostringstream oss; oss << val;
205 std::ostringstream oss; oss << val;
211 std::ostringstream oss; oss << val;
217 std::ostringstream oss; oss << val;
223 std::ostringstream oss;
225 oss.setf(std::ios::fixed, std::ios::floatfield);
232 std::ostringstream oss;
234 oss.setf(std::ios::fixed, std::ios::floatfield);
241 std::ostringstream oss;
243 oss.setf(std::ios::fixed, std::ios::floatfield);
251 #if defined(AJA_BASE_USECPP_11)
252 std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t> converterX;
253 wstr = converterX.from_bytes(str);
256 #if defined(AJA_WINDOWS)
257 const char *tmpPtr = str.c_str();
258 uint32_t codePage = CP_UTF8;
260 size_t len = MultiByteToWideChar(codePage, flags, tmpPtr, (
int)str.length(),
NULL, 0);
262 int retVal = MultiByteToWideChar(codePage, flags, tmpPtr, (
int)str.length(), &wstr[0], (
int)len);
268 std::mbstate_t state = std::mbstate_t();
269 mbrlen(
NULL, 0, &state);
270 const char *tmpPtr = str.c_str();
271 int len = (int)mbsrtowcs(
NULL, &tmpPtr, 0, &state);
275 wstr.resize((
size_t)len);
276 int num_chars = (int)mbsrtowcs(&wstr[0], &tmpPtr, wstr.length(), &state);
288 #if defined(AJA_BASE_USECPP_11)
289 std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t> converterX;
290 str = converterX.to_bytes(wstr);
293 #if defined(AJA_WINDOWS)
294 const wchar_t *tmpPtr = wstr.c_str();
295 uint32_t codePage = CP_UTF8;
297 size_t len = WideCharToMultiByte(codePage, flags, tmpPtr, (
int)wstr.length(),
NULL, 0,
NULL,
NULL);
299 int retVal = WideCharToMultiByte(codePage, flags, tmpPtr, (
int)wstr.length(), &str[0], (
int)len,
NULL,
NULL);
305 std::mbstate_t state = std::mbstate_t();
306 mbrlen(
NULL, 0, &state);
307 const wchar_t *tmpPtr = wstr.c_str();
308 int len = (int)wcsrtombs(
NULL, &tmpPtr, 0, &state);
312 str.resize((
size_t)len);
313 int num_chars = (int)wcsrtombs(&str[0], &tmpPtr, str.length(), &state);
322 inline size_t local_min (
const size_t & a,
const size_t & b)
324 #if defined(AJA_WINDOWS) && !defined(AJA_BASE_USECPP_11)
332 return std::min(a, b);
338 if(c_str ==
NULL || c_str_size < 1)
341 size_t maxSize =
local_min(str.size(), c_str_size-1);
342 for(
size_t i=0;i<maxSize;++i)
346 c_str[maxSize] =
'\0';
350 void split (
const std::string & str,
const char delim, std::vector<std::string> & elems)
353 std::stringstream ss(str);
355 while(std::getline(ss, item, delim))
357 elems.push_back(item);
361 if (str.length() > 0 && str[str.length()-1] == delim)
367 void split (
const std::wstring & str,
const wchar_t delim, std::vector<std::wstring> & elems)
370 std::wstringstream ss(str);
372 while(std::getline(ss, item, delim))
374 elems.push_back(item);
378 if (str.length() > 0 && str[str.length()-1] == delim)
380 elems.push_back(L
"");
384 std::vector<std::string>
split (
const std::string & str,
const char delim)
386 std::vector<std::string> elems;
387 split(str, delim, elems);
391 std::vector<std::wstring>
split (
const std::wstring & str,
const wchar_t delim)
393 std::vector<std::wstring> elems;
394 split(str, delim, elems);
398 std::vector<std::string>
split (
const std::string & inStr,
const std::string & inDelim)
400 std::vector<std::string> result;
402 size_t delimPos(inStr.find(inDelim, startPos));
403 while (delimPos != std::string::npos)
405 const std::string item (inStr.substr(startPos, delimPos - startPos));
406 result.push_back(item);
407 startPos = delimPos + inDelim.length();
408 delimPos = inStr.find(inDelim, startPos);
410 if (startPos < inStr.length())
411 result.push_back(inStr.substr(startPos, inStr.length()-startPos));
412 else if (startPos == inStr.length())
413 result.push_back(std::string());
417 std::vector<std::wstring>
split (
const std::wstring & inStr,
const std::wstring & inDelim)
419 std::vector<std::wstring> result;
421 size_t delimPos(inStr.find(inDelim, startPos));
422 while (delimPos != std::wstring::npos)
424 const std::wstring item (inStr.substr(startPos, delimPos - startPos));
425 result.push_back(item);
426 startPos = delimPos + inDelim.length();
427 delimPos = inStr.find(inDelim, startPos);
429 if (startPos < inStr.length())
430 result.push_back(inStr.substr(startPos, inStr.length()-startPos));
431 else if (startPos == inStr.length())
432 result.push_back(std::wstring());
436 std::string &
lower (std::string & str)
438 std::transform (str.begin(), str.end(), str.begin(), ::tolower);
442 std::string &
upper (std::string & str)
444 std::transform (str.begin(), str.end(), str.begin(), ::toupper);
448 std::string &
lstrip (std::string & str,
const std::string & ws)
450 str.erase(0, str.find_first_not_of(ws));
454 std::string &
rstrip (std::string & str,
const std::string & ws)
457 str.erase (str.find_last_not_of(ws)+1, str.length()-1);
461 std::string &
strip (std::string & str,
const std::string & ws)
468 std::string
join (
const std::vector<std::string> & parts,
const std::string & delim)
470 std::ostringstream oss;
471 for (std::vector<std::string>::const_iterator it(parts.begin()); it != parts.end(); )
474 if (++it != parts.end())
480 std::string
join (
const std::set<std::string> & parts,
const std::string & delim)
482 std::ostringstream oss;
483 for (std::set<std::string>::const_iterator it(parts.begin()); it != parts.end(); )
486 if (++it != parts.end())
492 char*
safer_strncpy(
char* target,
const char* source,
size_t num,
size_t maxSize)
494 int32_t lastIndex = (int32_t)maxSize-1;
496 if (lastIndex < 0 || target ==
NULL)
502 num = (size_t)lastIndex;
504 char *retVal = strncpy(target, source, num);
512 {
static const std::string sHexDigits(
"0123456789ABCDEFabcdef");
513 return sHexDigits.find(inChr) != std::string::npos;
517 {
static const std::string sDecDigits(
"0123456789");
518 return sDecDigits.find(inChr) != std::string::npos;
522 {
static const std::string sLegalChars(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
523 return sLegalChars.find(inChr) != std::string::npos;
528 if (inStr.length() > inMaxLength)
530 for (
size_t ndx(0); ndx < inStr.size(); ndx++)
538 if (inStr.length() < 3)
540 std::string hexStr(inStr);
lower(hexStr);
541 if (hexStr[0] ==
'0' && hexStr[1] ==
'x')
543 if (hexStr.length() > 16)
545 for (
size_t ndx(0); ndx < hexStr.size(); ndx++)
548 while (hexStr.length() != 16)
549 hexStr =
'0' + hexStr;
550 std::istringstream iss(hexStr);
552 iss >> std::hex >> u64;
558 for (
size_t ndx(0); ndx < inStr.size(); ndx++)