tex

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 17 Imported by: 2

README

tex

  • 基本扩展
  • 扩展一些数据类型,能适配SQL/JSON
  • 提供基本的二进制读写数据
  • 提供高效的JSON算法和压缩算法
  • 提供位图数据
  • 提供interface到基本数据类型的转化

Documentation

Index

Constants

View Source
const MinRead = 512

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.

Variables

View Source
var (
	ErrByteBufferEmpty = errors.New("byte.buffer.empty")
	ErrReadWrongNum    = errors.New("byte.buffer.wrong.num")
	ErrSizeLimit       = errors.New("byte.buffer.size.limit")
)
View Source
var (
	JSONMarshal   = jsonStd.Marshal
	JSONUnmarshal = jsonStd.Unmarshal
)

标准jsoniter json库 100%兼容

View Source
var (
	JSONFastMarshal   = jsonFast.Marshal
	JSONFastUnmarshal = jsonFast.Unmarshal

	JSONFastMarshalSnappy   = fastMarshalSnappy
	JSONFastUnmarshalSnappy = fastUnmarshalSnappy

	TrySnappyCompress = compressJSON
)

快速jsoniter json库 -- 浮点数会丢失精度,小数点最多后6位

View Source
var (
	ErrInvalidByteJs = errors.New(`byte.invalid.string`)
)
View Source
var (
	ErrInvalidInt64Js = errors.New(`int64 invalid string`)
)
View Source
var (
	ErrInvalidUInt64Js = errors.New(`uint64 invalid string`)
)
View Source
var ErrTooLarge = errors.New("bytes.Buffer: too large")

ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.

View Source
var (
	Snappy = &_Snappy{}
)

Functions

func KeyInMap

func KeyInMap(m map[string]interface{}, k string) bool

KeyInMap : Map中是否包含了key

func LoadJSONFile2Obj

func LoadJSONFile2Obj(fileName string, v interface{}) error

LoadJSONFile2Obj 读取json文件并序列化到传入的指针中

func MapVal2Bool

func MapVal2Bool(m map[string]interface{}, k string) bool

MapVal2Bool : 将map中的value转换为bool

func MapVal2Bytes

func MapVal2Bytes(m map[string]interface{}, k string) []byte

MapVal2Bytes : 将map中的value转换为[]byte

func MapVal2Float64

func MapVal2Float64(m map[string]interface{}, k string) float64

MapVal2Float64 : 将map中的value转换为float64

func MapVal2Int

func MapVal2Int(m map[string]interface{}, k string) int

MapVal2Int : 将map中的value转换为int

func MapVal2Int32

func MapVal2Int32(m map[string]interface{}, k string) int32

MapVal2Int32 : 将map中的value转换为int32

func MapVal2Int64

func MapVal2Int64(m map[string]interface{}, k string) int64

MapVal2Int64 : 将map中的value转换为int64

func MapVal2String

func MapVal2String(m map[string]interface{}, k string) string

MapVal2String : 将map中的value转换为string

func MapVal2StringList

func MapVal2StringList(m map[string]interface{}, k string) []string

MapVal2StringList : 将map中的value转换为string list

func MapVal2Time

func MapVal2Time(m map[string]interface{}, k string) (time.Time, bool)

MapVal2Time : 将map中的value转换为time

func ToBool

func ToBool(v interface{}) bool

ToBool 将基本类型转换为bool

func ToBytes

func ToBytes(v interface{}) []byte

ToBytes 将基本类型转换为[]byte

func ToFloat64

func ToFloat64(v interface{}) float64

ToFloat64 将基本类型转换为浮点型

func ToInt

func ToInt(v interface{}) int

ToInt 将基本类型转换为整型

func ToInt32

func ToInt32(v interface{}) int32

ToInt32 将基本类型转换为32位整型

func ToInt64

func ToInt64(v interface{}) int64

ToInt64 将基本类型转换为64位整型

func ToString

func ToString(v interface{}) string

ToString 将基本类型转换为字符串

func ToStringList

func ToStringList(v interface{}) []string

ToStringList 将基本类型转换为字符串list

func ToTime

func ToTime(v interface{}) (time.Time, bool)

ToTime 转换为time.Time

Types

type Base64Bytes

