C的字符串操作接口实现

近期在写一个关于用HTTP控制storm的的UI功能。已经实现完毕,採用在nginx里面增加相应的模块调用来实现,模块中调用一个动态载入的SO。这个SO用THRIFT和zookeeper client实现对storm的控制和信息的存储固化。開始我使用了std::string来做字符串处理。后来发现string实在太多未知的对象构建和内存分配,加上我想把so改用纯C的实现。

std::string自然就须要被替换。在网络上找了非常久相关C语言的string 实现。没有。于是參照《C语言接口与实现》里字符串操作的实现接口,是实现了一个便捷的C字符串处理的接口。接口例如以下:

typedef char* cstring;

/*init and destory*/
cstring		cstr_init();
cstring		cstr_initchar(const char* v, size_t v_size);
void		cstr_destroy(cstring s);

/*get string context*/
char*		cstr_value(cstring s);
/*cstring size*/
size_t		cstr_size(cstring s);
uint8_t		cstr_empty(cstring s);
/*update size by ''*/
void		cstr_update(cstring s);
/*cstring cat*/
cstring		cstr_cat(cstring dst, cstring src);
cstring		cstr_catchar(cstring dst, const char* v, size_t v_size);
/*cstring copy*/
cstring		cstr_cpy(cstring dst, cstring src);
cstring		cstr_cpychar(cstring dst, const char* v, size_t v_size);
/*cstring cmp*/
int32_t		cstr_cmp(cstring s1, cstring s2);
int32_t		cstr_cmpchar(cstring s, const char* v, size_t v_size);
/*cstring lower upper*/
void		cstr_tolower(cstring s);
void		cstr_toupper(cstring s);
/*del cset from cstring*/
cstring		cstr_trim(cstring s, const char* cset);
void		cstr_clear(cstring s);
/*sprintf format*/
cstring		str_printf(cstring s, const char* fmt, ...);
/*number to cstring*/
cstring		itocstr(cstring s, int64_t v);
/*cstring to number*/
int64_t		cstrtoi(cstring s);

/*cstring dump*/
void		cstr_dump(cstring s);
#endif

当中cstring是一个字符串句柄,指向详细的字符串内容。cstring的前面还有8个字节。前面4个字节表示字符串的长度。后面4个字节表示缓冲区还剩余的长度。

其结构例如以下:

/*define string struct*/
typedef struct string_s{
    int32_t len;<span style="white-space:pre">	</span>/*字符串的长度*/
    int32_t free;<span style="white-space:pre">	</span>/*缓冲区还剩余的长度*/
    char buf[];<span style="white-space:pre">		</span>/*cstring的指针地址*/
}string_t;
函数cstr_init()会为string_t开辟一个大于其sizeof大小的内存块。前面8字节用于struct结构信息的存储,后面用于字符串内容存储。

在调用类似cstr_cat函数假设free的内存不够时。接口会自己主动分配一个更大的内存块来取代。这样做的目的是为了调用者不用关心内存溢出的问题。

关于这个接口的所有代码实现。已经放到gti hub上,能够做为C的字符串操作的參考。

git地址https://github.com/yuanrongxi/c_string


原文地址:https://www.cnblogs.com/slgkaifa/p/6823345.html