9 static const char rcsid[] __attribute__((used)) =
"$Id: String.cpp 1911 2017-09-02 17:37:35Z sella $";
14 #include "../variant/Variant.h"
27 #include <boost/algorithm/string/replace.hpp>
29 #define BUFFER_SIZE (1024 * 32)
32 using namespace sella::util;
43 } __attribute__((packed));
45 static const unsigned char gzip_header[] = {
46 0x1f, 0x8b, Z_DEFLATED, 0,
51 std::string String::sprintf(
const char* format, ...) throw () {
54 va_start(args, format);
55 std::string buffer = vsprintf(format, args);
61 std::string String::sprintf(
const std::string &format, ...) throw () {
64 va_start(args, format);
65 std::string buffer = vsprintf(format.c_str(), args);
71 std::string String::vsprintf(
const char* format, va_list args)
throw () {
72 char buffer[BUFFER_SIZE];
74 vsnprintf(buffer,
sizeof(buffer), format, args);
79 std::string String::vsprintf(
const std::string &format, va_list args)
throw () {
80 char buffer[BUFFER_SIZE];
82 vsnprintf(buffer,
sizeof(buffer), format.c_str(), args);
84 return std::string(buffer);
87 int String::strcmp(
const std::string &a,
const std::string &b)
throw () {
88 size_t len = std::min(a.size(), b.size());
90 for (
size_t i = 0; i < len; i++) {
96 if (a.size() != b.size()) {
97 return (a.size() - b.size());
103 int String::strcasecmp(
const std::string &a,
const std::string &b)
throw () {
104 size_t len = std::min(a.size(), b.size());
106 for (
size_t i = 0; i < len; i++) {
107 if (std::tolower(a[i]) != std::tolower(b[i])) {
108 return (a[i] - b[i]);
112 if (a.size() != b.size()) {
113 return (a.size() - b.size());
119 int String::strncmp(
const std::string &a,
const std::string &b,
size_t len)
throw () {
120 size_t size = std::min(std::min(a.size(), b.size()), len);
122 for (
size_t i = 0; i < size; i++) {
124 return (a[i] - b[i]);
128 if (len != size && a.size() != b.size()) {
129 return (a.size() - b.size());
135 int String::strncasecmp(
const std::string &a,
const std::string &b,
size_t len)
throw () {
136 size_t size = std::min(std::min(a.size(), b.size()), len);
138 for (
size_t i = 0; i < size; i++) {
139 if (std::tolower(a[i]) != std::tolower(b[i])) {
140 return (a[i] - b[i]);
144 if (len != size && a.size() != b.size()) {
145 return (a.size() - b.size());
151 bool String::str_imatch(
const std::string &a,
const std::string &b)
throw () {
152 if (a.size() != b.size()) {
156 size_t len = std::min(a.size(), b.size());
158 for (
size_t i = 0; i < len; i++) {
159 if (std::tolower(a[i]) != std::tolower(b[i])) {
167 std::string String::substr_replace(
const std::string &src,
const std::string &search,
const std::string &replace,
bool global)
throw () {
168 size_t i = 0, inc = (replace.length()) ? replace.length() : 1;
169 std::string dst = src;
171 if (!search.empty()) {
173 if ((i = dst.find(search, i)) == std::string::npos) {
177 dst.replace(i, search.length(), replace);
188 std::string& String::substr_replace(std::string &src,
const std::string &search,
const std::string &replace,
bool global)
throw () {
189 size_t i = 0, inc = (replace.length()) ? replace.length() : 1;
191 if (!search.empty()) {
193 if ((i = src.find(search, i)) == std::string::npos) {
197 src.replace(i, search.length(), replace);
208 std::string String::substr_ireplace(
const std::string &src,
const std::string &search,
const std::string &replace,
bool global)
throw () {
210 return boost::algorithm::ireplace_all_copy(src, search, replace);
212 return boost::algorithm::ireplace_first_copy(src, search, replace);
216 std::string& String::substr_ireplace(std::string &src,
const std::string &search,
const std::string &replace,
bool global)
throw () {
218 boost::algorithm::ireplace_all(src, search, replace);
220 boost::algorithm::ireplace_first(src, search, replace);
226 std::string String::escape_percent(
const std::string &src)
throw () {
227 return substr_replace(src,
"%",
"%%");
230 std::string String::escape(
const std::string &src,
const std::string &chars,
const std::string &escape)
throw () {
234 for (
auto s = src.begin(), end = src.end(); s != end; ++s) {
237 for (
auto c = chars.begin(), end = chars.end(); c != end; ++c) {
239 result += escape + *s;
253 std::string String::regex_replace(
const std::string &src,
const boost::regex ®ex,
const std::string &replace, MatchFlag mFlags)
throw (RegexException) {
255 return boost::regex_replace(src, regex, replace, mFlags);
256 }
catch (boost::bad_expression &e) {
257 RETHROW_CODE(RegexException, 0, e);
258 }
catch (std::exception &e) {
259 RETHROW_CODE(RegexException, 0, e);
263 std::string String::regex_replace(
const std::string &src,
const std::string ®ex,
const std::string &replace, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
265 const boost::regex rx(regex, reFlags);
267 return boost::regex_replace(src, rx, replace, mFlags);
268 }
catch (boost::bad_expression &e) {
269 RETHROW_CODE(RegexException, 0, e);
270 }
catch (std::exception &e) {
271 RETHROW_CODE(RegexException, 0, e);
275 std::string& String::regex_replace(std::string &src,
const boost::regex ®ex,
const std::string &replace, MatchFlag mFlags)
throw (RegexException) {
277 src = boost::regex_replace(src, regex, replace, mFlags);
280 }
catch (boost::bad_expression &e) {
281 RETHROW_CODE(RegexException, 0, e);
282 }
catch (std::exception &e) {
283 RETHROW_CODE(RegexException, 0, e);
287 std::string& String::regex_replace(std::string &src,
const std::string ®ex,
const std::string &replace, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
289 const boost::regex rx(regex, reFlags);
291 src = boost::regex_replace(src, rx, replace, mFlags);
294 }
catch (boost::bad_expression &e) {
295 RETHROW_CODE(RegexException, 0, e);
296 }
catch (std::exception &e) {
297 RETHROW_CODE(RegexException, 0, e);
301 std::string String::regex_ireplace(
const std::string &src,
const boost::regex ®ex,
const std::string &replace, MatchFlag mFlags)
throw (RegexException) {
303 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
305 return regex_replace(src, rx, replace, mFlags);
306 }
catch (boost::bad_expression &e) {
307 RETHROW_CODE(RegexException, 0, e);
308 }
catch (std::exception &e) {
309 RETHROW_CODE(RegexException, 0, e);
313 std::string String::regex_ireplace(
const std::string &src,
const std::string ®ex,
const std::string &replace, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
315 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
317 return regex_replace(src, rx, replace, mFlags);
318 }
catch (boost::bad_expression &e) {
319 RETHROW_CODE(RegexException, 0, e);
320 }
catch (std::exception &e) {
321 RETHROW_CODE(RegexException, 0, e);
325 std::string& String::regex_ireplace(std::string &src,
const boost::regex ®ex,
const std::string &replace, MatchFlag mFlags)
throw (RegexException) {
327 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
329 return regex_replace(src, rx, replace, mFlags);
330 }
catch (boost::bad_expression &e) {
331 RETHROW_CODE(RegexException, 0, e);
332 }
catch (std::exception &e) {
333 RETHROW_CODE(RegexException, 0, e);
337 std::string& String::regex_ireplace(std::string &src,
const std::string ®ex,
const std::string &replace, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
339 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
341 return regex_replace(src, rx, replace, mFlags);
342 }
catch (boost::bad_expression &e) {
343 RETHROW_CODE(RegexException, 0, e);
344 }
catch (std::exception &e) {
345 RETHROW_CODE(RegexException, 0, e);
349 bool String::regex_match(
const std::string &src,
const boost::regex ®ex, MatchFlag mFlags)
throw (RegexException) {
351 return boost::regex_match(src, regex, mFlags);
352 }
catch (boost::bad_expression &e) {
353 RETHROW_CODE(RegexException, 0, e);
354 }
catch (std::exception &e) {
355 RETHROW_CODE(RegexException, 0, e);
359 bool String::regex_match(
const std::string &src,
const std::string ®ex, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
361 const boost::regex rx(regex, reFlags);
363 return regex_match(src, rx, mFlags);
364 }
catch (boost::bad_expression &e) {
365 RETHROW_CODE(RegexException, 0, e);
366 }
catch (std::exception &e) {
367 RETHROW_CODE(RegexException, 0, e);
371 bool String::regex_match(
const std::string &src,
const boost::regex ®ex, boost::cmatch &matches, MatchFlag mFlags)
throw (RegexException) {
373 return boost::regex_match(src.c_str(), matches, regex);
374 }
catch (boost::bad_expression &e) {
375 RETHROW_CODE(RegexException, 0, e);
376 }
catch (std::exception &e) {
377 RETHROW_CODE(RegexException, 0, e);
381 bool String::regex_match(
const std::string &src,
const std::string ®ex, boost::cmatch &matches, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
383 const boost::regex rx(regex, reFlags);
385 return regex_match(src, rx, matches, mFlags);
386 }
catch (boost::bad_expression &e) {
387 RETHROW_CODE(RegexException, 0, e);
388 }
catch (std::exception &e) {
389 RETHROW_CODE(RegexException, 0, e);
393 bool String::regex_match(
const std::string &src,
const boost::regex ®ex, std::string &match, MatchFlag mFlags)
throw (RegexException) {
396 if (regex_match(src, regex, m, mFlags)) {
405 bool String::regex_match(
const std::string &src,
const std::string ®ex, std::string &match, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
407 const boost::regex rx(regex, reFlags);
409 return regex_match(src, rx, match, mFlags);
410 }
catch (boost::bad_expression &e) {
411 RETHROW_CODE(RegexException, 0, e);
412 }
catch (std::exception &e) {
413 RETHROW_CODE(RegexException, 0, e);
417 bool String::regex_match(
const std::string &src,
const boost::regex ®ex, std::vector<std::string> &matches, MatchFlag mFlags)
throw (RegexException) {
420 if (regex_match(src, regex, m, mFlags)) {
421 for (
size_t i = 0; i < m.size(); i++) {
422 matches.push_back(m[i]);
431 bool String::regex_match(
const std::string &src,
const std::string ®ex, std::vector<std::string> &matches, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
433 const boost::regex rx(regex, reFlags);
435 return regex_match(src, rx, matches, mFlags);
436 }
catch (boost::bad_expression &e) {
437 RETHROW_CODE(RegexException, 0, e);
438 }
catch (std::exception &e) {
439 RETHROW_CODE(RegexException, 0, e);
443 bool String::regex_imatch(
const std::string &src,
const boost::regex ®ex, MatchFlag mFlags)
throw (RegexException) {
445 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
447 return regex_match(src, rx, mFlags);
448 }
catch (boost::bad_expression &e) {
449 RETHROW_CODE(RegexException, 0, e);
450 }
catch (std::exception &e) {
451 RETHROW_CODE(RegexException, 0, e);
455 bool String::regex_imatch(
const std::string &src,
const std::string ®ex, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
457 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
459 return regex_match(src, rx, mFlags);
460 }
catch (boost::bad_expression &e) {
461 RETHROW_CODE(RegexException, 0, e);
462 }
catch (std::exception &e) {
463 RETHROW_CODE(RegexException, 0, e);
467 bool String::regex_imatch(
const std::string &src,
const boost::regex ®ex, boost::cmatch &matches, MatchFlag mFlags)
throw (RegexException) {
469 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
471 return regex_match(src, rx, matches, mFlags);
472 }
catch (boost::bad_expression &e) {
473 RETHROW_CODE(RegexException, 0, e);
474 }
catch (std::exception &e) {
475 RETHROW_CODE(RegexException, 0, e);
479 bool String::regex_imatch(
const std::string &src,
const std::string ®ex, boost::cmatch &matches, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
481 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
483 return regex_match(src, rx, matches, mFlags);
484 }
catch (boost::bad_expression &e) {
485 RETHROW_CODE(RegexException, 0, e);
486 }
catch (std::exception &e) {
487 RETHROW_CODE(RegexException, 0, e);
491 bool String::regex_imatch(
const std::string &src,
const boost::regex ®ex, std::string &match, MatchFlag mFlags)
throw (RegexException) {
493 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
495 return regex_match(src, rx, match, mFlags);
496 }
catch (boost::bad_expression &e) {
497 RETHROW_CODE(RegexException, 0, e);
498 }
catch (std::exception &e) {
499 RETHROW_CODE(RegexException, 0, e);
503 bool String::regex_imatch(
const std::string &src,
const std::string ®ex, std::string &match, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
505 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
507 return regex_match(src, rx, match, mFlags);
508 }
catch (boost::bad_expression &e) {
509 RETHROW_CODE(RegexException, 0, e);
510 }
catch (std::exception &e) {
511 RETHROW_CODE(RegexException, 0, e);
515 bool String::regex_imatch(
const std::string &src,
const boost::regex ®ex, std::vector<std::string> &matches, MatchFlag mFlags)
throw (RegexException) {
517 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
519 return regex_match(src, rx, matches, mFlags);
520 }
catch (boost::bad_expression &e) {
521 RETHROW_CODE(RegexException, 0, e);
522 }
catch (std::exception &e) {
523 RETHROW_CODE(RegexException, 0, e);
527 bool String::regex_imatch(
const std::string &src,
const std::string ®ex, std::vector<std::string> &matches, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
529 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
531 return regex_match(src, rx, matches, mFlags);
532 }
catch (boost::bad_expression &e) {
533 RETHROW_CODE(RegexException, 0, e);
534 }
catch (std::exception &e) {
535 RETHROW_CODE(RegexException, 0, e);
539 bool String::regex_search(
const std::string &src,
const boost::regex ®ex, MatchFlag mFlags)
throw (RegexException) {
541 return boost::regex_search(src, regex, mFlags);
542 }
catch (boost::bad_expression &e) {
543 RETHROW_CODE(RegexException, 0, e);
544 }
catch (std::exception &e) {
545 RETHROW_CODE(RegexException, 0, e);
549 bool String::regex_search(
const std::string &src,
const std::string ®ex, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
551 const boost::regex rx(regex, reFlags);
553 return regex_search(src, rx, mFlags);
554 }
catch (boost::bad_expression &e) {
555 RETHROW_CODE(RegexException, 0, e);
556 }
catch (std::exception &e) {
557 RETHROW_CODE(RegexException, 0, e);
561 bool String::regex_search(
const std::string &src,
const boost::regex ®ex, boost::cmatch &matches, MatchFlag mFlags)
throw (RegexException) {
563 return boost::regex_search(src.c_str(), matches, regex);
564 }
catch (boost::bad_expression &e) {
565 RETHROW_CODE(RegexException, 0, e);
566 }
catch (std::exception &e) {
567 RETHROW_CODE(RegexException, 0, e);
571 bool String::regex_search(
const std::string &src,
const std::string ®ex, boost::cmatch &matches, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
573 const boost::regex rx(regex, reFlags);
575 return regex_search(src, rx, matches, mFlags);
576 }
catch (boost::bad_expression &e) {
577 RETHROW_CODE(RegexException, 0, e);
578 }
catch (std::exception &e) {
579 RETHROW_CODE(RegexException, 0, e);
583 bool String::regex_search(
const std::string &src,
const boost::regex ®ex, std::string &match, MatchFlag mFlags)
throw (RegexException) {
586 if (regex_search(src, regex, m, mFlags)) {
595 bool String::regex_search(
const std::string &src,
const std::string ®ex, std::string &match, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
597 const boost::regex rx(regex, reFlags);
599 return regex_search(src, rx, match, mFlags);
600 }
catch (boost::bad_expression &e) {
601 RETHROW_CODE(RegexException, 0, e);
602 }
catch (std::exception &e) {
603 RETHROW_CODE(RegexException, 0, e);
607 bool String::regex_search(
const std::string &src,
const boost::regex ®ex, std::vector<std::string> &matches,
bool global,
bool subexp, MatchFlag mFlags)
throw (RegexException) {
609 const char *p = src.c_str();
612 while (boost::regex_search(p, m, regex, mFlags)) {
614 for (
size_t i = 0; i < m.size(); i++) {
615 matches.push_back(m[i]);
618 matches.push_back(m[0]);
620 p = m.suffix().str().c_str();
625 return (!matches.empty());
626 }
catch (boost::bad_expression &e) {
627 RETHROW_CODE(RegexException, 0, e);
628 }
catch (std::exception &e) {
629 RETHROW_CODE(RegexException, 0, e);
633 bool String::regex_search(
const std::string &src,
const std::string ®ex, std::vector<std::string> &matches,
bool global,
bool subexp, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
635 const boost::regex rx(regex, reFlags);
637 return regex_search(src, rx, matches, global, subexp, mFlags);
638 }
catch (boost::bad_expression &e) {
639 RETHROW_CODE(RegexException, 0, e);
640 }
catch (std::exception &e) {
641 RETHROW_CODE(RegexException, 0, e);
645 bool String::regex_search(
const std::string &src,
const boost::regex ®ex, std::set<std::string> &matches,
bool global,
bool subexp, MatchFlag mFlags)
throw (RegexException) {
650 while (boost::regex_search(s, m, regex, mFlags)) {
656 matches.insert(m[0]);
661 s = m.suffix().str();
664 return (!matches.empty());
665 }
catch (boost::bad_expression &e) {
666 RETHROW_CODE(RegexException, 0, e);
667 }
catch (std::exception &e) {
668 RETHROW_CODE(RegexException, 0, e);
672 bool String::regex_search(
const std::string &src,
const std::string ®ex, std::set<std::string> &matches,
bool global,
bool subexp, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
674 const boost::regex rx(regex, reFlags);
676 return regex_search(src, rx, matches, global, subexp, mFlags);
677 }
catch (boost::bad_expression &e) {
678 RETHROW_CODE(RegexException, 0, e);
679 }
catch (std::exception &e) {
680 RETHROW_CODE(RegexException, 0, e);
684 bool String::regex_search_sub(
const std::string &src,
const boost::regex ®ex, std::vector<std::string> &matches,
bool global, MatchFlag mFlags)
throw (RegexException) {
689 while (boost::regex_search(s, m, regex, mFlags)) {
690 for (
size_t i = 1, size = m.size(); i < size; i++) {
691 matches.push_back(m[i]);
696 s = m.suffix().str();
699 return (!matches.empty());
700 }
catch (boost::bad_expression &e) {
701 RETHROW_CODE(RegexException, 0, e);
702 }
catch (std::exception &e) {
703 RETHROW_CODE(RegexException, 0, e);
707 bool String::regex_search_sub(
const std::string &src,
const std::string ®ex, std::vector<std::string> &matches,
bool global, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
709 const boost::regex rx(regex, reFlags);
711 return regex_search_sub(src, rx, matches, global, mFlags);
712 }
catch (boost::bad_expression &e) {
713 RETHROW_CODE(RegexException, 0, e);
714 }
catch (std::exception &e) {
715 RETHROW_CODE(RegexException, 0, e);
719 bool String::regex_search_sub(
const std::string &src,
const boost::regex ®ex, std::set<std::string> &matches,
bool global, MatchFlag mFlags)
throw (RegexException) {
724 while (boost::regex_search(s, m, regex, mFlags)) {
725 for (
size_t i = 1, size = m.size(); i < size; i++) {
726 matches.insert(m[i]);
731 s = m.suffix().str();
734 return (!matches.empty());
735 }
catch (boost::bad_expression &e) {
736 RETHROW_CODE(RegexException, 0, e);
737 }
catch (std::exception &e) {
738 RETHROW_CODE(RegexException, 0, e);
742 bool String::regex_search_sub(
const std::string &src,
const std::string ®ex, std::set<std::string> &matches,
bool global, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
744 const boost::regex rx(regex, reFlags);
746 return regex_search_sub(src, rx, matches, global, mFlags);
747 }
catch (boost::bad_expression &e) {
748 RETHROW_CODE(RegexException, 0, e);
749 }
catch (std::exception &e) {
750 RETHROW_CODE(RegexException, 0, e);
754 bool String::regex_isearch(
const std::string &src,
const boost::regex ®ex, MatchFlag mFlags)
throw (RegexException) {
756 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
758 return regex_search(src, rx, mFlags);
759 }
catch (boost::bad_expression &e) {
760 RETHROW_CODE(RegexException, 0, e);
761 }
catch (std::exception &e) {
762 RETHROW_CODE(RegexException, 0, e);
766 bool String::regex_isearch(
const std::string &src,
const std::string ®ex, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
768 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
770 return regex_search(src, rx, mFlags);
771 }
catch (boost::bad_expression &e) {
772 RETHROW_CODE(RegexException, 0, e);
773 }
catch (std::exception &e) {
774 RETHROW_CODE(RegexException, 0, e);
778 bool String::regex_isearch(
const std::string &src,
const boost::regex ®ex, boost::cmatch &matches, MatchFlag mFlags)
throw (RegexException) {
780 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
782 return regex_search(src, rx, matches, mFlags);
783 }
catch (boost::bad_expression &e) {
784 RETHROW_CODE(RegexException, 0, e);
785 }
catch (std::exception &e) {
786 RETHROW_CODE(RegexException, 0, e);
790 bool String::regex_isearch(
const std::string &src,
const std::string ®ex, boost::cmatch &matches, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
792 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
794 return regex_search(src, rx, matches, mFlags);
795 }
catch (boost::bad_expression &e) {
796 RETHROW_CODE(RegexException, 0, e);
797 }
catch (std::exception &e) {
798 RETHROW_CODE(RegexException, 0, e);
802 bool String::regex_isearch(
const std::string &src,
const boost::regex ®ex, std::string &match, MatchFlag mFlags)
throw (RegexException) {
804 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
806 return regex_search(src, rx, match, mFlags);
807 }
catch (boost::bad_expression &e) {
808 RETHROW_CODE(RegexException, 0, e);
809 }
catch (std::exception &e) {
810 RETHROW_CODE(RegexException, 0, e);
814 bool String::regex_isearch(
const std::string &src,
const std::string ®ex, std::string &match, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
816 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
818 return regex_search(src, rx, match, mFlags);
819 }
catch (boost::bad_expression &e) {
820 RETHROW_CODE(RegexException, 0, e);
821 }
catch (std::exception &e) {
822 RETHROW_CODE(RegexException, 0, e);
826 bool String::regex_isearch(
const std::string &src,
const boost::regex ®ex, std::vector<std::string> &matches,
bool global,
bool subexp, MatchFlag mFlags)
throw (RegexException) {
828 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
830 return regex_search(src, rx, matches, global, subexp, mFlags);
831 }
catch (boost::bad_expression &e) {
832 RETHROW_CODE(RegexException, 0, e);
833 }
catch (std::exception &e) {
834 RETHROW_CODE(RegexException, 0, e);
838 bool String::regex_isearch(
const std::string &src,
const std::string ®ex, std::vector<std::string> &matches,
bool global,
bool subexp, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
840 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
842 return regex_search(src, rx, matches, global, subexp, mFlags);
843 }
catch (boost::bad_expression &e) {
844 RETHROW_CODE(RegexException, 0, e);
845 }
catch (std::exception &e) {
846 RETHROW_CODE(RegexException, 0, e);
850 bool String::regex_isearch(
const std::string &src,
const boost::regex ®ex, std::set<std::string> &matches,
bool global,
bool subexp, MatchFlag mFlags)
throw (RegexException) {
852 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
854 return regex_search(src, rx, matches, global, subexp, mFlags);
855 }
catch (boost::bad_expression &e) {
856 RETHROW_CODE(RegexException, 0, e);
857 }
catch (std::exception &e) {
858 RETHROW_CODE(RegexException, 0, e);
862 bool String::regex_isearch(
const std::string &src,
const std::string ®ex, std::set<std::string> &matches,
bool global,
bool subexp, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
864 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
866 return regex_search(src, rx, matches, global, subexp, mFlags);
867 }
catch (boost::bad_expression &e) {
868 RETHROW_CODE(RegexException, 0, e);
869 }
catch (std::exception &e) {
870 RETHROW_CODE(RegexException, 0, e);
874 bool String::regex_isearch_sub(
const std::string &src,
const boost::regex ®ex, std::vector<std::string> &matches,
bool global, MatchFlag mFlags)
throw (RegexException) {
876 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
878 return regex_search_sub(src, rx, matches, global, mFlags);
879 }
catch (boost::bad_expression &e) {
880 RETHROW_CODE(RegexException, 0, e);
881 }
catch (std::exception &e) {
882 RETHROW_CODE(RegexException, 0, e);
886 bool String::regex_isearch_sub(
const std::string &src,
const std::string ®ex, std::vector<std::string> &matches,
bool global, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
888 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
890 return regex_search_sub(src, rx, matches, global, mFlags);
891 }
catch (boost::bad_expression &e) {
892 RETHROW_CODE(RegexException, 0, e);
893 }
catch (std::exception &e) {
894 RETHROW_CODE(RegexException, 0, e);
898 bool String::regex_isearch_sub(
const std::string &src,
const boost::regex ®ex, std::set<std::string> &matches,
bool global, MatchFlag mFlags)
throw (RegexException) {
900 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
902 return regex_search_sub(src, rx, matches, global, mFlags);
903 }
catch (boost::bad_expression &e) {
904 RETHROW_CODE(RegexException, 0, e);
905 }
catch (std::exception &e) {
906 RETHROW_CODE(RegexException, 0, e);
910 bool String::regex_isearch_sub(
const std::string &src,
const std::string ®ex, std::set<std::string> &matches,
bool global, REFlag reFlags, MatchFlag mFlags)
throw (RegexException) {
912 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
914 return regex_search_sub(src, rx, matches, global, mFlags);
915 }
catch (boost::bad_expression &e) {
916 RETHROW_CODE(RegexException, 0, e);
917 }
catch (std::exception &e) {
918 RETHROW_CODE(RegexException, 0, e);
922 std::vector<std::string> String::regex_split(
const std::string &str,
const boost::regex &delim,
bool empty,
size_t limit)
throw (RegexException) {
923 std::vector<std::string> vector;
928 boost::sregex_token_iterator it(str.begin(), str.end(), delim, -1);
929 boost::sregex_token_iterator end;
931 for (; it != end && chunks < limit; ++it, chunks++) {
932 if (empty || !it->str().empty()) {
933 vector.push_back(*it);
938 }
catch (boost::bad_expression &e) {
939 RETHROW_CODE(RegexException, 0, e);
940 }
catch (std::exception &e) {
941 RETHROW_CODE(RegexException, 0, e);
945 std::vector<std::string> String::regex_split(
const std::string &str,
const std::string ®ex,
bool empty,
size_t limit, REFlag reFlags)
throw (RegexException) {
946 std::vector<std::string> vector;
951 const boost::regex delim(regex, reFlags);
953 boost::sregex_token_iterator it(str.begin(), str.end(), delim, -1);
954 boost::sregex_token_iterator end;
956 for (; it != end && chunks < limit; ++it, chunks++) {
957 if (empty || !it->str().empty()) {
958 vector.push_back(*it);
963 }
catch (boost::bad_expression &e) {
964 RETHROW_CODE(RegexException, 0, e);
965 }
catch (std::exception &e) {
966 RETHROW_CODE(RegexException, 0, e);
970 std::vector<std::string> String::regex_isplit(
const std::string &str,
const boost::regex &delim,
bool empty,
size_t limit)
throw (RegexException) {
971 std::vector<std::string> vector;
976 const boost::regex idelim(delim.str(), delim.flags() | boost::regex_constants::icase);
978 boost::sregex_token_iterator it(str.begin(), str.end(), idelim, -1);
979 boost::sregex_token_iterator end;
981 for (; it != end && chunks < limit; ++it, chunks++) {
982 if (empty || !it->str().empty()) {
983 vector.push_back(*it);
988 }
catch (boost::bad_expression &e) {
989 RETHROW_CODE(RegexException, 0, e);
990 }
catch (std::exception &e) {
991 RETHROW_CODE(RegexException, 0, e);
995 std::vector<std::string> String::regex_isplit(
const std::string &str,
const std::string ®ex,
bool empty,
size_t limit, REFlag reFlags)
throw (RegexException) {
996 std::vector<std::string> vector;
1001 const boost::regex delim(regex, reFlags | boost::regex_constants::icase);
1003 boost::sregex_token_iterator it(str.begin(), str.end(), delim, -1);
1004 boost::sregex_token_iterator end;
1006 for (; it != end && chunks < limit; ++it, chunks++) {
1007 if (empty || !it->str().empty()) {
1008 vector.push_back(*it);
1013 }
catch (boost::bad_expression &e) {
1014 RETHROW_CODE(RegexException, 0, e);
1015 }
catch (std::exception &e) {
1016 RETHROW_CODE(RegexException, 0, e);
1020 std::vector<std::string> String::split(
const std::string &str,
const std::string &delim,
bool exact,
bool empty,
size_t limit)
throw () {
1021 std::vector<std::string> vector;
1022 std::size_t prev = 0, pos, chunks = 0;
1025 while (++chunks < limit && (pos = str.find(delim, prev)) != std::string::npos) {
1026 if (pos > prev || (empty && pos == prev)) {
1027 vector.push_back(str.substr(prev, pos - prev));
1030 prev = pos + delim.length();
1033 while (++chunks < limit && (pos = str.find_first_of(delim, prev)) != std::string::npos) {
1034 if (pos > prev || (empty && pos == prev)) {
1035 vector.push_back(str.substr(prev, pos - prev));
1043 if (chunks >= limit || prev < str.length() || (empty && prev == str.length())) {
1044 vector.push_back(str.substr(prev, std::string::npos));
1050 std::vector<std::string> String::split(
const std::string &str,
char delim,
bool empty,
size_t limit)
throw () {
1051 std::vector<std::string> vector;
1053 std::stringstream ss(str);
1054 std::size_t chunks = 0;
1056 while (++chunks < limit && std::getline(ss, s, delim)) {
1057 if (empty || !s.empty()) {
1058 vector.push_back(s);
1062 if (chunks >= limit) {
1063 if (std::getline(ss, s)) {
1064 vector.push_back(s);
1067 if (empty && !str.empty() && str[str.length() - 1] == delim) {
1068 vector.push_back(std::string());
1075 std::vector<std::string> String::split_csv(
const std::string &line,
const std::string &delim,
bool empty,
size_t limit)
throw () {
1076 std::vector<std::string> parts;
1079 std::size_t chunks = 0;
1081 for (
size_t i = 0, size = line.size(); i < size; i++) {
1082 if (!quote && line[i] ==
'"') {
1088 if (quote && line[i] ==
'"') {
1089 if (size > i && line[i + 1] ==
'"') {
1098 if (!quote && delim.find(line[i]) != std::string::npos) {
1099 if (empty || !part.empty()) {
1100 parts.push_back(part);
1102 if (chunks++ >= limit) {
1109 part.push_back(line[i]);
1113 if (empty || !part.empty()) {
1114 parts.push_back(part);
1120 std::string String::join(
const std::vector<std::string> &src,
const std::string &delim,
char quote,
const std::string &empty,
const std::string &lbr,
const std::string &rbr,
size_t threshold)
throw () {
1121 const size_t size = src.size();
1126 if (size > threshold && !lbr.empty()) {
1130 for (
auto it = src.cbegin(), end = src.cend(); it != end; ++it) {
1131 if (quote !=
'\0') {
1132 dst.append(1, quote).append(*it).append(1, quote).append(delim);
1134 dst.append(*it).append(delim);
1138 dst.erase(dst.size() - delim.size());
1140 if (size > threshold && !rbr.empty()) {
1150 std::string String::join(
const std::vector<std::string> &src,
char delim,
char quote,
const std::string &empty,
const std::string &lbr,
const std::string &rbr,
size_t threshold)
throw () {
1151 const size_t size = src.size();
1156 if (size > threshold && !lbr.empty()) {
1160 for (
auto it = src.cbegin(), end = src.cend(); it != end; ++it) {
1161 if (quote !=
'\0') {
1162 dst.append(1, quote).append(*it).append(1, quote).push_back(delim);
1164 dst.append(*it).push_back(delim);
1168 dst.erase(dst.size() - 1);
1170 if (size > threshold && !rbr.empty()) {
1180 std::string String::join(
const std::list<std::string> &src,
const std::string &delim,
char quote,
const std::string &empty,
const std::string &lbr,
const std::string &rbr,
size_t threshold)
throw () {
1181 const size_t size = src.size();
1186 if (size > threshold && !lbr.empty()) {
1190 for (
auto it = src.cbegin(), end = src.cend(); it != end; ++it) {
1191 if (quote !=
'\0') {
1192 dst.append(1, quote).append(*it).append(1, quote).append(delim);
1194 dst.append(*it).append(delim);
1198 dst.erase(dst.size() - delim.size());
1200 if (size > threshold && !rbr.empty()) {
1210 std::string String::join(
const std::list<std::string> &src,
char delim,
char quote,
const std::string &empty,
const std::string &lbr,
const std::string &rbr,
size_t threshold)
throw () {
1211 const size_t size = src.size();
1216 if (size > threshold && !lbr.empty()) {
1220 for (
auto it = src.cbegin(), end = src.cend(); it != end; ++it) {
1221 if (quote !=
'\0') {
1222 dst.append(1, quote).append(*it).append(1, quote).push_back(delim);
1224 dst.append(*it).push_back(delim);
1228 dst.erase(dst.size() - 1);
1230 if (size > threshold && !rbr.empty()) {
1240 std::string String::join(
const std::set<std::string> &src,
const std::string &delim,
char quote,
const std::string &empty,
const std::string &lbr,
const std::string &rbr,
size_t threshold)
throw () {
1241 const size_t size = src.size();
1246 if (size > threshold && !lbr.empty()) {
1250 for (
auto it = src.cbegin(), end = src.cend(); it != end; ++it) {
1251 if (quote !=
'\0') {
1252 dst.append(1, quote).append(*it).append(1, quote).append(delim);
1254 dst.append(*it).append(delim);
1258 dst.erase(dst.size() - delim.size());
1260 if (size > threshold && !rbr.empty()) {
1270 std::string String::join(
const std::set<std::string> &src,
char delim,
char quote,
const std::string &empty,
const std::string &lbr,
const std::string &rbr,
size_t threshold)
throw () {
1271 const size_t size = src.size();
1276 if (size > threshold && !lbr.empty()) {
1280 for (
auto it = src.cbegin(), end = src.cend(); it != end; ++it) {
1281 if (quote !=
'\0') {
1282 dst.append(1, quote).append(*it).append(1, quote).push_back(delim);
1284 dst.append(*it).push_back(delim);
1288 dst.erase(dst.size() - 1);
1290 if (size > threshold && !rbr.empty()) {
1300 std::string String::join(
const std::vector<variant::Variant> &src,
const std::string &delim,
char quote,
const std::string &empty,
const std::string &lbr,
const std::string &rbr,
size_t threshold)
throw () {
1301 const size_t size = src.size();
1304 std::string dst, str;
1306 if (size > threshold && !lbr.empty()) {
1310 for (
auto it = src.cbegin(), end = src.cend(); it != end; ++it) {
1317 if (quote !=
'\0') {
1318 dst.append(1, quote).append(str).append(1, quote).append(delim);
1320 dst.append(str).append(delim);
1324 dst.erase(dst.size() - delim.size());
1326 if (size > threshold && !rbr.empty()) {
1336 std::string String::join(
const std::vector<variant::Variant> &src,
char delim,
char quote,
const std::string &empty,
const std::string &lbr,
const std::string &rbr,
size_t threshold)
throw () {
1337 const size_t size = src.size();
1340 std::string dst, str;
1342 if (size > threshold && !lbr.empty()) {
1346 for (
auto it = src.cbegin(), end = src.cend(); it != end; ++it) {
1353 if (quote !=
'\0') {
1354 dst.append(1, quote).append(str).append(1, quote).push_back(delim);
1356 dst.append(str).push_back(delim);
1360 dst.erase(dst.size() - 1);
1362 if (size > threshold && !rbr.empty()) {
1372 std::string String::trim(
const std::string &str,
const std::string &space)
throw () {
1373 return ltrim(rtrim(str, space), space);
1376 std::string String::trim(
const std::string &str,
char space)
throw () {
1377 return ltrim(rtrim(str, space), space);
1380 std::string& String::trim(std::string &str,
const std::string &space)
throw () {
1381 return ltrim(rtrim(str, space), space);
1384 std::string& String::trim(std::string &str,
char space)
throw () {
1385 return ltrim(rtrim(str, space), space);
1388 std::string String::rtrim(
const std::string &str,
const std::string &space)
throw () {
1391 if ((i = str.find_last_not_of(space)) != std::string::npos) {
1392 return str.substr(0, i + 1);
1394 return std::string();
1400 std::string String::rtrim(
const std::string &str,
char space)
throw () {
1403 if ((i = str.find_last_not_of(space)) != std::string::npos) {
1404 return str.substr(0, i + 1);
1406 return std::string();
1412 std::string& String::rtrim(std::string &str,
const std::string &space)
throw () {
1415 if ((i = str.find_last_not_of(space)) != std::string::npos) {
1416 str.substr(0, i + 1).swap(str);
1424 std::string& String::rtrim(std::string &str,
char space)
throw () {
1427 if ((i = str.find_last_not_of(space)) != std::string::npos) {
1428 str.substr(0, i + 1).swap(str);
1436 std::string String::ltrim(
const std::string &str,
const std::string &space)
throw () {
1439 if ((i = str.find_first_not_of(space)) != std::string::npos) {
1440 return str.substr(i);
1442 return std::string();
1448 std::string String::ltrim(
const std::string &str,
char space)
throw () {
1451 if ((i = str.find_first_not_of(space)) != std::string::npos) {
1452 return str.substr(i);
1454 return std::string();
1460 std::string& String::ltrim(std::string &str,
const std::string &space)
throw () {
1463 if ((i = str.find_first_not_of(space)) != std::string::npos) {
1464 str.substr(i).swap(str);
1472 std::string& String::ltrim(std::string &str,
char space)
throw () {
1475 if ((i = str.find_first_not_of(space)) != std::string::npos) {
1476 str.substr(i).swap(str);
1484 std::string String::lower(
const std::string &src)
throw () {
1485 std::string dst(src);
1487 std::transform(dst.begin(), dst.end(), dst.begin(), ::tolower);
1492 std::string& String::lower(std::string &src)
throw () {
1493 std::transform(src.begin(), src.end(), src.begin(), ::tolower);
1498 std::string String::upper(
const std::string &src)
throw () {
1499 std::string dst(src);
1501 std::transform(dst.begin(), dst.end(), dst.begin(), ::toupper);
1506 std::string& String::upper(std::string &src)
throw () {
1507 std::transform(src.begin(), src.end(), src.begin(), ::toupper);
1512 std::string String::ucfirst(
const std::string &src)
throw () {
1513 std::string dst(lower(src));
1515 for (
auto it = dst.begin(), end = dst.end(); it != end; ++it) {
1516 if (!isspace(*it)) {
1517 *it = std::toupper(*it);
1526 std::string& String::ucfirst(std::string &src)
throw () {
1529 for (
auto it = src.begin(), end = src.end(); it != end; ++it) {
1530 if (!isspace(*it)) {
1531 *it = std::toupper(*it);
1540 std::string String::ucword(
const std::string &src)
throw () {
1541 std::string dst(src);
1544 for (
auto it = dst.begin(), end = dst.end(); it != end; ++it) {
1546 *it = std::toupper(*it);
1548 *it = std::tolower(*it);
1551 space = isspace(*it);
1557 std::string& String::ucword(std::string &src)
throw () {
1560 for (
auto it = src.begin(), end = src.end(); it != end; ++it) {
1562 *it = std::toupper(*it);
1564 *it = std::tolower(*it);
1567 space = isspace(*it);
1573 std::string String::itoa(int64_t src,
int base)
throw () {
1574 int i, c = 0, negative = 0;
1577 if (UNLIKELY((
unsigned int) base > 16)) {
1596 if (negative) dst[0] =
'-';
1600 dst[i--] = RadixLookup()[src % base];
1607 std::string String::itoa(uint64_t src,
int base)
throw () {
1611 if (UNLIKELY((
unsigned int) base > 16)) {
1626 dst[i--] = RadixLookup()[src % base];
1633 char* String::itoa(int64_t src,
char *dst,
size_t &len,
int base)
throw () {
1634 int i, c = 0, negative = 0;
1636 if (UNLIKELY(base < 2 || base > 16)) {
1653 if (UNLIKELY(c >= (
int) len)) {
1659 if (negative) dst[0] =
'-';
1663 dst[i--] = RadixLookup()[src % base];
1672 char* String::itoa(uint64_t src,
char *dst,
size_t &len,
int base)
throw () {
1675 if (UNLIKELY((
unsigned int) base > 16)) {
1686 if (UNLIKELY(c >= (
int) len)) {
1694 dst[i--] = RadixLookup()[src % base];
1703 std::string String::bandwidth(int64_t bps,
int decimals)
throw () {
1704 if (decimals == 0) {
1705 if (bps % 1000000000000000000LL == 0 && bps >= 1000000000000000000LL) {
1706 return String::sprintf(
"%" PRId64
"Ebs", bps / 1000000000000000000LL);
1707 }
else if (bps % 1000000000000000LL == 0 && bps >= 1000000000000000LL) {
1708 return String::sprintf(
"%" PRId64
"Pbs", bps / 1000000000000000LL);
1709 }
else if (bps % 1000000000000LL == 0 && bps >= 1000000000000LL) {
1710 return String::sprintf(
"%" PRId64
"Tbs", bps / 1000000000000LL);
1711 }
else if (bps % 1000000000LL == 0 && bps >= 1000000000LL) {
1712 return String::sprintf(
"%" PRId64
"Gbs", bps / 1000000000LL);
1713 }
else if (bps % 1000000LL == 0 && bps >= 1000000LL) {
1714 return String::sprintf(
"%" PRId64
"Mbs", bps / 1000000LL);
1715 }
else if (bps % 1000LL == 0 && bps >= 1000LL) {
1716 return String::sprintf(
"%" PRId64
"Kbs", bps / 1000LL);
1719 double adj = pow(10, decimals);
1722 if (bps >= 1000000000000000000LL) {
1723 buf = String::sprintf(
"%gEbs", roundf((
double) bps / 1000000000000000000.0f * adj) / adj);
1724 }
else if (bps >= 1000000000000000LL) {
1725 buf = String::sprintf(
"%gPbs", roundf((
double) bps / 1000000000000000.0f * adj) / adj);
1726 }
else if (bps >= 1000000000000LL) {
1727 buf = String::sprintf(
"%gTbs", roundf((
double) bps / 1000000000000.0f * adj) / adj);
1728 }
else if (bps >= 1000000000LL) {
1729 buf = String::sprintf(
"%gGbs", roundf((
double) bps / 1000000000.0f * adj) / adj);
1730 }
else if (bps >= 1000000LL) {
1731 buf = String::sprintf(
"%gMbs", roundf((
double) bps / 1000000.0f * adj) / adj);
1732 }
else if (bps >= 1000LL) {
1733 buf = String::sprintf(
"%gKbs", roundf((
double) bps / 1000.0f * adj) / adj);
1736 if (LIKELY(!buf.empty())) {
1737 size_t pos = buf.find(
"e+", 3);
1739 if (pos != buf.npos) {
1740 size_t off = buf.find_first_of(
"EPTGMK", pos + 2);
1742 buf.replace(pos, off - pos,
"", 0);
1749 return String::sprintf(
"%" PRId64
"bps", bps);
1752 std::string String::bandwidth(uint64_t bps,
int decimals)
throw () {
1753 if (decimals == 0) {
1754 if (bps % 1000000000000000000LLU == 0 && bps >= 1000000000000000000LLU) {
1755 return String::sprintf(
"%" PRIu64
"Ebs", bps / 1000000000000000000LLU);
1756 }
else if (bps % 1000000000000000LLU == 0 && bps >= 1000000000000000LLU) {
1757 return String::sprintf(
"%" PRIu64
"Pbs", bps / 1000000000000000LLU);
1758 }
else if (bps % 1000000000000LLU == 0 && bps >= 1000000000000LLU) {
1759 return String::sprintf(
"%" PRIu64
"Tbs", bps / 1000000000000LLU);
1760 }
else if (bps % 1000000000LLU == 0 && bps >= 1000000000LLU) {
1761 return String::sprintf(
"%" PRIu64
"Gbs", bps / 1000000000LLU);
1762 }
else if (bps % 1000000LLU == 0 && bps >= 1000000LLU) {
1763 return String::sprintf(
"%" PRIu64
"Mbs", bps / 1000000LLU);
1764 }
else if (bps % 1000LLU == 0 && bps >= 1000LLU) {
1765 return String::sprintf(
"%" PRIu64
"Kbs", bps / 1000LLU);
1768 double adj = pow(10, decimals);
1771 if (bps >= 1000000000000000000LLU) {
1772 buf = String::sprintf(
"%gEbs", roundf((
double) bps / 1000000000000000000.0f * adj) / adj);
1773 }
else if (bps >= 1000000000000000LLU) {
1774 buf = String::sprintf(
"%gPbs", roundf((
double) bps / 1000000000000000.0f * adj) / adj);
1775 }
else if (bps >= 1000000000000LLU) {
1776 buf = String::sprintf(
"%gTbs", roundf((
double) bps / 1000000000000.0f * adj) / adj);
1777 }
else if (bps >= 1000000000LLU) {
1778 buf = String::sprintf(
"%gGbs", roundf((
double) bps / 1000000000.0f * adj) / adj);
1779 }
else if (bps >= 1000000LLU) {
1780 buf = String::sprintf(
"%gMbs", roundf((
double) bps / 1000000.0f * adj) / adj);
1781 }
else if (bps >= 1000LLU) {
1782 buf = String::sprintf(
"%gKbs", roundf((
double) bps / 1000.0f * adj) / adj);
1785 if (LIKELY(!buf.empty())) {
1786 size_t pos = buf.find(
"e+", 3);
1788 if (pos != buf.npos) {
1789 size_t off = buf.find_first_of(
"EPTGMK", pos + 2);
1791 buf.replace(pos, off - pos,
"", 0);
1798 return String::sprintf(
"%" PRIu64
"bps", bps);
1801 std::string String::rate(int64_t bps,
int decimals,
const std::string &suffix)
throw () {
1802 if (decimals == 0) {
1803 if (bps % 1000000000000000000LL == 0 && bps >= 1000000000000000000LL) {
1804 return String::sprintf(
"%" PRId64
"E%s", bps / 1000000000000000000LL, suffix.c_str());
1805 }
else if (bps % 1000000000000000LL == 0 && bps >= 1000000000000000LL) {
1806 return String::sprintf(
"%" PRId64
"P%s", bps / 1000000000000000LL, suffix.c_str());
1807 }
else if (bps % 1000000000000LL == 0 && bps >= 1000000000000LL) {
1808 return String::sprintf(
"%" PRId64
"T%s", bps / 1000000000000LL, suffix.c_str());
1809 }
else if (bps % 1000000000LL == 0 && bps >= 1000000000LL) {
1810 return String::sprintf(
"%" PRId64
"G%s", bps / 1000000000LL, suffix.c_str());
1811 }
else if (bps % 1000000LL == 0 && bps >= 1000000LL) {
1812 return String::sprintf(
"%" PRId64
"M%s", bps / 1000000LL, suffix.c_str());
1813 }
else if (bps % 1000LL == 0 && bps >= 1000LL) {
1814 return String::sprintf(
"%" PRId64
"K%s", bps / 1000LL, suffix.c_str());
1817 double adj = pow(10, decimals);
1820 if (bps >= 1000000000000000000LL) {
1821 buf = String::sprintf(
"%gE%s", roundf((
double) bps / 1000000000000000000.0f * adj) / adj, suffix.c_str());
1822 }
else if (bps >= 1000000000000000LL) {
1823 buf = String::sprintf(
"%gP%s", roundf((
double) bps / 1000000000000000.0f * adj) / adj, suffix.c_str());
1824 }
else if (bps >= 1000000000000LL) {
1825 buf = String::sprintf(
"%gT%s", roundf((
double) bps / 1000000000000.0f * adj) / adj, suffix.c_str());
1826 }
else if (bps >= 1000000000LL) {
1827 buf = String::sprintf(
"%gG%s", roundf((
double) bps / 1000000000.0f * adj) / adj, suffix.c_str());
1828 }
else if (bps >= 1000000LL) {
1829 buf = String::sprintf(
"%gM%s", roundf((
double) bps / 1000000.0f * adj) / adj, suffix.c_str());
1830 }
else if (bps >= 1000LL) {
1831 buf = String::sprintf(
"%gK%s", roundf((
double) bps / 1000.0f * adj) / adj, suffix.c_str());
1834 if (LIKELY(!buf.empty())) {
1835 size_t pos = buf.find(
"e+", 3);
1837 if (pos != buf.npos) {
1838 size_t off = buf.find_first_of(
"EPTGMK", pos + 2);
1840 buf.replace(pos, off - pos,
"", 0);
1847 return String::sprintf(
"%" PRId64
"b%s", bps, suffix.c_str());
1850 std::string String::rate(uint64_t bps,
int decimals,
const std::string &suffix)
throw () {
1851 if (decimals == 0) {
1852 if (bps % 1000000000000000000LLU == 0 && bps >= 1000000000000000000LLU) {
1853 return String::sprintf(
"%" PRIu64
"E%s", bps / 1000000000000000000LLU, suffix.c_str());
1854 }
else if (bps % 1000000000000000LLU == 0 && bps >= 1000000000000000LLU) {
1855 return String::sprintf(
"%" PRIu64
"P%s", bps / 1000000000000000LLU, suffix.c_str());
1856 }
else if (bps % 1000000000000LLU == 0 && bps >= 1000000000000LLU) {
1857 return String::sprintf(
"%" PRIu64
"T%s", bps / 1000000000000LLU, suffix.c_str());
1858 }
else if (bps % 1000000000LLU == 0 && bps >= 1000000000LLU) {
1859 return String::sprintf(
"%" PRIu64
"G%s", bps / 1000000000LLU, suffix.c_str());
1860 }
else if (bps % 1000000LLU == 0 && bps >= 1000000LLU) {
1861 return String::sprintf(
"%" PRIu64
"M%s", bps / 1000000LLU, suffix.c_str());
1862 }
else if (bps % 1000LLU == 0 && bps >= 1000LLU) {
1863 return String::sprintf(
"%" PRIu64
"K%s", bps / 1000LLU, suffix.c_str());
1866 double adj = pow(10, decimals);
1869 if (bps >= 1000000000000000000LLU) {
1870 buf = String::sprintf(
"%gE%s", roundf((
double) bps / 1000000000000000000.0f * adj) / adj, suffix.c_str());
1871 }
else if (bps >= 1000000000000000LLU) {
1872 buf = String::sprintf(
"%gP%s", roundf((
double) bps / 1000000000000000.0f * adj) / adj, suffix.c_str());
1873 }
else if (bps >= 1000000000000LLU) {
1874 buf = String::sprintf(
"%gT%s", roundf((
double) bps / 1000000000000.0f * adj) / adj, suffix.c_str());
1875 }
else if (bps >= 1000000000LLU) {
1876 buf = String::sprintf(
"%gG%s", roundf((
double) bps / 1000000000.0f * adj) / adj, suffix.c_str());
1877 }
else if (bps >= 1000000LLU) {
1878 buf = String::sprintf(
"%gM%s", roundf((
double) bps / 1000000.0f * adj) / adj, suffix.c_str());
1879 }
else if (bps >= 1000LLU) {
1880 buf = String::sprintf(
"%gK%s", roundf((
double) bps / 1000.0f * adj) / adj, suffix.c_str());
1883 if (LIKELY(!buf.empty())) {
1884 size_t pos = buf.find(
"e+", 3);
1886 if (pos != buf.npos) {
1887 size_t off = buf.find_first_of(
"EPTGMK", pos + 2);
1889 buf.replace(pos, off - pos,
"", 0);
1896 return String::sprintf(
"%" PRIu64
"b%s", bps, suffix.c_str());
1899 std::string String::dataSize(int64_t bytes,
int decimals)
throw () {
1900 if (decimals == 0) {
1901 if (bytes % (int64_t) pow(2, 60) == 0 && bytes >= (int64_t) pow(2, 60)) {
1902 return String::sprintf(
"%" PRId64
"EB", bytes / (int64_t) pow(2, 60));
1903 }
else if (bytes % (int64_t) pow(2, 50) == 0 && bytes >= (int64_t) pow(2, 50)) {
1904 return String::sprintf(
"%" PRId64
"PB", bytes / (int64_t) pow(2, 50));
1905 }
else if (bytes % (int64_t) pow(2, 40) == 0 && bytes >= (int64_t) pow(2, 40)) {
1906 return String::sprintf(
"%" PRId64
"TB", bytes / (int64_t) pow(2, 40));
1907 }
else if (bytes % (int64_t) pow(2, 30) == 0 && bytes >= (int64_t) pow(2, 30)) {
1908 return String::sprintf(
"%" PRId64
"GB", bytes / (int64_t) pow(2, 30));
1909 }
else if (bytes % (int64_t) pow(2, 20) == 0 && bytes >= (int64_t) pow(2, 20)) {
1910 return String::sprintf(
"%" PRId64
"MB", bytes / (int64_t) pow(2, 20));
1911 }
else if (bytes % (int64_t) pow(2, 10) == 0 && bytes >= (int64_t) pow(2, 10)) {
1912 return String::sprintf(
"%" PRId64
"KB", bytes / (int64_t) pow(2, 10));
1915 double adj = pow(10, decimals);
1918 if (bytes >= (int64_t) pow(2, 60)) {
1919 buf = String::sprintf(
"%gEB", roundf((
double) bytes / pow(2, 60) * adj) / adj);
1920 }
else if (bytes >= (int64_t) pow(2, 50)) {
1921 buf = String::sprintf(
"%gPB", roundf((
double) bytes / pow(2, 50) * adj) / adj);
1922 }
else if (bytes >= (int64_t) pow(2, 40)) {
1923 buf = String::sprintf(
"%gTB", roundf((
double) bytes / pow(2, 40) * adj) / adj);
1924 }
else if (bytes >= (int64_t) pow(2, 30)) {
1925 buf = String::sprintf(
"%gGB", roundf((
double) bytes / pow(2, 30) * adj) / adj);
1926 }
else if (bytes >= (int64_t) pow(2, 20)) {
1927 buf = String::sprintf(
"%gMB", roundf((
double) bytes / pow(2, 20) * adj) / adj);
1928 }
else if (bytes >= (int64_t) pow(2, 10)) {
1929 buf = String::sprintf(
"%gKB", roundf((
double) bytes / pow(2, 10) * adj) / adj);
1932 if (LIKELY(!buf.empty())) {
1933 size_t pos = buf.find(
"e+", 3);
1935 if (pos != buf.npos) {
1936 size_t off = buf.find_first_of(
"EPTGMK", pos + 2);
1938 buf.replace(pos, off - pos,
"", 0);
1945 return String::sprintf(
"%lld", bytes);
1948 std::string String::dataSize(uint64_t bytes,
int decimals)
throw () {
1949 if (decimals == 0) {
1950 if (bytes % (uint64_t) pow(2, 60) == 0 && bytes >= (uint64_t) pow(2, 60)) {
1951 return String::sprintf(
"%" PRIu64
"EB", bytes / (uint64_t) pow(2, 60));
1952 }
else if (bytes % (uint64_t) pow(2, 50) == 0 && bytes >= (uint64_t) pow(2, 50)) {
1953 return String::sprintf(
"%" PRIu64
"PB", bytes / (uint64_t) pow(2, 50));
1954 }
else if (bytes % (uint64_t) pow(2, 40) == 0 && bytes >= (uint64_t) pow(2, 40)) {
1955 return String::sprintf(
"%" PRIu64
"TB", bytes / (uint64_t) pow(2, 40));
1956 }
else if (bytes % (uint64_t) pow(2, 30) == 0 && bytes >= (uint64_t) pow(2, 30)) {
1957 return String::sprintf(
"%" PRIu64
"GB", bytes / (uint64_t) pow(2, 30));
1958 }
else if (bytes % (uint64_t) pow(2, 20) == 0 && bytes >= (uint64_t) pow(2, 20)) {
1959 return String::sprintf(
"%" PRIu64
"MB", bytes / (uint64_t) pow(2, 20));
1960 }
else if (bytes % (uint64_t) pow(2, 10) == 0 && bytes >= (uint64_t) pow(2, 10)) {
1961 return String::sprintf(
"%" PRIu64
"KB", bytes / (uint64_t) pow(2, 10));
1964 double adj = pow(10, decimals);
1967 if (bytes >= (uint64_t) pow(2, 60)) {
1968 buf = String::sprintf(
"%gEB", roundf((
double) bytes / pow(2, 60) * adj) / adj);
1969 }
else if (bytes >= (uint64_t) pow(2, 50)) {
1970 buf = String::sprintf(
"%gPB", roundf((
double) bytes / pow(2, 50) * adj) / adj);
1971 }
else if (bytes >= (uint64_t) pow(2, 40)) {
1972 buf = String::sprintf(
"%gTB", roundf((
double) bytes / pow(2, 40) * adj) / adj);
1973 }
else if (bytes >= (uint64_t) pow(2, 30)) {
1974 buf = String::sprintf(
"%gGB", roundf((
double) bytes / pow(2, 30) * adj) / adj);
1975 }
else if (bytes >= (uint64_t) pow(2, 20)) {
1976 buf = String::sprintf(
"%gMB", roundf((
double) bytes / pow(2, 20) * adj) / adj);
1977 }
else if (bytes >= (uint64_t) pow(2, 10)) {
1978 buf = String::sprintf(
"%gKB", roundf((
double) bytes / pow(2, 10) * adj) / adj);
1981 if (LIKELY(!buf.empty())) {
1982 size_t pos = buf.find(
"e+", 3);
1984 if (pos != buf.npos) {
1985 size_t off = buf.find_first_of(
"EPTGMK", pos + 2);
1987 buf.replace(pos, off - pos,
"", 0);
1994 return String::sprintf(
"%llu", bytes);
1997 uint64_t String::dataSize(
const std::string &dataSize)
throw () {
1998 uint64_t number = std::stoull(dataSize);
2000 if (dataSize.size() > 1) {
2001 size_t offset = (isalpha(dataSize[dataSize.size() - 2]) == 0) ? 1 : 2;
2003 switch (::tolower(dataSize[dataSize.size() - offset])) {
2005 number *= pow(2, 60);
2009 number *= pow(2, 50);
2013 number *= pow(2, 40);
2017 number *= pow(2, 30);
2021 number *= pow(2, 20);
2025 number *= pow(2, 10);
2033 std::string String::dataRate(int64_t bps,
int decimals)
throw () {
2034 if (decimals == 0) {
2035 if (bps % 1000000000000000000LL == 0 && bps >= 1000000000000000000LL) {
2036 return String::sprintf(
"%" PRId64
"e", bps / 1000000000000000000LL);
2037 }
else if (bps % 1000000000000000LL == 0 && bps >= 1000000000000000LL) {
2038 return String::sprintf(
"%" PRId64
"p", bps / 1000000000000000LL);
2039 }
else if (bps % 1000000000000LL == 0 && bps >= 1000000000000LL) {
2040 return String::sprintf(
"%" PRId64
"t", bps / 1000000000000LL);
2041 }
else if (bps % 1000000000LL == 0 && bps >= 1000000000LL) {
2042 return String::sprintf(
"%" PRId64
"g", bps / 1000000000LL);
2043 }
else if (bps % 1000000LL == 0 && bps >= 1000000LL) {
2044 return String::sprintf(
"%" PRId64
"m", bps / 1000000LL);
2045 }
else if (bps % 1000LL == 0 && bps >= 1000LL) {
2046 return String::sprintf(
"%" PRId64
"k", bps / 1000LL);
2049 double adj = pow(10, decimals);
2052 if (bps >= 1000000000000000000LL) {
2053 buf = String::sprintf(
"%0ge", roundf((
double) bps / 1000000000000000000.0f * adj) / adj);
2054 }
else if (bps >= 1000000000000000LL) {
2055 buf = String::sprintf(
"%0gp", roundf((
double) bps / 1000000000000000.0f * adj) / adj);
2056 }
else if (bps >= 1000000000000LL) {
2057 buf = String::sprintf(
"%0gt", roundf((
double) bps / 1000000000000.0f * adj) / adj);
2058 }
else if (bps >= 1000000000LL) {
2059 buf = String::sprintf(
"%0gg", roundf((
double) bps / 1000000000.0f * adj) / adj);
2060 }
else if (bps >= 1000000LL) {
2061 buf = String::sprintf(
"%0gm", roundf((
double) bps / 1000000.0f * adj) / adj);
2062 }
else if (bps >= 1000LL) {
2063 buf = String::sprintf(
"%0gk", roundf((
double) bps / 1000.0f * adj) / adj);
2066 if (LIKELY(!buf.empty())) {
2067 size_t pos = buf.find(
"e+", 3);
2069 if (pos != buf.npos) {
2070 size_t off = buf.find_first_of(
"eptgmk", pos + 2);
2072 buf.replace(pos, off - pos,
"", 0);
2079 return String::sprintf(
"%" PRId64, bps);
2082 std::string String::dataRate(uint64_t bps,
int decimals)
throw () {
2083 if (decimals == 0) {
2084 if (bps % 1000000000000000000LLU == 0 && bps >= 1000000000000000000LLU) {
2085 return String::sprintf(
"%" PRIu64
"e", bps / 1000000000000000000LLU);
2086 }
else if (bps % 1000000000000000LLU == 0 && bps >= 1000000000000000LLU) {
2087 return String::sprintf(
"%" PRIu64
"p", bps / 1000000000000000LLU);
2088 }
else if (bps % 1000000000000LLU == 0 && bps >= 1000000000000LLU) {
2089 return String::sprintf(
"%" PRIu64
"t", bps / 1000000000000LLU);
2090 }
else if (bps % 1000000000LLU == 0 && bps >= 1000000000LLU) {
2091 return String::sprintf(
"%" PRIu64
"g", bps / 1000000000LLU);
2092 }
else if (bps % 1000000LLU == 0 && bps >= 1000000LLU) {
2093 return String::sprintf(
"%" PRIu64
"m", bps / 1000000LLU);
2094 }
else if (bps % 1000LLU == 0 && bps >= 1000LLU) {
2095 return String::sprintf(
"%" PRIu64
"k", bps / 1000LLU);
2098 double adj = pow(10, decimals);
2101 if (bps >= 1000000000000000000LLU) {
2102 buf = String::sprintf(
"%0ge", roundf((
double) bps / 1000000000000000000.0f * adj) / adj);
2103 }
else if (bps >= 1000000000000000LLU) {
2104 buf = String::sprintf(
"%0gp", roundf((
double) bps / 1000000000000000.0f * adj) / adj);
2105 }
else if (bps >= 1000000000000LLU) {
2106 buf = String::sprintf(
"%0gt", roundf((
double) bps / 1000000000000.0f * adj) / adj);
2107 }
else if (bps >= 1000000000LLU) {
2108 buf = String::sprintf(
"%0gg", roundf((
double) bps / 1000000000.0f * adj) / adj);
2109 }
else if (bps >= 1000000LLU) {
2110 buf = String::sprintf(
"%0gm", roundf((
double) bps / 1000000.0f * adj) / adj);
2111 }
else if (bps >= 1000LLU) {
2112 buf = String::sprintf(
"%0gk", roundf((
double) bps / 1000.0f * adj) / adj);
2115 if (LIKELY(!buf.empty())) {
2116 size_t pos = buf.find(
"e+", 3);
2118 if (pos != buf.npos) {
2119 size_t off = buf.find_first_of(
"eptgmk", pos + 2);
2121 buf.replace(pos, off - pos,
"", 0);
2128 return String::sprintf(
"%" PRIu64, bps);
2131 uint64_t String::dataRate(
const std::string &dataRate)
throw () {
2132 uint64_t number = std::stoull(dataRate);
2134 if (dataRate.size() > 1) {
2135 size_t offset = (isalpha(dataRate[dataRate.size() - 2]) == 0) ? 1 : 2;
2137 switch (::tolower(dataRate[dataRate.size() - offset])) {
2139 number *= pow(10, 18);
2143 number *= pow(10, 15);
2147 number *= pow(10, 12);
2151 number *= pow(10, 9);
2155 number *= pow(10, 6);
2159 number *= pow(10, 3);
2167 std::string String::duration(int64_t usec,
int decimals)
throw () {
2168 if (decimals == 0) {
2169 if (usec % (7LL * 86400LL * 1000000LL) == 0 && usec >= (7LL * 86400LL * (int64_t) pow(10, 6))) {
2170 return String::sprintf(
"%" PRId64
"w", usec / (7LL * 86400LL * 1000000LL));
2171 }
else if (usec % (86400LL * 1000000LL) == 0 && usec >= (86400LL * (int64_t) pow(10, 6))) {
2172 return String::sprintf(
"%" PRId64
"d", usec / (86400LL * 1000000LL));
2173 }
else if (usec % (3600LL * 1000000LL) == 0 && usec >= (3600LL * (int64_t) pow(10, 6))) {
2174 return String::sprintf(
"%" PRId64
"h", usec / (3600LL * 1000000LL));
2175 }
else if (usec % (60LL * 1000000LL) == 0 && usec >= (60LL * (int64_t) pow(10, 6))) {
2176 return String::sprintf(
"%" PRId64
"min", usec / (60LL * 1000000LL));
2177 }
else if (usec % 1000000LL == 0 && usec >= (int64_t) pow(10, 6)) {
2178 return String::sprintf(
"%" PRId64
"s", usec / 1000000LL);
2179 }
else if (usec % 1000LL == 0 && usec >= 1000LL) {
2180 return String::sprintf(
"%" PRId64
"ms", usec / 1000LL);
2183 double adj = pow(10, decimals);
2186 if (usec >= (7LL * 86400LL * 1000000LL)) {
2187 double sec = (double) usec / 604800000000.0f;
2189 buf = String::sprintf(
"%gw", roundf(sec * adj) / adj);
2190 }
else if (usec >= (86400LL * 1000000LL)) {
2191 double sec = (double) usec / 86400000000.0f;
2193 buf = String::sprintf(
"%gd", roundf(sec * adj) / adj);
2194 }
else if (usec >= (3600LL * 1000000LL)) {
2195 double sec = (double) usec / 3600000000.0f;
2197 buf = String::sprintf(
"%gh", roundf(sec * adj) / adj);
2198 }
else if (usec >= (60LL * 1000000LL)) {
2199 double sec = (double) usec / 60000000.0f;
2201 buf = String::sprintf(
"%gmin", roundf(sec * adj) / adj);
2202 }
else if (usec >= 1000000LL) {
2203 double sec = (double) usec / 1000000.0f;
2205 buf = String::sprintf(
"%gs", roundf(sec * adj) / adj);
2206 }
else if (usec >= 1000LL) {
2207 double sec = (double) usec / 1000.0f;
2209 buf = String::sprintf(
"%gms", roundf(sec * adj) / adj);
2213 if (LIKELY(!buf.empty())) {
2214 size_t pos = buf.find(
"e+", 3);
2216 if (pos != buf.npos) {
2217 printf(
"FOFOFOFOFO\n");
2218 size_t off = buf.find_first_of(
"wdhms", pos + 2);
2220 buf.replace(pos, off - pos,
"", 0);
2226 if (LIKELY(!buf.empty())) {
2232 return String::sprintf(
"%" PRId64
"us", usec);
2235 std::string String::duration(uint64_t usec,
int decimals)
throw () {
2236 if (decimals == 0) {
2237 if (usec % (7LLU * 86400LLU * 1000000LLU) == 0 && usec >= (7LLU * 86400LLU * 1000000LLU)) {
2238 return String::sprintf(
"%" PRIu64
"w", usec / (7LLU * 86400LLU * 1000000LLU));
2239 }
else if (usec % (86400LLU * 1000000LLU) == 0 && usec >= (86400LLU * 1000000LLU)) {
2240 return String::sprintf(
"%" PRIu64
"d", usec / (86400LLU * 1000000LLU));
2241 }
else if (usec % (3600LLU * 1000000LLU) == 0 && usec >= (3600LLU * 1000000LLU)) {
2242 return String::sprintf(
"%" PRIu64
"h", usec / (3600LLU * 1000000LLU));
2243 }
else if (usec % (60LLU * 1000000LLU) == 0 && usec >= (60LLU * 1000000LLU)) {
2244 return String::sprintf(
"%" PRIu64
"min", usec / (60LLU * 1000000LLU));
2245 }
else if (usec % 1000000LLU == 0 && usec >= 1000000LLU) {
2246 return String::sprintf(
"%" PRIu64
"s", usec / 1000000LLU);
2247 }
else if (usec % 1000LLU == 0 && usec >= 1000LLU) {
2248 return String::sprintf(
"%" PRIu64
"ms", usec / 1000LLU);
2251 double adj = pow(10, decimals);
2254 if (usec >= (7LLU * 86400LLU * 1000000LLU)) {
2255 double sec = (double) usec / 604800000000.0f;
2257 buf = String::sprintf(
"%gw", roundf(sec * adj) / adj);
2258 }
else if (usec >= (86400LLU * 1000000LLU)) {
2259 double sec = (double) usec / 86400000000.0f;
2261 buf = String::sprintf(
"%gd", roundf(sec * adj) / adj);
2262 }
else if (usec >= (3600LLU * 1000000LLU)) {
2263 double sec = (double) usec / 3600000000.0f;
2265 buf = String::sprintf(
"%gh", roundf(sec * adj) / adj);
2266 }
else if (usec >= (60LLU * 1000000LLU)) {
2267 double sec = (double) usec / 60000000.0f;
2269 buf = String::sprintf(
"%gmin", roundf(sec * adj) / adj);
2270 }
else if (usec >= 1000000LLU) {
2271 double sec = (double) usec / 1000000.0f;
2273 buf = String::sprintf(
"%gs", roundf(sec * adj) / adj);
2274 }
else if (usec >= 1000LLU) {
2275 double sec = (double) usec / 1000.0f;
2277 buf = String::sprintf(
"%gms", roundf(sec * adj) / adj);
2281 if (LIKELY(!buf.empty())) {
2282 size_t pos = buf.find(
"e+", 3);
2284 if (pos != buf.npos) {
2285 size_t off = buf.find_first_of(
"wdhms", pos + 2);
2287 buf.replace(pos, off - pos,
"", 0);
2293 if (LIKELY(!buf.empty())) {
2299 return String::sprintf(
"%" PRIu64
"u", usec);
2302 std::string String::scale(int64_t value,
int decimals)
throw () {
2303 if (decimals == 0) {
2304 if (value % 1000000000000000LL == 0 && value >= 1000000000000000LL) {
2305 return String::sprintf(
"%" PRId64
"q", value / 1000000000000000LL);
2306 }
else if (value % 1000000000000LL == 0 && value >= 1000000000000LL) {
2307 return String::sprintf(
"%" PRId64
"t", value / 1000000000000LL);
2308 }
else if (value % 1000000000LL == 0 && value >= 1000000000LL) {
2309 return String::sprintf(
"%" PRId64
"b", value / 1000000000LL);
2310 }
else if (value % 1000000LL == 0 && value >= 1000000LL) {
2311 return String::sprintf(
"%" PRId64
"m", value / 1000000LL);
2312 }
else if (value % 1000LL == 0 && value >= 1000LL) {
2313 return String::sprintf(
"%" PRId64
"k", value / 1000LL);
2316 double adj = pow(10, decimals);
2319 if (value >= 1000000000000000LL) {
2320 return String::sprintf(
"%0gq", roundf((
double) value / 1000000000000000.0f * adj) / adj);
2321 }
else if (value >= 1000000000000LL) {
2322 return String::sprintf(
"%0gt", roundf((
double) value / 1000000000000.0f * adj) / adj);
2323 }
else if (value >= 1000000000LL) {
2324 return String::sprintf(
"%0gb", roundf((
double) value / 1000000000.0f * adj) / adj);
2325 }
else if (value >= 1000000LL) {
2326 return String::sprintf(
"%0gm", roundf((
double) value / 1000000.0f * adj) / adj);
2327 }
else if (value >= 1000LL) {
2328 return String::sprintf(
"%0gk", roundf((
double) value / 1000.0f * adj) / adj);
2332 return String::sprintf(
"%" PRId64, value);
2335 std::string String::scale(uint64_t value,
int decimals)
throw () {
2336 if (decimals == 0) {
2337 if (value % 1000000000000000LLU == 0 && value >= 1000000000000000LLU) {
2338 return String::sprintf(
"%" PRIu64
"q", value / 1000000000000000LLU);
2339 }
else if (value % 1000000000000LLU == 0 && value >= 1000000000000LLU) {
2340 return String::sprintf(
"%" PRIu64
"t", value / 1000000000000LLU);
2341 }
else if (value % 1000000000LLU == 0 && value >= 1000000000LLU) {
2342 return String::sprintf(
"%" PRIu64
"b", value / 1000000000LLU);
2343 }
else if (value % 1000000LLU == 0 && value >= 1000000LLU) {
2344 return String::sprintf(
"%" PRIu64
"m", value / 1000000LLU);
2345 }
else if (value % 1000LLU == 0 && value >= 1000LLU) {
2346 return String::sprintf(
"%" PRIu64
"k", value / 1000LLU);
2349 double adj = pow(10, decimals);
2352 if (value >= 1000000000000000LLU) {
2353 return String::sprintf(
"%0gq", roundf((
double) value / 1000000000000000.0f * adj) / adj);
2354 }
else if (value >= 1000000000000LLU) {
2355 return String::sprintf(
"%0gt", roundf((
double) value / 1000000000000.0f * adj) / adj);
2356 }
else if (value >= 1000000000LLU) {
2357 return String::sprintf(
"%0gb", roundf((
double) value / 1000000000.0f * adj) / adj);
2358 }
else if (value >= 1000000LLU) {
2359 return String::sprintf(
"%0gm", roundf((
double) value / 1000000.0f * adj) / adj);
2360 }
else if (value >= 1000LLU) {
2361 return String::sprintf(
"%0gk", roundf((
double) value / 1000.0f * adj) / adj);
2365 return String::sprintf(
"%" PRIu64, value);
2368 std::string String::deflate(
const std::string &src, Algo algo,
int level)
throw (CompressionException) {
2369 size_t src_len = src.size();
2374 size_t buf_len = ::compressBound(src_len);
2375 char *buf = (
char*) malloc(buf_len);
2378 if (UNLIKELY(buf == NULL)) {
2379 THROW(CompressionException,
"Failed to allocate inflate buffer of %zd bytes", buf_len);
2382 memset(&strm, 0,
sizeof(strm));
2384 if (UNLIKELY(::deflateInit(&strm, level) != Z_OK)) {
2387 THROW(CompressionException,
"deflateInit failure");
2390 strm.next_in = (Bytef*) src.data();
2391 strm.avail_in = src_len;
2393 strm.next_out = (Bytef*) buf;
2394 strm.avail_out = buf_len;
2396 ret = ::deflate(&strm, Z_FINISH);
2398 if (ret == Z_STREAM_END) {
2399 dst.append(buf, strm.total_out);
2403 ::deflateEnd(&strm);
2405 if (UNLIKELY(ret != Z_STREAM_END)) {
2406 THROW(CompressionException,
"Failure during zlib deflate (%d): %s", ret, strm.msg);
2408 }
else if (algo == LZ4) {
2410 size_t buf_len = ::LZ4_compressBound(src_len) +
sizeof(
struct lz4);
2411 char *buf = (
char*) malloc(buf_len);
2412 struct lz4 *
lz4 = (
struct lz4*) buf;
2414 if (UNLIKELY(buf == NULL)) {
2415 THROW(CompressionException,
"Failed to allocate inflate buffer of %zd bytes", buf_len);
2418 if (UNLIKELY((c = ::LZ4_compress_default(src.data(), lz4->data, src_len, buf_len)) < 0)) {
2421 THROW(CompressionException,
"Failure during LZ4 deflate");
2424 lz4->magic = 0x184D2204;
2425 lz4->size = src_len;
2427 dst.append(buf, c +
sizeof(
struct lz4));
2430 }
else if (algo == Gzip) {
2432 size_t src_len = src.size();
2433 size_t buf_len = ::compressBound(src_len) +
sizeof(gzip_header) + 8;
2434 char *buf = (
char*) malloc(buf_len);
2435 unsigned long crc = ::crc32(0L, Z_NULL, 0);
2438 if (UNLIKELY(buf == NULL)) {
2439 THROW(CompressionException,
"Failed to allocate inflate buffer of %zd bytes", buf_len);
2442 memset(&strm, 0,
sizeof(strm));
2444 if (UNLIKELY(::deflateInit2(&strm, level, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK)) {
2447 THROW(CompressionException,
"deflateInit2 failure");
2451 dst.append((
const char*) gzip_header,
sizeof(gzip_header));
2454 strm.next_in = (Bytef*) src.data();
2455 strm.avail_in = src_len;
2457 crc = ::crc32(crc, strm.next_in, src_len);
2459 strm.next_out = (Bytef*) buf;
2460 strm.avail_out = buf_len;
2462 if (LIKELY((ret = ::deflate(&strm, Z_FINISH)) == Z_STREAM_END)) {
2463 dst.append(buf, strm.total_out);
2467 ::deflateEnd(&strm);
2469 if (UNLIKELY(ret != Z_STREAM_END)) {
2470 THROW(CompressionException,
"Failure during gzip deflate (%d): %s", ret, strm.msg);
2476 c[0] = (crc >> 0) & 0xff;
2477 c[1] = (crc >> 8) & 0xff;
2478 c[2] = (crc >> 16) & 0xff;
2479 c[3] = (crc >> 24) & 0xff;
2480 c[4] = (src_len >> 0) & 0xff;
2481 c[5] = (src_len >> 8) & 0xff;
2482 c[6] = (src_len >> 16) & 0xff;
2483 c[7] = (src_len >> 24) & 0xff;
2485 dst.append((
const char*) c,
sizeof(c));
2487 THROW(CompressionException,
"Unsupported compression algorithm");
2493 std::vector<char> String::deflate(
const std::vector<char> &src, Algo algo,
int level,
size_t size)
throw (CompressionException) {
2494 size_t src_len = (size == 0) ? src.size() : size;
2495 std::vector<char> dst;
2498 size_t buf_len = ::compressBound(src_len) +
sizeof(gzip_header) + 8;
2499 char *buf = (
char*) malloc(buf_len);
2502 if (UNLIKELY(buf == NULL)) {
2503 THROW(CompressionException,
"Failed to allocate inflate buffer of %zd bytes", buf_len);
2506 memset(&strm, 0,
sizeof(strm));
2508 if (UNLIKELY(::deflateInit(&strm, level) != Z_OK)) {
2511 THROW(CompressionException,
"deflateInit failure");
2514 strm.next_in = (Bytef*) src.data();
2515 strm.avail_in = src_len;
2517 strm.next_out = (Bytef*) buf;
2518 strm.avail_out = buf_len;
2520 int ret = ::deflate(&strm, Z_FINISH);
2522 if (LIKELY(ret == Z_STREAM_END)) {
2523 for (
size_t i = 0; i < strm.total_out; i++) {
2524 dst.push_back(buf[i]);
2529 ::deflateEnd(&strm);
2531 if (UNLIKELY(ret != Z_STREAM_END)) {
2532 THROW(CompressionException,
"Failure during deflate (%d): %s", ret, strm.msg);
2534 }
else if (algo == LZ4) {
2536 size_t buf_len = ::LZ4_compressBound(src_len) +
sizeof(
struct lz4);
2537 char *buf = (
char*) malloc(buf_len);
2538 struct lz4 *lz4 = (
struct lz4*) buf;
2540 if (UNLIKELY(buf == NULL)) {
2541 THROW(CompressionException,
"Failed to allocate inflate buffer of %zd bytes", buf_len);
2544 if (UNLIKELY((c = ::LZ4_compress_default(src.data(), lz4->data, src_len, buf_len)) < 0)) {
2547 THROW(CompressionException,
"Failure during LZ4 deflate");
2550 lz4->magic = 0x184D2204;
2551 lz4->size = src_len;
2553 dst.reserve(c +
sizeof(
struct lz4));
2555 for (
size_t i = 0; i < c +
sizeof(
struct lz4); i++) {
2556 dst.push_back(buf[i]);
2560 }
else if (algo == Gzip) {
2562 size_t src_len = src.size();
2563 size_t buf_len = ::compressBound(src_len) +
sizeof(gzip_header) + 8;
2564 char *buf = (
char*) malloc(buf_len);
2565 unsigned long crc = ::crc32(0L, Z_NULL, 0);
2568 if (UNLIKELY(buf == NULL)) {
2569 THROW(CompressionException,
"Failed to allocate inflate buffer of %zd bytes", buf_len);
2572 memset(&strm, 0,
sizeof(strm));
2574 if (UNLIKELY(::deflateInit2(&strm, level, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK)) {
2577 THROW(CompressionException,
"deflateInit2 failure");
2580 dst.reserve(
sizeof(gzip_header) + (src_len / 10) + 8);
2583 for (
size_t i = 0; i <
sizeof(gzip_header); i++) {
2584 dst.push_back(gzip_header[i]);
2588 strm.next_in = (Bytef*) src.data();
2589 strm.avail_in = src_len;
2591 crc = ::crc32(crc, strm.next_in, src_len);
2593 strm.next_out = (Bytef*) buf;
2594 strm.avail_out = buf_len;
2596 if (LIKELY((ret = ::deflate(&strm, Z_FINISH)) == Z_STREAM_END)) {
2597 dst.reserve(
sizeof(gzip_header) + strm.total_out + 8);
2599 for (
size_t i = 0; i < strm.total_out; i++) {
2600 dst.push_back(buf[i]);
2605 ::deflateEnd(&strm);
2607 if (UNLIKELY(ret != Z_STREAM_END)) {
2608 THROW(CompressionException,
"Failure during gzip deflate (%d): %s", ret, strm.msg);
2614 c[0] = (crc >> 0) & 0xff;
2615 c[1] = (crc >> 8) & 0xff;
2616 c[2] = (crc >> 16) & 0xff;
2617 c[3] = (crc >> 24) & 0xff;
2618 c[4] = (src_len >> 0) & 0xff;
2619 c[5] = (src_len >> 8) & 0xff;
2620 c[6] = (src_len >> 16) & 0xff;
2621 c[7] = (src_len >> 24) & 0xff;
2623 for (
size_t i = 0; i <
sizeof(c); i++) {
2624 dst.push_back(c[i]);
2627 THROW(CompressionException,
"Unsupported compression algorithm");
2633 std::string String::inflate(
const std::string &src, Algo algo)
throw (CompressionException) {
2636 if (src.size() < 8) {
2637 THROW(CompressionException,
"Malformed input to inflate");
2641 const union magic *u =
reinterpret_cast<const magic*
>(src.data());
2643 if (u->u16 == 0x0178) {
2645 }
else if (u->u16 == 0x9c78) {
2647 }
else if (u->u32 == 0x184D2204) {
2649 }
else if (u->u16 == 0x5e78) {
2651 }
else if (u->u16 == 0xDA78) {
2653 }
else if (u->u16 == 0x8B1F) {
2663 memset(&strm, 0,
sizeof(strm));
2665 if (UNLIKELY(::inflateInit(&strm) != Z_OK)) {
2666 THROW(CompressionException,
"inflateInit failure");
2669 strm.next_in = (Bytef*) src.data();
2670 strm.avail_in = src.size();
2673 strm.next_out = (Bytef*) buf;
2674 strm.avail_out =
sizeof(buf);
2676 ret = ::inflate(&strm, 0);
2678 if (dst.size() < strm.total_out) {
2679 dst.append(buf, strm.total_out - dst.size());
2681 }
while (ret == Z_OK);
2683 ::inflateEnd(&strm);
2685 if (UNLIKELY(ret != Z_STREAM_END)) {
2686 THROW(CompressionException,
"Failure during inflate (%d): %s", ret, strm.msg);
2688 }
else if (algo == LZ4) {
2690 struct lz4 *lz4 = (
struct lz4*) src.c_str();
2691 char *buf = (
char*) malloc(lz4->size);
2693 if (UNLIKELY(buf == NULL)) {
2694 THROW(CompressionException,
"Failed to allocate inflate buffer");
2697 if (UNLIKELY((c = ::LZ4_decompress_safe(lz4->data, buf, src.size() -
sizeof(
struct lz4), lz4->size)) < 0)) {
2700 THROW(CompressionException,
"Failure during LZ4 inflate");
2707 }
else if (algo == Gzip) {
2712 memset(&strm, 0,
sizeof(strm));
2714 if (UNLIKELY(::inflateInit2(&strm, -MAX_WBITS) != Z_OK)) {
2715 THROW(CompressionException,
"inflateInit failure");
2718 strm.next_in = (Bytef*) src.data() +
sizeof(gzip_header);
2719 strm.avail_in = src.size() -
sizeof(gzip_header) - 8;
2722 strm.next_out = (Bytef*) buf;
2723 strm.avail_out =
sizeof(buf);
2725 ret = ::inflate(&strm, 0);
2727 if (dst.size() < strm.total_out) {
2728 dst.append(buf, strm.total_out - dst.size());
2730 }
while (ret == Z_OK);
2732 ::inflateEnd(&strm);
2734 if (UNLIKELY(ret != Z_STREAM_END)) {
2735 THROW(CompressionException,
"Failure during inflate (%d): %s", ret, strm.msg);
2738 THROW(CompressionException,
"Unsupported compression algorithm");
2744 std::vector<char> String::inflate(
const std::vector<char> &src, Algo algo,
size_t size)
throw (CompressionException) {
2745 size_t len = (size == 0) ? src.size() : size;
2746 std::vector<char> dst;
2749 THROW(CompressionException,
"Malformed input to inflate");
2753 const union magic *u =
reinterpret_cast<const magic*
>(src.data());
2755 if (u->u16 == 0x0178) {
2757 }
else if (u->u16 == 0x9c78) {
2759 }
else if (u->u32 == 0x184D2204) {
2761 }
else if (u->u16 == 0x5e78) {
2763 }
else if (u->u16 == 0xDA78) {
2765 }
else if (u->u32 == 0x184D2A50) {
2767 }
else if (u->u16 == 0x8B1F) {
2777 memset(&strm, 0,
sizeof(strm));
2779 if (UNLIKELY(::inflateInit(&strm) != Z_OK)) {
2780 THROW(CompressionException,
"inflateInit failure");
2783 strm.next_in = (Bytef*) src.data();
2784 strm.avail_in = (size == 0) ? src.size(): size;
2787 strm.next_out = (Bytef*) buf;
2788 strm.avail_out =
sizeof(buf);
2790 ret = ::inflate(&strm, 0);
2792 if (dst.size() < strm.total_out) {
2793 for (
size_t i = 0, len = strm.total_out - dst.size(); i < len; i++) {
2794 dst.push_back(buf[i]);
2797 }
while (ret == Z_OK);
2799 ::inflateEnd(&strm);
2801 if (UNLIKELY(ret != Z_STREAM_END)) {
2802 THROW(CompressionException,
"Failure during inflate (%d): %s", ret, strm.msg);
2804 }
else if (algo == LZ4) {
2806 struct lz4 *lz4 = (
struct lz4*) src.data();
2807 char *buf = (
char*) malloc(lz4->size);
2809 if (UNLIKELY(buf == NULL)) {
2810 THROW(CompressionException,
"Failed to allocate inflate buffer");
2813 if (UNLIKELY((c = ::LZ4_decompress_safe(lz4->data, buf, len -
sizeof(
struct lz4), lz4->size)) < 0)) {
2816 THROW(CompressionException,
"Failure during LZ4 inflate");
2821 for (
size_t i = 0; i < (size_t) c; i++) {
2822 dst.push_back(buf[i]);
2826 }
else if (algo == Gzip) {
2831 memset(&strm, 0,
sizeof(strm));
2833 if (UNLIKELY(::inflateInit2(&strm, -MAX_WBITS) != Z_OK)) {
2834 THROW(CompressionException,
"inflateInit failure");
2837 strm.next_in = (Bytef*) src.data() +
sizeof(gzip_header);
2838 strm.avail_in = src.size() -
sizeof(gzip_header) - 8;
2841 strm.next_out = (Bytef*) buf;
2842 strm.avail_out =
sizeof(buf);
2844 ret = ::inflate(&strm, 0);
2846 if (dst.size() < strm.total_out) {
2847 for (
size_t i = 0, len = strm.total_out - dst.size(); i < len; i++) {
2848 dst.push_back(buf[i]);
2851 }
while (ret == Z_OK);
2853 ::inflateEnd(&strm);
2855 if (UNLIKELY(ret != Z_STREAM_END)) {
2856 THROW(CompressionException,
"Failure during inflate (%d): %s", ret, strm.msg);
2859 THROW(CompressionException,
"Unsupported compression algorithm");
2865 std::string String::encrypt(
const std::string &buffer,
const std::string &password,
const std::string &salt, uint32_t seed)
throw (CryptException) {
2866 Crypt crypt(
"twofish",
"cfb", 256, seed);
2868 crypt.setFlipSeed(CRYPT_SERIAL_XOR);
2870 if (!crypt.setPassword(password.c_str(), salt.c_str())) {
2871 THROW(CryptException,
"Failed to set password and salt");
2874 std::string buf(buffer, 0);
2876 if (!crypt.encrypt((
char*) buf.data(), buf.size())) {
2877 for (
size_t i = 0; i < buf.size(); i++) {
2881 THROW(CryptException,
"Failed to encrypt buffer");
2887 std::string& String::encrypt(std::string &buffer,
const std::string &password,
const std::string &salt, uint32_t seed)
throw (CryptException) {
2888 Crypt crypt(
"twofish",
"cfb", 256, seed);
2890 crypt.setFlipSeed(CRYPT_SERIAL_XOR);
2892 if (!crypt.setPassword(password.c_str(), salt.c_str())) {
2893 THROW(CryptException,
"Failed to set password and salt");
2896 if (!crypt.encrypt((
char*) buffer.data(), buffer.size())) {
2897 THROW(CryptException,
"Failed to encrypt buffer");
2903 std::string String::decrypt(
const std::string &buffer,
const std::string &password,
const std::string &salt, uint32_t seed)
throw (CryptException) {
2904 Crypt crypt(
"twofish",
"cfb", 256, seed);
2906 crypt.setFlipSeed(CRYPT_SERIAL_XOR);
2908 if (!crypt.setPassword(password.c_str(), salt.c_str())) {
2909 THROW(CryptException,
"Failed to set password and salt");
2912 std::string buf(buffer, 0);
2914 if (!crypt.decrypt((
char*) buf.data(), buf.size())) {
2915 for (
size_t i = 0; i < buf.size(); i++) {
2919 THROW(CryptException,
"Failed to decrypt buffer");
2925 std::string& String::decrypt(std::string &buffer,
const std::string &password,
const std::string &salt, uint32_t seed)
throw (CryptException) {
2926 Crypt crypt(
"twofish",
"cfb", 256, seed);
2928 crypt.setFlipSeed(CRYPT_SERIAL_XOR);
2930 if (!crypt.setPassword(password.c_str(), salt.c_str())) {
2931 THROW(CryptException,
"Failed to set password and salt");
2934 if (!crypt.decrypt((
char*) buffer.data(), buffer.size())) {
2935 THROW(CryptException,
"Failed to decrypt buffer");
2941 std::string String::random(
size_t len,
const std::string &charset, uint32_t seed)
throw () {
2942 struct drand48_data state;
2944 size_t size = charset.size();
2947 buffer.reserve(len);
2950 seed = ~(Time::gettimeofday_usec()) ^ 0xA5B65A69;
2953 srand48_r(seed, &state);
2955 for (
size_t i = 0; i < len; i++) {
2956 lrand48_r(&state, &r);
2957 char c = charset.at(r % (size - 1));
2959 buffer.push_back(c);
2992 static const std::string base64_chars_std =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2993 static const std::string base64_chars_alt =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
2995 static inline bool is_base64(
unsigned char c) {
2996 return (isalnum(c) || c ==
'+' || c ==
'/');
2999 std::string String::base64_encode(
unsigned char const *src,
unsigned int in_len,
bool standard)
throw () {
3003 unsigned char char_array_3[3];
3004 unsigned char char_array_4[4];
3005 const std::string &base64_chars = (standard) ? base64_chars_std : base64_chars_alt;
3008 char_array_3[i++] = *(src++);
3010 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
3011 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
3012 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
3013 char_array_4[3] = char_array_3[2] & 0x3f;
3015 for (i = 0; (i < 4) ; i++) {
3016 ret += base64_chars[char_array_4[i]];
3024 for (j = i; j < 3; j++) {
3025 char_array_3[j] =
'\0';
3028 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
3029 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
3030 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
3031 char_array_4[3] = char_array_3[2] & 0x3f;
3033 for (j = 0; j < i + 1; j++) {
3034 ret += base64_chars[char_array_4[j]];
3037 while (standard && i++ < 3) {
3045 std::string String::base64_encode(
const std::string &src,
bool standard)
throw () {
3046 unsigned char const *p = (
unsigned char const*) src.data();
3048 return base64_encode(p, src.size());
3051 std::string String::base64_decode(
const std::string &src,
bool standard)
throw () {
3052 int in_len = src.size();
3056 unsigned char char_array_4[4], char_array_3[3];
3058 const std::string &base64_chars = (standard) ? base64_chars_std : base64_chars_alt;
3060 while (in_len-- && (src[in_] !=
'=') && is_base64(src[in_])) {
3061 char_array_4[i++] = src[in_];
3065 for (i = 0; i < 4; i++) {
3066 char_array_4[i] = base64_chars.find(char_array_4[i]);
3069 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
3070 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
3071 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
3073 for (i = 0; i < 3; i++) {
3074 ret += char_array_3[i];
3082 for (j = i; j < 4; j++) {
3083 char_array_4[j] = 0;
3086 for (j = 0; j < 4; j++) {
3087 char_array_4[j] = base64_chars.find(char_array_4[j]);
3090 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
3091 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
3092 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
3094 for (j = 0; j < i - 1; j++) {
3095 ret += char_array_3[j];
3102 bool String::isPrintable(
const std::string &src)
throw () {
3103 bool printable =
true;
3105 for (
auto i = src.begin(), end = src.end(); i != end; ++i) {
3106 if (!std::isprint(*i)) {