type Base64Bytes []byte

func (*Base64Bytes) Scan

func (i *Base64Bytes) Scan(value interface{}) error

func (Base64Bytes) Value

func (i Base64Bytes) Value() (driver.Value, error)

type Buffer

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

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.

func NewBuffer

func NewBuffer(buf []byte) *Buffer

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

In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.

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.

In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.

func (*Buffer) Bytes

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

Bytes returns a slice of length b.Len() holding the unread portion of the buffer. The slice is valid for use only until the next buffer modification (that is, only until the next call to a method like Read, Write, Reset, or Truncate). The slice aliases the buffer content at least until the next buffer modification, so immediate changes to the slice will affect the result of future reads.

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap returns the capacity of the buffer's underlying byte slice, that is, the total space allocated for the buffer's data.

func (*Buffer) Grow

func (b *Buffer) Grow(n int)

Grow grows the buffer's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to the buffer without another allocation. If n is negative, Grow will panic. If the buffer can't grow it will panic with ErrTooLarge.

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) ReWrite

func (b *Buffer) ReWrite(pos int, p []byte)

ReWrite add a pos rewrite

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err 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 io.EOF (unless len(p) is zero); otherwise it is nil.

func (*Buffer) ReadByte

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

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

func (*Buffer) ReadFrom

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

ReadFrom reads data from r until EOF and appends it to the buffer, growing the buffer as needed. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned. If the buffer becomes too large, ReadFrom will panic with ErrTooLarge.

func (*Buffer) ReadRune

