diff options
author | Håvard Pettersen <havardpe@oath.com> | 2017-09-29 10:14:36 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@oath.com> | 2017-09-29 10:16:47 +0000 |
commit | 33868f33f656c44c6fc318d51297b1b336ede170 (patch) | |
tree | 70c56fadacea960334a55bb85d8c5b82bc32400c | |
parent | e2b6377c44b7029674d32c00a3a11b4ea5a975e2 (diff) |
add try_read function to mirror try_unread
-rw-r--r-- | vespalib/src/tests/data/input_reader/input_reader_test.cpp | 17 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/data/input_reader.h | 13 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/data/slime/json_format.cpp | 6 |
3 files changed, 31 insertions, 5 deletions
diff --git a/vespalib/src/tests/data/input_reader/input_reader_test.cpp b/vespalib/src/tests/data/input_reader/input_reader_test.cpp index c74b16400df..535c188d01e 100644 --- a/vespalib/src/tests/data/input_reader/input_reader_test.cpp +++ b/vespalib/src/tests/data/input_reader/input_reader_test.cpp @@ -138,4 +138,21 @@ TEST("require that bytes can be unread when appropriate") { EXPECT_TRUE(!src.failed()); } +TEST("require that try read finds eof without failing the reader") { + const char *data = "12345"; + MemoryInput memory_input(data); + ChunkedInput input(memory_input, 3); + InputReader src(input); + EXPECT_EQUAL(src.try_read(), '1'); + EXPECT_EQUAL(src.try_read(), '2'); + EXPECT_EQUAL(src.try_read(), '3'); + EXPECT_EQUAL(src.try_read(), '4'); + EXPECT_EQUAL(src.try_read(), '5'); + EXPECT_TRUE(src.try_unread()); + EXPECT_EQUAL(src.try_read(), '5'); + EXPECT_EQUAL(src.try_read(), '\0'); + EXPECT_TRUE(!src.try_unread()); + EXPECT_TRUE(!src.failed()); +} + TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/vespa/vespalib/data/input_reader.h b/vespalib/src/vespa/vespalib/data/input_reader.h index 29f6b88b506..f92ced7e508 100644 --- a/vespalib/src/vespa/vespalib/data/input_reader.h +++ b/vespalib/src/vespa/vespalib/data/input_reader.h @@ -74,6 +74,19 @@ public: } /** + * Try to read a single byte. This function will not fail the + * reader with buffer underflow if eof is reached. + * + * @return the next input byte, or 0 if eof is reached + **/ + char try_read() { + if (__builtin_expect(obtain() > 0, true)) { + return _data.data[_pos++]; + } + return 0; + } + + /** * Try to unread a single byte. This will work for data that is * read, but not yet evicted. Note that after eof is found (the * obtain function returns 0), unreading will not be possible. diff --git a/vespalib/src/vespa/vespalib/data/slime/json_format.cpp b/vespalib/src/vespa/vespalib/data/slime/json_format.cpp index 6eb314141c1..18bf5289f0d 100644 --- a/vespalib/src/vespa/vespalib/data/slime/json_format.cpp +++ b/vespalib/src/vespa/vespalib/data/slime/json_format.cpp @@ -180,11 +180,7 @@ struct JsonDecoder { JsonDecoder(InputReader &reader) : in(reader), c(in.read()), key(), value() {} void next() { - if (in.obtain() > 0) { - c = in.read(); - } else { - c = 0; - } + c = in.try_read(); } bool skip(char x) { |