source: code/trunk/vendor/github.com/lib/pq/buf.go@ 822

Last change on this file since 822 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 1.6 KB
Line 
1package pq
2
3import (
4 "bytes"
5 "encoding/binary"
6
7 "github.com/lib/pq/oid"
8)
9
10type readBuf []byte
11
12func (b *readBuf) int32() (n int) {
13 n = int(int32(binary.BigEndian.Uint32(*b)))
14 *b = (*b)[4:]
15 return
16}
17
18func (b *readBuf) oid() (n oid.Oid) {
19 n = oid.Oid(binary.BigEndian.Uint32(*b))
20 *b = (*b)[4:]
21 return
22}
23
24// N.B: this is actually an unsigned 16-bit integer, unlike int32
25func (b *readBuf) int16() (n int) {
26 n = int(binary.BigEndian.Uint16(*b))
27 *b = (*b)[2:]
28 return
29}
30
31func (b *readBuf) string() string {
32 i := bytes.IndexByte(*b, 0)
33 if i < 0 {
34 errorf("invalid message format; expected string terminator")
35 }
36 s := (*b)[:i]
37 *b = (*b)[i+1:]
38 return string(s)
39}
40
41func (b *readBuf) next(n int) (v []byte) {
42 v = (*b)[:n]
43 *b = (*b)[n:]
44 return
45}
46
47func (b *readBuf) byte() byte {
48 return b.next(1)[0]
49}
50
51type writeBuf struct {
52 buf []byte
53 pos int
54}
55
56func (b *writeBuf) int32(n int) {
57 x := make([]byte, 4)
58 binary.BigEndian.PutUint32(x, uint32(n))
59 b.buf = append(b.buf, x...)
60}
61
62func (b *writeBuf) int16(n int) {
63 x := make([]byte, 2)
64 binary.BigEndian.PutUint16(x, uint16(n))
65 b.buf = append(b.buf, x...)
66}
67
68func (b *writeBuf) string(s string) {
69 b.buf = append(append(b.buf, s...), '\000')
70}
71
72func (b *writeBuf) byte(c byte) {
73 b.buf = append(b.buf, c)
74}
75
76func (b *writeBuf) bytes(v []byte) {
77 b.buf = append(b.buf, v...)
78}
79
80func (b *writeBuf) wrap() []byte {
81 p := b.buf[b.pos:]
82 binary.BigEndian.PutUint32(p, uint32(len(p)))
83 return b.buf
84}
85
86func (b *writeBuf) next(c byte) {
87 p := b.buf[b.pos:]
88 binary.BigEndian.PutUint32(p, uint32(len(p)))
89 b.pos = len(b.buf) + 1
90 b.buf = append(b.buf, c, 0, 0, 0, 0)
91}
Note: See TracBrowser for help on using the repository browser.