[SDS阅读理解/7]源码中的函数/4
来源:互联网 发布:linux配置网卡mac 编辑:程序博客网 时间:2024/05/18 00:48
每天记录几个把它记录完吧。还有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 21int 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
- [SDS阅读理解/7]源码中的函数/4
- [SDS阅读理解/4]源码中的函数/1
- [SDS阅读理解/3]源码中的函数/0
- [SDS阅读理解/5]源码中的函数/2
- [SDS阅读理解/6]源码中的函数/3
- [SDS阅读理解/8]源码中的函数/5
- [SDS阅读理解/9]源码中的函数/6
- [SDS阅读理解/1]源码中的宏
- [SDS阅读理解/2]源码中的数据结构
- [SDS阅读理解/0]前言
- redis源码阅读笔记(一):sds
- Redis源码阅读笔记—sds
- Redis源码阅读笔记(1)-- 动态字符串sds
- ThinkPHP源码阅读理解
- jQuery源码阅读(九)---ready函数理解
- redis 源码笔记--sds
- Redis源码解析【SDS】
- jquery源码阅读知识储备(7)JS函数中的argument属性
- 开始踏上通往PARADISE的不归路
- HDFS及NameNode元数据管理机制
- 计算机视觉: 物体分类,场景分类,事件分类
- leetcode 11 Container With Most Water
- 10686 DeathGod不知道的事情(by 201727010224)
- [SDS阅读理解/7]源码中的函数/4
- 判断一个数是否为一个素数
- 重定向和管道
- Windows系统80端口被System占用
- 各类排序算法的比较
- 快学Scala-元祖、集合
- 二叉树的遍历
- YOLO源码详解(一)-训练
- Linux去重 排序 计数 分隔 命令 awk sort wc uniq