ReadLines reads lines from the reader, optionally skipping the first skipLines lines. If lineCount is 0, no line limit is applied. If readLimit is 0, no byte limit is applied. Stops when either limit is reached or EOF. Returns lines (with trailing newlines), stop reason, and error. Stop reason is St
(reader io.Reader, lineCount int, skipLines int, readLimit int)
| 23 | // Stop reason is StopReasonEOF when EOF is reached, StopReasonReadLimit when byte limit is reached, |
| 24 | // or empty string for natural returns (line count limit or no limits applied). |
| 25 | func ReadLines(reader io.Reader, lineCount int, skipLines int, readLimit int) ([]string, string, error) { |
| 26 | bufReader := bufio.NewReader(reader) |
| 27 | lines := make([]string, 0) |
| 28 | bytesRead := 0 |
| 29 | skippedLines := 0 |
| 30 | |
| 31 | for { |
| 32 | line, err := bufReader.ReadString('\n') |
| 33 | if len(line) > 0 { |
| 34 | bytesRead += len(line) |
| 35 | |
| 36 | if skippedLines < skipLines { |
| 37 | skippedLines++ |
| 38 | } else { |
| 39 | lines = append(lines, line) |
| 40 | if lineCount > 0 && len(lines) >= lineCount { |
| 41 | return lines, "", nil |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if readLimit > 0 && bytesRead >= readLimit { |
| 46 | return lines, StopReasonReadLimit, nil |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if err != nil { |
| 51 | if err == io.EOF { |
| 52 | return lines, StopReasonEOF, nil |
| 53 | } |
| 54 | return nil, "", err |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // readLastNLineOffsets reads all line offsets from the reader, keeping only the last maxLines in a sliding window. |
| 60 | // keepFirst indicates whether offset 0 should be included (true if starting from file beginning). |
no outgoing calls
no test coverage detected