MCPcopy
hub / github.com/noahgorstein/jqp / ScanLinesWithDynamicBufferSize

Function ScanLinesWithDynamicBufferSize

tui/utils/scan.go:20–38  ·  view source on GitHub ↗

ScanLinesWithDynamicBufferSize scans the input byte slice line by line, using a dynamically increasing buffer size. It starts with an initial buffer size of 64KB and doubles the buffer size each time a line exceeds the current buffer size, up to the specified maximum buffer size. If a line exceeds

(input []byte, maxBufferSize int, processLine func([]byte) error)

Source from the content-addressed store, hash-verified

18// The function returns an error if the input exceeds the maximum buffer size or if any other
19// error occurs during line processing. It returns nil if all lines are processed successfully.
20func ScanLinesWithDynamicBufferSize(input []byte, maxBufferSize int, processLine func([]byte) error) error {
21 scanner := bufio.NewScanner(bytes.NewReader(input))
22 initialBufferSize := 64 * 1024 // 64KB initial buffer size
23
24 for bufferSize := initialBufferSize; bufferSize <= maxBufferSize; bufferSize *= 2 {
25 if err := scanWithBufferSize(scanner, bufferSize, maxBufferSize, processLine); err != nil {
26 if errors.Is(err, bufio.ErrTooLong) {
27 // Buffer size is too small, retry with a larger buffer
28 continue
29 }
30 return err
31 }
32 // All lines are processed successfully
33 return nil
34 }
35
36 // Input exceeds maximum buffer size
37 return fmt.Errorf("input exceeds maximum buffer size of %d bytes", maxBufferSize)
38}
39
40func scanWithBufferSize(scanner *bufio.Scanner, bufferSize, maxBufferSize int, processLine func([]byte) error) error {
41 scanner.Buffer(make([]byte, bufferSize), maxBufferSize)

Callers 3

IsValidJSONLinesFunction · 0.85
PrettifyFunction · 0.85

Calls 1

scanWithBufferSizeFunction · 0.85

Tested by

no test coverage detected