Redis source code reading numeric to longlong2str, redislonglong2str

Source: Internet
Author: User

Redis source code reading numeric to longlong2str, redislonglong2str
Redis converts long to string at the underlying layer for storage. The main function is in the sds module. The following two functions convert long to char and unsiged long to char. The general idea is: 1. Convert the value from the end to the first to a character, 2 to calculate the length, and add the Terminator. 3. Reverse the string. 4 if the long type is used, a negative number should be considered.

int sdsll2str(char *s, long long value) {    char *p, aux;    unsigned long long v;    size_t l;    /* Generate the string representation, this method produces     * an reversed string. */    v = (value < 0) ? -value : value;    p = s;    do {        *p++ = '0'+(v%10);        v /= 10;    } while(v);    if (value < 0) *p++ = '-';    /* Compute length and add null term. */    l = p-s;    *p = '\0';    /* Reverse the string. */    p--;    while(s < p) {        aux = *s;        *s = *p;        *p = aux;        s++;        p--;    }    return l;}/* Identical sdsll2str(), but for unsigned long long type. */int sdsull2str(char *s, unsigned long long v) {    char *p, aux;    size_t l;    /* Generate the string representation, this method produces     * an reversed string. */    p = s;    do {        *p++ = '0'+(v%10);        v /= 10;    } while(v);    /* Compute length and add null term. */    l = p-s;    *p = '\0';    /* Reverse the string. */    p--;    while(s < p) {        aux = *s;        *s = *p;        *p = aux;        s++;        p--;    }    return l;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.