|
| 1 | +package ios |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/jf-tech/go-corelib/maths" |
| 8 | +) |
| 9 | + |
| 10 | +// LineEditFunc edits a line and returns a resulting line. Note in-place editing is highly encouraged, |
| 11 | +// for performance reasons, when the resulting line is no longer than the original. If your edited line |
| 12 | +// is longer then the original `line`, however, you MUST allocate and return a new []byte. Directly |
| 13 | +// appending at the end of the original `line` will result in undefined behavior. |
| 14 | +type LineEditFunc func(line []byte) ([]byte, error) |
| 15 | + |
| 16 | +// LineEditingReader implements io.Reader interface with a line editing mechanism. LineEditingReader reads data from |
| 17 | +// underlying io.Reader and invokes the caller supplied edit function for each of the line (defined as |
| 18 | +// []byte ending with '\n', therefore it works on both Mac/Linux and Windows, where '\r\n' is used). |
| 19 | +// Note the last line before EOF will be edited as well even if it doesn't end with '\n'. Usage is highly |
| 20 | +// flexible: the editing function can do in-place editing such as character replacement, prefix/suffix |
| 21 | +// stripping, or word replacement, etc., as long as the line length isn't changed; or it can replace a line |
| 22 | +// with a completely newly allocated and written line with no length restriction (although performance |
| 23 | +// would be slower compared to in-place editing). |
| 24 | +type LineEditingReader struct { |
| 25 | + r io.Reader |
| 26 | + edit LineEditFunc |
| 27 | + bufSize int // initial buf size and future buf growth increment. |
| 28 | + buf []byte // note len(buf) == cap(buf), we always use the full capacity of the buf. |
| 29 | + buf0 int // buf[:buf0] edited line(s) ready to be returned to caller. |
| 30 | + buf1 int // buf[buf0:buf1] unedited lines. |
| 31 | + err error |
| 32 | +} |
| 33 | + |
| 34 | +func (r *LineEditingReader) scanEndOfLine(buf []byte) int { |
| 35 | + if lf := bytes.IndexByte(buf, '\n'); lf >= 0 { |
| 36 | + return lf |
| 37 | + } |
| 38 | + if r.err == io.EOF { |
| 39 | + return len(buf) - 1 |
| 40 | + } |
| 41 | + return -1 |
| 42 | +} |
| 43 | + |
| 44 | +// Read implements io.Reader interface for LineEditingReader. |
| 45 | +func (r *LineEditingReader) Read(p []byte) (int, error) { |
| 46 | + n := 0 |
| 47 | + for { |
| 48 | + if r.buf0 > 0 { |
| 49 | + n = copy(p, r.buf[:r.buf0]) |
| 50 | + r.buf0 -= n |
| 51 | + r.buf1 -= n |
| 52 | + copy(r.buf, r.buf[n:r.buf1+n]) |
| 53 | + return n, nil |
| 54 | + } else if r.err != nil { |
| 55 | + return 0, r.err |
| 56 | + } |
| 57 | + |
| 58 | + if r.buf1 >= len(r.buf) { |
| 59 | + newBuf := make([]byte, len(r.buf)+r.bufSize) |
| 60 | + copy(newBuf, r.buf) |
| 61 | + r.buf = newBuf |
| 62 | + } |
| 63 | + |
| 64 | + n, r.err = r.r.Read(r.buf[r.buf1:]) |
| 65 | + r.buf1 += n |
| 66 | + lf := r.scanEndOfLine(r.buf[r.buf0:r.buf1]) |
| 67 | + for ; lf >= 0; lf = r.scanEndOfLine(r.buf[r.buf0:r.buf1]) { |
| 68 | + lineLen := lf + 1 |
| 69 | + edited, err := r.edit(r.buf[r.buf0 : r.buf0+lineLen]) |
| 70 | + if err != nil { |
| 71 | + r.err = err |
| 72 | + break |
| 73 | + } |
| 74 | + editedLen := len(edited) |
| 75 | + delta := lineLen - editedLen |
| 76 | + if len(r.buf)-r.buf1+delta < 0 { |
| 77 | + // only expand the buf if there is no room left for the edited line growth. |
| 78 | + newBuf := make([]byte, len(r.buf)+maths.MaxInt(r.bufSize, -delta)) |
| 79 | + copy(newBuf, r.buf[:r.buf1]) |
| 80 | + r.buf = newBuf |
| 81 | + } |
| 82 | + if delta > 0 { |
| 83 | + // This is the case where the edited line is shorter than the original line. |
| 84 | + // Image we have: |
| 85 | + // xyz\nabc |
| 86 | + // where "xyz\n" is in-placed edited to drop the first letter to "yz\n". |
| 87 | + // If we shift "abc" up by delta (1) first, then we would've overwritten the "\n" in "yz\n" |
| 88 | + // and the edited would now be "yza". |
| 89 | + // Therefore, if edited is shorter, we need to move/copy edited to be at buf0 first |
| 90 | + // before we shift the rest of the buffer (up to buf1) up. |
| 91 | + copy(r.buf[r.buf0:r.buf0+editedLen], edited) |
| 92 | + copy(r.buf[r.buf0+editedLen:r.buf1-delta], r.buf[r.buf0+lineLen:r.buf1]) |
| 93 | + } else { |
| 94 | + // Now if edited is longer, we need to move the rest buffer out first, before we can copy |
| 95 | + // the edited into the buffer. |
| 96 | + copy(r.buf[r.buf0+editedLen:r.buf1-delta], r.buf[r.buf0+lineLen:r.buf1]) |
| 97 | + copy(r.buf[r.buf0:r.buf0+editedLen], edited) |
| 98 | + } |
| 99 | + r.buf0 += editedLen |
| 100 | + r.buf1 -= delta |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// NewLineEditingReader2 creates a new LineEditingReader with custom buffer size. |
| 106 | +func NewLineEditingReader2(r io.Reader, edit LineEditFunc, bufSize int) *LineEditingReader { |
| 107 | + buf := make([]byte, bufSize) |
| 108 | + return &LineEditingReader{ |
| 109 | + r: r, |
| 110 | + edit: edit, |
| 111 | + bufSize: bufSize, |
| 112 | + buf: buf, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +const ( |
| 117 | + defaultLineEditingReaderBufSize = 1024 |
| 118 | +) |
| 119 | + |
| 120 | +// NewLineEditingReader creates a new LineEditingReader with the default buffer size. |
| 121 | +func NewLineEditingReader(r io.Reader, edit LineEditFunc) *LineEditingReader { |
| 122 | + return NewLineEditingReader2(r, edit, defaultLineEditingReaderBufSize) |
| 123 | +} |
0 commit comments