func (b *Buffer) ReadRune() (r rune, size int, err 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 io.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 to be empty, but it retains the underlying storage for use by future writes. Reset is the same as 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>".

To build strings more efficiently, see the strings.Builder type.

func (*Buffer) Truncate

func (b *Buffer) Truncate(n int)

Truncate discards all but the first n unread bytes from the buffer but continues to use the same allocated storage. It panics if n is negative or greater than the length of the buffer.

func (*Buffer) UnreadByte

func (b *Buffer) UnreadByte() error

UnreadByte unreads the last byte returned by the most recent successful read operation that read at least one byte. If a write has happened since the last read, if the last read returned an error, or if the read read zero bytes, UnreadByte returns an error.

func (*Buffer) UnreadRune

func (b *Buffer) UnreadRune() error

UnreadRune unreads the last rune returned by ReadRune. If the most recent read or write operation on the buffer was not a successful ReadRune, UnreadRune returns an error. (In this regard it is stricter than UnreadByte, which will unread the last byte from any read operation.)

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with ErrTooLarge.

func (*Buffer) WriteByte

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

WriteByte appends the byte c to the buffer, growing the buffer as needed. The returned error is always nil, but is included to match bufio.Writer's WriteByte. If the buffer becomes too large, WriteByte will panic with ErrTooLarge.

func (*Buffer) WriteRune

func (b *Buffer) WriteRune(r rune) (n int, err 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. The buffer is grown as needed; if it becomes too large, WriteRune will panic with ErrTooLarge.

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (n int, err error)

WriteString appends the contents of s to the buffer, growing the buffer as needed. The return value n is the length of s; err is always nil. If the buffer becomes too large, WriteString will panic with ErrTooLarge.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (n int64, err 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; it always fits into an int, but it is int64 to match the io.WriterTo interface. Any error encountered during the write is also returned.

type BufferX

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

BufferX buffer implement

func NewBufferX

func NewBufferX() *BufferX

NewBufferX new buffer with a default size as unpack

func NewBufferXFrom

func NewBufferXFrom(data []byte) *BufferX

NewBufferXFrom new buffer from an exist bytes as packer

func NewSpecBufferX

func NewSpecBufferX(n int) *BufferX

NewSpecBufferX new small

func (*BufferX) Bytes

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

Bytes all buffer bytes

func (*BufferX) Len

func (b *BufferX) Len() int

Len : buffer len

func (*BufferX) ReWrite

func (b *BufferX) ReWrite(pos int, p []byte)

ReWrite buffer

func (*BufferX) ReWriteU32

func (b *BufferX) ReWriteU32(pos int, v uint32)

ReWriteU32 rewrite uint32

func (*BufferX) Read

func (b *BufferX) Read(p []byte) error

read to a buffer

func (*BufferX) ReadBool

func (b *BufferX) ReadBool() (bool, error)

ReadBool read a bool

func (*BufferX) ReadF64

func (b *BufferX) ReadF64() (float64, error)

ReadF64 read float64

func (*BufferX) ReadI16

func (b *BufferX) ReadI16() (int16, error)

ReadI16 read int16

func (*BufferX) ReadI32

func (b *BufferX) ReadI32() (int32, error)

ReadI32 read int32

func (*BufferX) ReadI64

func (b *BufferX) ReadI64() (int64, error)

ReadI64 read int64

func (*BufferX) ReadLimitString

func (b *BufferX) ReadLimitString(limit uint32) (string, error)

ReadLimitString read limit size string

func (*BufferX) ReadN

func (b *BufferX) ReadN(n int) ([]byte, error)

ReadN read n length buffer

func (*BufferX) ReadString

func (b *BufferX) ReadString() (string, error)

ReadString read string

func (*BufferX) ReadU16

func (b *BufferX) ReadU16() (uint16, error)

ReadU16 read uint16

func (*BufferX) ReadU32

func (b *BufferX) ReadU32() (uint32, error)

ReadU32 read uint32

func (*BufferX) ReadU64

func (b *BufferX) ReadU64() (uint64, error)

ReadU64 read uint64

func (*BufferX) ReadU8

func (b *BufferX) ReadU8() (byte, error)

ReadU8 read a byte

func (*BufferX) Reset

func (b *BufferX) Reset()

Reset clear all buffer bytes

func (*BufferX) Write

func (b *BufferX) Write(p []byte)

write to a buffer

func (*BufferX) WriteBool

func (b *BufferX) WriteBool(v bool)

WriteBool write a bool

func (*BufferX) WriteF64

func (b *BufferX) WriteF64(v float64)

WriteF64 write float64

func (*BufferX) WriteI16

func (b *BufferX) WriteI16(v int16)

WriteI16 write int16

func (*BufferX) WriteI32

func (b *BufferX) WriteI32(v int32)

WriteI32 write int32

func (*BufferX) WriteI64

func (b *BufferX) WriteI64(v int64)

WriteI64 write int64

func (*BufferX) WriteLimitString

func (b *BufferX) WriteLimitString(limit uint32, val string) error

WriteLimitString write limit size string

func (*BufferX) WriteString

func (b *BufferX) WriteString(val string)

WriteString write string

func (*BufferX) WriteU16

func (b *BufferX) WriteU16(v uint16)

WriteU16 write uint16

func (*BufferX) WriteU32

func (b *BufferX) WriteU32(v uint32)

WriteU32 write uint32

func (*BufferX) WriteU64

func (b *BufferX) WriteU64(v uint64)

WriteU64 write uint64

func (*BufferX) WriteU8

func (b *BufferX) WriteU8(v byte)

WriteU8 write a byte

func (*BufferX) ZReadN

func (b *BufferX) ZReadN(n int) ([]byte, error)

ZReadN read n length buffer - no copy

type CompressType

type CompressType byte
const (
	CompressThreshHold              = 256
	CompressSnappy     CompressType = 128
)

type IBufferX

type IBufferX interface {
	//Len : length
	Len() int
	//Read specific p, if length is not enough, return error
	Read(p []byte) error
	//ReadN n bytes, if length is not enough, return error
	ReadN(n int) ([]byte, error)
	//Write to buffer
	Write(p []byte)
	//Bytes left bytes
	Bytes() []byte
	//Reset reset cursor
	Reset()

	//ReWrite a buffer
	ReWrite(pos int, p []byte)
	//ReWriteU32 rewrite with a specific pos
	ReWriteU32(pos int, v uint32)

	//ReadU8 read byte
	ReadU8() (byte, error)
	//WriteU8 write byte
	WriteU8(byte)

	//ReadBool read bool
	ReadBool() (bool, error)
	//WriteBool write bool
	WriteBool(bool)

	//ReadLimitString read string
	ReadLimitString(limit uint32) (string, error)
	//WriteLimitString write string
	WriteLimitString(limit uint32, val string) error

	//ReadString read string
	ReadString() (string, error)
	//WriteString write string
	WriteString(val string)

	//ReadU16 read uint16
	ReadU16() (uint16, error)
	//WriteU16 write uint16
	WriteU16(uint16)

	//ReadI16 read int16
	ReadI16() (int16, error)
	//WriteI16 write int16
	WriteI16(int16)

	//ReadU32 read uint32
	ReadU32() (uint32, error)
	//WriteU32 write uint32
	WriteU32(uint32)

	//ReadI32 read int32
	ReadI32() (int32, error)
	//WriteI32 write int32
	WriteI32(int32)

	//ReadU64 read uint64
	ReadU64() (uint64, error)
	//WriteU64 write uint64
	WriteU64(uint64)

	//ReadI64 read int64
	ReadI64() (int64, error)
	//WriteI64 write int64
	WriteI64(int64)

	//ReadF64 read float64
	ReadF64() (float64, error)
	//WriteF64 write float64
	WriteF64(float64)
}

IBufferX buffer interface

type IReaderX

type IReaderX interface {
	//Read : read specific p, if length is not enough, return error
	Read(p []byte) error
	//ReadN : read n bytes, if length is not enough, return error
	ReadN(n int) ([]byte, error)

	ZReadN(n int) ([]byte, error)

	//ReadByte : read byte
	ReadByte() (byte, error)
	//ReadBool : read bool
	ReadBool() (bool, error)

	//ReadLimitString : read string
	ReadLimitString(limit uint32) (string, error)

	//ReadString : read string
	ReadString() (string, error)

	//ReadU16 : read uint16
	ReadU16() (uint16, error)

	//ReadI16 : read int16
	ReadI16() (int16, error)

	//ReadU32 : read uint32
	ReadU32() (uint32, error)

	//ReadI32 : read int32
	ReadI32() (int32, error)

	//ReadU64 : read uint64
	ReadU64() (uint64, error)

	//ReadI64 : read int64
	ReadI64() (int64, error)

	//ReadF64 : read float64
	ReadF64() (float64, error)
}

IReaderX reader interface

type JsByte

type JsByte []byte

JsByte json could not support readable []byte use string to replace the byte array

func (*JsByte) FromString

func (i *JsByte) FromString(strBuf string) error

FromString from string

func (JsByte) MarshalJSON

func (i JsByte) MarshalJSON() ([]byte, error)

MarshalJSON marshal json

func (JsByte) ToJS

func (i JsByte) ToJS() []byte

ToJS split byte to string use '/' split

func (JsByte) ToString

func (i JsByte) ToString() string

ToString split byte to byte use '/' split

func (*JsByte) UnmarshalJSON

func (i *JsByte) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshal json

type JsInt64

type JsInt64 int64

JsInt64 json could not support large number use string to replace the number if a field is int64

func MapVal2JsInt64

func MapVal2JsInt64(m map[string]interface{}, k string) JsInt64

MapVal2JsInt64 : 将map中的value转换为 json int64

func ToJsInt64

func ToJsInt64(v interface{}) JsInt64

ToJsInt64 将基本类型转换为json int64整型

func (JsInt64) MarshalJSON

func (i JsInt64) MarshalJSON() ([]byte, error)

MarshalJSON marshal json

func (*JsInt64) UnmarshalJSON

func (i *JsInt64) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshal json

type JsUInt64

type JsUInt64 uint64

JsUInt64 json could not support large number use string to replace the number if a field is uint64

func (JsUInt64) MarshalJSON

func (i JsUInt64) MarshalJSON() ([]byte, error)

MarshalJSON marshal json

func (*JsUInt64) UnmarshalJSON

func (i *JsUInt64) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshal json

type ReaderX

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

ReaderX buffer implement

func NewReaderX

func NewReaderX(reader io.Reader) *ReaderX

NewReaderX new with io.reader

func (*ReaderX) Read

func (b *ReaderX) Read(p []byte) error

Read : read to a buffer

func (*ReaderX) ReadBool

func (b *ReaderX) ReadBool() (bool, error)

ReadBool read a bool

func (*ReaderX) ReadByte

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

ReadByte read a byte

func (*ReaderX) ReadF64

func (b *ReaderX) ReadF64() (float64, error)

ReadF64 read float64

func (*ReaderX) ReadI16

func (b *ReaderX) ReadI16() (int16, error)

ReadI16 read int16

func (*ReaderX) ReadI32

func (b *ReaderX) ReadI32() (int32, error)

ReadI32 read int32

func (*ReaderX) ReadI64

func (b *ReaderX) ReadI64() (int64, error)

ReadI64 read int64

func (*ReaderX) ReadLimitString

func (b *ReaderX) ReadLimitString(limit uint32) (string, error)

ReadLimitString read limit size string

func (*ReaderX) ReadN

func (b *ReaderX) ReadN(n int) ([]byte, error)

ReadN read n length buffer

func (*ReaderX) ReadString

func (b *ReaderX) ReadString() (string, error)

ReadString read string

func (*ReaderX) ReadU16

func (b *ReaderX) ReadU16() (uint16, error)

ReadU16 read uint16

func (*ReaderX) ReadU32

func (b *ReaderX) ReadU32() (uint32, error)

ReadU32 read uint32

func (*ReaderX) ReadU64

func (b *ReaderX) ReadU64() (uint64, error)

ReadU64 read uint64

func (*ReaderX) ZReadN

func (b *ReaderX) ZReadN(n int) ([]byte, error)

ZReadN read n length buffer - no copy

type U128BitMap

type U128BitMap struct {
	Low  U64BitMap
	High U64BitMap
}

U128BitMap use 2 uint64 Low and High

func (*U128BitMap) Bytes

func (u *U128BitMap) Bytes() []byte

func (*U128BitMap) FromBytes

func (u *U128BitMap) FromBytes(buf []byte)

func (*U128BitMap) Full

func (u *U128BitMap) Full() bool

func (*U128BitMap) Left

func (u *U128BitMap) Left() []byte

func (*U128BitMap) Len

func (u *U128BitMap) Len() int

func (*U128BitMap) ReverseLen

func (u *U128BitMap) ReverseLen() int

func (*U128BitMap) Set

func (u *U128BitMap) Set(i byte)

func (*U128BitMap) Values

func (u *U128BitMap) Values() []byte

type U64BitMap

type U64BitMap uint64

func (U64BitMap) Bytes

func (b U64BitMap) Bytes() []byte

func (*U64BitMap) FromBytes

func (b *U64BitMap) FromBytes(buf []byte)

func (U64BitMap) Full

func (b U64BitMap) Full() bool

func (U64BitMap) Left

func (b U64BitMap) Left() []byte

func (U64BitMap) LeftX

func (b U64BitMap) LeftX(plus byte) []byte

func (U64BitMap) Len

func (b U64BitMap) Len() int

func (U64BitMap) ReverseLen

func (b U64BitMap) ReverseLen() int

func (*U64BitMap) Set

func (b *U64BitMap) Set(i byte)

func (U64BitMap) Values

func (b U64BitMap) Values() []byte

func (U64BitMap) ValuesX

func (b U64BitMap) ValuesX(plus byte) []byte

type Unix2Time

type Unix2Time time.Time

Unix2Time 秒时间戳时间

func (*Unix2Time) Scan

func (s *Unix2Time) Scan(value interface{}) error

Scan : sql scan

func (Unix2Time) Value

func (s Unix2Time) Value() (driver.Value, error)

Value : sql value

type UnixNano2Time

type UnixNano2Time time.Time

UnixNano2Time 纳秒时间戳时间

func (*UnixNano2Time) Scan

func (s *UnixNano2Time) Scan(value interface{}) error

Scan : sql scan

func (UnixNano2Time) Value

func (s UnixNano2Time) Value() (driver.Value, error)

Value : sql value

type UnixStamp

type UnixStamp int64

UnixStamp 时间转成秒(只需要秒/Database中的datetime)

func (UnixStamp) MarshalJSON

func (i UnixStamp) MarshalJSON() ([]byte, error)

MarshalJSON marshal json

func (*UnixStamp) Scan

func (i *UnixStamp) Scan(value interface{}) error

Scan : sql scan

func (*UnixStamp) UnmarshalJSON

func (i *UnixStamp) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshal json

func (UnixStamp) Value

func (i UnixStamp) Value() (driver.Value, error)

Value : sql value

Jump to

Keyboard shortcuts

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