Go Home Page
The Go Programming Language

Package bytes

import "bytes"

The bytes package implements functions for the manipulation of byte slices. Analagous to the facilities of the strings package.

Package files

buffer.go bytes.go bytes_decl.go

Constants

MinRead is the minimum slice size passed to a Read call by Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond what is required to hold the contents of r, ReadFrom will not grow the underlying buffer.

const MinRead = 512

func Add

func Add(s, t []byte) []byte

Add appends the contents of t to the end of s and returns the result. If s has enough capacity, it is extended in place; otherwise a new array is allocated and returned.

func AddByte

func AddByte(s []byte, t byte) []byte

AddByte appends byte b to the end of s and returns the result. If s has enough capacity, it is extended in place; otherwise a new array is allocated and returned.

func Compare

func Compare(a, b []byte) int

Compare returns an integer comparing the two byte arrays lexicographically. The result will be 0 if a==b, -1 if a < b, and +1 if a > b

func Count

func Count(s, sep []byte) int

Count counts the number of non-overlapping instances of sep in s.

func Equal

func Equal(a, b []byte) bool

Equal returns a boolean reporting whether a == b.

func Fields

func Fields(s []byte) [][]byte

Fields splits the array s around each instance of one or more consecutive white space characters, returning a slice of subarrays of s or an empty list if s contains only white space.

func HasPrefix

func HasPrefix(s, prefix []byte) bool

HasPrefix tests whether the byte array s begins with prefix.

func HasSuffix

func HasSuffix(s, suffix []byte) bool

HasSuffix tests whether the byte array s ends with suffix.

func Index

func Index(s, sep []byte) int

Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.

func IndexAny

func IndexAny(s []byte, chars string) int

IndexAny interprets s as a sequence of UTF-8 encoded Unicode code points. It returns the byte index of the first occurrence in s of any of the Unicode code points in chars. It returns -1 if chars is empty or if there is no code point in common.

func IndexByte

func IndexByte(s []byte, c byte) int

IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.

func Join

func Join(a [][]byte, sep []byte) []byte

Join concatenates the elements of a to create a single byte array. The separator sep is placed between elements in the resulting array.

func LastIndex

func LastIndex(s, sep []byte) int

LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.

func Map

func Map(mapping func(rune int) int, s []byte) []byte

Map returns a copy of the byte array s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement. The characters in s and the output are interpreted as UTF-8 encoded Unicode code points.

func Repeat

func Repeat(b []byte, count int) []byte

Repeat returns a new byte slice consisting of count copies of b.

func Replace

func Replace(s, old, new []byte, n int) []byte

Replace returns a copy of the slice s with the first n non-overlapping instances of old replaced by new. If n < 0, there is no limit on the number of replacements.

func Runes

func Runes(s []byte) []int

Runes returns a slice of runes (Unicode code points) equivalent to s.

func Split

func Split(s, sep []byte, n int) [][]byte

Split splits the array s around each instance of sep, returning an array of subarrays of s. If sep is empty, Split splits s after each UTF-8 sequence. If n >= 0, Split splits s into at most n subarrays; the last subarray will contain an unsplit remainder. Thus if n == 0, the result will ne nil.

func SplitAfter

func SplitAfter(s, sep []byte, n int) [][]byte

SplitAfter splits the array s after each instance of sep, returning an array of subarrays of s. If sep is empty, SplitAfter splits s after each UTF-8 sequence. If n >= 0, SplitAfter splits s into at most n subarrays; the last subarray will contain an unsplit remainder. Thus if n == 0, the result will ne nil.

func ToLower

func ToLower(s []byte) []byte

ToUpper returns a copy of the byte array s with all Unicode letters mapped to their lower case.

func ToTitle

func ToTitle(s []byte) []byte

ToTitle returns a copy of the byte array s with all Unicode letters mapped to their title case.

func ToUpper

func ToUpper(s []byte) []byte

ToUpper returns a copy of the byte array s with all Unicode letters mapped to their upper case.

func Trim

func Trim(s []byte, cutset string) []byte

Trim returns a subslice of s by slicing off all leading and trailing UTF-8 encoded Unicode code points contained in cutset.

func TrimFunc

