SDS 模块构建了一个字符串对象抽象, 这个抽象字符串具有以下特性:
如果你的程序需要以上这些特性, 那么可以考虑重用 Redis 的这个 SDS 模块。
SDS 模块的具体定义,以及操作 API ,可以参考 《Redis 设计与实现》 中的相关章节。
sds.h
、 sds.c
、 zmalloc.h
和 zmalloc.c
四个文件到新建文件夹。#include <stddef.h>
到 zamlloc.h
,解决 size_t
未定义的问题。zamlloc.c
中移除 #include "config.h"
,因为现在已经不需要配置文件了。以下驱动程序展示了如何使用 SDS 模块,
并测试了其中的 sdsnew
、 sdslen
、 sdsavail
、 sdsdup
和 sdscat
等函数。
// main.c
#include <assert.h>
#include <string.h>
#include "sds.h"
void create_sds_and_check_its_property(void)
{
sds s = sdsnew("hello");
// 验证长度
assert(
sdslen(s) == 5
);
// 验证空白位置
assert(
sdsavail(s) == 0
);
// 验证已有内容
assert(
memcmp(s, "hello\0", 6) == 0
);
// 释放
sdsfree(s);
}
void test_sdsdup(void)
{
sds s = sdsnew("hello");
// 创建 s 的一个副本
sds another = sdsdup(s);
// 长度对比
assert(
sdslen(s) == sdslen(another)
);
// 空间对比
assert(
sdsavail(s) == sdsavail(another)
);
// 内容对比
assert(
memcmp(s, another, 6) == 0
);
// 释放
sdsfree(s);
sdsfree(another);
}
void test_sdscat(void)
{
const char const total[] = "hello moto\0";
sds s = sdsnew("hello");
// 追加内容
const char const append[] = " moto";
sdscat(s, append);
// 长度对比
assert(
sdslen(s) == strlen(total)
);
// 空间对比
assert(
// 追加之后的字符串
// 会预留大小相当于现有字符串长度的空间
sdsavail(s) == sdslen(s)
);
// 内容对比
assert(
memcmp(s, total, strlen(total)+1) == 0
);
// 释放空间
sdsfree(s);
}
int main(void)
{
create_sds_and_check_its_property();
test_sdsdup();
test_sdscat();
}