utils

package
v4.1.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2018 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoArgValue = errors.New("no Args value for the given key")

ErrNoArgValue is returned when Args value with the given key is missing.

Functions

func AppendHTMLEscape

func AppendHTMLEscape(dst []byte, s string) []byte

AppendHTMLEscape appends html-escaped s to dst and returns the extended dst.

func AppendHTMLEscapeBytes

func AppendHTMLEscapeBytes(dst, s []byte) []byte

AppendHTMLEscapeBytes appends html-escaped s to dst and returns the extended dst.

func AppendHTTPDate

func AppendHTTPDate(dst []byte, date time.Time) []byte

AppendHTTPDate appends HTTP-compliant (RFC1123) representation of date to dst and returns the extended dst.

func AppendIPv4

func AppendIPv4(dst []byte, ip net.IP) []byte

AppendIPv4 appends string representation of the given ip v4 to dst and returns the extended dst.

func AppendQuotedArg

func AppendQuotedArg(dst, src []byte) []byte

AppendQuotedArg appends url-encoded src to dst and returns appended dst.

func AppendUint

func AppendUint(dst []byte, n int) []byte

AppendUint appends n to dst and returns the extended dst.

func ParseHTTPDate

func ParseHTTPDate(date []byte) (time.Time, error)

ParseHTTPDate parses HTTP-compliant (RFC1123) date.

func ParseIPv4

func ParseIPv4(dst net.IP, ipStr []byte) (net.IP, error)

ParseIPv4 parses ip address from ipStr into dst and returns the extended dst.

func ParseUfloat

func ParseUfloat(buf []byte) (float64, error)

ParseUfloat parses unsigned float from buf.

func ParseUint

func ParseUint(buf []byte) (int, error)

ParseUint parses uint from buf.

func ReleaseArgs

func ReleaseArgs(a *Args)

ReleaseArgs returns the object acquired via AquireArgs to the pool.

Do not access the released Args object, otherwise data races may occur.

func ReleaseByteBuffer

func ReleaseByteBuffer(b *ByteBuffer)

ReleaseByteBuffer returns byte buffer to the pool.

ByteBuffer.B mustn't be touched after returning it to the pool. Otherwise data races will occur.

func ToJsonStr

func ToJsonStr(s []byte, escapeHTML bool) []byte

ToJsonStr converts the string to json string.

Example
s := `<>&{}""`
fmt.Printf("%s\n", ToJsonStr([]byte(s), true))
fmt.Printf("%s\n", ToJsonStr([]byte(s), false))
Output:

"\u003c\u003e\u0026{}\"\""
"<>&{}\"\""

Types

type Args

type Args struct {
	// contains filtered or unexported fields
}

Args represents query arguments.

It is forbidden copying Args instances. Create new instances instead and use CopyTo().

Args instance MUST NOT be used from concurrently running goroutines.

func AcquireArgs

func AcquireArgs() *Args

AcquireArgs returns an empty Args object from the pool.

The returned Args may be returned to the pool with ReleaseArgs when no longer needed. This allows reducing GC load.

func (*Args) Add

func (a *Args) Add(key, value string)

Add adds 'key=value' argument.

Multiple values for the same key may be added.

func (*Args) AddBytesK

func (a *Args) AddBytesK(key []byte, value string)

AddBytesK adds 'key=value' argument.

Multiple values for the same key may be added.

func (*Args) AddBytesKV

func (a *Args) AddBytesKV(key, value []byte)

AddBytesKV adds 'key=value' argument.

Multiple values for the same key may be added.

func (*Args) AddBytesV

func (a *Args) AddBytesV(key string, value []byte)

AddBytesV adds 'key=value' argument.

Multiple values for the same key may be added.

func (*Args) AppendBytes

func (a *Args) AppendBytes(dst []byte) []byte

AppendBytes appends query string to dst and returns the extended dst.

func (*Args) CopyTo

func (a *Args) CopyTo(dst *Args)

CopyTo copies all args to dst.

