zoukankan      html  css  js  c++  java
  • redis 源码阅读 数值转字符 longlong2str

    redis 在底层中会把long long转成string 再做存储。 主个功能是在sds模块里。
    下面两函数是把long long 转成 char  和   unsiged long long 转成 char。
    大致的思路是:
    1 把数值从尾到头一个一个转成字符,
    2 算出长度,加上结束符。
    3 把字符串反转一下。
    4 如果是 long long 型 要考虑有负数的情况。
     
    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 = '';
        /* 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 = '';
        /* Reverse the string. */
        p--;
        while(s < p) {
            aux = *s;
            *s = *p;
            *p = aux;
            s++;
            p--;
        }
        return l;
    }
  • 相关阅读:
    python迭代器
    初识html
    跨域(jsonp)方法
    闭包
    pycharm软件配置
    插槽slot
    git常用操作
    在mac中使用Charles抓包操作
    防止网页被嵌入框架
    H5唤起APP
  • 原文地址:https://www.cnblogs.com/fangshenghui/p/5700098.html
Copyright © 2011-2022 走看看