-
Notifications
You must be signed in to change notification settings - Fork 156
/
line_delimiter.go
160 lines (144 loc) · 4.24 KB
/
line_delimiter.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package file
import (
"github.com/loggie-io/loggie/pkg/core/log"
"github.com/loggie-io/loggie/pkg/util"
"strings"
"sync"
)
// LineTerminator is the option storing the line terminator characters
// Supported newline reference: https://en.wikipedia.org/wiki/Newline#Unicode
type LineTerminator uint8
const (
Custom = "custom"
// InvalidTerminator is the invalid terminator
InvalidTerminator LineTerminator = iota
// AutoLineTerminator accepts both LF and CR+LF
AutoLineTerminator
// LineFeed is the unicode char LF
LineFeed
// VerticalTab is the unicode char VT
VerticalTab
// FormFeed is the unicode char FF
FormFeed
// CarriageReturn is the unicode char CR
CarriageReturn
// CarriageReturnLineFeed is the unicode chars CR+LF
CarriageReturnLineFeed
// NextLine is the unicode char NEL
NextLine
// LineSeparator is the unicode char LS
LineSeparator
// ParagraphSeparator is the unicode char PS
ParagraphSeparator
// NullTerminator
NullTerminator
)
var (
lineTerminators = map[string]LineTerminator{
"auto": AutoLineTerminator,
"line_feed": LineFeed,
"vertical_tab": VerticalTab,
"form_feed": FormFeed,
"carriage_return": CarriageReturn,
"carriage_return_line_feed": CarriageReturnLineFeed,
"next_line": NextLine,
"line_separator": LineSeparator,
"paragraph_separator": ParagraphSeparator,
"null_terminator": NullTerminator,
}
lineTerminatorCharacters = map[LineTerminator][]byte{
AutoLineTerminator: {'\u000A'},
LineFeed: {'\u000A'},
VerticalTab: {'\u000B'},
FormFeed: {'\u000C'},
CarriageReturn: {'\u000D'},
CarriageReturnLineFeed: []byte("\u000D\u000A"),
NextLine: {'\u0085'},
LineSeparator: []byte("\u2028"),
ParagraphSeparator: []byte("\u2029"),
NullTerminator: {'\u0000'},
}
)
type LineEndingsValue struct {
value []byte
encodeValue []byte
}
type LineEndings struct {
mutex sync.RWMutex
endLineMap map[string]*LineEndingsValue
}
var globalLineEnd LineEndings
func (end *LineEndings) Init() {
end.mutex.Lock()
defer end.mutex.Unlock()
if end.endLineMap == nil {
end.endLineMap = make(map[string]*LineEndingsValue)
}
}
func (end *LineEndings) AddLineEnd(pipelineName string, sourceName string, lineDelimiterValue *LineDelimiterValue) error {
end.mutex.Lock()
defer end.mutex.Unlock()
lineType, ok := lineTerminators[lineDelimiterValue.LineType]
if !ok && lineDelimiterValue.LineType != Custom {
lineType = AutoLineTerminator
}
var key strings.Builder
key.WriteString(pipelineName)
key.WriteString(":")
key.WriteString(sourceName)
if lineDelimiterValue.LineType == Custom {
bytes, err := util.Encode(lineDelimiterValue.Charset, []byte(lineDelimiterValue.LineValue))
if err != nil {
log.Error("encode error:%s", err)
}
end.endLineMap[key.String()] = &LineEndingsValue{
value: []byte(lineDelimiterValue.LineValue),
encodeValue: bytes,
}
return nil
}
bytes, err := util.Encode(lineDelimiterValue.Charset, lineTerminatorCharacters[lineType])
if err != nil {
log.Error("encode error:%s", err)
}
end.endLineMap[key.String()] = &LineEndingsValue{
value: lineTerminatorCharacters[lineType],
encodeValue: bytes,
}
return nil
}
func (end *LineEndings) GetLineEnd(pipelineName string, sourceName string) []byte {
end.mutex.RLock()
defer end.mutex.RUnlock()
var key strings.Builder
key.WriteString(pipelineName)
key.WriteString(":")
key.WriteString(sourceName)
value, ok := end.endLineMap[key.String()]
if !ok {
return []byte("\n")
}
return value.value
}
func (end *LineEndings) GetEncodeLineEnd(pipelineName string, sourceName string) []byte {
end.mutex.RLock()
defer end.mutex.RUnlock()
var key strings.Builder
key.WriteString(pipelineName)
key.WriteString(":")
key.WriteString(sourceName)
value, ok := end.endLineMap[key.String()]
if !ok {
return []byte("\n")
}
return value.encodeValue
}
func (end *LineEndings) RemoveLineEnd(pipelineName string, sourceName string) {
end.mutex.Lock()
defer end.mutex.Unlock()
var key strings.Builder
key.WriteString(pipelineName)
key.WriteString(":")
key.WriteString(sourceName)
delete(end.endLineMap, key.String())
}