1 Star 0 Fork 0

zhangjungang/beats

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
strip_newline.go 1010 Bytes
Copy Edit Raw Blame History
package reader
// StripNewline reader removes the last trailing newline characters from
// read lines.
type StripNewline struct {
reader Reader
}
// NewStripNewline creates a new line reader stripping the last tailing newline.
func NewStripNewline(r Reader) *StripNewline {
return &StripNewline{r}
}
// Next returns the next line.
func (p *StripNewline) Next() (Message, error) {
message, err := p.reader.Next()
if err != nil {
return message, err
}
L := message.Content
message.Content = L[:len(L)-lineEndingChars(L)]
return message, err
}
// isLine checks if the given byte array is a line, means has a line ending \n
func isLine(l []byte) bool {
return l != nil && len(l) > 0 && l[len(l)-1] == '\n'
}
// lineEndingChars returns the number of line ending chars the given by array has
// In case of Unix/Linux files, it is -1, in case of Windows mostly -2
func lineEndingChars(l []byte) int {
if !isLine(l) {
return 0
}
if len(l) > 1 && l[len(l)-2] == '\r' {
return 2
}
return 1
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.5.0

Search