00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: String.cpp 1230 2014-11-16 02:32:01Z sella $";
00010
00011 #include "String.h"
00012
00013 #include <math.h>
00014 #include <string.h>
00015 #include <sstream>
00016
00017 #include <cctype>
00018 #include <algorithm>
00019
00020 #include <boost/algorithm/string/replace.hpp>
00021
00022 #define BUFFER_SIZE (1024 * 32)
00023
00024 using namespace sella::util;
00025
00026 std::string String::sprintf(const char* format, ...) throw () {
00027 va_list args;
00028
00029 va_start(args, format);
00030 std::string buffer = vsprintf(format, args);
00031 va_end(args);
00032
00033 return buffer;
00034 }
00035
00036 std::string String::sprintf(const std::string &format, ...) throw () {
00037 va_list args;
00038
00039 va_start(args, format);
00040 std::string buffer = vsprintf(format.c_str(), args);
00041 va_end(args);
00042
00043 return buffer;
00044 }
00045
00046 std::string String::vsprintf(const char* format, va_list args) throw () {
00047 char buffer[BUFFER_SIZE];
00048
00049 vsnprintf(buffer, sizeof(buffer), format, args);
00050
00051 return std::string(buffer);
00052 }
00053
00054 std::string String::vsprintf(const std::string &format, va_list args) throw () {
00055 char buffer[BUFFER_SIZE];
00056
00057 vsnprintf(buffer, sizeof(buffer), format.c_str(), args);
00058
00059 return std::string(buffer);
00060 }
00061
00062 std::string String::substr_replace(const std::string &src, const std::string &search, const std::string &replace, bool global) throw () {
00063 size_t i = 0, inc = (replace.length()) ? replace.length() : 1;
00064 std::string dst = src;
00065
00066 while (true) {
00067 if ((i = dst.find(search, i)) == std::string::npos) {
00068 break;
00069 }
00070
00071 dst.replace(i, search.length(), replace);
00072
00073 if (!global) break;
00074
00075 i += inc;
00076 }
00077
00078 return dst;
00079 }
00080
00081 std::string& String::substr_replace(std::string &src, const std::string &search, const std::string &replace, bool global) throw () {
00082 size_t i = 0, inc = (replace.length()) ? replace.length() : 1;
00083
00084 while (true) {
00085 if ((i = src.find(search, i)) == std::string::npos) {
00086 break;
00087 }
00088
00089 src.replace(i, search.length(), replace);
00090
00091 if (!global) break;
00092
00093 i += inc;
00094 }
00095
00096 return src;
00097 }
00098
00099 std::string String::substr_ireplace(const std::string &src, const std::string &search, const std::string &replace, bool global) throw () {
00100 if (global) {
00101 return boost::algorithm::ireplace_all_copy(src, search, replace);
00102 } else {
00103 return boost::algorithm::ireplace_first_copy(src, search, replace);
00104 }
00105 }
00106
00107 std::string& String::substr_ireplace(std::string &src, const std::string &search, const std::string &replace, bool global) throw () {
00108 if (global) {
00109 boost::algorithm::ireplace_all(src, search, replace);
00110 } else {
00111 boost::algorithm::ireplace_first(src, search, replace);
00112 }
00113
00114 return src;
00115 }
00116
00117 std::string& String::substr_replace2(std::string &src, const std::string &search, const std::string &replace, bool global) throw () {
00118 return substr_replace(src, search, replace, global);
00119 }
00120
00121 std::string& String::substr_ireplace2(std::string &src, const std::string &search, const std::string &replace, bool global) throw () {
00122 return substr_ireplace(src, search, replace, global);
00123 }
00124
00125 std::string String::regex_replace(const std::string &src, const boost::regex ®ex, const std::string &replace, MatchFlag mFlags) throw (RegexException) {
00126 try {
00127 return boost::regex_replace(src, regex, replace, mFlags);
00128 } catch (boost::bad_expression &e) {
00129 RETHROW_CODE(RegexException, 0, e);
00130 } catch (std::exception &e) {
00131 RETHROW_CODE(RegexException, 0, e);
00132 }
00133 }
00134
00135 std::string String::regex_replace(const std::string &src, const std::string ®ex, const std::string &replace, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00136 try {
00137 const boost::regex rx(regex, reFlags);
00138
00139 return boost::regex_replace(src, rx, replace, mFlags);
00140 } catch (boost::bad_expression &e) {
00141 RETHROW_CODE(RegexException, 0, e);
00142 } catch (std::exception &e) {
00143 RETHROW_CODE(RegexException, 0, e);
00144 }
00145 }
00146
00147 std::string& String::regex_replace(std::string &src, const boost::regex ®ex, const std::string &replace, MatchFlag mFlags) throw (RegexException) {
00148 try {
00149 src = boost::regex_replace(src, regex, replace, mFlags);
00150
00151 return src;
00152 } catch (boost::bad_expression &e) {
00153 RETHROW_CODE(RegexException, 0, e);
00154 } catch (std::exception &e) {
00155 RETHROW_CODE(RegexException, 0, e);
00156 }
00157 }
00158
00159 std::string& String::regex_replace(std::string &src, const std::string ®ex, const std::string &replace, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00160 try {
00161 const boost::regex rx(regex, reFlags);
00162
00163 src = boost::regex_replace(src, rx, replace, mFlags);
00164
00165 return src;
00166 } catch (boost::bad_expression &e) {
00167 RETHROW_CODE(RegexException, 0, e);
00168 } catch (std::exception &e) {
00169 RETHROW_CODE(RegexException, 0, e);
00170 }
00171 }
00172
00173 std::string String::regex_ireplace(const std::string &src, const boost::regex ®ex, const std::string &replace, MatchFlag mFlags) throw (RegexException) {
00174 try {
00175 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00176
00177 return regex_replace(src, rx, replace, mFlags);
00178 } catch (boost::bad_expression &e) {
00179 RETHROW_CODE(RegexException, 0, e);
00180 } catch (std::exception &e) {
00181 RETHROW_CODE(RegexException, 0, e);
00182 }
00183 }
00184
00185 std::string String::regex_ireplace(const std::string &src, const std::string ®ex, const std::string &replace, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00186 try {
00187 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00188
00189 return regex_replace(src, rx, replace, mFlags);
00190 } catch (boost::bad_expression &e) {
00191 RETHROW_CODE(RegexException, 0, e);
00192 } catch (std::exception &e) {
00193 RETHROW_CODE(RegexException, 0, e);
00194 }
00195 }
00196
00197 std::string& String::regex_ireplace(std::string &src, const boost::regex ®ex, const std::string &replace, MatchFlag mFlags) throw (RegexException) {
00198 try {
00199 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00200
00201 return regex_replace(src, rx, replace, mFlags);
00202 } catch (boost::bad_expression &e) {
00203 RETHROW_CODE(RegexException, 0, e);
00204 } catch (std::exception &e) {
00205 RETHROW_CODE(RegexException, 0, e);
00206 }
00207 }
00208
00209 std::string& String::regex_ireplace(std::string &src, const std::string ®ex, const std::string &replace, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00210 try {
00211 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00212
00213 return regex_replace(src, rx, replace, mFlags);
00214 } catch (boost::bad_expression &e) {
00215 RETHROW_CODE(RegexException, 0, e);
00216 } catch (std::exception &e) {
00217 RETHROW_CODE(RegexException, 0, e);
00218 }
00219 }
00220
00221 bool String::regex_match(const std::string &src, const boost::regex ®ex, MatchFlag mFlags) throw (RegexException) {
00222 try {
00223 return boost::regex_match(src, regex, mFlags);
00224 } catch (boost::bad_expression &e) {
00225 RETHROW_CODE(RegexException, 0, e);
00226 } catch (std::exception &e) {
00227 RETHROW_CODE(RegexException, 0, e);
00228 }
00229 }
00230
00231 bool String::regex_match(const std::string &src, const std::string ®ex, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00232 try {
00233 const boost::regex rx(regex, reFlags);
00234
00235 return regex_match(src, rx, mFlags);
00236 } catch (boost::bad_expression &e) {
00237 RETHROW_CODE(RegexException, 0, e);
00238 } catch (std::exception &e) {
00239 RETHROW_CODE(RegexException, 0, e);
00240 }
00241 }
00242
00243 bool String::regex_match(const std::string &src, const boost::regex ®ex, boost::cmatch &matches, MatchFlag mFlags) throw (RegexException) {
00244 try {
00245 return boost::regex_match(src.c_str(), matches, regex);
00246 } catch (boost::bad_expression &e) {
00247 RETHROW_CODE(RegexException, 0, e);
00248 } catch (std::exception &e) {
00249 RETHROW_CODE(RegexException, 0, e);
00250 }
00251 }
00252
00253 bool String::regex_match(const std::string &src, const std::string ®ex, boost::cmatch &matches, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00254 try {
00255 const boost::regex rx(regex, reFlags);
00256
00257 return regex_match(src, rx, matches, mFlags);
00258 } catch (boost::bad_expression &e) {
00259 RETHROW_CODE(RegexException, 0, e);
00260 } catch (std::exception &e) {
00261 RETHROW_CODE(RegexException, 0, e);
00262 }
00263 }
00264
00265 bool String::regex_match(const std::string &src, const boost::regex ®ex, std::string &match, MatchFlag mFlags) throw (RegexException) {
00266 boost::cmatch m;
00267
00268 if (regex_match(src, regex, m, mFlags)) {
00269 match = m[0];
00270
00271 return true;
00272 }
00273
00274 return false;
00275 }
00276
00277 bool String::regex_match(const std::string &src, const std::string ®ex, std::string &match, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00278 try {
00279 const boost::regex rx(regex, reFlags);
00280
00281 return regex_match(src, rx, match, mFlags);
00282 } catch (boost::bad_expression &e) {
00283 RETHROW_CODE(RegexException, 0, e);
00284 } catch (std::exception &e) {
00285 RETHROW_CODE(RegexException, 0, e);
00286 }
00287 }
00288
00289 bool String::regex_match(const std::string &src, const boost::regex ®ex, std::vector<std::string> &matches, MatchFlag mFlags) throw (RegexException) {
00290 boost::cmatch m;
00291
00292 if (regex_match(src, regex, m, mFlags)) {
00293 for (size_t i = 0; i < m.size(); i++) {
00294 matches.push_back(m[i]);
00295 }
00296
00297 return true;
00298 }
00299
00300 return false;
00301 }
00302
00303 bool String::regex_match(const std::string &src, const std::string ®ex, std::vector<std::string> &matches, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00304 try {
00305 const boost::regex rx(regex, reFlags);
00306
00307 return regex_match(src, rx, matches, mFlags);
00308 } catch (boost::bad_expression &e) {
00309 RETHROW_CODE(RegexException, 0, e);
00310 } catch (std::exception &e) {
00311 RETHROW_CODE(RegexException, 0, e);
00312 }
00313 }
00314
00315 bool String::regex_imatch(const std::string &src, const boost::regex ®ex, MatchFlag mFlags) throw (RegexException) {
00316 try {
00317 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00318
00319 return regex_match(src, rx, mFlags);
00320 } catch (boost::bad_expression &e) {
00321 RETHROW_CODE(RegexException, 0, e);
00322 } catch (std::exception &e) {
00323 RETHROW_CODE(RegexException, 0, e);
00324 }
00325 }
00326
00327 bool String::regex_imatch(const std::string &src, const std::string ®ex, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00328 try {
00329 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00330
00331 return regex_match(src, rx, mFlags);
00332 } catch (boost::bad_expression &e) {
00333 RETHROW_CODE(RegexException, 0, e);
00334 } catch (std::exception &e) {
00335 RETHROW_CODE(RegexException, 0, e);
00336 }
00337 }
00338
00339 bool String::regex_imatch(const std::string &src, const boost::regex ®ex, boost::cmatch &matches, MatchFlag mFlags) throw (RegexException) {
00340 try {
00341 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00342
00343 return regex_match(src, rx, matches, mFlags);
00344 } catch (boost::bad_expression &e) {
00345 RETHROW_CODE(RegexException, 0, e);
00346 } catch (std::exception &e) {
00347 RETHROW_CODE(RegexException, 0, e);
00348 }
00349 }
00350
00351 bool String::regex_imatch(const std::string &src, const std::string ®ex, boost::cmatch &matches, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00352 try {
00353 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00354
00355 return regex_match(src, rx, matches, mFlags);
00356 } catch (boost::bad_expression &e) {
00357 RETHROW_CODE(RegexException, 0, e);
00358 } catch (std::exception &e) {
00359 RETHROW_CODE(RegexException, 0, e);
00360 }
00361 }
00362
00363 bool String::regex_imatch(const std::string &src, const boost::regex ®ex, std::string &match, MatchFlag mFlags) throw (RegexException) {
00364 try {
00365 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00366
00367 return regex_match(src, rx, match, mFlags);
00368 } catch (boost::bad_expression &e) {
00369 RETHROW_CODE(RegexException, 0, e);
00370 } catch (std::exception &e) {
00371 RETHROW_CODE(RegexException, 0, e);
00372 }
00373 }
00374
00375 bool String::regex_imatch(const std::string &src, const std::string ®ex, std::string &match, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00376 try {
00377 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00378
00379 return regex_match(src, rx, match, mFlags);
00380 } catch (boost::bad_expression &e) {
00381 RETHROW_CODE(RegexException, 0, e);
00382 } catch (std::exception &e) {
00383 RETHROW_CODE(RegexException, 0, e);
00384 }
00385 }
00386
00387 bool String::regex_imatch(const std::string &src, const boost::regex ®ex, std::vector<std::string> &matches, MatchFlag mFlags) throw (RegexException) {
00388 try {
00389 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00390
00391 return regex_match(src, rx, matches, mFlags);
00392 } catch (boost::bad_expression &e) {
00393 RETHROW_CODE(RegexException, 0, e);
00394 } catch (std::exception &e) {
00395 RETHROW_CODE(RegexException, 0, e);
00396 }
00397 }
00398
00399 bool String::regex_imatch(const std::string &src, const std::string ®ex, std::vector<std::string> &matches, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00400 try {
00401 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00402
00403 return regex_match(src, rx, matches, mFlags);
00404 } catch (boost::bad_expression &e) {
00405 RETHROW_CODE(RegexException, 0, e);
00406 } catch (std::exception &e) {
00407 RETHROW_CODE(RegexException, 0, e);
00408 }
00409 }
00410
00411 bool String::regex_search(const std::string &src, const boost::regex ®ex, MatchFlag mFlags) throw (RegexException) {
00412 try {
00413 return boost::regex_search(src, regex, mFlags);
00414 } catch (boost::bad_expression &e) {
00415 RETHROW_CODE(RegexException, 0, e);
00416 } catch (std::exception &e) {
00417 RETHROW_CODE(RegexException, 0, e);
00418 }
00419 }
00420
00421 bool String::regex_search(const std::string &src, const std::string ®ex, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00422 try {
00423 const boost::regex rx(regex, reFlags);
00424
00425 return regex_search(src, rx, mFlags);
00426 } catch (boost::bad_expression &e) {
00427 RETHROW_CODE(RegexException, 0, e);
00428 } catch (std::exception &e) {
00429 RETHROW_CODE(RegexException, 0, e);
00430 }
00431 }
00432
00433 bool String::regex_search(const std::string &src, const boost::regex ®ex, boost::cmatch &matches, MatchFlag mFlags) throw (RegexException) {
00434 try {
00435 return boost::regex_search(src.c_str(), matches, regex);
00436 } catch (boost::bad_expression &e) {
00437 RETHROW_CODE(RegexException, 0, e);
00438 } catch (std::exception &e) {
00439 RETHROW_CODE(RegexException, 0, e);
00440 }
00441 }
00442
00443 bool String::regex_search(const std::string &src, const std::string ®ex, boost::cmatch &matches, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00444 try {
00445 const boost::regex rx(regex, reFlags);
00446
00447 return regex_search(src, rx, matches, mFlags);
00448 } catch (boost::bad_expression &e) {
00449 RETHROW_CODE(RegexException, 0, e);
00450 } catch (std::exception &e) {
00451 RETHROW_CODE(RegexException, 0, e);
00452 }
00453 }
00454
00455 bool String::regex_search(const std::string &src, const boost::regex ®ex, std::string &match, MatchFlag mFlags) throw (RegexException) {
00456 boost::cmatch m;
00457
00458 if (regex_search(src, regex, m, mFlags)) {
00459 match = m[0];
00460
00461 return true;
00462 }
00463
00464 return false;
00465 }
00466
00467 bool String::regex_search(const std::string &src, const std::string ®ex, std::string &match, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00468 try {
00469 const boost::regex rx(regex, reFlags);
00470
00471 return regex_search(src, rx, match, mFlags);
00472 } catch (boost::bad_expression &e) {
00473 RETHROW_CODE(RegexException, 0, e);
00474 } catch (std::exception &e) {
00475 RETHROW_CODE(RegexException, 0, e);
00476 }
00477 }
00478
00479 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) {
00480 try {
00481 const char *p = src.c_str();
00482 boost::cmatch m;
00483
00484 while (boost::regex_search(p, m, regex)) {
00485 if (subexp) {
00486 for (size_t i = 0; i < m.size(); i++) {
00487 matches.push_back(m[i]);
00488 }
00489 } else {
00490 matches.push_back(m[0]);
00491 }
00492 p = m.suffix().str().c_str();
00493
00494 if (!global) break;
00495 }
00496
00497 return (!matches.empty());
00498 } catch (boost::bad_expression &e) {
00499 RETHROW_CODE(RegexException, 0, e);
00500 } catch (std::exception &e) {
00501 RETHROW_CODE(RegexException, 0, e);
00502 }
00503 }
00504
00505 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) {
00506 try {
00507 const boost::regex rx(regex, reFlags);
00508
00509 return regex_search(src, rx, matches, global, subexp, mFlags);
00510 } catch (boost::bad_expression &e) {
00511 RETHROW_CODE(RegexException, 0, e);
00512 } catch (std::exception &e) {
00513 RETHROW_CODE(RegexException, 0, e);
00514 }
00515 }
00516
00517 bool String::regex_isearch(const std::string &src, const boost::regex ®ex, MatchFlag mFlags) throw (RegexException) {
00518 try {
00519 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00520
00521 return regex_search(src, rx, mFlags);
00522 } catch (boost::bad_expression &e) {
00523 RETHROW_CODE(RegexException, 0, e);
00524 } catch (std::exception &e) {
00525 RETHROW_CODE(RegexException, 0, e);
00526 }
00527 }
00528
00529 bool String::regex_isearch(const std::string &src, const std::string ®ex, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00530 try {
00531 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00532
00533 return regex_search(src, rx, mFlags);
00534 } catch (boost::bad_expression &e) {
00535 RETHROW_CODE(RegexException, 0, e);
00536 } catch (std::exception &e) {
00537 RETHROW_CODE(RegexException, 0, e);
00538 }
00539 }
00540
00541 bool String::regex_isearch(const std::string &src, const boost::regex ®ex, boost::cmatch &matches, MatchFlag mFlags) throw (RegexException) {
00542 try {
00543 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00544
00545 return regex_search(src, rx, matches, mFlags);
00546 } catch (boost::bad_expression &e) {
00547 RETHROW_CODE(RegexException, 0, e);
00548 } catch (std::exception &e) {
00549 RETHROW_CODE(RegexException, 0, e);
00550 }
00551 }
00552
00553 bool String::regex_isearch(const std::string &src, const std::string ®ex, boost::cmatch &matches, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00554 try {
00555 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00556
00557 return regex_search(src, rx, matches, mFlags);
00558 } catch (boost::bad_expression &e) {
00559 RETHROW_CODE(RegexException, 0, e);
00560 } catch (std::exception &e) {
00561 RETHROW_CODE(RegexException, 0, e);
00562 }
00563 }
00564
00565 bool String::regex_isearch(const std::string &src, const boost::regex ®ex, std::string &match, MatchFlag mFlags) throw (RegexException) {
00566 try {
00567 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00568
00569 return regex_search(src, rx, match, mFlags);
00570 } catch (boost::bad_expression &e) {
00571 RETHROW_CODE(RegexException, 0, e);
00572 } catch (std::exception &e) {
00573 RETHROW_CODE(RegexException, 0, e);
00574 }
00575 }
00576
00577 bool String::regex_isearch(const std::string &src, const std::string ®ex, std::string &match, REFlag reFlags, MatchFlag mFlags) throw (RegexException) {
00578 try {
00579 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00580
00581 return regex_search(src, rx, match, mFlags);
00582 } catch (boost::bad_expression &e) {
00583 RETHROW_CODE(RegexException, 0, e);
00584 } catch (std::exception &e) {
00585 RETHROW_CODE(RegexException, 0, e);
00586 }
00587 }
00588
00589 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) {
00590 try {
00591 const boost::regex rx(regex.str(), regex.flags() | boost::regex_constants::icase);
00592
00593 return regex_search(src, rx, matches, global, subexp, mFlags);
00594 } catch (boost::bad_expression &e) {
00595 RETHROW_CODE(RegexException, 0, e);
00596 } catch (std::exception &e) {
00597 RETHROW_CODE(RegexException, 0, e);
00598 }
00599 }
00600
00601 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) {
00602 try {
00603 const boost::regex rx(regex, reFlags | boost::regex_constants::icase);
00604
00605 return regex_search(src, rx, matches, global, subexp, mFlags);
00606 } catch (boost::bad_expression &e) {
00607 RETHROW_CODE(RegexException, 0, e);
00608 } catch (std::exception &e) {
00609 RETHROW_CODE(RegexException, 0, e);
00610 }
00611 }
00612
00613 std::vector<std::string> String::split(const std::string &str, const std::string &delim, bool exact, bool empty, size_t limit) throw () {
00614 std::vector<std::string> vector;
00615 std::size_t prev = 0, pos, chunks = 0;
00616
00617 if (exact) {
00618 while (++chunks < limit && (pos = str.find(delim, prev)) != std::string::npos) {
00619 if (pos > prev || (empty && pos == prev)) {
00620 vector.push_back(str.substr(prev, pos - prev));
00621 }
00622
00623 prev = pos + delim.length();
00624 }
00625 } else {
00626 while (++chunks < limit && (pos = str.find_first_of(delim, prev)) != std::string::npos) {
00627 if (pos > prev || (empty && pos == prev)) {
00628 vector.push_back(str.substr(prev, pos - prev));
00629 }
00630
00631 prev = pos + 1;
00632 }
00633
00634 }
00635
00636 if (chunks >= limit || prev < str.length() || (empty && prev == str.length())) {
00637 vector.push_back(str.substr(prev, std::string::npos));
00638 }
00639
00640 return vector;
00641 }
00642
00643 std::vector<std::string> String::split(const std::string &str, char delim, bool empty, size_t limit) throw () {
00644 std::vector<std::string> vector;
00645 std::string s;
00646 std::stringstream ss(str);
00647 std::size_t chunks = 0;
00648
00649 while (++chunks < limit && std::getline(ss, s, delim)) {
00650 if (empty || !s.empty()) {
00651 vector.push_back(s);
00652 }
00653 }
00654
00655 if (chunks >= limit) {
00656 if (std::getline(ss, s)) {
00657 vector.push_back(s);
00658 }
00659 } else {
00660 if (empty && !str.empty() && str[str.length() - 1] == delim) {
00661 vector.push_back(std::string());
00662 }
00663 }
00664
00665 return vector;
00666 }
00667
00668 std::string String::join(const std::vector<std::string> &src, const std::string &delim) throw () {
00669 std::string dst;
00670
00671 if (!src.empty()) {
00672 for (auto it = src.cbegin(); it != src.cend(); ++it) {
00673 dst.append(*it).append(delim);
00674 }
00675
00676 dst.erase(dst.size() - delim.size());
00677 }
00678
00679 return dst;
00680 }
00681
00682 std::string String::join(const std::vector<std::string> &src, char delim) throw () {
00683 std::string dst;
00684
00685 if (!src.empty()) {
00686 for (auto it = src.cbegin(); it != src.cend(); ++it) {
00687 dst.append(*it).push_back(delim);
00688 }
00689
00690 dst.erase(dst.size() - 1);
00691 }
00692
00693 return dst;
00694 }
00695
00696 std::string String::join(const std::list<std::string> &src, const std::string &delim) throw () {
00697 std::string dst;
00698
00699 if (!src.empty()) {
00700 for (auto it = src.cbegin(); it != src.cend(); ++it) {
00701 dst.append(*it).append(delim);
00702 }
00703
00704 dst.erase(dst.size() - delim.size());
00705 }
00706
00707 return dst;
00708 }
00709
00710 std::string String::join(const std::list<std::string> &src, char delim) throw () {
00711 std::string dst;
00712
00713 if (!src.empty()) {
00714 for (auto it = src.cbegin(); it != src.cend(); ++it) {
00715 dst.append(*it).push_back(delim);
00716 }
00717
00718 dst.erase(dst.size() - 1);
00719 }
00720
00721 return dst;
00722 }
00723
00724 std::string String::join(const std::set<std::string> &src, const std::string &delim) throw () {
00725 std::string dst;
00726
00727 if (!src.empty()) {
00728 for (auto it = src.cbegin(); it != src.cend(); ++it) {
00729 dst.append(*it).append(delim);
00730 }
00731
00732 dst.erase(dst.size() - delim.size());
00733 }
00734
00735 return dst;
00736 }
00737
00738 std::string String::join(const std::set<std::string> &src, char delim) throw () {
00739 std::string dst;
00740
00741 if (!src.empty()) {
00742 for (auto it = src.cbegin(); it != src.cend(); ++it) {
00743 dst.append(*it).push_back(delim);
00744 }
00745
00746 dst.erase(dst.size() - 1);
00747 }
00748
00749 return dst;
00750 }
00751
00752 std::string String::trim(const std::string &str, const std::string &space) throw () {
00753 return ltrim(rtrim(str, space), space);
00754 }
00755
00756 std::string String::trim(const std::string &str, char space) throw () {
00757 return ltrim(rtrim(str, space), space);
00758 }
00759
00760 std::string& String::trim(std::string &str, const std::string &space) throw () {
00761 return ltrim(rtrim(str, space), space);
00762 }
00763
00764 std::string& String::trim(std::string &str, char space) throw () {
00765 return ltrim(rtrim(str, space), space);
00766 }
00767
00768 std::string String::rtrim(const std::string &str, const std::string &space) throw () {
00769 size_t i;
00770
00771 if ((i = str.find_last_not_of(space)) != std::string::npos) {
00772 return str.substr(0, i + 1);
00773 } else {
00774 return std::string();
00775 }
00776
00777 return str;
00778 }
00779
00780 std::string String::rtrim(const std::string &str, char space) throw () {
00781 size_t i;
00782
00783 if ((i = str.find_last_not_of(space)) != std::string::npos) {
00784 return str.substr(0, i + 1);
00785 } else {
00786 return std::string();
00787 }
00788
00789 return str;
00790 }
00791
00792 std::string& String::rtrim(std::string &str, const std::string &space) throw () {
00793 size_t i;
00794
00795 if ((i = str.find_last_not_of(space)) != std::string::npos) {
00796 str.substr(0, i + 1).swap(str);
00797 } else {
00798 str.clear();
00799 }
00800
00801 return str;
00802 }
00803
00804 std::string& String::rtrim(std::string &str, char space) throw () {
00805 size_t i;
00806
00807 if ((i = str.find_last_not_of(space)) != std::string::npos) {
00808 str.substr(0, i + 1).swap(str);
00809 } else {
00810 str.clear();
00811 }
00812
00813 return str;
00814 }
00815
00816 std::string String::ltrim(const std::string &str, const std::string &space) throw () {
00817 size_t i;
00818
00819 if ((i = str.find_first_not_of(space)) != std::string::npos) {
00820 return str.substr(i);
00821 } else {
00822 return std::string();
00823 }
00824
00825 return str;
00826 }
00827
00828 std::string String::ltrim(const std::string &str, char space) throw () {
00829 size_t i;
00830
00831 if ((i = str.find_first_not_of(space)) != std::string::npos) {
00832 return str.substr(i);
00833 } else {
00834 return std::string();
00835 }
00836
00837 return str;
00838 }
00839
00840 std::string& String::ltrim(std::string &str, const std::string &space) throw () {
00841 size_t i;
00842
00843 if ((i = str.find_first_not_of(space)) != std::string::npos) {
00844 str.substr(i).swap(str);
00845 } else {
00846 str.clear();
00847 }
00848
00849 return str;
00850 }
00851
00852 std::string& String::ltrim(std::string &str, char space) throw () {
00853 size_t i;
00854
00855 if ((i = str.find_first_not_of(space)) != std::string::npos) {
00856 str.substr(i).swap(str);
00857 } else {
00858 str.clear();
00859 }
00860
00861 return str;
00862 }
00863
00864 std::string String::lower(const std::string &src) throw () {
00865 std::string dst(src);
00866
00867 std::transform(dst.begin(), dst.end(), dst.begin(), ::tolower);
00868
00869 return dst;
00870 }
00871
00872 std::string& String::lower(std::string &src) throw () {
00873 std::transform(src.begin(), src.end(), src.begin(), ::tolower);
00874
00875 return src;
00876 }
00877
00878 std::string String::upper(const std::string &src) throw () {
00879 std::string dst(src);
00880
00881 std::transform(dst.begin(), dst.end(), dst.begin(), ::toupper);
00882
00883 return dst;
00884 }
00885
00886 std::string& String::upper(std::string &src) throw () {
00887 std::transform(src.begin(), src.end(), src.begin(), ::toupper);
00888
00889 return src;
00890 }
00891
00892 std::string String::ucfirst(const std::string &src) throw () {
00893 std::string dst(lower(src));
00894
00895 for (auto it = dst.begin(); it != dst.end(); ++it) {
00896 if (!isspace(*it)) {
00897 *it = std::toupper(*it);
00898
00899 break;
00900 }
00901 }
00902
00903 return dst;
00904 }
00905
00906 std::string& String::ucfirst(std::string &src) throw () {
00907 lower(src);
00908
00909 for (auto it = src.begin(); it != src.end(); ++it) {
00910 if (!isspace(*it)) {
00911 *it = std::toupper(*it);
00912
00913 break;
00914 }
00915 }
00916
00917 return src;
00918 }
00919
00920 std::string String::ucword(const std::string &src) throw () {
00921 std::string dst(src);
00922 bool space = true;
00923
00924 for (auto it = dst.begin(); it != dst.end(); ++it) {
00925 if (space) {
00926 *it = std::toupper(*it);
00927 } else {
00928 *it = std::tolower(*it);
00929 }
00930
00931 space = isspace(*it);
00932 }
00933
00934 return src;
00935 }
00936
00937 std::string& String::ucword(std::string &src) throw () {
00938 bool space = true;
00939
00940 for (auto it = src.begin(); it != src.end(); ++it) {
00941 if (space) {
00942 *it = std::toupper(*it);
00943 } else {
00944 *it = std::tolower(*it);
00945 }
00946
00947 space = isspace(*it);
00948 }
00949
00950 return src;
00951 }
00952
00953 std::string String::bandwidth(int64_t bps, int decimals) throw () {
00954 if (decimals == 0) {
00955 if (bps % 1000000000000000LL == 0 && bps >= 1000000000000000LL) {
00956 return String::sprintf("%"PRId64"Pbs", bps / 1000000000000000LL);
00957 } else if (bps % 1000000000000LL == 0 && bps >= 1000000000000LL) {
00958 return String::sprintf("%"PRId64"Tbs", bps / 1000000000000LL);
00959 } else if (bps % 1000000000LL == 0 && bps >= 1000000000LL) {
00960 return String::sprintf("%"PRId64"Gbs", bps / 1000000000LL);
00961 } else if (bps % 1000000LL == 0 && bps >= 1000000LL) {
00962 return String::sprintf("%"PRId64"Mbs", bps / 1000000LL);
00963 } else if (bps % 1000LL == 0 && bps >= 1000LL) {
00964 return String::sprintf("%"PRId64"Kbs", bps / 1000LL);
00965 }
00966 } else {
00967 double adj = pow(10, decimals);
00968
00969 if (bps >= 1000000000000000LL) {
00970 return String::sprintf("%gPbs", roundf((double) bps / 1000000000000000.0f * adj) / adj);
00971 } else if (bps >= 1000000000000LL) {
00972 return String::sprintf("%gTbs", roundf((double) bps / 1000000000000.0f * adj) / adj);
00973 } else if (bps >= 1000000000LL) {
00974 return String::sprintf("%gGbs", roundf((double) bps / 1000000000.0f * adj) / adj);
00975 } else if (bps >= 1000000LL) {
00976 return String::sprintf("%gMbs", roundf((double) bps / 1000000.0f * adj) / adj);
00977 } else if (bps >= 1000LL) {
00978 return String::sprintf("%gKbs", roundf((double) bps / 1000.0f * adj) / adj);
00979 }
00980 }
00981
00982 return String::sprintf("%"PRId64"bps", bps);
00983 }
00984
00985 std::string String::bandwidth(uint64_t bps, int decimals) throw () {
00986 if (decimals == 0) {
00987 if (bps % 1000000000000000LLU == 0 && bps >= 1000000000000000LLU) {
00988 return String::sprintf("%"PRIu64"Pbs", bps / 1000000000000000LLU);
00989 } else if (bps % 1000000000000LLU == 0 && bps >= 1000000000000LLU) {
00990 return String::sprintf("%"PRIu64"Tbs", bps / 1000000000000LLU);
00991 } else if (bps % 1000000000LLU == 0 && bps >= 1000000000LLU) {
00992 return String::sprintf("%"PRIu64"Gbs", bps / 1000000000LLU);
00993 } else if (bps % 1000000LLU == 0 && bps >= 1000000LLU) {
00994 return String::sprintf("%"PRIu64"Mbs", bps / 1000000LLU);
00995 } else if (bps % 1000LLU == 0 && bps >= 1000LLU) {
00996 return String::sprintf("%"PRIu64"Kbs", bps / 1000LLU);
00997 }
00998 } else {
00999 double adj = pow(10, decimals);
01000
01001 if (bps >= 1000000000000000LLU) {
01002 return String::sprintf("%gPbs", roundf((double) bps / 1000000000000000.0f * adj) / adj);
01003 } else if (bps >= 1000000000000LLU) {
01004 return String::sprintf("%gTbs", roundf((double) bps / 1000000000000.0f * adj) / adj);
01005 } else if (bps >= 1000000000LLU) {
01006 return String::sprintf("%gGbs", roundf((double) bps / 1000000000.0f * adj) / adj);
01007 } else if (bps >= 1000000LLU) {
01008 return String::sprintf("%gMbs", roundf((double) bps / 1000000.0f * adj) / adj);
01009 } else if (bps >= 1000LLU) {
01010 return String::sprintf("%gKbs", roundf((double) bps / 1000.0f * adj) / adj);
01011 }
01012 }
01013
01014 return String::sprintf("%"PRIu64"bps", bps);
01015 }
01016
01017 #ifdef __LP64__
01018 std::string String::bandwidth(long long bps, int decimals) throw () {
01019 return bandwidth((int64_t) bps, decimals);
01020 }
01021
01022 std::string String::bandwidth(unsigned long long bps, int decimals) throw () {
01023 return bandwidth((uint64_t) bps,decimals);
01024 }
01025 #else
01026 std::string String::bandwidth(long bps, int decimals) throw () {
01027 return bandwidth((int64_t) bps, decimals);
01028 }
01029
01030 std::string String::bandwidth(unsigned long bps, int decimals) throw () {
01031 return bandwidth((uint64_t) bps, decimals);
01032 }
01033 #endif
01034
01035 std::string String::dataSize(int64_t bytes, int decimals) throw () {
01036 if (decimals == 0) {
01037 if (bytes % (int64_t) pow(2, 50) == 0 && bytes >= (int64_t) pow(2, 50)) {
01038 return String::sprintf("%"PRId64"PB", bytes / (int64_t) pow(2, 50));
01039 } else if (bytes % (int64_t) pow(2, 40) == 0 && bytes >= (int64_t) pow(2, 40)) {
01040 return String::sprintf("%"PRId64"TB", bytes / (int64_t) pow(2, 40));
01041 } else if (bytes % (int64_t) pow(2, 30) == 0 && bytes >= (int64_t) pow(2, 30)) {
01042 return String::sprintf("%"PRId64"GB", bytes / (int64_t) pow(2, 30));
01043 } else if (bytes % (int64_t) pow(2, 20) == 0 && bytes >= (int64_t) pow(2, 20)) {
01044 return String::sprintf("%"PRId64"MB", bytes / (int64_t) pow(2, 20));
01045 } else if (bytes % (int64_t) pow(2, 10) == 0 && bytes >= (int64_t) pow(2, 10)) {
01046 return String::sprintf("%"PRId64"KB", bytes / (int64_t) pow(2, 10));
01047 }
01048 } else {
01049 double adj = pow(10, decimals);
01050
01051 if (bytes >= (int64_t) pow(2, 50)) {
01052 return String::sprintf("%gPB", roundf((double) bytes / pow(2, 50) * adj) / adj);
01053 } else if (bytes >= (int64_t) pow(2, 40)) {
01054 return String::sprintf("%gTB", roundf((double) bytes / pow(2, 40) * adj) / adj);
01055 } else if (bytes >= (int64_t) pow(2, 30)) {
01056 return String::sprintf("%gGB", roundf((double) bytes / pow(2, 30) * adj) / adj);
01057 } else if (bytes >= (int64_t) pow(2, 20)) {
01058 return String::sprintf("%gMB", roundf((double) bytes / pow(2, 20) * adj) / adj);
01059 } else if (bytes >= (int64_t) pow(2, 10)) {
01060 return String::sprintf("%gKB", roundf((double) bytes / pow(2, 10) * adj) / adj);
01061 }
01062 }
01063
01064 return String::sprintf("%lldB", bytes);
01065 }
01066
01067 std::string String::dataSize(uint64_t bytes, int decimals) throw () {
01068 if (decimals == 0) {
01069 if (bytes % (uint64_t) pow(2, 50) == 0 && bytes >= (uint64_t) pow(2, 50)) {
01070 return String::sprintf("%"PRIu64"PB", bytes / (uint64_t) pow(2, 50));
01071 } else if (bytes % (uint64_t) pow(2, 40) == 0 && bytes >= (uint64_t) pow(2, 40)) {
01072 return String::sprintf("%"PRIu64"TB", bytes / (uint64_t) pow(2, 40));
01073 } else if (bytes % (uint64_t) pow(2, 30) == 0 && bytes >= (uint64_t) pow(2, 30)) {
01074 return String::sprintf("%"PRIu64"GB", bytes / (uint64_t) pow(2, 30));
01075 } else if (bytes % (uint64_t) pow(2, 20) == 0 && bytes >= (uint64_t) pow(2, 20)) {
01076 return String::sprintf("%"PRIu64"MB", bytes / (uint64_t) pow(2, 20));
01077 } else if (bytes % (uint64_t) pow(2, 10) == 0 && bytes >= (uint64_t) pow(2, 10)) {
01078 return String::sprintf("%"PRIu64"KB", bytes / (uint64_t) pow(2, 10));
01079 }
01080 } else {
01081 double adj = pow(10, decimals);
01082
01083 if (bytes >= (uint64_t) pow(2, 50)) {
01084 return String::sprintf("%gPB", roundf((double) bytes / pow(2, 50) * adj) / adj);
01085 } else if (bytes >= (uint64_t) pow(2, 40)) {
01086 return String::sprintf("%gTB", roundf((double) bytes / pow(2, 40) * adj) / adj);
01087 } else if (bytes >= (uint64_t) pow(2, 30)) {
01088 return String::sprintf("%gGB", roundf((double) bytes / pow(2, 30) * adj) / adj);
01089 } else if (bytes >= (uint64_t) pow(2, 20)) {
01090 return String::sprintf("%gMB", roundf((double) bytes / pow(2, 20) * adj) / adj);
01091 } else if (bytes >= (uint64_t) pow(2, 10)) {
01092 return String::sprintf("%gKB", roundf((double) bytes / pow(2, 10) * adj) / adj);
01093 }
01094 }
01095
01096 return String::sprintf("%lluB", bytes);
01097 }
01098
01099 #ifdef __LP64__
01100 std::string String::dataSize(long long bytes, int decimals) throw () {
01101 return dataSize((int64_t) bytes, decimals);
01102 }
01103
01104 std::string String::dataSize(unsigned long long bytes, int decimals) throw () {
01105 return dataSize((uint64_t) bytes, decimals);
01106 }
01107 #else
01108 std::string String::dataSize(long bytes, int decimals) throw () {
01109 return dataSize((int64_t) bytes, decimals);
01110 }
01111
01112 std::string String::dataSize(unsigned long bytes, int decimals) throw () {
01113 return dataSize((uint64_t) bytes, decimals);
01114 }
01115 #endif
01116
01117 std::string String::dataRate(int64_t bps, int decimals) throw () {
01118 if (decimals == 0) {
01119 if (bps % 1000000000000000LL == 0 && bps >= 1000000000000000LL) {
01120 return String::sprintf("%gp", bps / 1000000000000000LL);
01121 } else if (bps % 1000000000000LL == 0 && bps >= 1000000000000LL) {
01122 return String::sprintf("%"PRId64"t", bps / 1000000000000LL);
01123 } else if (bps % 1000000000LL == 0 && bps >= 1000000000LL) {
01124 return String::sprintf("%"PRId64"g", bps / 1000000000LL);
01125 } else if (bps % 1000000LL == 0 && bps >= 1000000LL) {
01126 return String::sprintf("%"PRId64"m", bps / 1000000LL);
01127 } else if (bps % 1000LL == 0 && bps >= 1000LL) {
01128 return String::sprintf("%"PRId64"k", bps / 1000LL);
01129 }
01130 } else {
01131 double adj = pow(10, decimals);
01132
01133 if (bps >= 1000000000000000LL) {
01134 return String::sprintf("%gp", roundf((double) bps / 1000000000000000.0f * adj) / adj);
01135 } else if (bps >= 1000000000000LL) {
01136 return String::sprintf("%gt", roundf((double) bps / 1000000000000.0f * adj) / adj);
01137 } else if (bps >= 1000000000LL) {
01138 return String::sprintf("%gg", roundf((double) bps / 1000000000.0f * adj) / adj);
01139 } else if (bps >= 1000000LL) {
01140 return String::sprintf("%gm", roundf((double) bps / 1000000.0f * adj) / adj);
01141 } else if (bps >= 1000LL) {
01142 return String::sprintf("%gk", roundf((double) bps / 1000.0f * adj) / adj);
01143 }
01144 }
01145
01146 return String::sprintf("%"PRId64"b", bps);
01147 }
01148
01149 std::string String::dataRate(uint64_t bps, int decimals) throw () {
01150 if (decimals == 0) {
01151 if (bps % 1000000000000000LLU == 0 && bps >= 1000000000000000LLU) {
01152 return String::sprintf("%"PRIu64"p", bps / 1000000000000000LLU);
01153 } else if (bps % 1000000000000LLU == 0 && bps >= 1000000000000LLU) {
01154 return String::sprintf("%"PRIu64"t", bps / 1000000000000LLU);
01155 } else if (bps % 1000000000LLU == 0 && bps >= 1000000000LLU) {
01156 return String::sprintf("%"PRIu64"g", bps / 1000000000LLU);
01157 } else if (bps % 1000000LLU == 0 && bps >= 1000000LLU) {
01158 return String::sprintf("%"PRIu64"m", bps / 1000000LLU);
01159 } else if (bps % 1000LLU == 0 && bps >= 1000LLU) {
01160 return String::sprintf("%"PRIu64"k", bps / 1000LLU);
01161 }
01162 } else {
01163 double adj = pow(10, decimals);
01164
01165 if (bps >= 1000000000000000LLU) {
01166 return String::sprintf("%gp", roundf((double) bps / 1000000000000000.0f * adj) / adj);
01167 } else if (bps >= 1000000000000LLU) {
01168 return String::sprintf("%gt", roundf((double) bps / 1000000000000.0f * adj) / adj);
01169 } else if (bps >= 1000000000LLU) {
01170 return String::sprintf("%gg", roundf((double) bps / 1000000000.0f * adj) / adj);
01171 } else if (bps >= 1000000LLU) {
01172 return String::sprintf("%gm", roundf((double) bps / 1000000.0f * adj) / adj);
01173 } else if (bps >= 1000LLU) {
01174 return String::sprintf("%gk", roundf((double) bps / 1000.0f * adj) / adj);
01175 }
01176 }
01177
01178 return String::sprintf("%"PRIu64"", bps);
01179 }
01180
01181 #ifdef __LP64__
01182 std::string String::dataRate(long long bps, int decimals) throw () {
01183 return dataRate((int64_t) bps, decimals);
01184 }
01185
01186 std::string String::dataRate(unsigned long long bps, int decimals) throw () {
01187 return dataRate((uint64_t) bps, decimals);
01188 }
01189 #else
01190 std::string String::dataRate(long bps, int decimals) throw () {
01191 return dataRate((int64_t) bps, decimals);
01192 }
01193
01194 std::string String::dataRate(unsigned long bps, int decimals) throw () {
01195 return dataRate((uint64_t) bps, decimals);
01196 }
01197 #endif
01198
01199 std::string String::duration(int64_t usec, int decimals) throw () {
01200 if (decimals == 0) {
01201 if (usec % (7LL * 86400LL * (int64_t) pow(10, 6)) == 0 && usec >= (7LL * 86400LL * (int64_t) pow(10, 6))) {
01202 return String::sprintf("%"PRId64"w", usec / (7LL * 86400LL * (int64_t) pow(10, 6)));
01203 } else if (usec % (86400LL * (int64_t) pow(10, 6)) == 0 && usec >= (86400LL * (int64_t) pow(10, 6))) {
01204 return String::sprintf("%"PRId64"d", usec / (86400LL * (int64_t) pow(10, 6)));
01205 } else if (usec % (3600LL * (int64_t) pow(10, 6)) == 0 && usec >= (3600LL * (int64_t) pow(10, 6))) {
01206 return String::sprintf("%"PRId64"h", usec / (3600LL * (int64_t) pow(10, 6)));
01207 } else if (usec % (60LL * (int64_t) pow(10, 6)) == 0 && usec >= (60LL * (int64_t) pow(10, 6))) {
01208 return String::sprintf("%"PRId64"min", usec / (60LL * (int64_t) pow(10, 6)));
01209 } else if (usec % (int64_t) pow(10, 6) == 0 && usec >= (int64_t) pow(10, 6)) {
01210 return String::sprintf("%"PRId64"s", usec / (int64_t) pow(10, 6));
01211 } else if (usec % (int64_t) pow(10, 3) == 0 && usec >= (int64_t) pow(10, 3)) {
01212 return String::sprintf("%"PRId64"ms", usec / (int64_t) pow(10, 3));
01213 }
01214 } else {
01215 double adj = pow(10, decimals);
01216
01217 if (usec >= (7LL * 86400LL * (int64_t) pow(10, 6))) {
01218 return String::sprintf("%gw", roundf((double) usec / 7.0f * 86400.0f * pow(10, 6) * adj) / adj);
01219 } else if (usec >= (86400LL * (int64_t) pow(10, 6))) {
01220 return String::sprintf("%gd", roundf((double) usec / 86400.0f * pow(10, 6) * adj) / adj);
01221 } else if (usec >= (3600LL * (int64_t) pow(10, 6))) {
01222 return String::sprintf("%gh", roundf((double) usec / 3600.0f * pow(10, 6) * adj) / adj);
01223 } else if (usec >= (60LL * (int64_t) pow(10, 6))) {
01224 return String::sprintf("%gmin", roundf((double) usec / 60.0f * pow(10, 6) * adj) / adj);
01225 } else if (usec >= (int64_t) pow(10, 6)) {
01226 return String::sprintf("%gs", roundf((double) usec / pow(10, 6) * adj) / adj);
01227 } else if (usec >= (int64_t) pow(10, 3)) {
01228 return String::sprintf("%gms", roundf((double) usec / pow(10, 3) * adj) / adj);
01229 }
01230 }
01231
01232 return String::sprintf("%"PRId64"us", usec);
01233 }
01234
01235 std::string String::duration(uint64_t usec, int decimals) throw () {
01236 if (decimals == 0) {
01237 if (usec % (7LLU * 86400LLU * (uint64_t) pow(10, 6)) == 0 && usec >= (7LLU * 86400LLU * (uint64_t) pow(10, 6))) {
01238 return String::sprintf("%"PRIu64"w", usec / (7LLU * 86400LLU * (uint64_t) pow(10, 6)));
01239 } else if (usec % (86400LLU * (uint64_t) pow(10, 6)) == 0 && usec >= (86400LLU * (uint64_t) pow(10, 6))) {
01240 return String::sprintf("%"PRIu64"d", usec / (86400LLU * (uint64_t) pow(10, 6)));
01241 } else if (usec % (3600LLU * (uint64_t) pow(10, 6)) == 0 && usec >= (3600LLU * (uint64_t) pow(10, 6))) {
01242 return String::sprintf("%"PRIu64"h", usec / (3600LLU * (uint64_t) pow(10, 6)));
01243 } else if (usec % (60LLU * (uint64_t) pow(10, 6)) == 0 && usec >= (60LLU * (uint64_t) pow(10, 6))) {
01244 return String::sprintf("%"PRIu64"min", usec / (60LLU * (uint64_t) pow(10, 6)));
01245 } else if (usec % (uint64_t) pow(10, 6) == 0 && usec >= (uint64_t) pow(10, 6)) {
01246 return String::sprintf("%"PRIu64"s", usec / (uint64_t) pow(10, 6));
01247 } else if (usec % (uint64_t) pow(10, 3) == 0 && usec >= (uint64_t) pow(10, 3)) {
01248 return String::sprintf("%"PRIu64"ms", usec / (uint64_t) pow(10, 3));
01249 }
01250 } else {
01251 double adj = pow(10, decimals);
01252
01253 if (usec >= (7LLU * 86400LLU * (uint64_t) pow(10, 6))) {
01254 return String::sprintf("%gw", roundf((double) usec / 7.0f * 86400.0f * pow(10, 6) * adj) / adj);
01255 } else if (usec >= (86400LLU * (uint64_t) pow(10, 6))) {
01256 return String::sprintf("%gd", roundf((double) usec / 86400.0f * pow(10, 6) * adj) / adj);
01257 } else if (usec >= (3600LLU * (uint64_t) pow(10, 6))) {
01258 return String::sprintf("%gh", roundf((double) usec / 3600.0f * pow(10, 6) * adj) / adj);
01259 } else if (usec >= (60LLU * (uint64_t) pow(10, 6))) {
01260 return String::sprintf("%gmin", roundf((double) usec / 60.0f * pow(10, 6) * adj) / adj);
01261 } else if (usec >= (uint64_t) pow(10, 6)) {
01262 return String::sprintf("%gs", roundf((double) usec / pow(10, 6) * adj) / adj);
01263 } else if (usec >= (uint64_t) pow(10, 3)) {
01264 return String::sprintf("%gms", roundf((double) usec / pow(10, 3) * adj) / adj);
01265 }
01266 }
01267
01268 return String::sprintf("%"PRIu64"u", usec);
01269 }
01270
01271 #ifdef __LP64__
01272 std::string String::duration(long long usec, int decimals) throw () {
01273 return duration((int64_t) usec, decimals);
01274 }
01275
01276 std::string String::duration(unsigned long long usec, int decimals) throw () {
01277 return duration((uint64_t) usec, decimals);
01278 }
01279 #else
01280 std::string String::duration(long usec, int decimals) throw () {
01281 return duration((int64_t) usec, decimals);
01282 }
01283
01284 std::string String::duration(unsigned long usec, int decimals) throw () {
01285 return duration((uint64_t) usec, decimals);
01286 }
01287 #endif
01288
01289
01290
01291