Skip to content

Commit 87b8b93

Browse files
authored
Merge pull request tree-sitter#59 from tree-sitter/input-string-with-length
Input string with length
2 parents 819b63e + 93e94bf commit 87b8b93

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

include/tree_sitter/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void ts_document_set_language(TSDocument *, const TSLanguage *);
104104
TSInput ts_document_input(TSDocument *);
105105
void ts_document_set_input(TSDocument *, TSInput);
106106
void ts_document_set_input_string(TSDocument *, const char *);
107+
void ts_document_set_input_string_with_length(TSDocument *, const char *, uint32_t);
107108
TSLogger ts_document_logger(const TSDocument *);
108109
void ts_document_set_logger(TSDocument *, TSLogger);
109110
void ts_document_print_debugging_graphs(TSDocument *, bool);

spec/runtime/document_spec.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ describe("Document", [&]() {
116116
AssertThat(spy_input->strings_read, Equals(vector<string>({" [null, 2" })));
117117
});
118118

119+
it("allows setting input string with length", [&]() {
120+
const char content[] = { '1' };
121+
ts_document_set_input_string_with_length(document, content, 1);
122+
ts_document_parse(document);
123+
TSNode new_root = ts_document_root_node(document);
124+
AssertThat(ts_node_end_char(new_root), Equals<size_t>(1));
125+
assert_node_string_equals(
126+
new_root,
127+
"(number)");
128+
});
129+
119130
it("reads from the new input correctly when the old input was blank", [&]() {
120131
ts_document_set_input_string(document, "");
121132
ts_document_parse(document);

src/runtime/document.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ void ts_document_set_input_string(TSDocument *self, const char *text) {
7777
}
7878
}
7979

80+
void ts_document_set_input_string_with_length(TSDocument *self, const char *text, uint32_t length) {
81+
ts_document_invalidate(self);
82+
TSInput input = ts_string_input_make_with_length(text, length);
83+
ts_document_set_input(self, input);
84+
if (input.payload) {
85+
self->owns_input = true;
86+
}
87+
}
88+
8089
void ts_document_edit(TSDocument *self, TSInputEdit edit) {
8190
if (!self->tree)
8291
return;

src/runtime/string_input.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ int ts_string_input_seek(void *payload, uint32_t character, uint32_t byte) {
2727
}
2828

2929
TSInput ts_string_input_make(const char *string) {
30+
return ts_string_input_make_with_length(string, strlen(string));
31+
}
32+
33+
TSInput ts_string_input_make_with_length(const char *string, uint32_t length) {
3034
TSStringInput *input = ts_malloc(sizeof(TSStringInput));
3135
if (!input)
3236
goto error;
3337

3438
input->string = string;
3539
input->position = 0;
36-
input->length = strlen(string);
40+
input->length = length;
3741
return (TSInput){
3842
.payload = input,
3943
.read = ts_string_input_read,

src/runtime/string_input.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
#include "tree_sitter/runtime.h"
99

1010
TSInput ts_string_input_make(const char *);
11+
TSInput ts_string_input_make_with_length(const char *, uint32_t);
1112

1213
#ifdef __cplusplus
1314
}

0 commit comments

Comments
 (0)