tree-sitter/spec/runtime/helpers/spy_reader.cc

38 lines
962 B
C++
Raw Normal View History

2014-06-23 18:50:03 -07:00
#include "runtime/helpers/spy_reader.h"
#include <algorithm>
using std::string;
static const char * spy_read(void *data, size_t *bytes_read) {
2014-08-06 13:00:35 -07:00
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) {
2014-08-06 13:00:35 -07:00
SpyReader *reader = static_cast<SpyReader *>(data);
reader->strings_read.push_back("");
reader->position = position;
return 0;
}
SpyReader::SpyReader(string content, size_t chunk_size) :
content(content),
position(0),
chunk_size(chunk_size),
input({
2014-04-08 20:34:29 -07:00
this,
spy_read,
spy_seek,
2014-06-09 13:02:39 -07:00
nullptr,
}) {}
void SpyReader::clear() {
strings_read.clear();
}