diff --git a/spec/runtime/helpers/spy_reader.cc b/spec/runtime/helpers/spy_reader.cc index 2a86d23f..e7f38f49 100644 --- a/spec/runtime/helpers/spy_reader.cc +++ b/spec/runtime/helpers/spy_reader.cc @@ -5,23 +5,23 @@ using std::string; static const char * spy_read(void *data, size_t *bytes_read) { SpyReader *reader = static_cast(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; + string result = reader->content.substr(reader->position, reader->chunk_size); + reader->position += result.size(); + reader->strings_read.back() += result; + *bytes_read = result.size(); + memcpy(reader->buffer, result.data(), result.size()); + return reader->buffer; } -static int spy_seek(void *data, size_t position) { +static int spy_seek(void *data, TSLength position) { SpyReader *reader = static_cast(data); reader->strings_read.push_back(""); - reader->position = position; + reader->position = position.bytes; return 0; } SpyReader::SpyReader(string content, size_t chunk_size) : + buffer(new char[chunk_size]), content(content), position(0), chunk_size(chunk_size), @@ -35,3 +35,7 @@ SpyReader::SpyReader(string content, size_t chunk_size) : void SpyReader::clear() { strings_read.clear(); } + +SpyReader::~SpyReader() { + delete buffer; +} diff --git a/spec/runtime/helpers/spy_reader.h b/spec/runtime/helpers/spy_reader.h index 375c70f9..c3578f3a 100644 --- a/spec/runtime/helpers/spy_reader.h +++ b/spec/runtime/helpers/spy_reader.h @@ -8,9 +8,11 @@ class SpyReader { public: SpyReader(std::string content, size_t chunk_size); + ~SpyReader(); void clear(); + char *buffer; std::string content; size_t position; size_t chunk_size;