- while (ln && limit--) {
- /* Abort when the node is no longer in range. */
- if (reverse) {
- if (!zslValueGteMin(ln->score,&range)) break;
- } else {
- if (!zslValueLteMax(ln->score,&range)) break;
+ /* We don't know in advance how many matching elements there are in the
+ * list, so we push this object that will represent the multi-bulk
+ * length in the output buffer, and will "fix" it later */
+ if (!justcount)
+ replylen = addDeferredMultiBulkLength(c);
+
+ /* If there is an offset, just traverse the number of elements without
+ * checking the score because that is done in the next loop. */
+ while (eptr && offset--)
+ if (reverse)
+ zzlPrev(zl,&eptr,&sptr);
+ else
+ zzlNext(zl,&eptr,&sptr);
+
+ while (eptr && limit--) {
+ score = zzlGetScore(sptr);
+
+ /* Abort when the node is no longer in range. */
+ if (reverse) {
+ if (!zslValueGteMin(score,&range)) break;
+ } else {
+ if (!zslValueLteMax(score,&range)) break;
+ }
+
+ /* Do our magic */
+ rangelen++;
+ if (!justcount) {
+ redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong));
+ if (vstr == NULL)
+ addReplyBulkLongLong(c,vlong);
+ else
+ addReplyBulkCBuffer(c,vstr,vlen);
+
+ if (withscores)
+ addReplyDouble(c,score);
+ }
+
+ /* Move to next node */
+ if (reverse)
+ zzlPrev(zl,&eptr,&sptr);
+ else
+ zzlNext(zl,&eptr,&sptr);