[SDS阅读理解/7]源码中的函数/4

每天记录几个把它记录完吧。还有20多个

       第十九个。该函数的作用是将t指向的那串字符拷贝len长度个到s中。注意是从s的首地址开始,会覆盖原来的内容。

// 作者注释
/* Destructively modify the sds string 's' to hold the specified binary
 * safe string pointed by 't' of length 'len' bytes. */

sds sdscpylen(sds s, const char *t, size_t len) {
    // 调用sdsalloc()函数获取s的总申请空间,判断是否够长度
    // 来储存拷贝的内容。该函数的原理前面章节有讲。
    if (sdsalloc(s) < len) {
        // 如果空间不够,则调用下面这个函数为s增加不够的空间
        // 也就是len - sdslen(s),下面这两个函数的原理前面
        // 章节有讲
        s = sdsMakeRoomFor(s,len-sdslen(s));
        // 申请失败就返回NULL
        if (s == NULL) return NULL;
    }
    // 成功后拷贝内容
    memcpy(s, t, len);
    // 设置结尾符
    s[len] = '\0';
    // 设置s的实际长度,该函数原理前面章节有讲
    sdssetlen(s, len);
    return s;
}

       第二十个。该函数的作用也是拷贝。

// 作者注释
/* Like sdscpylen() but 't' must be a null-termined string so that the length
 * of the string is obtained with strlen(). */

sds sdscpy(sds s, const char *t) {
    // 调用上面讲的函数进行拷贝
    // t必须以0结尾,才能通过strlen()来计算它的长度
    return sdscpylen(s, t, strlen(t));
}

       第二十一个。该函数的作用是将一个long long类型的整数value转换成字符串存放在s中。s指向的空间至少有21字节。函数返回该字符串的长度。

// 作者注释
/* Helper for sdscatlonglong() doing the actual number -> string
 * conversion. 's' must point to a string with room for at least
 * SDS_LLSTR_SIZE bytes.
 *
 * The function returns the length of the null-terminated string
 * representation stored at 's'. */

#define SDS_LLSTR_SIZE 21
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. */
    // 将整数value转成正数,典型的( ? : )运算
    v = (value < 0) ? -value : value;
    // p指向s
    p = s;
    // 通过%和/来截取一个位上的整数,再指针偏移来储存它
    // 弄懂这几个符号的作用就容易理解了
    do {
        *p++ = '0'+(v%10);
        v /= 10;
    } while(v);
    // 如果value是个负数,还得加上‘-’号。注意是放后面,而不是前面
    if (value < 0) *p++ = '-';

    /* Compute length and add null term. */
    // s指向首地址,p指向尾地址,它们两个指针相减就是
    // 它们之间隔多少个字节了。
    l = p-s;
    // 最后面还得加个结尾符
    *p = '\0';

    /* Reverse the string. */
    // 因为转换成字符串后是反过来的,所以得再反过来一次
    // 除去结尾符
    p--;
    // 因为p和s都是指向同一个内存空间的,只是位置不同
    // 如果不懂这里的算法,按着代码画个图就容易理解了
    // 代码本身没有难的,只是过程可能难理解
    while(s < p) {
        aux = *s;
        *s = *p;
        *p = aux;
        s++;
        p--;
    }
    // 返回长度
    return l;
}

       第二十二个。该函数的原理和上一个一样,只是这个是将unsigned longlong类型(非负整数)的整数转换成字符串。代码没变,就不注释了。

// 作者注释
/* 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;
}

       第二十三个。该函数的作用还是将long long类型的整数转换成一个sds字符串。

// 作者注释
/* Create an sds string from a long long value. It is much faster than:
 *
 * sdscatprintf(sdsempty(),"%lld\n", value);
 */

sds sdsfromlonglong(long long value) {
    // 需要用来临时储存字符的变量
    char buf[SDS_LLSTR_SIZE];
    // 调用第二十个函数将value转换成字符串存到buf
    // 并获取它的长度len
    int len = sdsll2str(buf,value);

    // 调用sdsnewlen()这个函数创建一个sds变量
    // 并将buf的内容存过去,然后将这个变量返回给我们
    // 该函数的原理前面章节有讲
    return sdsnewlen(buf,len);
}

       好了,就记录到这先。:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值