Commit Graph

14 Commits

Author SHA1 Message Date
Billy Keyes 53bcdf7e5d Fix EOF error for some files without final newline (#27)
If a file was an exact multiple of 1024 bytes (the size of an internal
buffer) and was missing a final newline, the LineReaderAt implementation
would drop the last line, leading to an unexpected EOF error on apply.

In addition to fixing the bug, slightly change the behavior of
ReadLineAt to reflect how it is actually used:

  1. Clarify that the return value n includes all lines instead of only
     lines with a final newline. This was already true except in the
     case of the bug fixed by this commit.

  2. Only return io.EOF if fewer lines are read than requested. The
     previous implementation also returned io.EOF if the last line was
     missing a final newline, but this was confusing and didn't really
     serve a purpose.

This is technically a breaking change for external implementations but
an implementation that exactly followed the "spec" was already broken in
certain edge cases.
2021-08-19 17:32:18 -07:00
Billy Keyes 09f3004fce Fix flushing when data is larger than the buffer (#2)
copyFrom and copyLinesFrom did not increment the start offset after
reading, so the same lines were copied over and over again.
2020-02-01 10:58:43 -08:00
Billy Keyes 0f3872a5a4 Fix panic with empty input in LineReaderAt 2020-01-26 13:37:09 -08:00
Billy Keyes fa88623500 Improve clarity of LineReaderAt implementation
Try to avoid confusion about whether variables refer to byte or line
counts/positions. It still isn't perfect, but I'm not sure how to
further clarify without being overly verbose.
2020-01-25 22:27:59 -08:00
Billy Keyes 206a57eb7a Add tests for LineReaderAt implementation
This adds minimal new coverage, since the apply tests already exercise
this, but it's complicated enough that dedicated tests will be helpful.
2020-01-25 22:26:21 -08:00
Billy Keyes 18058d1e15 Move apply methods to an Applier type
This removes the distinction between "strict" and "fuzzy" application
by allowing future methods on Applier that control settings. It also
avoids state tracking in the text fragment apply signature by moving it
into the Applier type.

While in practice, an Applier will be used once and discarded, the
capability is provided to reset it for multiple uses.
2020-01-25 17:01:43 -08:00
Billy Keyes 3d1274d16e Remove unused LineReader interface
This is no longer used for text application, so revert the parser back
to using StringReader.
2020-01-25 16:57:23 -08:00
Billy Keyes 0b7af3feaa Implement text application using LineReaderAt
This is functionally equivalent to the previous version (except for one
error case), but uses the new interface. I think the code is simpler
overall because it removes the line tracking.
2020-01-25 16:56:33 -08:00
Billy Keyes f3702115a6 Add LineReaderAt interface and implementation
This is a line-oriented parallel to io.ReaderAt, meant for text applies.
While the mapping isn't quite as clean as in the binary case, a text
apply still reads a fixed chunk of lines starting at a specific line
number and modifies them. This also allows a consistent interface for
strict and fuzzy applies.

The implementation wraps an io.ReaderAt and reads data in chunks,
indexing line boundaries as it goes. This is probably not the most
efficient way to implement this interface, but it works and allows file
application to take a consistent interface.
2020-01-25 16:56:32 -08:00
Billy Keyes 774281f01b Standardize on int64 for line numbers
This matches the type used for positions in text fragments.
2020-01-09 22:11:25 -08:00
Billy Keyes 93d46e805c Add initial pass at ApplyStrict functions
Text fragment application is implemented but untested and with several
TODOs, binary fragment application is unimplemented and will panic.
2020-01-05 20:37:59 -08:00
Billy Keyes d471b7e1a3 Add internal function to unwrap LineReaders
When applying, we need to copy all data after the last fragment line.
This function provides a way to get back the possibly-buffered io.Reader
that backs the LineReader.
2020-01-04 23:32:16 -08:00
Billy Keyes 3ec1c74c34 Improve docs for StringReader and LineReader
Document the method as part of the type so it is processed by godoc.
2020-01-04 20:03:00 -08:00
Billy Keyes a1f934e852 Add LineReader and StringReader interfaces
LineReader is a wrapper around StringReader that counts line numbers.
This is not currently used by the parser, but will be used by the apply
functions.
2020-01-04 12:02:37 -08:00