26 bool operator()(
const std::string &left,
const std::string &right)
const {
27 unsigned len=(
unsigned int)std::min(left.length(), right.length());
31 for(
unsigned i=0; i<len; i++) {
44 return left.length()<right.length();
50 unsigned len=(
unsigned int)std::min(left.length(), right.length());
54 for(
unsigned i=0; i<len; i++) {
67 return left.length()<right.length() ? -1 : (left.length()==right.length() ? 0 : 1);
77 T_
To(
const std::string &value) {
85 # define ISENUMUPGRADED decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum
88 typename std::enable_if<std::is_constructible<T_, std::string>::value, T_>::type
89 To(
const std::string &value) {
94 typename std::enable_if<!std::is_constructible<T_, std::string>::value && !ISENUMUPGRADED, T_>::type
95 To(
const std::string &value) {
96 std::stringstream ss(value);
105 typename std::enable_if<std::is_constructible<T_, const char*>::value, T_>::type
106 To(
const char *value) {
111 typename std::enable_if<!std::is_constructible<T_, const char*>::value && std::is_constructible<T_, std::string>::value, T_>::type
112 To(
const char *value) {
117 typename std::enable_if<!std::is_constructible<T_, const char*>::value &&
118 !std::is_constructible<T_, std::string>::value &&
121 To(
const char *value) {
122 std::stringstream ss(value);
131 inline char To<char>(
const std::string &value) {
133 return (
char)std::strtol(value.c_str(), &n, 10);
137 inline unsigned char To<unsigned char>(
const std::string &value) {
139 return (
unsigned char)std::strtol(value.c_str(), &n, 10);
143 inline short To<short>(
const std::string &value) {
145 return (
short)std::strtol(value.c_str(), &n, 10);
149 inline unsigned short To<unsigned short>(
const std::string &value) {
151 return (
unsigned short)std::strtol(value.c_str(), &n, 10);
155 inline int To<int>(
const std::string &value) {
157 return (
int)std::strtol(value.c_str(), &n, 10);
161 inline unsigned To<unsigned>(
const std::string &value) {
163 return (
unsigned)std::strtol(value.c_str(), &n, 10);
167 inline long To<long>(
const std::string &value) {
169 return (
long)std::strtol(value.c_str(), &n, 10);
173 inline unsigned long To<unsigned long>(
const std::string &value) {
175 return (
unsigned long)std::strtol(value.c_str(), &n, 10);
179 inline long long To<long long>(
const std::string &value) {
181 return (
long long)std::strtol(value.c_str(), &n, 10);
185 inline unsigned long long To<unsigned long long>(
const std::string &value) {
187 return (
unsigned long long)std::strtol(value.c_str(), &n, 10);
191 inline float To<float>(
const std::string &value) {
192 if(value==
"")
return 0;
194 return (
float)std::strtof(value.c_str(),
nullptr);
198 inline double To<double>(
const std::string &value) {
199 if(value==
"")
return 0;
201 return std::strtod(value.c_str(),
nullptr);
205 inline long double To<long double>(
const std::string &value) {
206 if(value==
"")
return 0;
208 return std::strtold(value.c_str(),
nullptr);
212 inline bool To<bool>(
const std::string &value) {
213 if(value==
"false" || value==
"no" || value==
"" || To<int>(value)==0)
220 inline char To<char>(
const char *value) {
221 return (
char)std::strtol(value,
nullptr, 10);
225 inline unsigned char To<unsigned char>(
const char *value) {
226 return (
unsigned char)std::strtol(value,
nullptr, 10);
230 inline short To<short>(
const char *value) {
231 return (
short)std::strtol(value,
nullptr, 10);
235 inline unsigned short To<unsigned short>(
const char *value) {
236 return (
unsigned short)std::strtol(value,
nullptr, 10);
240 inline int To<int>(
const char *value) {
241 return (
int)std::strtol(value,
nullptr, 10);
245 inline unsigned To<unsigned>(
const char *value) {
246 return (
unsigned)std::strtol(value,
nullptr, 10);
250 inline long To<long>(
const char *value) {
251 return (
long)std::strtol(value,
nullptr, 10);
255 inline unsigned long To<unsigned long>(
const char *value) {
256 return (
unsigned long)std::strtol(value,
nullptr, 10);
260 inline long long To<long long>(
const char *value) {
261 return (
long long)std::strtol(value,
nullptr, 10);
265 inline unsigned long long To<unsigned long long>(
const char *value) {
266 return (
unsigned long long)std::strtol(value,
nullptr, 10);
270 inline float To<float>(
const char *value) {
271 return (
float)std::atof(value);
275 inline double To<double>(
const char *value) {
276 return std::atof(value);
280 inline long double To<long double>(
const char *value) {
281 return std::atof(value);
285 inline bool To<bool>(
const char *value) {
286 return To<bool>(std::string(value));
293 T_ HexTo(
const std::string &value);
296 inline int HexTo<int>(
const std::string &value) {
297 return std::stoi(value,
nullptr, 16);
301 inline long HexTo<long>(
const std::string &value) {
302 return std::stol(value,
nullptr, 16);
306 inline long long HexTo<long long>(
const std::string &value) {
307 return std::stoll(value,
nullptr, 16);
311 inline unsigned int HexTo<unsigned int>(
const std::string &value) {
312 return (
unsigned int)std::stoul(value,
nullptr, 16);
316 inline unsigned long HexTo<unsigned long>(
const std::string &value) {
317 return std::stoul(value,
nullptr, 16);
321 inline unsigned long long HexTo<unsigned long long>(
const std::string &value) {
322 return std::stoull(value,
nullptr, 16);
328 inline std::string
PadStart(std::string str, std::size_t len,
char pad =
' ') {
330 str.insert(0, len - str.size(), pad);
337 inline std::string
PadEnd(std::string str, std::size_t len,
char pad =
' ') {
339 str.insert(str.end(), len - str.size(), pad);
349 inline std::string
Replace(std::string str,
const std::string &find,
const std::string &replace) {
350 std::string::size_type l=0;
352 auto flen=find.length();
353 auto rlen=replace.length();
355 if(!find.length())
return str;
357 while( (l=str.find(find, l)) != str.npos ) {
359 str.insert(l, replace);
370 inline std::string
Trim(std::string str,
const std::string &chars=
" \t\n\r") {
371 if(!str.length())
return "";
373 const char *ptr=str.c_str();
374 while(*ptr && chars.find_first_of(*ptr)!=chars.npos) {
377 str=str.substr(ptr-str.c_str());
379 while(str.length() && chars.find_first_of(str[str.length()-1])!=chars.npos) {
380 str.resize(str.length()-1);
390 inline std::string
TrimStart(std::string str,
const std::string &chars=
" \t\n\r") {
391 if(!str.length())
return "";
393 const char *ptr=str.c_str();
394 while(*ptr && chars.find_first_of(*ptr)!=chars.npos) {
397 str=str.substr(ptr-str.c_str());
406 inline std::string
TrimEnd(std::string str,
const std::string &chars=
" \t\n\r") {
407 while(str.length() && chars.find_first_of(str[str.length()-1])!=chars.npos) {
408 str.resize(str.length()-1);
417 for(
auto it=str.begin();it!=str.end();++it) {
427 for(
auto it=str.begin();it!=str.end();++it) {
436 inline std::string
From(
const char &value) {
437 return std::to_string(value);
440 inline std::string
From(
const unsigned char &value) {
441 return std::to_string(value);
444 inline std::string
From(
const int &value) {
445 return std::to_string(value);
448 inline std::string
From(
const unsigned &value) {
449 return std::to_string(value);
452 inline std::string
From(
const long &value) {
453 return std::to_string(value);
456 inline std::string
From(
const unsigned long &value) {
457 return std::to_string(value);
460 inline std::string
From(
const long long &value) {
461 return std::to_string(value);
464 inline std::string
From(
const unsigned long long &value) {
465 return std::to_string(value);
468 inline std::string
From(
const float &value) {
469 std::stringstream ss;
471 ss<<std::fixed<<std::setprecision(0)<<value;
474 ss<<std::setprecision(7)<<value;
480 inline std::string
From(
const double &value) {
481 std::stringstream ss;
483 ss<<std::fixed<<std::setprecision(0)<<value;
486 ss<<std::setprecision(14)<<value;
491 inline std::string
From(
const long double &value) {
492 std::stringstream ss;
494 ss<<std::fixed<<std::setprecision(0)<<value;
497 ss<<std::setprecision(28)<<value;
502 inline std::string
From(
const std::string &value) {
507 typename std::enable_if<std::is_convertible<T_, std::string>::value, std::string>::type
508 From(
const T_ &item) {
509 return (std::string)item;
513 typename std::enable_if<!std::is_convertible<T_, std::string>::value && !decltype(
gorgon__enum_tr_loc((*(T_*)
nullptr)))::isupgradedenum, std::string>::type
514 From(const T_ &item) {
515 std::stringstream ss;
530 static one test(decltype(((std::ostream*)
nullptr)->
operator<<((TT*)
nullptr))) {
return one(); }
532 static two test(...) {
return two(); }
535 static const bool Value =
sizeof( test(*(std::ostream*)
nullptr) )==1;
538 inline void streamthis(std::stringstream &stream) {
541 template<
class T_,
class ...P_>
542 void streamthis(std::stringstream &stream,
const T_ &first,
const P_&... rest) {
545 streamthis(stream, rest...);
552 IsStreamable<T_>::Value ||
553 std::is_convertible<T_, std::string>::value ||
559 template<
class ...P_>
561 std::stringstream ss;
562 streamthis(ss, rest...);
570 if((c & 0b10000000) == 0b00000000)
return 1;
572 if((c & 0b11100000) == 0b11000000)
return 2;
574 if((c & 0b11110000) == 0b11100000)
return 3;
576 if((c & 0b11111000) == 0b11110000)
return 4;
584 if(c < 0x80)
return 1;
586 if(c < 0x800)
return 2;
588 if(c < 0x10000)
return 3;
590 if(c < 0x0010FFFF)
return 4;
598 while(i < s.length()) {
616 s.resize(s.size() + bytes);
618 auto it = s.rbegin();
621 while(cur < bytes-1) {
622 *it = (c & 0b00111111) | 0b10000000;
631 else if(bytes == 2) {
632 *it = c | 0b11000000;
634 else if(bytes == 3) {
635 *it = c | 0b11100000;
637 else if(bytes == 4) {
638 *it = c | 0b11110000;
663 while(cur < bytes-1) {
664 *it = (c & 0b00111111) | 0b10000000;
673 else if(bytes == 2) {
674 *it = c | 0b11000000;
676 else if(bytes == 3) {
677 *it = c | 0b11100000;
679 else if(bytes == 4) {
680 *it = c | 0b11110000;
685 s.insert(pos, it, bytes);
730 std::string
From(
const T_ &item) {
return ""; }
735 std::string
Join(
const T_ &vec,
const std::string &glue =
", ") {
737 int gluesize = (int)glue.size();
739 for(
const std::string &s : vec) {
741 totalsize += gluesize;
743 totalsize += (int)s.size();
747 ret.resize(totalsize);
750 char *data = &ret[0];
751 const char *gluedata = glue.data();
753 for(
const std::string &s : vec) {
755 std::memcpy(data, gluedata, gluesize);
759 std::memcpy(data, s.data(), s.size());
779 inline std::string
Extract(std::string &original,
const std::string &marker,
bool trim =
false) {
780 auto pos=original.find(marker);
782 if(pos==original.npos) {
789 std::string ret=original.substr(0, pos);
790 original=original.substr(pos+marker.length());
811 inline std::string
Extract(std::string &original,
char marker,
bool trim =
false) {
812 auto pos=original.find_first_of(marker);
814 if(pos==original.npos) {
821 std::string ret=original.substr(0, pos);
822 original=original.substr(pos+1);
858 for(
auto &c : original) {
864 else if(inquotes==2) {
870 std::string temp=original.substr(0, pos);
871 original=original.substr(pos+1);
878 else if(c==
'"' && (quotetype==QuoteType::Double || quotetype==
QuoteType::Both)) {
894 # undef ISENUMUPGRADED