summaryrefslogtreecommitdiff
path: root/lsp-write-request.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-22 16:38:32 -0500
committerpommicket <pommicket@gmail.com>2022-12-22 16:38:32 -0500
commit23e138019964662b4e847770eeda2bb5d3130f7a (patch)
treec92d36b878d26594cab5a35110dff6ad546d6a3d /lsp-write-request.c
parent6bb4da5fab94d2ed3d093b996674fd1cc28eda2f (diff)
better write_string
Diffstat (limited to 'lsp-write-request.c')
-rw-r--r--lsp-write-request.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/lsp-write-request.c b/lsp-write-request.c
index e5d671d..f375559 100644
--- a/lsp-write-request.c
+++ b/lsp-write-request.c
@@ -74,11 +74,23 @@ static void write_arr_elem(JSONWriter *o) {
}
}
-static void write_string(JSONWriter *o, const char* string) {
- // @TODO: escape in-place
- char *escaped = json_escape(string);
- str_builder_appendf(&o->builder, "\"%s\"", escaped);
- free(escaped);
+static void write_escaped(JSONWriter *o, const char *string) {
+ StrBuilder *b = &o->builder;
+ size_t output_index = str_builder_len(b);
+ size_t capacity = 2 * strlen(string) + 1;
+ // append a bunch of null bytes which will hold the escaped string
+ str_builder_append_null(b, capacity);
+ char *out = str_builder_get_ptr(b, output_index);
+ // do the escaping
+ size_t length = json_escape_to(out, capacity, string);
+ // shrink down to just the escaped text
+ str_builder_shrink(&o->builder, output_index + length);
+}
+
+static void write_string(JSONWriter *o, const char *string) {
+ str_builder_append(&o->builder, "\"");
+ write_escaped(o, string);
+ str_builder_append(&o->builder, "\"");
}
static void write_key(JSONWriter *o, const char *key) {
@@ -121,10 +133,10 @@ static void write_key_string(JSONWriter *o, const char *key, const char *s) {
}
static void write_file_uri(JSONWriter *o, DocumentID document) {
- // @OPTIM : could store escaped document paths instead of document paths
- char *escaped_path = json_escape(o->lsp->document_paths[document]);
- str_builder_appendf(&o->builder, "\"file:///%s\"", escaped_path);
- free(escaped_path);
+ const char *path = o->lsp->document_paths[document];
+ str_builder_append(&o->builder, "\"file:///");
+ write_escaped(o, path);
+ str_builder_append(&o->builder, "\"");
}
static void write_key_file_uri(JSONWriter *o, const char *key, DocumentID document) {