Skip to content

Conversation

@jf-tech
Copy link
Owner

@jf-tech jf-tech commented Nov 27, 2021

LineEditingReader implements io.Reader interface with a line editing mechanism. LineEditingReader reads data from underlying io.Reader and invokes the caller supplied edit function for each of the line (defined as []byte ending with '\n', therefore it works on both Mac/Linux and Windows, where '\r\n' is used). Note the last line before EOF will be edited as well even if it doesn't end with '\n'. Usage is highly flexible: the editing function can do in-place editing such as character replacement, prefix/suffix stripping, or word replacement, etc., as long as the line length isn't increased; or it can replace a line with a completely newly allocated and written line with no length restriction (although performance might be slower compared to in-place editing).

ios.LineEditingReader is at least as performant as ios.BytesReplacingReader:

BenchmarkLineEditingReader_RawIORead-8 23300 51319 ns/op 1103392 B/op 23 allocs/op BenchmarkLineEditingReader_UseLineEditingReader-8 3343 351305 ns/op 1104512 B/op 25 allocs/op BenchmarkLineEditingReader_CompareWithBytesReplacingReader-8 978 1226656 ns/op 1107648 B/op 26 allocs/op 

This PR is motivated from real usage case discussed in jf-tech/omniparser#154

FYI @samolds

…anism. LineReader implements io.Reader interface with a line editing mechanism. LineReader reads data from underlying io.Reader and invokes the caller supplied edit function for each of the line (defined as []byte ending with '\n', therefore it works on both Mac/Linux and Windows, where '\r\n' is used). Note the last line before EOF will be edited as well even if it doesn't end with '\n'. Usage is highly flexible: the editing function can do in-place editing such as character replacement, prefix/suffix stripping, or word replacement, etc., as long as the line length isn't changed; or it can replace a line with a completely newly allocated and written line with no length restriction (although performance would be slower compared to in-place editing). ios.LineReader is at least as performant as ios.BytesReplacingReader: ``` BenchmarkLineReader_RawIORead-8 23300 51319 ns/op 1103392 B/op 23 allocs/op BenchmarkLineReader_UseLineReader-8 3343 351305 ns/op 1104512 B/op 25 allocs/op BenchmarkLineReader_CompareWithBytesReplacingReader-8 978 1226656 ns/op 1107648 B/op 26 allocs/op ``` This PR is motivated from real usage case discussed in jf-tech/omniparser#154
@codecov
Copy link

codecov bot commented Nov 27, 2021

Codecov Report

Merging #22 (1b13911) into master (99ea835) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@ Coverage Diff @@ ## master #22 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 18 19 +1 Lines 633 686 +53 ========================================= + Hits 633 686 +53 
Impacted Files Coverage Δ
ios/lineEditingReader.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 99ea835...1b13911. Read the comment docs.

@jf-tech jf-tech changed the title Introduce ios.LineReader, an io.Reader wrapper with line editing mechanism Introduce ios.LineEditingReader, an io.Reader wrapper with line editing mechanism Nov 27, 2021
@samolds
Copy link

samolds commented Nov 30, 2021

I think this looks great. This solves the issue I was having here jf-tech/omniparser#154.

@jf-tech jf-tech merged commit 44cb4fd into master Nov 30, 2021
@jf-tech jf-tech deleted the line_reader branch November 30, 2021 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants