SDS相比传统C语言的字符串有以下好处:

(1)空间预分配和惰性释放,这就可以减少内存重新分配的次数

(2)O(1)的时间复杂度获取字符串的长度

(3)二进制安全

主要总结一下sds.c和sds.h中的关键函数

1、sdsmapchars

 /* Modify the string substituting all the occurrences of the set of
* characters specified in the 'from' string to the corresponding character
* in the 'to' array.
*
* 将字符串 s 中,
* 所有在 from 中出现的字符,替换成 to 中的字符
*
* For instance: sdsmapchars(mystring, "ho", "01", 2)
* will have the effect of turning the string "hello" into "0ell1".
*
* 比如调用 sdsmapchars(mystring, "ho", "01", 2)
* 就会将 "hello" 转换为 "0ell1"
*
* The function returns the sds string pointer, that is always the same
* as the input pointer since no resize is needed.
* 因为无须对 sds 进行大小调整,
* 所以返回的 sds 输入的 sds 一样
*
* T = O(N^2)
*/
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
size_t j, i, l = sdslen(s); // 遍历输入字符串
for (j = ; j < l; j++) {
// 遍历映射
for (i = ; i < setlen; i++) {
// 替换字符串
if (s[j] == from[i]) {
s[j] = to[i];
break;
}
}
}
return s;
}

2、sdstrim

/*
* 对 sds 左右两端进行修剪,清除其中 cset 指定的所有字符
*
* 比如 sdsstrim(xxyyabcyyxy, "xy") 将返回 "abc"
*
* 复杂性:
* T = O(M*N),M 为 SDS 长度, N 为 cset 长度。
*/
/* Remove the part of the string from left and from right composed just of
* contiguous characters found in 'cset', that is a null terminted C string.
*
* After the call, the modified sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call.
*
* Example:
*
* s = sdsnew("AA...AA.a.aa.aHelloWorld :::");
* s = sdstrim(s,"A. :");
* printf("%s\n", s);
*
* Output will be just "Hello World".
*/ /*
惰性空间释放 惰性空间释放用于优化 SDS 的字符串缩短操作: 当 SDS 的 API 需要缩短 SDS 保存的字符串时,
程序并不立即使用内存重分配来回收缩短后多出来的字节,
而是使用 free 属性将这些字节的数量记录起来, 并等待将来使用。 举个例子, sdstrim 函数接受一个 SDS 和一个 C 字符串作为参数,
从 SDS 左右两端分别移除所有在 C 字符串中出现过的字符。
*/
//把释放的字符串字节数添加到free中,凭借free和len就可以有效管理空间 //接受一个 SDS 和一个 C 字符串作为参数, 从 SDS 左右两端分别移除所有在 C 字符串中出现过的字符。 sds sdstrim(sds s, const char *cset) {
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
char *start, *end, *sp, *ep;
size_t len; // 设置和记录指针
sp = start = s;
ep = end = s+sdslen(s)-; // 修剪, T = O(N^2)
/***
* 函数原型:extern char *strchr(char *str,char character)
* 参数说明:str为一个字符串的指针,character为一个待查找字符。
* 所在库名:#include <string.h>
* 函数功能:从字符串str中寻找字符character第一次出现的位置。
* 返回说明:返回指向第一次出现字符character位置的指针,如果没找到则返回NULL。
* 其它说明:还有一种格式char *strchr( const char *string, int c ),这里字符串是以int型给出的。
*
* cset是我们要在首尾trim的字符串 在原始字符串里面的首尾分别取一个字符判断是否在cset中
* 双指针操作去找到最后有效的首尾指针的地址
***/
while(sp <= end && strchr(cset, *sp)) sp++;
while(ep > start && strchr(cset, *ep)) ep--; // 计算 trim 完毕之后剩余的字符串长度
len = (sp > ep) ? : ((ep-sp)+); // 如果有需要,前移字符串内容
// T = O(N)
if (sh->buf != sp) memmove(sh->buf, sp, len); // 添加终结符
sh->buf[len] = '\0'; // 更新属性
sh->free = sh->free+(sh->len-len);
sh->len = len; // 返回修剪后的 sds
return s;
}

3、sdsll2str

 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 < ) ? -value : value;
p = s;
do {
*p++ = ''+(v%);
v /= ;
} while(v);
if (value < ) *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;
}

4、sdssplitlen

 /* Split 's' with separator in 'sep'. An array
* of sds strings is returned. *count will be set
* by reference to the number of tokens returned.
*
* 使用分隔符 sep 对 s 进行分割,返回一个 sds 字符串的数组。
* *count 会被设置为返回数组元素的数量。
*
* On out of memory, zero length string, zero length
* separator, NULL is returned.
*
* 如果出现内存不足、字符串长度为 0 或分隔符长度为 0
* 的情况,返回 NULL
*
* Note that 'sep' is able to split a string using
* a multi-character separator. For example
* sdssplit("foo_-_bar","_-_"); will return two
* elements "foo" and "bar".
*
* 注意分隔符可以的是包含多个字符的字符串
*
* This version of the function is binary-safe but
* requires length arguments. sdssplit() is just the
* same function but for zero-terminated strings.
*
* 这个函数接受 len 参数,因此它是二进制安全的。
* (文档中提到的 sdssplit() 已废弃)
*
* T = O(N^2)
*/
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) {
int elements = , slots = , start = , j;
sds *tokens; if (seplen < || len < ) return NULL; tokens = zmalloc(sizeof(sds)*slots);
if (tokens == NULL) return NULL; if (len == ) {
*count = ;
return tokens;
} // T = O(N^2)
for (j = ; j < (len-(seplen-)); j++) {
/* make sure there is room for the next element and the final one */
if (slots < elements+) {
sds *newtokens; slots *= ;
newtokens = zrealloc(tokens,sizeof(sds)*slots);
if (newtokens == NULL) goto cleanup;
tokens = newtokens;
}
/* search the separator */
// T = O(N)
if ((seplen == && *(s+j) == sep[]) || (memcmp(s+j,sep,seplen) == )) {
tokens[elements] = sdsnewlen(s+start,j-start);
if (tokens[elements] == NULL) goto cleanup;
elements++;
start = j+seplen;
j = j+seplen-; /* skip the separator */
}
}
/* Add the final element. We are sure there is room in the tokens array. */
tokens[elements] = sdsnewlen(s+start,len-start);
if (tokens[elements] == NULL) goto cleanup;
elements++;
*count = elements;
return tokens; cleanup:
{
int i;
for (i = ; i < elements; i++) sdsfree(tokens[i]);
zfree(tokens);
*count = ;
return NULL;
}
}

最新文章

  1. Gamma函数是如何被发现的?
  2. S1的小成果:MyKTV系统
  3. ASP.NET DAY1
  4. android杂记
  5. 用WPF实现查找结果高亮显示
  6. 兼容所有浏览器的设为首页收藏本站js代码
  7. echo&#160;&quot;scale=100;&#160;a(1)*4&quot;&#160;|&#160;bc&#160;-l&#160;输出圆周率
  8. 每天一道LeetCode--169.Majority Elemen
  9. LeeCode-Merge Sorted Array
  10. for循环例子1、2、3
  11. 1596: [Usaco2008 Jan]电话网络
  12. Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码
  13. jackson json转bean忽略没有的字段 not marked as ignorable
  14. linux 乌班图 lnmp环境搭建
  15. springMVC下载中文文件名乱码【原】
  16. 移动端line-height问题
  17. Java基础中的RMI介绍与使用
  18. vertical-align_CSS参考手册_web前端开发参考手册系列
  19. ASP.NET C# List分页
  20. OpenCV——阈值化

热门文章

  1. iOS:图像选取器控制器控件UIImagePickerController的详解
  2. XSS攻击过滤函数
  3. tomcat 部署 RESTful 服务实例
  4. java程序计算数独游戏
  5. ffmpeg对rtmp的基本操作[转]
  6. GitHub页面布局乱了,怎么解决??
  7. 尚学堂 hadoop
  8. iOS UITableView获取特定位置的cell
  9. 微信小程序之下拉刷新,上拉更多列表实现
  10. Leetcode Find Minimum in Rotated Sorted Array 题解