mirror of
https://github.com/fluencelabs/redis
synced 2025-03-27 21:01:04 +00:00
string2ll(): better commenting.
This commit is contained in:
parent
b9a8e56439
commit
f4ac796c34
@ -346,6 +346,7 @@ int string2ll(const char *s, size_t slen, long long *value) {
|
|||||||
int negative = 0;
|
int negative = 0;
|
||||||
unsigned long long v;
|
unsigned long long v;
|
||||||
|
|
||||||
|
/* A zero length string is not a valid number. */
|
||||||
if (plen == slen)
|
if (plen == slen)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -355,6 +356,8 @@ int string2ll(const char *s, size_t slen, long long *value) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Handle negative numbers: just set a flag and continue like if it
|
||||||
|
* was a positive number. Later convert into negative. */
|
||||||
if (p[0] == '-') {
|
if (p[0] == '-') {
|
||||||
negative = 1;
|
negative = 1;
|
||||||
p++; plen++;
|
p++; plen++;
|
||||||
@ -372,6 +375,7 @@ int string2ll(const char *s, size_t slen, long long *value) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Parse all the other digits, checking for overflow at every step. */
|
||||||
while (plen < slen && p[0] >= '0' && p[0] <= '9') {
|
while (plen < slen && p[0] >= '0' && p[0] <= '9') {
|
||||||
if (v > (ULLONG_MAX / 10)) /* Overflow. */
|
if (v > (ULLONG_MAX / 10)) /* Overflow. */
|
||||||
return 0;
|
return 0;
|
||||||
@ -388,6 +392,8 @@ int string2ll(const char *s, size_t slen, long long *value) {
|
|||||||
if (plen < slen)
|
if (plen < slen)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
/* Convert to negative if needed, and do the final overflow check when
|
||||||
|
* converting from unsigned long long to long long. */
|
||||||
if (negative) {
|
if (negative) {
|
||||||
if (v > ((unsigned long long)(-(LLONG_MIN+1))+1)) /* Overflow. */
|
if (v > ((unsigned long long)(-(LLONG_MIN+1))+1)) /* Overflow. */
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user