func TrimFunc(s []byte, f func(r int) bool) []byte

TrimFunc returns a subslice of s by slicing off all leading and trailing UTF-8 encoded Unicode code points c that satisfy f(c).

func TrimLeft

func TrimLeft(s []byte, cutset string) []byte

TrimLeft returns a subslice of s by slicing off all leading UTF-8 encoded Unicode code points contained in cutset.

func TrimLeftFunc

func TrimLeftFunc(s []byte, f func(r int) bool) []byte

TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8 encoded Unicode code points c that satisfy f(c).

func TrimRight

func TrimRight(s []byte, cutset string) []byte

TrimRight returns a subslice of s by slicing off all trailing UTF-8 encoded Unicode code points that are contained in cutset.

func TrimRightFunc

func TrimRightFunc(s []byte, f func(r int) bool) []byte

TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8 encoded Unicode code points c that satisfy f(c).

func TrimSpace

func TrimSpace(s []byte) []byte

TrimSpace returns a subslice of s by slicing off all leading and trailing white space, as as defined by Unicode.

type Buffer

A Buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use.

type Buffer struct {
    // contains unexported fields
}

func NewBuffer

func NewBuffer(buf []byte) *Buffer

NewBuffer creates and initializes a new Buffer using buf as its initial contents. It is intended to prepare a Buffer to read existing data. It can also be used to to size the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.

func NewBufferString

func NewBufferString(s string) *Buffer

NewBufferString creates and initializes a new Buffer using string s as its initial contents. It is intended to prepare a buffer to read an existing string.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a slice of the contents of the unread portion of the buffer; len(b.Bytes()) == b.Len(). If the caller changes the contents of the returned slice, the contents of the buffer will change provided there are no intervening method calls on the Buffer.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the number of bytes of the unread portion of the buffer; b.Len() == len(b.Bytes()).

func (*Buffer) Next

func (b *Buffer) Next(n int) []byte

Next returns a slice containing the next n bytes from the buffer, advancing the buffer as if the bytes had been returned by Read. If there are fewer than n bytes in the buffer, Next returns the entire buffer. The slice is only valid until the next call to a read or write method.

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err os.Error)

Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is os.EOF even if len(p) is zero; otherwise it is nil.

func (*Buffer) ReadByte

func (b *Buffer) ReadByte() (c byte, err os.Error)

ReadByte reads and returns the next byte from the buffer. If no byte is available, it returns error os.EOF.

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err os.Error)

ReadFrom reads data from r until EOF and appends it to the buffer. The return value n is the number of bytes read. Any error except os.EOF encountered during the read is also returned.

func (*Buffer) ReadRune

func (b *Buffer) ReadRune() (r int, size int, err os.Error)

ReadRune reads and returns the next UTF-8-encoded Unicode code point from the buffer. If no bytes are available, the error returned is os.EOF. If the bytes are an erroneous UTF-8 encoding, it consumes one byte and returns U+FFFD, 1.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer so it has no content. b.Reset() is the same as b.Truncate(0).

func (*Buffer) String

func (b *Buffer) String() string

String returns the contents of the unread portion of the buffer as a string. If the Buffer is a nil pointer, it returns "<nil>".

func (*Buffer) Truncate

func (b *Buffer) Truncate(n int)

Truncate discards all but the first n unread bytes from the buffer. It is an error to call b.Truncate(n) with n > b.Len().

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err os.Error)

Write appends the contents of p to the buffer. The return value n is the length of p; err is always nil.

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(c byte) os.Error

WriteByte appends the byte c to the buffer. The returned error is always nil, but is included to match bufio.Writer's WriteByte.

func (*Buffer) WriteRune

func (b *Buffer) WriteRune(r int) (n int, err os.Error)

WriteRune appends the UTF-8 encoding of Unicode code point r to the buffer, returning its length and an error, which is always nil but is included to match bufio.Writer's WriteRune.

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (n int, err os.Error)

WriteString appends the contents of s to the buffer. The return value n is the length of s; err is always nil.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (n int64, err os.Error)

WriteTo writes data to w until the buffer is drained or an error occurs. The return value n is the number of bytes written. Any error encountered during the write is also returned.