func (*Args) Del

func (a *Args) Del(key string)

Del deletes argument with the given key from query args.

func (*Args) DelBytes

func (a *Args) DelBytes(key []byte)

DelBytes deletes argument with the given key from query args.

func (*Args) GetBool

func (a *Args) GetBool(key string) bool

GetBool returns boolean value for the given key.

true is returned for '1', 'y' and 'yes' values, otherwise false is returned.

func (*Args) GetUfloat

func (a *Args) GetUfloat(key string) (float64, error)

GetUfloat returns ufloat value for the given key.

func (*Args) GetUfloatOrZero

func (a *Args) GetUfloatOrZero(key string) float64

GetUfloatOrZero returns ufloat value for the given key.

Zero (0) is returned on error.

func (*Args) GetUint

func (a *Args) GetUint(key string) (int, error)

GetUint returns uint value for the given key.

func (*Args) GetUintOrZero

func (a *Args) GetUintOrZero(key string) int

GetUintOrZero returns uint value for the given key.

Zero (0) is returned on error.

func (*Args) Has

func (a *Args) Has(key string) bool

Has returns true if the given key exists in Args.

func (*Args) HasBytes

func (a *Args) HasBytes(key []byte) bool

HasBytes returns true if the given key exists in Args.

func (*Args) Len

func (a *Args) Len() int

Len returns the number of query args.

func (*Args) Parse

func (a *Args) Parse(s string)

Parse parses the given string containing query args.

func (*Args) ParseBytes

func (a *Args) ParseBytes(b []byte)

ParseBytes parses the given b containing query args.

func (*Args) Peek

func (a *Args) Peek(key string) []byte

Peek returns query arg value for the given key.

Returned value is valid until the next Args call.

func (*Args) PeekBytes

func (a *Args) PeekBytes(key []byte) []byte

PeekBytes returns query arg value for the given key.

Returned value is valid until the next Args call.

func (*Args) PeekMulti

func (a *Args) PeekMulti(key string) [][]byte

PeekMulti returns all the arg values for the given key.

func (*Args) PeekMultiBytes

func (a *Args) PeekMultiBytes(key []byte) [][]byte

PeekMultiBytes returns all the arg values for the given key.

func (*Args) QueryString

func (a *Args) QueryString() []byte

QueryString returns query string for the args.

The returned value is valid until the next call to Args methods.

func (*Args) Reset

func (a *Args) Reset()

Reset clears query args.

func (*Args) Set

func (a *Args) Set(key, value string)

Set sets 'key=value' argument.

func (*Args) SetBytesK

func (a *Args) SetBytesK(key []byte, value string)

SetBytesK sets 'key=value' argument.

func (*Args) SetBytesKV

func (a *Args) SetBytesKV(key, value []byte)

SetBytesKV sets 'key=value' argument.

func (*Args) SetBytesV

func (a *Args) SetBytesV(key string, value []byte)

SetBytesV sets 'key=value' argument.

func (*Args) SetUint

func (a *Args) SetUint(key string, value int)

SetUint sets uint value for the given key.

func (*Args) SetUintBytes

func (a *Args) SetUintBytes(key []byte, value int)

SetUintBytes sets uint value for the given key.

func (*Args) String

func (a *Args) String() string

String returns string representation of query args.

func (*Args) VisitAll

func (a *Args) VisitAll(f func(key, value []byte))

VisitAll calls f for each existing arg.

f must not retain references to key and value after returning. Make key and/or value copies if you need storing them after returning.

func (*Args) WriteTo

func (a *Args) WriteTo(w io.Writer) (int64, error)

WriteTo writes query string to w.

WriteTo implements io.WriterTo interface.

type BufferPool

type BufferPool struct {
	// contains filtered or unexported fields
}

BufferPool represents byte buffer pool.

Distinct pools may be used for distinct types of byte buffers. Properly determined byte buffer types with their own pools may help reducing memory waste.

func (*BufferPool) Get

