C语言字符串处理库cstring

cstring概述

cstring是一个小型且简单的C库,用于定义和操作可扩展C样式的字符串。字符串表示为cstring_t结构的实例, 并由库的函数进行操作。它提供了一个简单的API,由以下函数组成:

cstring_error()cstring_init()cstring_create()cstring_createLen()cstring_createEx()cstring_createLenEx()cstring_destroy()cstring_yield2()cstring_setCapacity()cstring_assign()cstring_assignLen()cstring_copy()cstring_append()cstring_appendLen()cstring_truncate()cstring_swap()cstring_readline() (v3.5 +)cstring_writeline() (v3.5 +)cstring_insert() (v3.5 +)cstring_insertLen() (v3.5 +)cstring_replace() (v3.5 +)cstring_replaceLen() (v3.5 +)cstring_replaceAll() (v3.5 +)

下面的示例创建一个cstring实例,然后将另外两个c样式字符串附加到该实例,将其打印到stdout,将其截断,再次打印,然后将其销毁。

cstring_t str; // create a string from the C-style string "Hello," cstring_create(&str, "Hello,"); // append a space cstring_appendLen(&str, " ", 1); // append "World!" cstring_append(&str, "World!"); // print it out, passing length+ptr to printf printf("cstring contents: %.*s\n", str.len, str.ptr); // truncate it to length 5 cstring_truncate(&str, 5); // print it again, passing the ptr member to puts() puts(str.ptr); // destroy it cstring_destroy(&str);

cstring最新版本 为3.6.2 ,下载地址为http://synesis.com.au/software/cstring。