]> git.saurik.com Git - redis.git/commitdiff
STRLEN command implemented
authorantirez <antirez@gmail.com>
Tue, 27 Jul 2010 08:09:26 +0000 (10:09 +0200)
committerantirez <antirez@gmail.com>
Tue, 27 Jul 2010 08:09:26 +0000 (10:09 +0200)
src/redis.c
src/redis.h
src/t_string.c
tests/unit/basic.tcl

index f6040dfe7dcfbd3d857d45fc8a8c7e64bbce76dc..c8b1c78146bd66d4b9bd4ef2f5c9807f2a6fa691 100644 (file)
@@ -74,6 +74,7 @@ struct redisCommand readonlyCommandTable[] = {
     {"setex",setexCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
     {"append",appendCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
     {"substr",substrCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
+    {"strlen",strlenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
     {"del",delCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
     {"exists",existsCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
     {"incr",incrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
index 7aca2abcc0cf7bc82d7c953e1c6134735ac1953a..63041860923e81331cc4279bbc1cc1bb75a3c7eb 100644 (file)
@@ -859,6 +859,7 @@ void blpopCommand(redisClient *c);
 void brpopCommand(redisClient *c);
 void appendCommand(redisClient *c);
 void substrCommand(redisClient *c);
+void strlenCommand(redisClient *c);
 void zrankCommand(redisClient *c);
 void zrevrankCommand(redisClient *c);
 void hsetCommand(redisClient *c);
index 281bd6be942372ed74fae41ba01816b12533ab4a..f55595c2b0cdad49a922c12cc4e76708845c8cc0 100644 (file)
@@ -252,4 +252,13 @@ void substrCommand(redisClient *c) {
     decrRefCount(o);
 }
 
+void strlenCommand(redisClient *c) {
+    robj *o;
+
+    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
+        checkType(c,o,REDIS_STRING)) return;
 
+    o = getDecodedObject(o);
+    addReplyLongLong(c,sdslen(o->ptr));
+    decrRefCount(o);
+}
index 0d50fa1b6644ca6f252e774e05054532358f12ef..f888cabcbc4efca0cc8425def6dbdefd4b84763f 100644 (file)
@@ -368,4 +368,18 @@ start_server {tags {"basic"}} {
         r expire z 10000
         list [r msetnx x A y B z C] [r mget x y z]
     } {0 {1 {} {}}}
+
+    test {STRLEN against non existing key} {
+        r strlen notakey
+    } {0}
+
+    test {STRLEN against integer} {
+        r set myinteger -555
+        r strlen myinteger
+    } {4}
+
+    test {STRLEN against plain string} {
+        r set mystring "foozzz0123456789 baz"
+        r strlen mystring
+    }
 }