-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.go
51 lines (47 loc) · 904 Bytes
/
utils.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
package tcpserver
import (
"bufio"
"errors"
)
var (
// ErrBufferLimitExceeded is returned when specified buffer limit
// was exceeded.
ErrBufferLimitExceeded = errors.New("buffer limit exceeded")
)
// ReadBytesLimit reads bytes as bufio.Reader.ReadBytes with limit.
func ReadBytesLimit(b *bufio.Reader, delim byte, lim int) (line []byte, err error) {
line = make([]byte, 0)
for len(line) <= lim {
var buf []byte
buf, err = b.ReadSlice(delim)
line = append(line, buf...)
if err != bufio.ErrBufferFull {
break
}
}
if err == nil && len(line) > lim {
err = ErrBufferLimitExceeded
}
return
}
// trimCrLf trims CRLF at end of buf. (obsolete)
func trimCrLf(buf []byte) []byte {
l := len(buf)
if l == 0 {
return buf
}
l--
if buf[l] != '\n' {
return buf
}
buf = buf[0:l]
if l == 0 {
return buf
}
l--
if buf[l] != '\r' {
return buf
}
buf = buf[0:l]
return buf
}