Add message identities and plan the remaining feedback work
This commit is contained in:
1 parent
8676893d59
commit
d40f58d79a
4 files changed
+44
-3
No files matched your search
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
#include "strbuf.h"
|
||||
#include "mem.h"
|
||||
#include "debug_config.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -69,6 +70,29 @@ void strbuf_addc(struct strbuf *sb, char c) {
|
||||
sb->buf[sb->len] = '\0';
|
||||
}
|
||||
|
||||
/* Экранирует ASCII-управляющие байты; UTF-8 сохраняется без преобразования. */
|
||||
int strbuf_add_json_string(struct strbuf* sb, const char* text) {
|
||||
if (!sb || strbuf_addf(sb, "\"") < 0) goto failed;
|
||||
const unsigned char* p = (const unsigned char*)(text ? text : "");
|
||||
while (*p) {
|
||||
int rc;
|
||||
if (*p == '"' || *p == '\\') rc = strbuf_addf(sb, "\\%c", *p);
|
||||
else if (*p < 32) rc = strbuf_addf(sb, "\\u%04x", *p);
|
||||
else {
|
||||
const unsigned char* start = p;
|
||||
while (*p >= 32 && *p != '"' && *p != '\\') p++;
|
||||
rc = strbuf_addf(sb, "%.*s", (int)(p - start), start);
|
||||
if (rc < 0) goto failed;
|
||||
continue;
|
||||
}
|
||||
if (rc < 0) goto failed;
|
||||
p++;
|
||||
}
|
||||
if (strbuf_addf(sb, "\"") >= 0) return 0;
|
||||
failed:
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "strbuf: JSON string allocation failed"); return -1;
|
||||
}
|
||||
|
||||
void strbuf_adds(struct strbuf *sb, const char *s) {
|
||||
if (!s) return;
|
||||
size_t slen = strlen(s);
|
||||
|
||||
@@ -41,6 +41,9 @@ void strbuf_reset(struct strbuf *sb);
|
||||
int strbuf_addf(struct strbuf *sb, const char *fmt, ...)
|
||||
__attribute__((format(printf, 2, 3)));
|
||||
|
||||
/* Добавить JSON-строку с кавычками и полным экранированием; NULL означает пустую строку. 0/-1. */
|
||||
int strbuf_add_json_string(struct strbuf* sb, const char* text);
|
||||
|
||||
void strbuf_addc(struct strbuf *sb, char c);
|
||||
void strbuf_adds(struct strbuf *sb, const char *s);
|
||||
|
||||
|
||||
Reference in new issue
Block a user