-
Notifications
You must be signed in to change notification settings - Fork 1
/
line.go
53 lines (44 loc) · 1.12 KB
/
line.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2019 Yegor Myskin. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tailor
import (
"unsafe"
)
// Line represents a returned line from the tailed file.
type Line struct {
fileName string
line []byte
}
// FileName returns the file name of the line.
func (l Line) FileName() string {
return l.fileName
}
// String returns an untrimmed string.
func (l Line) String() string {
return *(*string)(unsafe.Pointer(&l.line))
}
// StringTrimmed returns a trimmed string.
func (l Line) StringTrimmed() string {
trimmedString := l.BytesTrimmed()
return *(*string)(unsafe.Pointer(&trimmedString))
}
// Bytes returns a line.
func (l Line) Bytes() []byte {
return l.line
}
// BytesTrimmed trims the line from the \r and \n sequences. Not unicode safe.
func (l Line) BytesTrimmed() []byte {
// inline optimization with goto instead of for
Loop:
if len(l.line) == 0 {
return l.line
}
last := l.line[len(l.line)-1]
if last == '\n' || last == '\r' || last == ' ' {
l.line = l.line[:len(l.line)-1]
} else {
return l.line
}
goto Loop
}