diff options
author | antirez <antirez@gmail.com> | 2010-08-26 18:47:03 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2010-08-26 18:47:03 +0200 |
commit | ec7e138926b7b587adc247e8c64da6d3b1706434 (patch) | |
tree | c560ecf8b19225b036ff3a2791956474e9775856 /src/util.c | |
parent | 23c64fe50ddbc01f825ebe64f1a8b5f14c584327 (diff) | |
download | redis-ec7e138926b7b587adc247e8c64da6d3b1706434.tar.gz |
test for intset integer encodability test and some small refactoring
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/util.c b/src/util.c index cc2794f6a..e304ff839 100644 --- a/src/util.c +++ b/src/util.c @@ -200,24 +200,44 @@ int ll2string(char *s, size_t len, long long value) { return l; } -/* Check if the nul-terminated string 's' can be represented by a long +/* Check if the sds string 's' can be represented by a long long * (that is, is a number that fits into long without any other space or - * character before or after the digits). + * character before or after the digits, so that converting this number + * back to a string will result in the same bytes as the original string). * - * If so, the function returns REDIS_OK and *longval is set to the value + * If so, the function returns REDIS_OK and *llongval is set to the value * of the number. Otherwise REDIS_ERR is returned */ -int isStringRepresentableAsLong(sds s, long *longval) { +int isStringRepresentableAsLongLong(sds s, long long *llongval) { char buf[32], *endptr; - long value; + long long value; int slen; - value = strtol(s, &endptr, 10); + value = strtoll(s, &endptr, 10); if (endptr[0] != '\0') return REDIS_ERR; slen = 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 (sdslen(s) != (unsigned)slen || memcmp(buf,s,slen)) return REDIS_ERR; - if (longval) *longval = value; + if (llongval) *llongval = value; + return REDIS_OK; +} + +int isStringRepresentableAsLong(sds s, long *longval) { + long long ll; + + if (isStringRepresentableAsLongLong(s,&ll) == REDIS_ERR) return REDIS_ERR; + if (ll < LONG_MIN || ll > LONG_MAX) return REDIS_ERR; + *longval = (long)ll; return REDIS_OK; } + +int isObjectRepresentableAsLongLong(robj *o, long long *llongval) { + redisAssert(o->type == REDIS_STRING); + if (o->encoding == REDIS_ENCODING_INT) { + if (llongval) *llongval = (long) o->ptr; + return REDIS_OK; + } else { + return isStringRepresentableAsLongLong(o->ptr,llongval); + } +} |