util

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const PathSeparator = string(os.PathSeparator)

OS-specific path separator string

Variables

View Source
var VarChar [256]bool = func() (a [256]bool) {
	a['_'] = true
	for c := 'A'; c <= 'Z'; c++ {
		a[c] = true
		a[c+32] = true
	}
	for c := '0'; c <= '9'; c++ {
		a[c] = true
	}
	return
}()

VarChar is table for fast check that ASCII code is acceptable symbol of variable.

View Source
var VarCharFirst [256]bool = func() (a [256]bool) {
	a['_'] = true
	for c := 'A'; c <= 'Z'; c++ {
		a[c] = true
		a[c+32] = true
	}
	return
}()

VarCharFirst is table for fast check that ASCII code is acceptable first symbol of variable.

Functions

func B2S

func B2S(b []byte) string

B2S converts bytes slice to string without memory allocation.

func CacheSize

func CacheSize[K comparable, T Sizer](cache *Cache[K, T]) (size int64)

CacheSize returns size of given cache.

func Envfmt

func Envfmt(fpath string, envmap map[string]string) string

Envfmt replaces environment variables entries in file path to there values. Environment variables must be followed by those 3 patterns: $VAR, ${VAR}, %VAR%. Environment variables are looked at first in 'envmap' if it given, and then by os-call. This function works by two string passes, without superfluous memory allocations.

Example
package main

import (
	"fmt"
	"os"

	"github.com/slotopol/server/util"
)

func main() {
	os.Setenv("VAR", "/go")
	// successful patterns
	fmt.Println(util.Envfmt("$VAR/bin/", nil))
	fmt.Println(util.Envfmt("${VAR}/bin/", nil))
	fmt.Println(util.Envfmt("%VAR%/bin/", nil))
	fmt.Println(util.Envfmt("/home$VAR", nil))
	fmt.Println(util.Envfmt("/home%VAR%", map[string]string{"VAR": "/any/path"}))
	fmt.Println(util.Envfmt("$VAR%VAR%${VAR}", nil))
	// patterns with unknown variable
	fmt.Println(util.Envfmt("$VYR/bin/", nil))
	fmt.Println(util.Envfmt("${VAR}/${_foo_}", nil))
	// patterns with errors
	fmt.Println(util.Envfmt("$VAR$/bin/", nil))
	fmt.Println(util.Envfmt("${VAR/bin/", nil))
	fmt.Println(util.Envfmt("%VAR/bin/", nil))
	fmt.Println(util.Envfmt("/home${VAR", nil))
}
Output:

/go/bin/
/go/bin/
/go/bin/
/home/go
/home/any/path
/go/go/go
$VYR/bin/
/go/${_foo_}
/go$/bin/
${VAR/bin/
%VAR/bin/
/home${VAR

func JoinFilePath

func JoinFilePath(dir, base string) string

JoinFilePath performs fast join of two file path chunks. In some cases concatenates with OS-specific separator.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Println(util.JoinFilePath("dir/", "base.ext"))
	fmt.Println(util.JoinFilePath("dir", "/base.ext"))
	fmt.Println(util.JoinFilePath("dir/", "/base.ext"))
}
Output:

dir/base.ext
dir/base.ext
dir/base.ext

func JoinPath

func JoinPath(dir, base string) string

JoinPath performs fast join of two UNIX-like path chunks.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Println(util.JoinPath("dir", "base.ext"))
	fmt.Println(util.JoinPath("dir/", "base.ext"))
	fmt.Println(util.JoinPath("dir", "/base.ext"))
	fmt.Println(util.JoinPath("dir/", "/base.ext"))
}
Output:

dir/base.ext
dir/base.ext
dir/base.ext
dir/base.ext

func PathName

func PathName(fpath string) string

PathName returns name of file in given file path without extension.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Println(util.PathName("C:\\Windows\\system.ini"))
	fmt.Println(util.PathName("/go/bin/wpkbuild_win_x64.exe"))
	fmt.Println(util.PathName("wpkbuild_win_x64.exe"))
	fmt.Println(util.PathName("/go/bin/wpkbuild_linux_x64"))
	fmt.Println(util.PathName("wpkbuild_linux_x64"))
	fmt.Printf("'%s'\n", util.PathName("/go/bin/"))
}
Output:

system
wpkbuild_win_x64
wpkbuild_win_x64
wpkbuild_linux_x64
wpkbuild_linux_x64
''

func S2B

func S2B(s string) []byte

S2B converts string to bytes slice without memory allocation.

