Move StringInput into its own file
This commit is contained in:
parent
41d26aaceb
commit
03a5a97992
4 changed files with 55 additions and 36 deletions
37
src/runtime/string_input.c
Normal file
37
src/runtime/string_input.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include "runtime/string_input.h"
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
const char *string;
|
||||
size_t position;
|
||||
size_t length;
|
||||
} TSStringInput;
|
||||
|
||||
const char *ts_string_input_read(void *d, size_t *bytes_read) {
|
||||
TSStringInput *data = (TSStringInput *)d;
|
||||
if (data->position >= data->length) {
|
||||
*bytes_read = 0;
|
||||
return "";
|
||||
}
|
||||
size_t previous_position = data->position;
|
||||
data->position = data->length;
|
||||
*bytes_read = data->position - previous_position;
|
||||
return data->string + previous_position;
|
||||
}
|
||||
|
||||
int ts_string_input_seek(void *d, size_t position) {
|
||||
TSStringInput *data = (TSStringInput *)d;
|
||||
data->position = position;
|
||||
return (position < data->length);
|
||||
}
|
||||
|
||||
TSInput ts_string_input_make(const char *string) {
|
||||
TSStringInput *data = malloc(sizeof(TSStringInput));
|
||||
data->string = string;
|
||||
data->position = 0;
|
||||
data->length = strlen(string);
|
||||
return (TSInput) { .data = (void *)data,
|
||||
.read_fn = ts_string_input_read,
|
||||
.seek_fn = ts_string_input_seek,
|
||||
.release_fn = free };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue