本文整理汇总了Golang中bytes.Add函数的典型用法代码示例。如果您正苦于以下问题:Golang Add函数的具体用法?Golang Add怎么用?Golang Add使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Add函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readUntilCrLf
func readUntilCrLf(con *net.TCPConn) (line []byte, err os.Error) {
buf := make([]byte, 1)
var data []byte
crSeen := false
for {
_, err := con.Read(buf)
if err != nil {
if err == os.EOF {
break
} else {
return nil, err
}
}
if crSeen {
if buf[0] == 10 {
break
} else {
crSeen = false
data = bytes.Add(data, buf)
}
} else {
if buf[0] == 13 {
crSeen = true
} else {
data = bytes.Add(data, buf)
}
}
}
return data, nil
}
示例2: setPrefix
// Extract regular text from the beginning of the pattern.
// That text can be used by doExecute to speed up matching.
func (re *Regexp) setPrefix() {
var b []byte
var utf = make([]byte, utf8.UTFMax)
// First instruction is start; skip that.
i := re.inst.At(0).(instr).next().index()
Loop:
for i < re.inst.Len() {
inst := re.inst.At(i).(instr)
// stop if this is not a char
if inst.kind() != _CHAR {
break
}
// stop if this char can be followed by a match for an empty string,
// which includes closures, ^, and $.
switch re.inst.At(inst.next().index()).(instr).kind() {
case _BOT, _EOT, _ALT:
break Loop
}
n := utf8.EncodeRune(inst.(*_Char).char, utf)
b = bytes.Add(b, utf[0:n])
i = inst.next().index()
}
// point prefixStart instruction to first non-CHAR after prefix
re.prefixStart = re.inst.At(i).(instr)
re.prefixBytes = b
re.prefix = string(b)
}
示例3: UnmarshalJSON
// UnmarshalJSON sets *m to a copy of data.
func (m *RawMessage) UnmarshalJSON(data []byte) os.Error {
if m == nil {
return os.NewError("json.RawMessage: UnmarshalJSON on nil pointer")
}
*m = bytes.Add((*m)[0:0], data)
return nil
}
示例4: readHandshake
// readHandshake reads the next handshake message from
// the record layer.
// c.in.Mutex < L; c.out.Mutex < L.
func (c *Conn) readHandshake() (interface{}, os.Error) {
for c.hand.Len() < 4 {
if c.err != nil {
return nil, c.err
}
c.readRecord(recordTypeHandshake)
}
data := c.hand.Bytes()
n := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
if n > maxHandshake {
c.sendAlert(alertInternalError)
return nil, c.err
}
for c.hand.Len() < 4+n {
if c.err != nil {
return nil, c.err
}
c.readRecord(recordTypeHandshake)
}
data = c.hand.Next(4 + n)
var m handshakeMessage
switch data[0] {
case typeClientHello:
m = new(clientHelloMsg)
case typeServerHello:
m = new(serverHelloMsg)
case typeCertificate:
m = new(certificateMsg)
case typeCertificateRequest:
m = new(certificateRequestMsg)
case typeCertificateStatus:
m = new(certificateStatusMsg)
case typeServerHelloDone:
m = new(serverHelloDoneMsg)
case typeClientKeyExchange:
m = new(clientKeyExchangeMsg)
case typeCertificateVerify:
m = new(certificateVerifyMsg)
case typeNextProtocol:
m = new(nextProtoMsg)
case typeFinished:
m = new(finishedMsg)
default:
c.sendAlert(alertUnexpectedMessage)
return nil, alertUnexpectedMessage
}
// The handshake message unmarshallers
// expect to be able to keep references to data,
// so pass in a fresh copy that won't be overwritten.
data = bytes.Add(nil, data)
if !m.unmarshal(data) {
c.sendAlert(alertUnexpectedMessage)
return nil, alertUnexpectedMessage
}
return m, nil
}
示例5: EncodeSOA
func EncodeSOA(soa SOArecord) []byte {
var (
result []byte
temp32 []byte
)
mname := Encode(soa.Mname)
length := len(mname)
rname := Encode(soa.Rname)
length = length + len(rname)
length = length + (5 * 4) // Five 32-bits counter at the end
/* "It's probably cleaner to write to a bytes.Buffer than to
repeatedly call bytes.Add." Russ Cox, go-nuts ML */
result = bytes.Add(result, mname)
result = bytes.Add(result, rname)
temp32 = make([]byte, 4)
binary.BigEndian.PutUint32(temp32, soa.Serial)
result = bytes.Add(result, temp32)
binary.BigEndian.PutUint32(temp32, soa.Refresh)
result = bytes.Add(result, temp32)
binary.BigEndian.PutUint32(temp32, soa.Retry)
result = bytes.Add(result, temp32)
binary.BigEndian.PutUint32(temp32, soa.Expire)
result = bytes.Add(result, temp32)
binary.BigEndian.PutUint32(temp32, soa.Minimum)
result = bytes.Add(result, temp32)
return result[0:length]
}
示例6: sendMessageToReply
// To use with messages that receive a response from database
// 'opQuery', 'opGetMore'.
func (self *Connection) sendMessageToReply(m message, reqID int32) os.Error {
body := m.Bytes()
h := header(msgHeader{int32(len(body) + _HEADER_SIZE), reqID, 0, m.OpCode()})
msg := bytes.Add(h, body)
_, err := self.conn.Write(msg)
return err
}
示例7: Decode
func Decode(b []byte) []byte {
r := make([]byte, len(b)*2)[0:0]
for {
if b[0] != 128 {
break
}
if b[1] == 3 {
break
}
l := int(b[2]) + (int(b[3]) << 8) + (int(b[4]) << 16) + 6
if b[1] == 1 {
r = bytes.Add(r, b[6:l])
} else {
r = bytes.Add(r, hex.Encode(b[6:l]))
}
b = b[l:]
}
return r
}
示例8: writeMessage
func (c *Connection) writeMessage(m message) os.Error {
body := m.Bytes()
hb := header(int32(len(body)+16), m.RequestID(), 0, m.OpCode())
msg := bytes.Add(hb, body)
_, err := c.conn.Write(msg)
last_req = m.RequestID()
return err
}
示例9: Bytes
func (self *_Object) Bytes() []byte {
buf := bytes.NewBuffer([]byte{})
for k, v := range self.value {
buf.WriteByte(byte(v.Kind()))
buf.WriteString(k)
buf.WriteByte(0)
buf.Write(v.Bytes())
}
buf.WriteByte(0)
l := buf.Len() + 4
w32 := make([]byte, _WORD32)
pack.PutUint32(w32, uint32(l))
return bytes.Add(w32, buf.Bytes())
}
示例10: Repeat
func Repeat(alu []byte, n int) {
buf := bytes.Add(alu, alu)
off := 0
for n > 0 {
m := n
if m > Line {
m = Line
}
buf1 := out.NextWrite(m + 1)
copy(buf1, buf[off:])
buf1[m] = '\n'
if off += m; off >= len(alu) {
off -= len(alu)
}
n -= m
}
}
示例11: simpleQuery
func (self *Client) simpleQuery(command int, version int, size int, body []byte) ([]byte, os.Error) {
conn, err := self.connect()
if err != nil {
return nil, err
}
header := make([]byte, 8+len(body))
binary.BigEndian.PutUint16(header[0:2], uint16(command))
binary.BigEndian.PutUint16(header[2:4], uint16(version))
binary.BigEndian.PutUint32(header[4:8], uint32(size))
request := bytes.Add(header, body)
_, err = conn.Write(request)
if err != nil {
return nil, err
}
return self.getResponse(conn)
}
示例12: ReadAll
// ReadAll takes an os.File as it's argument and reads in all of it until
// it signals to stop with an EOF in
func ReadAll(file * os.File) ([]byte, os.Error) {
const BUFSIZE = 1024
result := make([]byte, 0)
buf := make([]byte, BUFSIZE)
var sub []byte
for { // ever
count, err2 := file.Read(buf)
sub = buf[0:count]
// get right slice to append to result
// append bytes to result
if count > 0 {
result = bytes.Add(result, sub)
}
// if EOF, return the result.
if err2 == os.EOF { return result, nil }
// on any other error return nil
if err2 != nil { return nil , err2 }
}
// should not get here
return nil, os.NewError("Can't happen?!");
}
示例13: TestRecords
func TestRecords(t *testing.T) {
for i, test := range recordsTests {
buf := bytes.NewBuffer(test.raw)
content, reqId, kind, err := readRecord(buf)
if err != nil {
t.Errorf("%d error reading record: %s", i, err.String())
goto write
}
for buf.Len() > 0 {
moreContent, _, _, err := readRecord(buf)
if err != nil {
t.Errorf("%d error reading additional record: %s", i, err.String())
goto write
}
content = bytes.Add(content, moreContent)
}
if kind != test.kind {
t.Errorf("%d got kind %d expected %d", i, kind, test.kind)
}
if reqId != test.reqId {
t.Errorf("%d got request ID %d expected %d", i, kind, test.kind)
}
if !bytes.Equal(content, test.content) {
t.Errorf("%d read wrong content", i)
}
write:
buf.Reset()
c := &lockReadWriteCloser{ReadWriteCloser: &nilCloser{buf}}
err = writeRecord(c, test.kind, test.reqId, test.content)
if err != nil {
t.Errorf("%d error writing record: %s", i, err.String())
continue
}
if !bytes.Equal(buf.Bytes(), test.raw) {
t.Errorf("%d wrote wrong content", i)
}
}
}
示例14: TestNextValueBig
func TestNextValueBig(t *testing.T) {
var scan scanner
item, rest, err := nextValue(jsonBig, &scan)
if err != nil {
t.Fatalf("nextValue: ", err)
}
if len(item) != len(jsonBig) || &item[0] != &jsonBig[0] {
t.Errorf("invalid item: %d %d", len(item), len(jsonBig))
}
if len(rest) != 0 {
t.Errorf("invalid rest: %d", len(rest))
}
item, rest, err = nextValue(bytes.Add(jsonBig, []byte("HELLO WORLD")), &scan)
if err != nil {
t.Fatalf("nextValue extra: ", err)
}
if len(item) != len(jsonBig) {
t.Errorf("invalid item: %d %d", len(item), len(jsonBig))
}
if string(rest) != "HELLO WORLD" {
t.Errorf("invalid rest: %d", len(rest))
}
}
示例15: ReadContinuedLineBytes
// ReadContinuedLineBytes is like ReadContinuedLine but
// returns a []byte instead of a string.
func (r *Reader) ReadContinuedLineBytes() ([]byte, os.Error) {
// Read the first line.
line, err := r.ReadLineBytes()
if err != nil {
return line, err
}
if len(line) == 0 { // blank line - no continuation
return line, nil
}
line = trim(line)
// Look for a continuation line.
c, err := r.R.ReadByte()
if err != nil {
// Delay err until we read the byte next time.
return line, nil
}
if c != ' ' && c != '\t' {
// Not a continuation.
r.R.UnreadByte()
return line, nil
}
// Read continuation lines.
for {
// Consume leading spaces; one already gone.
for {
c, err = r.R.ReadByte()
if err != nil {
break
}
if c != ' ' && c != '\t' {
r.R.UnreadByte()
break
}
}
var cont []byte
cont, err = r.ReadLineBytes()
cont = trim(cont)
line = bytes.Add(line, space)
line = bytes.Add(line, cont)
if err != nil {
break
}
// Check for leading space on next line.
if c, err = r.R.ReadByte(); err != nil {
break
}
if c != ' ' && c != '\t' {
r.R.UnreadByte()
break
}
}
// Delay error until next call.
if len(line) > 0 {
err = nil
}
return line, err
}