MCPcopy Create free account
hub / github.com/duckdb/duckdb / StringToInt

Method StringToInt

tools/shell/shell.cpp:623–662  ·  view source on GitHub ↗

** Interpret zArg as an integer value, possibly with suffixes. */

Source from the content-addressed store, hash-verified

621** Interpret zArg as an integer value, possibly with suffixes.
622*/
623int64_t ShellState::StringToInt(const string &arg) {
624 int64_t v = 0;
625 static const struct {
626 const char *zSuffix;
627 int iMult;
628 } aMult[] = {
629 {"KiB", 1024}, {"MiB", 1024 * 1024}, {"GiB", 1024 * 1024 * 1024},
630 {"KB", 1000}, {"MB", 1000000}, {"GB", 1000000000},
631 {"K", 1000}, {"M", 1000000}, {"G", 1000000000},
632 };
633 int i;
634 int isNeg = 0;
635 auto zArg = arg.c_str();
636 if (zArg[0] == '-') {
637 isNeg = 1;
638 zArg++;
639 } else if (zArg[0] == '+') {
640 zArg++;
641 }
642 if (zArg[0] == '0' && zArg[1] == 'x') {
643 int x;
644 zArg += 2;
645 while ((x = hexDigitValue(zArg[0])) >= 0) {
646 v = (v << 4) + x;
647 zArg++;
648 }
649 } else {
650 while (IsDigit(zArg[0])) {
651 v = v * 10 + zArg[0] - '0';
652 zArg++;
653 }
654 }
655 for (i = 0; i < ArraySize(aMult); i++) {
656 if (StringUtil::CIEquals(aMult[i].zSuffix, zArg)) {
657 v *= aMult[i].iMult;
658 break;
659 }
660 }
661 return isNeg ? -v : v;
662}
663
664string ShellState::ModeToString(RenderMode mode) {
665 switch (mode) {

Callers 1

SetPagerFunction · 0.80

Calls 2

hexDigitValueFunction · 0.85
c_strMethod · 0.80

Tested by

no test coverage detected