From 80091bbaacf83d82635f3202c1db3f61f56dc0d0 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 27 Jul 2010 10:09:26 +0200 Subject: [PATCH] STRLEN command implemented --- src/redis.c | 1 + src/redis.h | 1 + src/t_string.c | 9 +++++++++ tests/unit/basic.tcl | 14 ++++++++++++++ 4 files changed, 25 insertions(+) diff --git a/src/redis.c b/src/redis.c index f6040dfe7..c8b1c7814 100644 --- a/src/redis.c +++ b/src/redis.c @@ -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}, diff --git a/src/redis.h b/src/redis.h index 7aca2abcc..630418609 100644 --- a/src/redis.h +++ b/src/redis.h @@ -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); diff --git a/src/t_string.c b/src/t_string.c index 281bd6be9..f55595c2b 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -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); +} diff --git a/tests/unit/basic.tcl b/tests/unit/basic.tcl index 0d50fa1b6..f888cabcb 100644 --- a/tests/unit/basic.tcl +++ b/tests/unit/basic.tcl @@ -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 + } } -- GitLab