+ bitval = ((char*)o->ptr)[byte] & (1 << bit);
+ }
+
+ addReply(c, bitval ? shared.cone : shared.czero);
+}
+
+void setrangeCommand(redisClient *c) {
+ robj *o;
+ long offset;
+ sds value = c->argv[3]->ptr;
+
+ if (getLongFromObjectOrReply(c,c->argv[2],&offset,NULL) != REDIS_OK)
+ return;
+
+ if (offset < 0) {
+ addReplyError(c,"offset is out of range");
+ return;
+ }
+
+ o = lookupKeyWrite(c->db,c->argv[1]);
+ if (o == NULL) {
+ /* Return 0 when setting nothing on a non-existing string */
+ if (sdslen(value) == 0) {
+ addReply(c,shared.czero);
+ return;
+ }
+
+ /* Return when the resulting string exceeds allowed size */
+ if (checkStringLength(c,offset+sdslen(value)) != REDIS_OK)
+ return;
+
+ o = createObject(REDIS_STRING,sdsempty());
+ dbAdd(c->db,c->argv[1],o);
+ } else {
+ size_t olen;
+
+ /* Key exists, check type */
+ if (checkType(c,o,REDIS_STRING))
+ return;
+
+ /* Return existing string length when setting nothing */
+ olen = stringObjectLen(o);
+ if (sdslen(value) == 0) {
+ addReplyLongLong(c,olen);
+ return;
+ }
+
+ /* Return when the resulting string exceeds allowed size */
+ if (checkStringLength(c,offset+sdslen(value)) != REDIS_OK)
+ return;
+
+ /* Create a copy when the object is shared or encoded. */
+ if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
+ robj *decoded = getDecodedObject(o);
+ o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
+ decrRefCount(decoded);
+ dbOverwrite(c->db,c->argv[1],o);
+ }
+ }
+
+ if (sdslen(value) > 0) {
+ o->ptr = sdsgrowzero(o->ptr,offset+sdslen(value));
+ memcpy((char*)o->ptr+offset,value,sdslen(value));
+ signalModifiedKey(c->db,c->argv[1]);
+ server.dirty++;
+ }
+ addReplyLongLong(c,sdslen(o->ptr));
+}
+
+void getrangeCommand(redisClient *c) {
+ robj *o;
+ long start, end;
+ char *str, llbuf[32];
+ size_t strlen;
+
+ if (getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != REDIS_OK)
+ return;
+ if (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != REDIS_OK)
+ return;
+ if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptybulk)) == NULL ||
+ checkType(c,o,REDIS_STRING)) return;
+
+ if (o->encoding == REDIS_ENCODING_INT) {
+ str = llbuf;
+ strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
+ } else {
+ str = o->ptr;
+ strlen = sdslen(str);
+ }
+
+ /* Convert negative indexes */
+ if (start < 0) start = strlen+start;
+ if (end < 0) end = strlen+end;
+ if (start < 0) start = 0;
+ if (end < 0) end = 0;
+ if ((unsigned)end >= strlen) end = strlen-1;
+
+ /* Precondition: end >= 0 && end < strlen, so the only condition where
+ * nothing can be returned is: start > end. */
+ if (start > end) {
+ addReply(c,shared.emptybulk);
+ } else {
+ addReplyBulkCBuffer(c,(char*)str+start,end-start+1);