utils

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTLVRead is returned when there is an error reading a TLV object.
	ErrTLVRead = fmt.Errorf("TLV %s", "read error")
	// ErrTLVWrite is returned when  there is an error writing a TLV object.
	ErrTLVWrite = fmt.Errorf("TLV %s", "write error")
	// ErrTypeNotFound is returned when a request for a TLV type is made and none can be found.
	ErrTypeNotFound = fmt.Errorf("TLV %s", "type not found")
)
View Source
var ErrInvalidUtf8Rune = errors.New("Not Invalid Utf8 runes")
View Source
var NumTable = [16]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}

Functions

func BcdToString

func BcdToString(bcd []byte) string

func Bytes2StringSlice added in v0.2.0

func Bytes2StringSlice(in []byte, pl int) (ret []string)

func CopyByte added in v0.2.0

func CopyByte(dest []byte, src byte, index int) int

func CopyStr added in v0.2.0

func CopyStr(dest []byte, src string, index int, len int) int

func DiceCheck

func DiceCheck(prob float64) bool

DiceCheck 投概率骰子,得到结果比给定数字大则返回true,否则返回false

func Equal

func Equal(tlv1, tlv2 TLV) bool

Equal returns true if a pair of TLV objects are the same.

func FormatTime added in v0.2.0

func FormatTime(time time.Time) string

func GB18030ToUtf8

func GB18030ToUtf8(in string) (string, error)

func IntToFixStr

func IntToFixStr(i int64, l int) string

func IsBigEndian

func IsBigEndian() bool

func Now

func Now() (string, uint32)

func OctetString

func OctetString(s string, fixedLength int) string

func RandNum

func RandNum(min, max int) int

func StoBcd

func StoBcd(w string) []byte

func TimeStamp2Str

func TimeStamp2Str(t uint32) string

TimeStamp2Str converts a timestamp(MMDDHHMMSS) int to a string(10 bytes).

func ToBytes

func ToBytes(tlv TLV) ([]byte, error)

ToBytes returns bytes from a TLV object

func ToTPUDHISlices

func ToTPUDHISlices(content []byte, pkgLen int) (rt [][]byte)

ToTPUDHISlices 拆分为长短信切片 纯ASCII内容的拆分 pkgLen = 160 含中文内容的拆分 pkgLen = 140

func TrimOctetString

func TrimOctetString(in []byte) []byte

func TrimStr

func TrimStr(in []byte) string

func Ucs2ToUtf8

func Ucs2ToUtf8(in []byte) (string, error)

func Uint32HexString added in v0.2.0

func Uint32HexString(i uint32) string

func Uint64HexString added in v0.2.0

func Uint64HexString(i uint64) string

func Utf8ToGB18030

func Utf8ToGB18030(in string) (string, error)

func Utf8ToUcs2

func Utf8ToUcs2(in string) ([]byte, error)

func WriteObject

func WriteObject(tlv TLV, w io.Writer) error

WriteObject writes a TLV object to io.Writer

Types

type BcdSequence

type BcdSequence struct {
	sync.Mutex // 锁
	// contains filtered or unexported fields
}

BcdSequence 电信MsgId序号生成器,支持每分钟产生100万个不重复序号,超出后序号会重复 BCD 4bit编码,用4bit表示0-9的数字

func NewBcdSequence

func NewBcdSequence(w string) *BcdSequence

func (*BcdSequence) NextVal

func (tf *BcdSequence) NextVal() []byte

type CycleSequence

type CycleSequence struct {
	sync.Mutex // 锁
	// contains filtered or unexported fields
}

CycleSequence 生成可循环使用的序号 构成为: 0 | datacenter 2 bit | worker 3 bit| sequence 26 bit 最大支持32个节点,单节点2^26以内不会重复(67,108,864)

func NewCycleSequence

func NewCycleSequence(d int32, w int32) *CycleSequence

NewCycleSequence d for datacenter-id, w for worker-id

func (*CycleSequence) NextVal

func (s *CycleSequence) NextVal() int32

func (*CycleSequence) String

func (s *CycleSequence) String() string

type TLV

type TLV interface {
	Type() uint16
	Length() uint16
	Value() []byte
}

TLV represents a Type-Length-Value object.

func FromBytes

func FromBytes(data []byte) (TLV, error)

FromBytes returns a TLV object from bytes

func New

func New(typ uint16, val []byte) TLV

New returns a TLV object from the args

func ReadObject

func ReadObject(r io.Reader) (TLV, error)

ReadObject returns a TLV object from io.Reader

type TlvList

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

TlvList is ad double-linked list containing TLV objects.

func NewTlvList

func NewTlvList() *TlvList

NewTlvList returns a new, empty TLVList.

func Read

func Read(r io.Reader) (*TlvList, error)

Read takes an io.Reader and builds a TLVList from that.

func (*TlvList) Add

func (tl *TlvList) Add(typ uint16, value []byte)

Add pushes a new TLV object onto the TLVList. It builds the object from its args

func (*TlvList) AddObject

func (tl *TlvList) AddObject(obj TLV)

AddObject adds a TLV object onto the TLVList

func (*TlvList) Get

func (tl *TlvList) Get(typ uint16) (TLV, error)

Get checks the TLVList for any object matching the type, It returns the first one found. If the type could not be found, Get returns ErrTypeNotFound.

func (*TlvList) GetAll

func (tl *TlvList) GetAll(typ uint16) []TLV

GetAll checks the TLVList for all objects matching the type, returning a slice containing all matching objects. If no object has the requested type, an empty slice is returned.

func (*TlvList) Length

func (tl *TlvList) Length() int32

Length returns the number of objects int the TLVList.

func (*TlvList) Remove

func (tl *TlvList) Remove(typ uint16) int

Remove removes all objects with the requested type. It returns a count of the number of removed objects.

func (*TlvList) RemoveObject

func (tl *TlvList) RemoveObject(obj TLV) int

RemoveObject takes an TLV object as an argument, and removes all matching objects. It matches on not just type, but also the value contained in the object.

func (*TlvList) String

func (tl *TlvList) String() string

func (*TlvList) Write

func (tl *TlvList) Write(w io.Writer) error

Write writes out the TLVList to an io.Writer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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