Accept G, GB and GiB suffixes for options

This commit is contained in:
Integral
2026-03-07 14:49:39 +08:00
parent 6e1e545916
commit f1e77ab2ba
+37 -23
View File
@@ -347,7 +347,7 @@ static void errorOut(const char* msg)
/*! readU32FromCharChecked() : /*! readU32FromCharChecked() :
* @return 0 if success, and store the result in *value. * @return 0 if success, and store the result in *value.
* allows and interprets K, KB, KiB, M, MB and MiB suffix. * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
* Will also modify `*stringPtr`, advancing it to position where it stopped reading. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
* @return 1 if an overflow error occurs */ * @return 1 if an overflow error occurs */
static int readU32FromCharChecked(const char** stringPtr, unsigned* value) static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
@@ -362,15 +362,22 @@ static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
if (result < last) return 1; /* overflow error */ if (result < last) return 1; /* overflow error */
(*stringPtr)++ ; (*stringPtr)++ ;
} }
if ((**stringPtr=='K') || (**stringPtr=='M')) { if ((**stringPtr=='K') || (**stringPtr=='M') || (**stringPtr=='G')) {
unsigned const maxK = ((unsigned)(-1)) >> 10; switch (**stringPtr) {
if (result > maxK) return 1; /* overflow error */ case 'K':
result <<= 10; if (result > (((unsigned)-1) >> 10)) return 1; /* overflow error */
if (**stringPtr=='M') { result <<= 10;
if (result > maxK) return 1; /* overflow error */ break;
result <<= 10; case 'M':
if (result > (((unsigned)-1) >> 20)) return 1; /* overflow error */
result <<= 20;
break;
case 'G':
if (result > (((unsigned)-1) >> 30)) return 1; /* overflow error */
result <<= 30;
break;
} }
(*stringPtr)++; /* skip `K` or `M` */ (*stringPtr)++; /* skip `K`, `M` or `G` */
if (**stringPtr=='i') (*stringPtr)++; if (**stringPtr=='i') (*stringPtr)++;
if (**stringPtr=='B') (*stringPtr)++; if (**stringPtr=='B') (*stringPtr)++;
} }
@@ -380,7 +387,7 @@ static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
/*! readU32FromChar() : /*! readU32FromChar() :
* @return : unsigned integer value read from input in `char` format. * @return : unsigned integer value read from input in `char` format.
* allows and interprets K, KB, KiB, M, MB and MiB suffix. * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
* Will also modify `*stringPtr`, advancing it to position where it stopped reading. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
* Note : function will exit() program if digit sequence overflows */ * Note : function will exit() program if digit sequence overflows */
static unsigned readU32FromChar(const char** stringPtr) { static unsigned readU32FromChar(const char** stringPtr) {
@@ -392,7 +399,7 @@ static unsigned readU32FromChar(const char** stringPtr) {
/*! readIntFromChar() : /*! readIntFromChar() :
* @return : signed integer value read from input in `char` format. * @return : signed integer value read from input in `char` format.
* allows and interprets K, KB, KiB, M, MB and MiB suffix. * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
* Will also modify `*stringPtr`, advancing it to position where it stopped reading. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
* Note : function will exit() program if digit sequence overflows */ * Note : function will exit() program if digit sequence overflows */
static int readIntFromChar(const char** stringPtr) { static int readIntFromChar(const char** stringPtr) {
@@ -409,7 +416,7 @@ static int readIntFromChar(const char** stringPtr) {
/*! readSizeTFromCharChecked() : /*! readSizeTFromCharChecked() :
* @return 0 if success, and store the result in *value. * @return 0 if success, and store the result in *value.
* allows and interprets K, KB, KiB, M, MB and MiB suffix. * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
* Will also modify `*stringPtr`, advancing it to position where it stopped reading. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
* @return 1 if an overflow error occurs */ * @return 1 if an overflow error occurs */
static int readSizeTFromCharChecked(const char** stringPtr, size_t* value) static int readSizeTFromCharChecked(const char** stringPtr, size_t* value)
@@ -424,15 +431,22 @@ static int readSizeTFromCharChecked(const char** stringPtr, size_t* value)
if (result < last) return 1; /* overflow error */ if (result < last) return 1; /* overflow error */
(*stringPtr)++ ; (*stringPtr)++ ;
} }
if ((**stringPtr=='K') || (**stringPtr=='M')) { if ((**stringPtr=='K') || (**stringPtr=='M') || (**stringPtr=='G')) {
size_t const maxK = ((size_t)(-1)) >> 10; switch (**stringPtr) {
if (result > maxK) return 1; /* overflow error */ case 'K':
result <<= 10; if (result > (((size_t)-1) >> 10)) return 1; /* overflow error */
if (**stringPtr=='M') { result <<= 10;
if (result > maxK) return 1; /* overflow error */ break;
result <<= 10; case 'M':
if (result > (((size_t)-1) >> 20)) return 1; /* overflow error */
result <<= 20;
break;
case 'G':
if (result > (((size_t)-1) >> 30)) return 1; /* overflow error */
result <<= 30;
break;
} }
(*stringPtr)++; /* skip `K` or `M` */ (*stringPtr)++; /* skip `K`, `M` or `G` */
if (**stringPtr=='i') (*stringPtr)++; if (**stringPtr=='i') (*stringPtr)++;
if (**stringPtr=='B') (*stringPtr)++; if (**stringPtr=='B') (*stringPtr)++;
} }
@@ -442,7 +456,7 @@ static int readSizeTFromCharChecked(const char** stringPtr, size_t* value)
/*! readSizeTFromChar() : /*! readSizeTFromChar() :
* @return : size_t value read from input in `char` format. * @return : size_t value read from input in `char` format.
* allows and interprets K, KB, KiB, M, MB and MiB suffix. * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
* Will also modify `*stringPtr`, advancing it to position where it stopped reading. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
* Note : function will exit() program if digit sequence overflows */ * Note : function will exit() program if digit sequence overflows */
static size_t readSizeTFromChar(const char** stringPtr) { static size_t readSizeTFromChar(const char** stringPtr) {
@@ -830,7 +844,7 @@ static unsigned init_nbWorkers(unsigned defaultNbWorkers) {
NEXT_FIELD(__nb); \ NEXT_FIELD(__nb); \
_varu32 = readU32FromChar(&__nb); \ _varu32 = readU32FromChar(&__nb); \
if(*__nb != 0) { \ if(*__nb != 0) { \
errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed"); \ errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB, G, GB, GiB are allowed"); \
} \ } \
} }
@@ -839,7 +853,7 @@ static unsigned init_nbWorkers(unsigned defaultNbWorkers) {
NEXT_FIELD(__nb); \ NEXT_FIELD(__nb); \
_varTsize = readSizeTFromChar(&__nb); \ _varTsize = readSizeTFromChar(&__nb); \
if(*__nb != 0) { \ if(*__nb != 0) { \
errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed"); \ errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB, G, GB, GiB are allowed"); \
} \ } \
} }