func ToID

func ToID(s string) string

ToID is high performance function to bring filenames to lower case identifier with only letters, digits and '_', without superfluous allocations if it possible.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Println(util.ToID("Joker Dolphin"))
	fmt.Println(util.ToID("Lucky Lady's Charm"))
	fmt.Println(util.ToID("BetSoft/2 Million B.C."))
}
Output:

jokerdolphin
luckyladyscharm
betsoft/2millionbc

func ToKey

func ToKey(s string) string

ToKey is high performance function to bring filenames to lower case in ASCII and true slashes at once without superfluous allocations if it possible.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Println(util.ToKey("C:\\Windows\\Temp"))
}
Output:

c:/windows/temp

func ToLower

func ToLower(s string) string

ToLower is high performance function to bring filenames to lower case in ASCII without superfluous allocations if it possible.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Println(util.ToLower("C:\\Windows\\Temp"))
}
Output:

c:\windows\temp

func ToSlash

func ToSlash(s string) string

ToSlash brings filenames to true slashes without superfluous allocations if it possible.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Println(util.ToSlash("C:\\Windows\\Temp"))
}
Output:

C:/Windows/Temp

func ToUpper

func ToUpper(s string) string

ToUpper is high performance function to bring filenames to upper case in ASCII without superfluous allocations if it possible.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Println(util.ToUpper("C:\\Windows\\Temp"))
}
Output:

C:\WINDOWS\TEMP

Types

type Bimap

type Bimap[K comparable, T comparable] struct {
	// contains filtered or unexported fields
}

Bimap is bidirectional threads safe map.

func NewBimap

func NewBimap[K comparable, T comparable]() *Bimap[K, T]

NewBimap returns pointer to new Bimap object.

func (*Bimap[K, T]) DeleteDir

func (m *Bimap[K, T]) DeleteDir(key K) (val T, ok bool)

DeleteDir deletes key-value pair pointed by key, and returns deleted value.

func (*Bimap[K, T]) DeleteRev

func (m *Bimap[K, T]) DeleteRev(val T) (key K, ok bool)

DeleteRev deletes key-value pair pointed by value, and returns deleted key.

func (*Bimap[K, T]) GetDir

func (m *Bimap[K, T]) GetDir(key K) (val T, ok bool)

GetDir returns element in direct order, i.e. value pointed by key.

func (*Bimap[K, T]) GetRev

func (m *Bimap[K, T]) GetRev(val T) (key K, ok bool)

GetRev returns element in reverse order, i.e. key pointed by value.

func (*Bimap[K, T]) Len

func (m *Bimap[K, T]) Len() int

Len returns number of key-value pairs.

func (*Bimap[K, T]) Set

func (m *Bimap[K, T]) Set(key K, val T)

Set inserts key-value pair into map.

type Bitset128 added in v0.4.0

type Bitset128 [2]uint64

Bitset128 is bitset on 128 bites.

func MakeBitNum128 added in v0.4.0

func MakeBitNum128(count, from int) (bs Bitset128)

MakeBitNum128 creates bits set with first num bits.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	var bs = util.MakeBitNum128(77, 3)
	fmt.Printf("bitnum(77, 3) is %b%b", bs[1], bs[0])
}
Output:

bitnum(77, 3) is 11111111111111111111111111111111111111111111111111111111111111111111111111111000

func MakeBitset128 added in v0.4.0

func MakeBitset128(idx ...int) (bs Bitset128)

MakeBitset128 creates bits set from slice of integer indexes.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	var bs = util.MakeBitset128(1, 2, 3, 5, 14, 8, 63, 64, 65, 125, 127)
	fmt.Printf("bitset(1, 2, 3, 5, 14, 8, 63, 64, 65, 125, 127) is 0x%016x%016x", bs[1], bs[0])
}
Output:

bitset(1, 2, 3, 5, 14, 8, 63, 64, 65, 125, 127) is 0xa000000000000003800000000000412e

func (*Bitset128) And added in v0.4.0

func (bs *Bitset128) And(mask Bitset128) *Bitset128

func (*Bitset128) AndNot added in v0.4.0

func (bs *Bitset128) AndNot(mask Bitset128) *Bitset128

func (*Bitset128) Bits added in v0.4.0

func (bs *Bitset128) Bits() iter.Seq[int]

Bits iterates over ones in bitset.

func (*Bitset128) Expand added in v0.4.0

func (bs *Bitset128) Expand() []int

Expand returns bitset converted to slice with integer indexes.

func (*Bitset128) Is added in v0.4.0

