tree-sitter/spec/runtime/helpers/spy_reader.cc
Max Brunsfeld a3a7546450 In SpyReader, store entire strings read, not individual chunks
Makes assertions easier to write. We don't need to assert
that the strings were read chunk by chunk.
2014-03-18 13:23:00 -07:00

38 lines
1.1 KiB
C++

#include "helpers/spy_reader.h"
#include <algorithm>
using std::string;
static const char * spy_read(void *data, size_t *bytes_read) {
SpyReader *reader = static_cast<SpyReader *>(data);
size_t size = std::min(reader->chunk_size,
reader->content.length() - reader->position);
const char *result = reader->content.data() + reader->position;
reader->strings_read.back() += string(result, size);
reader->position += size;
*bytes_read = size;
return result;
}
static int spy_seek(void *data, size_t position) {
SpyReader *reader = static_cast<SpyReader *>(data);
reader->strings_read.push_back("");
reader->position = position;
return 0;
}
static void spy_release(void *data) {
SpyReader *reader = static_cast<SpyReader *>(data);
delete reader;
}
SpyReader::SpyReader(string content, size_t chunk_size) :
content(content),
position(0),
chunk_size(chunk_size),
input({
.read_fn = spy_read,
.seek_fn = spy_seek,
.release_fn = spy_release,
.data = this
}) {}