From 511e52ab554dc2e3a4f85966f1b6d8fb60053d2e Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sat, 19 Sep 2015 13:29:30 -0700 Subject: [PATCH] Clean up SpyInput test helper --- spec/runtime/helpers/spy_input.cc | 59 ++++++++++++++----------------- spec/runtime/helpers/spy_input.h | 7 ++-- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/spec/runtime/helpers/spy_input.cc b/spec/runtime/helpers/spy_input.cc index f2f6122b..b78a171e 100644 --- a/spec/runtime/helpers/spy_input.cc +++ b/spec/runtime/helpers/spy_input.cc @@ -5,6 +5,8 @@ using std::string; +static const size_t UTF8_MAX_CHAR_SIZE = 4; + static long byte_for_character(const char *str, size_t len, size_t goal_character) { size_t character = 0, byte = 0; int32_t dest_char; @@ -22,42 +24,34 @@ static long byte_for_character(const char *str, size_t len, size_t goal_characte return byte; } -static const char * spy_read(void *data, size_t *bytes_read) { - SpyInput *reader = static_cast(data); - return reader->read(bytes_read); -} - -static int spy_seek(void *data, TSLength byte_offset) { - SpyInput *reader = static_cast(data); - return reader->seek(byte_offset.bytes); -} - SpyInput::SpyInput(string content, size_t chars_per_chunk) : - chars_per_chunk(chars_per_chunk), - buffer_size(4 * chars_per_chunk), - buffer(new char[buffer_size]), - byte_offset(0), - content(content), - strings_read({""}) {} + chars_per_chunk(chars_per_chunk), + buffer_size(UTF8_MAX_CHAR_SIZE * chars_per_chunk), + buffer(new char[buffer_size]), + byte_offset(0), + content(content), + strings_read({""}) {} SpyInput::~SpyInput() { delete buffer; } -const char * SpyInput::read(size_t *bytes_read) { - if (byte_offset > content.size()) { +const char * SpyInput::read(void *payload, size_t *bytes_read) { + auto spy = static_cast(payload); + + if (spy->byte_offset > spy->content.size()) { *bytes_read = 0; return ""; } - const char *start = content.data() + byte_offset; - long byte_count = byte_for_character(start, content.size() - byte_offset, chars_per_chunk); + const char *start = spy->content.data() + spy->byte_offset; + long byte_count = byte_for_character(start, spy->content.size() - spy->byte_offset, spy->chars_per_chunk); if (byte_count < 0) - byte_count = content.size() - byte_offset; + byte_count = spy->content.size() - spy->byte_offset; *bytes_read = byte_count; - byte_offset += byte_count; - strings_read.back() += string(start, byte_count); + spy->byte_offset += byte_count; + spy->strings_read.back() += string(start, byte_count); /* * This class stores its entire `content` in a contiguous buffer, but we want @@ -67,23 +61,24 @@ const char * SpyInput::read(size_t *bytes_read) { * return a reference to that buffer, rather than a pointer into the main * content. */ - memset(buffer, 0, buffer_size); - memcpy(buffer, start, byte_count); - return buffer; + memset(spy->buffer, 0, spy->buffer_size); + memcpy(spy->buffer, start, byte_count); + return spy->buffer; } -int SpyInput::seek(size_t pos) { - if (strings_read.size() == 0 || strings_read.back().size() > 0) - strings_read.push_back(""); - byte_offset = pos; +int SpyInput::seek(void *payload, TSLength position) { + auto spy = static_cast(payload); + if (spy->strings_read.size() == 0 || spy->strings_read.back().size() > 0) + spy->strings_read.push_back(""); + spy->byte_offset = position.bytes; return 0; } TSInput SpyInput::input() { TSInput result; result.payload = this; - result.seek_fn = spy_seek; - result.read_fn = spy_read; + result.seek_fn = seek; + result.read_fn = read; return result; } diff --git a/spec/runtime/helpers/spy_input.h b/spec/runtime/helpers/spy_input.h index 3dd772f7..7a18e6dd 100644 --- a/spec/runtime/helpers/spy_input.h +++ b/spec/runtime/helpers/spy_input.h @@ -11,16 +11,17 @@ class SpyInput { char *buffer; size_t byte_offset; + static const char * read(void *, size_t *); + static int seek(void *, TSLength); + public: SpyInput(std::string content, size_t chars_per_chunk); ~SpyInput(); - void clear(); TSInput input(); + void clear(); bool insert(size_t position, std::string text); bool erase(size_t position, size_t len); - const char * read(size_t *len); - int seek(size_t position); std::string content; std::vector strings_read;