-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_encode.go
More file actions
158 lines (126 loc) · 2.89 KB
/
Copy pathmessage_encode.go
File metadata and controls
158 lines (126 loc) · 2.89 KB
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
package irc
import (
"bytes"
"io"
"sync"
)
var bufferPool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
func (m *Message) String() string {
buf := m.buffer()
s := buf.String()
bufferPool.Put(buf)
return s
}
// Bytes returns the message encoded as a byte slice. This slice is safe for
// reuse.
func (m *Message) Bytes() []byte {
buf := m.buffer()
arr := buf.Bytes()
arr2 := make([]byte, len(arr))
copy(arr2, arr)
bufferPool.Put(buf)
return arr2
}
// WriteToWithNewline writes the message to a writer with a terminating `\r\n`.
func (m *Message) WriteToWithNewline(w io.Writer) (n int64, err error) {
buf := m.buffer()
buf.WriteString("\r\n")
n, err = buf.WriteTo(w)
bufferPool.Put(buf)
return n, err
}
// MarshalText implements TextMarshaler for Message.
func (m *Message) MarshalText() (text []byte, err error) {
return m.Bytes(), nil
}
// UnmarshalText implements TextUnmarshaler for Message.
func (m *Message) UnmarshalText(text []byte) error {
return m.Parse(string(text))
}
func (m *Message) buffer() *bytes.Buffer {
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
if len(m.Tags) != 0 {
buf.WriteByte('@')
sep := false
for k, v := range m.Tags {
if sep {
buf.WriteByte(';')
} else {
sep = true
}
buf.WriteString(k)
if v != "" {
buf.WriteByte('=')
// buf is a bytes.Buffer, so writing to it cannot fail.
tagEscapeWrite(buf, v) //nolint:errcheck
}
}
buf.WriteByte(' ')
}
// A prefix must always have a servername/nick, so check this first,
// then decide to include the user and host.
if m.Prefix.Name != "" {
buf.WriteByte(':')
buf.WriteString(m.Prefix.Name)
if m.Prefix.User != "" {
buf.WriteByte('!')
buf.WriteString(m.Prefix.User)
}
if m.Prefix.Host != "" {
buf.WriteByte('@')
buf.WriteString(m.Prefix.Host)
}
buf.WriteByte(' ')
}
buf.WriteString(m.Command)
for _, p := range m.Params {
buf.WriteByte(' ')
buf.WriteString(p)
}
if m.Trailing != "" || m.ForcedTrailing {
buf.WriteString(" :")
buf.WriteString(m.Trailing)
}
return buf
}
// Len returns the length of the encoded message. This message does not
// actually encode the message, instead simulating encoding and only calculates
// the length.
func (m *Message) Len() int {
length := 0
if len(m.Tags) > 0 || m.ForcedTags {
length += len(m.Tags) + 1
for k, v := range m.Tags {
length += len(k)
if v != "" {
length += len(v) + 1
length += countEscapeable(v)
}
}
}
if m.Prefix.Name != "" {
length += len(m.Prefix.Name) + 2
if m.Prefix.User != "" {
length += len(m.Prefix.User) + 1
}
if m.Prefix.Host != "" {
length += len(m.Prefix.Host) + 1
}
}
length += len(m.Command) + 1
if len(m.Params) > 0 {
length += len(m.Params)
for _, p := range m.Params {
length += len(p)
}
}
if m.Trailing != "" || m.ForcedTrailing {
length += len(m.Trailing) + 1
}
return length
}