{"id":230611,"date":"2021-12-02T08:54:01","date_gmt":"2021-12-02T00:54:01","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=230611"},"modified":"2021-11-24T09:54:32","modified_gmt":"2021-11-24T01:54:32","slug":"c-dynamic-string","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/c-dynamic-string.html","title":{"rendered":"\u7528C\u5b9e\u73b0\u52a8\u6001\u6269\u5bb9\u7684string"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4f17\u6240\u5468\u77e5\uff0cC++ \u4e2d\u7684string\u4f7f\u7528\u6bd4\u8f83\u65b9\u4fbf\uff0c\u5173\u4e8eC++ \u4e2d\u7684string\u6e90\u7801\u5b9e\u73b0\u53ef\u4ee5\u770b\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a\u6e90\u7801\u5206\u6790C++\u7684string\u7684\u5b9e\u73b0
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4f17\u6240\u5468\u77e5\uff0cC++ \u4e2d\u7684string\u4f7f\u7528\u6bd4\u8f83\u65b9\u4fbf\uff0c\u5173\u4e8eC++ \u4e2d\u7684string\u6e90\u7801\u5b9e\u73b0\u53ef\u4ee5\u770b\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a\u6e90\u7801\u5206\u6790C++\u7684string\u7684\u5b9e\u73b0<\/p>\n

\u6700\u8fd1\u5de5\u4f5c\u4e2d\u4f7f\u7528C\u8bed\u8a00\uff0c\u4f46\u53c8\u82e6\u4e8e\u6ca1\u6709\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\uff0c\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u548c\u88c1\u526a\u90fd\u6bd4\u8f83\u9ebb\u70e6\uff0c\u800c\u4e14\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u9700\u8981\u7533\u8bf7\u5185\u5b58\uff0c\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u4e5f\u5f88\u5bb9\u6613\u51fabug\uff0c\u600e\u4e48\u9ad8\u6548\u7684\u5b9e\u73b0\u4e00\u4e2a\u4e0d\u9700\u8981\u5904\u7406\u5185\u5b58\u95ee\u9898\u5e76\u4e14\u53ef\u4ee5\u52a8\u6001\u6269\u5bb9\u8fdb\u884c\u62fc\u63a5\u548c\u88c1\u526a\u7684string\u5462\uff1f<\/p>\n

\u4e00\u4e2a\u597d\u7684string\u5e94\u8be5\u6709\u4ee5\u4e0b\u529f\u80fd\uff1f<\/p>\n

  • \u521b\u5efa\u5b57\u7b26\u4e32<\/li>\n
  • \u5220\u9664\u5b57\u7b26\u4e32<\/li>\n
  • \u5c3e\u90e8\u8ffd\u52a0\u5b57\u7b26\u4e32<\/li>\n
  • \u5934\u90e8\u63d2\u5165\u5b57\u7b26\u4e32<\/li>\n
  • \u4ece\u5c3e\u90e8\u5220\u9664N\u4e2a\u5b57\u7b26<\/li>\n
  • \u4ece\u5934\u90e8\u5220\u9664N\u4e2a\u5b57\u7b26<\/li>\n
  • \u88c1\u526a\u5b57\u7b26\u4e32<\/li>\n
  • \u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6<\/li>\n
  • \u83b7\u53d6\u5b8c\u6574\u5b57\u7b26\u4e32<\/li>\n

    \u4e0b\u9762\u6765\u770b\u770b\u5404\u4e2a\u529f\u80fd\u7684\u5b9e\u73b0\uff1a<\/p>\n

    \u9996\u5148\u5b9a\u4e49\u4e00\u4e2astring\u7684\u53e5\u67c4\uff0c\u76f8\u5f53\u4e8eC++\u4e2d\u7684\u5b9e\u4f8b<\/p>\n

    struct c_string;  \r\ntypedef struct c_string c_string_t; <\/pre>\n

    \u5728\u5185\u90e8string\u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n

    \r\n\/\/ string\u7684\u521d\u59cb\u5185\u5b58\u5927\u5c0f  \r\nstatic const size_t c_string_min_size = 32;  \r\nstruct c_string {  \r\n    char *str; \/\/ \u5b57\u7b26\u4e32\u6307\u9488  \r\n    size_t alloced; \/\/ \u5df2\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f  \r\n    size_t len; \/\/ \u5b57\u7b26\u4e32\u7684\u5b9e\u9645\u957f\u5ea6  \r\n}; <\/pre>\n

    \u521b\u5efa\u5b57\u7b26\u4e32\uff1a<\/p>\n

    \r\nc_string_t *c_string_create(void) {  \r\n    c_string_t *cs;  \r\n    cs = calloc(1, sizeof(*cs));  \r\n    cs->str = malloc(c_string_min_size);  \r\n    *cs->str = '\\0';  \r\n    \/\/ \u521d\u59cb\u5206\u914d\u5185\u5b58\u5927\u5c0f\u662f32\uff0c\u4e4b\u540e\u6bcf\u6b21\u4ee52\u500d\u5927\u5c0f\u6269\u5bb9  \r\n    cs->alloced = c_string_min_size;   \r\n    cs->len = 0; \r\n    return cs;  \r\n} <\/pre>\n

    \u9500\u6bc1\u5b57\u7b26\u4e32\uff1a<\/p>\n

    \r\nvoid c_string_destroy(c_string_t *cs) {  \r\n    if (cs == NULL) return;  \r\n    free(cs->str);  \r\n    free(cs);  \r\n} <\/pre>\n

    \u5185\u90e8\u5982\u4f55\u6269\u5bb9\u5462\uff1a<\/p>\n

    \r\nstatic void c_string_ensure_space(c_string_t *cs, size_t add_len) {  \r\n    if (cs == NULL || add_len == 0) return;  \r\n    if (cs->alloced >= cs->len + add_len + 1) return;  \r\n    while (cs->alloced < cs->len + add_len + 1) {  \r\n        cs->alloced <<= 1; \/\/ \u6bcf\u6b21\u4ee52\u500d\u5927\u5c0f\u6269\u5bb9  \r\n        if (cs->alloced == 0) {  \r\n            \/\/ \u5de6\u79fb\u5230\u6700\u540e\u53ef\u80fd\u4f1a\u53d8\u4e3a0\uff0c\u7531\u4e8ealloced\u662f\u65e0\u7b26\u53f7\u578b\uff0c\u51cf\u4e00\u5219\u4f1a\u53d8\u6210UINT_MAX  \r\n            cs->alloced--;  \r\n        }  \r\n    }  \r\n    cs->str = realloc(cs->str, cs->alloced);  \r\n} <\/pre>\n

    \u5728\u5c3e\u90e8\u8ffd\u52a0\u5b57\u7b26\u4e32\uff1a<\/p>\n

    \r\nvoid c_string_append_str(c_string_t *cs, const char *str, size_t len) {  \r\n    if (cs == NULL || str == NULL || *str == '\\0') return;  \r\n    if (len == 0) len = strlen(str);  \r\n    c_string_ensure_space(cs, len); \/\/ \u786e\u4fdd\u5185\u90e8\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u5b58\u50a8\u5b57\u7b26\u4e32  \r\n    memmove(cs->str + cs->len, str, len);  \r\n    cs->len += len;  \r\n    cs->str[cs->len] = '\\0';  \r\n} <\/pre>\n

    \u5728\u5c3e\u90e8\u8ffd\u52a0\u5b57\u7b26\uff1a<\/p>\n

    \r\nvoid c_string_append_char(c_string_t *cs, char c) {  \r\n    if (cs == NULL) return;  \r\n    c_string_ensure_space(cs, 1);  \r\n    cs->str[cs->len] = c;  \r\n    cs->len++;  \r\n    cs->str[cs->len] = '\\0';  \r\n} <\/pre>\n

    \u5728\u5c3e\u90e8\u8ffd\u52a0\u6574\u6570\uff1a<\/p>\n

    \r\nvoid c_string_append_int(c_string_t *cs, int val) {  \r\n    char str[12];  \r\n    if (cs == NULL) return;  \r\n    snprintf(str, sizeof(str), \"%d\", val); \/\/ \u6574\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32  \r\n    c_string_append_str(cs, str, 0);  \r\n} <\/pre>\n

    \u5728\u5934\u90e8\u63d2\u5165\u5b57\u7b26\u4e32\uff1a<\/p>\n

    \r\nvoid c_string_front_str(c_string_t *cs, const char *str, size_t len) {  \r\n    if (cs == NULL || str == NULL || *str == '\\0') return;  \r\n    if (len == 0) len = strlen(str);  \r\n    c_string_ensure_space(cs, len);  \r\n    memmove(cs->str + len, cs->str, cs->len);  \r\n    memmove(cs->str, str, len); \r\n    cs->len += len;  \r\n    cs->str[cs->len] = '\\0';  \r\n} <\/pre>\n

    \u5728\u5934\u90e8\u63d2\u5165\u5b57\u7b26\uff1a<\/p>\n

    \r\nvoid c_string_front_char(c_string_t *cs, char c) {  \r\n    if (cs == NULL) return;  \r\n    c_string_ensure_space(cs, 1);  \r\n    memmove(cs->str + 1, cs->str, cs->len);  \r\n    cs->str[0] = c;  \r\n    cs->len++;  \r\n    cs->str[cs->len] = '\\0';  \r\n} <\/pre>\n

    \u5728\u5934\u90e8\u63d2\u5165\u6574\u6570\uff1a<\/p>\n

    \r\nvoid c_string_front_int(c_string_t *cs, int val) {  \r\n    char str[12];  \r\n    if (cs == NULL) return;  \r\n    snprintf(str, sizeof(str), \"%d\", val);  \r\n    c_string_front_str(cs, str, 0);  \r\n} <\/pre>\n

    \u6e05\u7a7a\u5b57\u7b26\u4e32\uff1a<\/p>\n

    \r\nvoid c_string_clear(c_string_t *cs) {  \r\n    if (cs == NULL) return;  \r\n    c_string_truncate(cs, 0);  \r\n} <\/pre>\n

    \u88c1\u526a\u5b57\u7b26\u4e32\uff1a<\/p>\n

    \r\nvoid c_string_truncate(c_string_t *cs, size_t len) {  \r\n    if (cs == NULL || len >= cs->len) return;  \r\n    cs->lenlen = len;  \r\n    cs->str[cs->len] = '\\0';  \r\n} <\/pre>\n

    \u5220\u9664\u5934\u90e8\u7684N\u4e2a\u5b57\u7b26\uff1a<\/p>\n

    \r\nvoid c_string_drop_begin(c_string_t *cs, size_t len) {  \r\n    if (cs == NULL || len == 0) return;  \r\n    if (len >= cs->len) {  \r\n        c_string_clear(cs);  \r\n        return;  \r\n    }  \r\n    cs->len -= len;  \r\n    memmove(cs->str, cs->str + len, cs->len + 1);  \r\n} <\/pre>\n

    \u5220\u9664\u5c3e\u90e8\u7684N\u4e2a\u5b57\u7b26\uff1a<\/p>\n

    \r\nvoid c_string_drop_end(c_string_t *cs, size_t len) {  \r\n    if (cs == NULL || len == 0) return;  \r\n    if (len >= cs->len) {  \r\n        c_string_clear(cs);  \r\n        return;  \r\n    }  \r\n    cs->len -= len;  \r\n    cs->str[cs->len] = '\\0';  \r\n} <\/pre>\n

    \u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff1a<\/p>\n

    \r\nsize_t c_string_len(const c_string_t *cs) {  \r\n    if (cs == NULL) return 0;  \r\n    return cs->len;  \r\n} <\/pre>\n

    \u8fd4\u56de\u5b57\u7b26\u4e32\u6307\u9488\uff0c\u4f7f\u7528\u7684\u662f\u5185\u90e8\u7684\u5185\u5b58\uff1a<\/p>\n

    \r\nconst char *c_string_peek(const c_string_t *cs) {  \r\n    if (cs == NULL) return NULL;  \r\n    return cs->str;  \r\n} <\/pre>\n

    \u91cd\u65b0\u5206\u914d\u4e00\u5757\u5185\u5b58\u5b58\u50a8\u5b57\u7b26\u4e32\u8fd4\u56de\uff1a<\/p>\n

    \r\nchar *c_string_dump(const c_string_t *cs, size_t *len) {  \r\n    char *out;  \r\n    if (cs == NULL) return NULL;  \r\n    if (len != NULL) *len = cs->len;  \r\n    out = malloc(cs->len + 1);  \r\n    memcpy(out, cs->str, cs->len + 1);  \r\n    return out; \r\n }<\/pre>\n

    \u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

    \r\nint main() {  \r\n    c_string_t *cs = c_string_create();  \r\n    c_string_append_str(cs, \"123\", 0);  \r\n    c_string_append_char(cs, '4');  \r\n    c_string_append_int(cs, 5);  \r\n    printf(\"%s \\n\", c_string_peek(cs));  \r\n    c_string_front_str(cs, \"789\", 0);  \r\n    printf(\"%s \\n\", c_string_peek(cs));  \r\n    c_string_drop_begin(cs, 2);  \r\n    printf(\"%s \\n\", c_string_peek(cs));  \r\n    c_string_drop_end(cs, 2);  \r\n    printf(\"%s \\n\", c_string_peek(cs));  \r\n    c_string_destroy(cs);  \r\n    return 0;  \r\n} \r\n\u8f93\u51fa\uff1a\r\n\r\n12345  \r\n78912345  \r\n912345  \r\n9123 \r\n\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a\u5934\u6587\u4ef6\uff1a\r\n\r\n#include   \r\nstruct c_string;  \r\ntypedef struct c_string c_string_t;  \r\nc_string_t *c_string_create(void);  \r\nvoid c_string_destroy(c_string_t *cs);  \r\nvoid c_string_append_str(c_string_t *cs, const char *str, size_t len);  \r\nvoid c_string_append_char(c_string_t *cs, char c);  \r\nvoid c_string_append_int(c_string_t *cs, int val);  \r\nvoid c_string_front_str(c_string_t *cs, const char *str, size_t len);  \r\nvoid c_string_front_char(c_string_t *cs, char c);  \r\nvoid c_string_front_int(c_string_t *cs, int val);  \r\nvoid c_string_clear(c_string_t *cs);  \r\nvoid c_string_truncate(c_string_t *cs, size_t len);  \r\nvoid c_string_drop_begin(c_string_t *cs, size_t len);  \r\nvoid c_string_drop_end(c_string_t *cs, size_t len); \r\nsize_t c_string_len(const c_string_t *cs);  \r\nconst char *c_string_peek(const c_string_t *cs);  \r\nchar *c_string_dump(const c_string_t *cs, size_t *len); <\/pre>\n

    \u6e90\u6587\u4ef6\uff1a<\/p>\n

    \r\n#include   \r\n#include   \r\n#include   \r\n#include   \r\n#include   \r\nstatic const size_t c_string_min_size = 32; \r\nstruct c_string {  \r\n    char *str;  \r\n    size_t alloced;  \r\n    size_t len; \r\n};  \r\nc_string_t *c_string_create(void) {  \r\n    c_string_t *cs;  \r\n    cs = calloc(1, sizeof(*cs));  \r\n    cs->str = malloc(c_string_min_size);  \r\n    *cs->str = '\\0';  \r\n    cs->alloced = c_string_min_size;  \r\n    cs->len = 0;  \r\n    return cs;  \r\n}  \r\nvoid c_string_destroy(c_string_t *cs) {  \r\n    if (cs == NULL) return;  \r\n    free(cs->str);  \r\n    free(cs);  \r\n}  \r\nstatic void c_string_ensure_space(c_string_t *cs, size_t add_len) {  \r\n    if (cs == NULL || add_len == 0) return;  \r\n    if (cs->alloced >= cs->len + add_len + 1) return;  \r\n    while (cs->alloced < cs->len + add_len + 1) {  \r\n        cs->alloced <<= 1;  \r\n        if (cs->alloced == 0) {  \r\n            cs->alloced--;  \r\n        }  \r\n    }  \r\n    cs->str = realloc(cs->str, cs->alloced);  \r\n}  \r\nvoid c_string_append_str(c_string_t *cs, const char *str, size_t len) {  \r\n    if (cs == NULL || str == NULL || *str == '\\0') return;  \r\n    if (len == 0) len = strlen(str);  \r\n    c_string_ensure_space(cs, len);  \r\n    memmove(cs->str + cs->len, str, len);  \r\n    cs->len += len;  \r\n    cs->str[cs->len] = '\\0';  \r\n}  \r\nvoid c_string_append_char(c_string_t *cs, char c) {  \r\n    if (cs == NULL) return;  \r\n    c_string_ensure_space(cs, 1);  \r\n    cs->str[cs->len] = c;  \r\n    cs->len++;  \r\n    cs->str[cs->len] = '\\0';  \r\n}  \r\nvoid c_string_append_int(c_string_t *cs, int val) { \r\n    char str[12];  \r\n    if (cs == NULL) return;  \r\n    snprintf(str, sizeof(str), \"%d\", val);  \r\n    c_string_append_str(cs, str, 0);  \r\n}  \r\nvoid c_string_front_str(c_string_t *cs, const char *str, size_t len) {  \r\n    if (cs == NULL || str == NULL || *str == '\\0') return;  \r\n    if (len == 0) len = strlen(str);  \r\n    c_string_ensure_space(cs, len);  \r\n    memmove(cs->str + len, cs->str, cs->len);  \r\n    memmove(cs->str, str, len);  \r\n    cs->len += len;  \r\n    cs->str[cs->len] = '\\0';  \r\n}  \r\nvoid c_string_front_char(c_string_t *cs, char c) {  \r\n    if (cs == NULL) return;  \r\n    c_string_ensure_space(cs, 1);  \r\n    memmove(cs->str + 1, cs->str, cs->len);  \r\n    cs->str[0] = c;  \r\n    cs->len++;  \r\n    cs->str[cs->len] = '\\0';  \r\n}  \r\nvoid c_string_front_int(c_string_t *cs, int val) {  \r\n    char str[12]; \r\n    if (cs == NULL) return;  \r\n    snprintf(str, sizeof(str), \"%d\", val);  \r\n    c_string_front_str(cs, str, 0);  \r\n}  \r\nvoid c_string_clear(c_string_t *cs) {  \r\n    if (cs == NULL) return;  \r\n    c_string_truncate(cs, 0);  \r\n}  \r\nvoid c_string_truncate(c_string_t *cs, size_t len) {  \r\n    if (cs == NULL || len >= cs->len) return;  \r\n    cs->lenlen = len;  \r\n    cs->str[cs->len] = '\\0';  \r\n}  \r\nvoid c_string_drop_begin(c_string_t *cs, size_t len) {  \r\n    if (cs == NULL || len == 0) return;  \r\n    if (len >= cs->len) {  \r\n        c_string_clear(cs);  \r\n        return;  \r\n    }  \r\n    cs->len -= len;  \r\n    \/* +1 to move the NULL. *\/  \r\n    memmove(cs->str, cs->str + len, cs->len + 1);  \r\n}  \r\nvoid c_string_drop_end(c_string_t *cs, size_t len) {  \r\n    if (cs == NULL || len == 0) return;   \r\n    if (len >= cs->len) {  \r\n        c_string_clear(cs);  \r\n        return;  \r\n    }  \r\n    cs->len -= len;  \r\n    cs->str[cs->len] = '\\0';  \r\n}  \r\nsize_t c_string_len(const c_string_t *cs) {  \r\n    if (cs == NULL) return 0;  \r\n    return cs->len;  \r\n}  \r\nconst char *c_string_peek(const c_string_t *cs) {  \r\n    if (cs == NULL) return NULL;  \r\n    return cs->str;  \r\n}  \r\nchar *c_string_dump(const c_string_t *cs, size_t *len) {  \r\n    char *out;  \r\n    if (cs == NULL) return NULL;  \r\n    if (len != NULL) *len = cs->len;  \r\n    out = malloc(cs->len + 1);  \r\n    memcpy(out, cs->str, cs->len + 1);  \r\n    return out;  \r\n}  \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u4f17\u6240\u5468\u77e5\uff0cC++ \u4e2d\u7684string\u4f7f\u7528\u6bd4\u8f83\u65b9\u4fbf\uff0c\u5173\u4e8eC++ \u4e2d\u7684string\u6e90\u7801\u5b9e\u73b0\u53ef\u4ee5\u770b\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a\u6e90\u7801\u5206 […]<\/p>\n","protected":false},"author":317,"featured_media":153461,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-230611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/230611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=230611"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/230611\/revisions"}],"predecessor-version":[{"id":230614,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/230611\/revisions\/230614"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/153461"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=230611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=230611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=230611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}