Hi guys,
I am learning the redis source code recently and get a little bit confused for the usage of "string2ll" and "strtoll".
I think these two functions are nearly same in converting string to a long long number. The only difference might be that string2ll is written by redis developer while strtoll is from stdlib.h. But redis uses both. So I am wondering is there anything else to pay attention when use these 2 functions?
Also I found that rdbTryIntegerEncoding() in rdb.c could be replaced with a more easy-to-read implementation using string2ll. I made a PR for this.
the original rdbTryIntegerEncoding() is like:
int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) {
long long value;
char *endptr, buf[32];
/* Check if it's possible to encode this value as a number */
value = strtoll(s, &endptr, 10);
if (endptr[0] != '\0') return 0;
ll2string(buf,32,value);
/* If the number converted back into a string is not identical
* then it's not possible to encode the string as integer */
if (strlen(buf) != len || memcmp(buf,s,len)) return 0;
return rdbEncodeInteger(value,enc);
}
which could be improved using string2ll to make it more easy to read
int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) {
long long value;
if (string2ll(s, len, &value)) {
return rdbEncodeInteger(value, enc);
} else {
return 0;
}
}
Thanks for your time and patience.
Comment From: zuiderkwast
My guess is that string2ll was added as an optimization of storing values but not all occurrences of strtoll were replaced. Did you look at the git history of rdbTryIntegerEncoding and string2ll? It could give us a hint.
Anyway, I think the PR seems right.
Comment From: DarrenJiang13
solved in #8926