func (p *BufferPool) Get() *ByteBuffer

Get returns new byte buffer with zero length.

The byte buffer may be returned to the pool via Put after the use in order to minimize GC overhead.

func (*BufferPool) Put

func (p *BufferPool) Put(b *ByteBuffer)

Put releases byte buffer obtained via Get to the pool.

The buffer mustn't be accessed after returning to the pool.

type BufioReader

type BufioReader struct {
	// contains filtered or unexported fields
}

BufioReader an io.Reader object buffer with count and limit.

func NewBufioReader

func NewBufioReader(r io.Reader, limit ...int64) *BufioReader

NewBufioReader returns a new BufioReader whose buffer has the default size.

func NewBufioReaderSize

func NewBufioReaderSize(r io.Reader, size int, limit ...int64) *BufioReader

NewBufioReaderSize returns a new BufioReader whose buffer has at least the specified size. If the argument io.Reader is already a BufioReader with large enough size, it returns the underlying BufioReader.

func (*BufioReader) Buffered

func (b *BufioReader) Buffered() int

Buffered returns the number of bytes that can be read from the current buffer.

func (*BufioReader) Count

func (b *BufioReader) Count() int64

Count returns the count.

func (*BufioReader) Discard

func (b *BufioReader) Discard(n int) (discarded int, err error)

Discard skips the next n bytes, returning the number of bytes discarded.

If Discard skips fewer than n bytes, it also returns an error. If 0 <= n <= b.Buffered(), Discard is guaranteed to succeed without reading from the underlying io.Reader.

func (*BufioReader) Read

func (b *BufioReader) Read(p []byte) (int, error)

Read reads data into p. It returns the number of bytes read into p. The bytes are taken from at most one Read on the underlying Reader, hence n may be less than len(p). At EOF, the count will be zero and err will be io.EOF.

func (*BufioReader) ReadByte

func (b *BufioReader) ReadByte() (byte, error)

ReadByte reads and returns a single byte. If no byte is available, returns an error.

func (*BufioReader) Reset

func (b *BufioReader) Reset(r io.Reader)

Reset discards any buffered data, resets all state, switches the buffered reader to read from r, resets count, and resets limit.

func (*BufioReader) ResetCount

func (b *BufioReader) ResetCount()

ResetCount resets the count.

func (*BufioReader) ResetLimit

func (b *BufioReader) ResetLimit(limit int64)

ResetLimit resets the limit.

type BufioWriter

type BufioWriter struct {
	// contains filtered or unexported fields
}

BufioWriter implements buffering for an io.Writer object with count. If an error occurs writing to a BufioWriter, no more data will be accepted and all subsequent writes, and Flush, will return the error. After all data has been written, the client should call the Flush method to guarantee all data has been forwarded to the underlying io.Writer.

func NewBufioWriter

func NewBufioWriter(w io.Writer) *BufioWriter

NewBufioWriter returns a new BufioWriter whose buffer has the default size.

func NewBufioWriterSize

func NewBufioWriterSize(w io.Writer, size int) *BufioWriter

NewBufioWriterSize returns a new BufioWriter whose buffer has at least the specified size. If the argument io.Writer is already a BufioWriter with large enough size, it returns the underlying BufioWriter.

func (*BufioWriter) Available

func (b *BufioWriter) Available() int

Available returns how many bytes are unused in the buffer.

func (*BufioWriter) Buffered

func (b *BufioWriter) Buffered() int

Buffered returns the number of bytes that have been written into the current buffer.

func (*BufioWriter) Count

func (b *BufioWriter) Count() int64

Count returns the count.

func (*BufioWriter) Flush

func (b *BufioWriter) Flush() error

Flush writes any buffered data to the underlying io.Writer.

func (*BufioWriter) ReadFrom

func (b *BufioWriter) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom.

func (*BufioWriter) Reset

func (b *BufioWriter) Reset(w io.Writer)

Reset discards any unflushed buffered data, clears any error, resets b to write its output to w, and resets count.

