mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
- FIX: potential CWE-400 with bufio.ReadBytes & bufio.ReadSlices, with no limited read buffer - ADD: test to check overflow buffer with discard or error - REFACTOR: all buffering package, parsing process - UPDATE: doc, examples, test following changes - OPTIMIZE: rework code to optimize process - REWORK: benchmark to check benefice of optimization - FIX: wording error Package IOUtils/Multi: - REWORK: re-design all package to allow sequential/parallel mode - UPDATE: package with adaptive mode to allow switch automaticly between sequential and parallel mode following measurment of sample - OPTIMIZE: code to maximize bandwith and reduce time of write - UPDATE: documentation, test and comments - REWORK: testing organization and benchmark aggregation Package HttpServer: - FIX: bug with dial addr rewrite for healtcheck & testing PortUse Package Logger/HookFile: - FIX: bug with race condition on aggregator counter file Other: - Bump dependencies - FIX: format / import file
81 lines
3.1 KiB
Go
81 lines
3.1 KiB
Go
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2025 Nicolas JUHEL
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*
|
|
*/
|
|
|
|
package delim
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
|
|
libsiz "github.com/nabbar/golib/size"
|
|
)
|
|
|
|
// dlm is the internal implementation of the BufferDelim interface.
|
|
// It wraps an io.ReadCloser with a buffered reader and tracks the delimiter character.
|
|
//
|
|
// Fields:
|
|
// - i: The underlying io.ReadCloser that provides the input stream
|
|
// - r: The delimiter rune used to separate data chunks
|
|
// - b: The internal byte buffer used for reading chunks
|
|
// - s: The maximum size of the buffer
|
|
// - d: Flag indicating whether to discard data on buffer overflow
|
|
//
|
|
// The struct is not exported to maintain encapsulation and allow future implementation changes
|
|
// without breaking the public API.
|
|
type dlm struct {
|
|
m sync.Mutex
|
|
i io.ReadCloser // input io.ReadCloser
|
|
r rune // delimiter rune character
|
|
b []byte // buffer
|
|
s libsiz.Size // size of buffer
|
|
d bool // if max size is reached, discard overflow or return error
|
|
}
|
|
|
|
// Delim returns the delimiter rune configured for this BufferDelim instance.
|
|
// This value is set during construction via New() and remains constant for the lifetime of the instance.
|
|
func (o *dlm) Delim() rune {
|
|
return o.r
|
|
}
|
|
|
|
// getDelimByte converts the delimiter rune to a byte for scanning.
|
|
//
|
|
// IMPORTANT LIMITATION: This method assumes the delimiter fits within a single byte (0-255).
|
|
// For multi-byte Unicode delimiters (runes > 255), only the least significant byte is used,
|
|
// which will NOT produce the expected behavior. This is a known limitation of using
|
|
// scanning which only accepts byte delimiters.
|
|
//
|
|
// Supported delimiters include all ASCII characters (0-127) and extended ASCII (128-255):
|
|
// - '\n' (newline), '\r' (carriage return), '\t' (tab)
|
|
// - ',', '|', ';', ':', ' ' (common separators)
|
|
// - '\x00' (null byte for C-style strings)
|
|
// - Any single-byte character in range 0-255
|
|
//
|
|
// For multi-byte Unicode delimiters, consider using alternative approaches or
|
|
// contribute a scanner-based implementation.
|
|
func (o *dlm) getDelimByte() byte {
|
|
return byte(o.r)
|
|
}
|