RESP3: Double replies and aggregate lengths initial functions.

This commit is contained in:
antirez 2018-11-07 17:40:35 +01:00
parent 511298578a
commit 914ee43108

View File

@ -469,16 +469,15 @@ void setDeferredMultiBulkLength(client *c, void *node, long length) {
/* Add a double as a bulk reply */ /* Add a double as a bulk reply */
void addReplyDouble(client *c, double d) { void addReplyDouble(client *c, double d) {
char dbuf[128], sbuf[128];
int dlen, slen;
if (isinf(d)) { if (isinf(d)) {
/* Libc in odd systems (Hi Solaris!) will format infinite in a /* Libc in odd systems (Hi Solaris!) will format infinite in a
* different way, so better to handle it in an explicit way. */ * different way, so better to handle it in an explicit way. */
addReplyBulkCString(c, d > 0 ? "inf" : "-inf"); addReplyString(c, d > 0 ? ",inf\r\n" : "-inf\r\n",
d > 0 ? 6 : 7);
} else { } else {
dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d); char dbuf[MAX_LONG_DOUBLE_CHARS+3];
slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf); int dlen = snprintf(dbuf,sizeof(dbuf),",%.17g\r\n",d);
addReplyString(c,sbuf,slen); addReplyString(c,dbuf,dlen);
} }
} }
@ -486,9 +485,11 @@ void addReplyDouble(client *c, double d) {
* of the double instead of exposing the crude behavior of doubles to the * of the double instead of exposing the crude behavior of doubles to the
* dear user. */ * dear user. */
void addReplyHumanLongDouble(client *c, long double d) { void addReplyHumanLongDouble(client *c, long double d) {
robj *o = createStringObjectFromLongDouble(d,1); char buf[MAX_LONG_DOUBLE_CHARS];
addReplyBulk(c,o); int len = ld2string(buf,sizeof(buf),d,1);
decrRefCount(o); addReplyString(c,",",1);
addReplyString(c,buf,len);
addReplyString(c,"\r\n",2);
} }
/* Add a long long as integer reply or bulk len / multi bulk count. /* Add a long long as integer reply or bulk len / multi bulk count.
@ -524,11 +525,35 @@ void addReplyLongLong(client *c, long long ll) {
addReplyLongLongWithPrefix(c,ll,':'); addReplyLongLongWithPrefix(c,ll,':');
} }
void addReplyMultiBulkLen(client *c, long length) { void addReplyAggregateLen(client *c, long length, int prefix) {
if (length < OBJ_SHARED_BULKHDR_LEN) if (prefix == '*' && length < OBJ_SHARED_BULKHDR_LEN)
addReply(c,shared.mbulkhdr[length]); addReply(c,shared.mbulkhdr[length]);
else else
addReplyLongLongWithPrefix(c,length,'*'); addReplyLongLongWithPrefix(c,length,prefix);
}
void addReplyArrayLen(client *c, long length) {
addReplyAggregateLen(c,length,'*');
}
void addReplyMapLen(client *c, long length) {
addReplyAggregateLen(c,length,'%');
}
void addReplySetLen(client *c, long length) {
addReplyAggregateLen(c,length,'~');
}
void addReplyAttributeLen(client *c, long length) {
addReplyAggregateLen(c,length,'|');
}
void addReplyPushLen(client *c, long length) {
addReplyAggregateLen(c,length,'>');
}
void addReplyHelloLen(client *c, long length) {
addReplyAggregateLen(c,length,'@');
} }
/* Create the length prefix of a bulk reply, example: $2234 */ /* Create the length prefix of a bulk reply, example: $2234 */