func (bs *Bitset128) Is(n int) bool

Is checks that bit with given number is set.

func (*Bitset128) IsZero added in v0.4.0

func (bs *Bitset128) IsZero() bool

func (*Bitset128) LShift added in v0.4.0

func (bs *Bitset128) LShift(count int) *Bitset128

LShift implements left shift of bitset.

func (*Bitset128) Next added in v0.4.0

func (bs *Bitset128) Next(n int) int

Next helps iterate bits with no allocations as followed:

for n := bs.Next(-1); n != -1; n = bs.Next(n) {}

func (*Bitset128) Num added in v0.4.0

func (bs *Bitset128) Num() (count int)

Num returns number of one bits in set.

func (*Bitset128) Or added in v0.4.0

func (bs *Bitset128) Or(mask Bitset128) *Bitset128

func (*Bitset128) Pack added in v0.4.0

func (bs *Bitset128) Pack(idx []int) *Bitset128

Pack sets bits by slice of integer indexes.

func (*Bitset128) Res added in v0.4.0

func (bs *Bitset128) Res(n int) *Bitset128

Res resets bit with given number.

func (*Bitset128) Set added in v0.4.0

func (bs *Bitset128) Set(n int) *Bitset128

Set bit with given number.

func (*Bitset128) SetNum added in v0.4.0

func (bs *Bitset128) SetNum(count, from int) *Bitset128

SetNum sets first n bits.

func (*Bitset128) Toggle added in v0.4.0

func (bs *Bitset128) Toggle(n int) *Bitset128

Toggle bit with given number.

func (*Bitset128) Xor added in v0.4.0

func (bs *Bitset128) Xor(mask Bitset128) *Bitset128

type Bitset64 added in v0.4.0

type Bitset64 uint64

Bitset64 is bitset on 64 bites.

func MakeBitNum64 added in v0.4.0

func MakeBitNum64(count, from int) Bitset64

MakeBitNum creates bits set with first num bits.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Printf("bitnum(7, 3) is %b", util.MakeBitNum64(7, 3))
}
Output:

bitnum(7, 3) is 1111111000

func MakeBitset64 added in v0.4.0

func MakeBitset64(idx ...int) (bs Bitset64)

MakeBitset creates bits set from slice of integer indexes.

Example
package main

import (
	"fmt"

	"github.com/slotopol/server/util"
)

func main() {
	fmt.Printf("bitset(1, 2, 3, 5, 14, 8) is 0b%016b", util.MakeBitset64(1, 2, 3, 5, 14, 8))
}
Output:

bitset(1, 2, 3, 5, 14, 8) is 0b0100000100101110

func (*Bitset64) And added in v0.4.0

func (bs *Bitset64) And(mask Bitset64) *Bitset64

func (*Bitset64) AndNot added in v0.4.0

func (bs *Bitset64) AndNot(mask Bitset64) *Bitset64

func (Bitset64) Bits added in v0.4.0

func (bs Bitset64) Bits() iter.Seq[int]

Bits iterates over ones in bitset.

func (*Bitset64) Expand added in v0.4.0

func (bs *Bitset64) Expand() []int

Expand returns bitset converted to slice with integer indexes.

func (Bitset64) Is added in v0.4.0

func (bs Bitset64) Is(n int) bool

Is checks that bit with given number is set.

func (Bitset64) IsZero added in v0.4.0

func (bs Bitset64) IsZero() bool

func (Bitset64) Next added in v0.4.0

func (bs Bitset64) Next(n int) int

Next helps iterate bits with no allocations as followed:

for n := bs.Next(-1); n != -1; n = bs.Next(n) {}

func (Bitset64) Num added in v0.4.0

func (bs Bitset64) Num() int

Num returns number of one bits in set.

func (*Bitset64) Or added in v0.4.0

func (bs *Bitset64) Or(mask Bitset64) *Bitset64

func (*Bitset64) Pack added in v0.4.0

func (bs *Bitset64) Pack(idx []int) *Bitset64

Pack sets bits by slice of integer indexes.

func (*Bitset64) Res added in v0.4.0

func (bs *Bitset64) Res(n int) *Bitset64

Res resets bit with given number.

func (*Bitset64) Set added in v0.4.0

func (bs *Bitset64) Set(n int) *Bitset64

Set bit with given number.

func (*Bitset64) SetNum added in v0.4.0

func (bs *Bitset64) SetNum(count, from int) *Bitset64

SetNum sets first n bits.

func (*Bitset64) Toggle added in v0.4.0

func (bs *Bitset64) Toggle(n int) *Bitset64

Toggle bit with given number.

func (*Bitset64) Xor added in v0.4.0

func (bs *Bitset64) Xor(mask Bitset64) *Bitset64

type Cache

type Cache[K comparable, T any] struct {
	// contains filtered or unexported fields
}

Cache is LRU & FIFO threads safe cache.

func NewCache

func NewCache[K comparable, T any]() *Cache[K, T]

NewCache returns pointer to new Cache object.

func (*Cache[K, T]) Free

func (c *Cache[K, T]) Free(n int)

Free removes n first entries from cache.

func (*Cache[K, T]) Get

func (c *Cache[K, T]) Get(key K) (ret T, ok bool)

Get returns value pointed by given key, and brings the pair to top of cache.

func (*Cache[K, T]) Has

func (c *Cache[K, T]) Has(key K) (ok bool)

Has detects whether pair is present.

func (*Cache[K, T]) Items added in v0.4.0

func (c *Cache[K, T]) Items() iter.Seq2[K, T]

Items returns iterator for all key-value pairs. Iterator makes copy of the state and then yields for each pair.

func (*Cache[K, T]) Len

func (c *Cache[K, T]) Len() int

Len returns number of key-value pairs.

func (*Cache[K, T]) OnRemove

func (c *Cache[K, T]) OnRemove(efn func(K, T))

OnRemove changes callback function that is called when removing a pair.

func (*Cache[K, T]) Peek

func (c *Cache[K, T]) Peek(key K) (ret T, ok bool)

Peek returns value pointed by given key.

func (*Cache[K, T]) Poke

func (c *Cache[K, T]) Poke(key K, val T)

func (*Cache[K, T]) Remove

func (c *Cache[K, T]) Remove(key K) (ok bool)

func (*Cache[K, T]) Set

func (c *Cache[K, T]) Set(key K, val T)

func (*Cache[K, T]) ToLimit

func (c *Cache[K, T]) ToLimit(limit int)

ToLimit brings cache to limited count of entries.

func (*Cache[K, T]) Until

func (c *Cache[K, T]) Until(f func(K, T) bool)

Until removes first some entries from cache until given func returns true.

type RWMap

type RWMap[K comparable, T any] struct {
	// contains filtered or unexported fields
}

RWMap is threads safe map.

func (*RWMap[K, T]) Delete

func (rwm *RWMap[K, T]) Delete(key K)

Delete removes pair from map.

func (*RWMap[K, T]) Get

func (rwm *RWMap[K, T]) Get(key K) (ret T, ok bool)

Get returns value by pointed key.

func (*RWMap[K, T]) GetAndDelete

func (rwm *RWMap[K, T]) GetAndDelete(key K) (ret T, ok bool)

GetAndDelete removes pair from map and returns the value if it was.

func (*RWMap[K, T]) Has

func (rwm *RWMap[K, T]) Has(key K) (ok bool)

Has detects whether pair is present.

func (*RWMap[K, T]) Init

func (rwm *RWMap[K, T]) Init(capacity int)

func (*RWMap[K, T]) Items added in v0.4.0

func (rwm *RWMap[K, T]) Items() iter.Seq2[K, T]

Items returns iterator for all key-value pairs. Iterator makes copy of the state and then yields for each pair.

func (*RWMap[K, T]) Len

func (rwm *RWMap[K, T]) Len() int

Len returns number of key-value pairs.

func (*RWMap[K, T]) Set

func (rwm *RWMap[K, T]) Set(key K, val T)

Set inserts given key-value pair.

type Sizer

type Sizer interface {
	Size() int64
}

Sizer is interface that determine structure size itself.

type SqlBuf added in v0.3.0

type SqlBuf[T any] struct {
	// contains filtered or unexported fields
}

func (*SqlBuf[T]) Flush added in v0.3.0

func (sb *SqlBuf[T]) Flush(engine *xorm.Engine, d time.Duration) (err error)

func (*SqlBuf[T]) Init added in v0.3.0

func (sb *SqlBuf[T]) Init(capacity int)

func (*SqlBuf[T]) Last added in v0.3.0

func (sb *SqlBuf[T]) Last() time.Time

func (*SqlBuf[T]) Len added in v0.3.0

func (sb *SqlBuf[T]) Len() int

func (*SqlBuf[T]) Put added in v0.3.0

func (sb *SqlBuf[T]) Put(engine *xorm.Engine, val T) (err error)

Jump to

Keyboard shortcuts

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