func (*BufioWriter) ResetCount

func (b *BufioWriter) ResetCount()

ResetCount resets the count.

func (*BufioWriter) Write

func (b *BufioWriter) Write(p []byte) (int, error)

Write writes the contents of p into the buffer. It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the write is short.

func (*BufioWriter) WriteByte

func (b *BufioWriter) WriteByte(c byte) error

WriteByte writes a single byte.

func (*BufioWriter) WriteRune

func (b *BufioWriter) WriteRune(r rune) (int, error)

WriteRune writes a single Unicode code point, returning the number of bytes written and any error.

func (*BufioWriter) WriteString

func (b *BufioWriter) WriteString(s string) (int, error)

WriteString writes a string. It returns the number of bytes written. If the count is less than len(s), it also returns an error explaining why the write is short.

type ByteBuffer

type ByteBuffer struct {

	// B is a byte buffer to use in append-like workloads.
	// See example code for details.
	B []byte
}

ByteBuffer provides byte buffer, which can be used for minimizing memory allocations.

ByteBuffer may be used with functions appending data to the given []byte slice. See example code for details.

Use Get for obtaining an empty byte buffer.

func AcquireByteBuffer

func AcquireByteBuffer() *ByteBuffer

AcquireByteBuffer returns an empty byte buffer from the pool.

Got byte buffer may be returned to the pool via Put call. This reduces the number of memory allocations required for byte buffer management.

func (*ByteBuffer) Bytes

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

Bytes returns b.B, i.e. all the bytes accumulated in the buffer.

The purpose of this function is bytes.Buffer compatibility.

func (*ByteBuffer) ChangeLen

func (b *ByteBuffer) ChangeLen(newLen int)

ChangeLen changes the buffer length.

func (*ByteBuffer) Len

func (b *ByteBuffer) Len() int

Len returns the size of the byte buffer.

func (*ByteBuffer) ReadFrom

func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom.

The function appends all the data read from r to b.

func (*ByteBuffer) Reset

func (b *ByteBuffer) Reset()

Reset makes ByteBuffer.B empty.

func (*ByteBuffer) Set

func (b *ByteBuffer) Set(p []byte)

Set sets ByteBuffer.B to p.

func (*ByteBuffer) SetString

func (b *ByteBuffer) SetString(s string)

SetString sets ByteBuffer.B to s.

func (*ByteBuffer) String

func (b *ByteBuffer) String() string

String returns string representation of ByteBuffer.B.

func (*ByteBuffer) Write

func (b *ByteBuffer) Write(p []byte) (int, error)

Write implements io.Writer - it appends p to ByteBuffer.B

func (*ByteBuffer) WriteByte

func (b *ByteBuffer) WriteByte(c byte) error

WriteByte appends the byte c to the buffer.

The purpose of this function is bytes.Buffer compatibility.

The function always returns nil.

func (*ByteBuffer) WriteString

func (b *ByteBuffer) WriteString(s string) (int, error)

WriteString appends s to ByteBuffer.B.

type ReaderWrap

type ReaderWrap struct {
	// contains filtered or unexported fields
}

ReaderWrap reader wrapper

func (*ReaderWrap) Read

func (rp *ReaderWrap) Read(p []byte) (int, error)

Read reads bytes.

func (*ReaderWrap) Readed

func (rp *ReaderWrap) Readed() int

Readed returns readed bytes length.

func (*ReaderWrap) Reset

func (rp *ReaderWrap) Reset(r io.Reader)

Reset resets itself.

type WriterWrap

type WriterWrap struct {
	// contains filtered or unexported fields
}

WriterWrap writer wrapper

func (*WriterWrap) Reset

func (wp *WriterWrap) Reset(w io.Writer)

Reset resets itself.

func (*WriterWrap) Write

func (wp *WriterWrap) Write(p []byte) (int, error)

Write writes bytes.

func (*WriterWrap) Writed

func (wp *WriterWrap) Writed() int

Writed returns writed bytes length.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL