Documentation ¶
Index ¶
- Constants
- Variables
- func B2S(b []byte) string
- func CacheSize[K comparable, T Sizer](cache *Cache[K, T]) (size int64)
- func Envfmt(fpath string, envmap map[string]string) string
- func JoinFilePath(dir, base string) string
- func JoinPath(dir, base string) string
- func PathName(fpath string) string
- func S2B(s string) []byte
- func ToID(s string) string
- func ToKey(s string) string
- func ToLower(s string) string
- func ToSlash(s string) string
- func ToUpper(s string) string
- type Bimap
- type Bitset128
- func (bs *Bitset128) And(mask Bitset128) *Bitset128
- func (bs *Bitset128) AndNot(mask Bitset128) *Bitset128
- func (bs *Bitset128) Bits() iter.Seq[int]
- func (bs *Bitset128) Expand() []int
- func (bs *Bitset128) Is(n int) bool
- func (bs *Bitset128) IsZero() bool
- func (bs *Bitset128) LShift(count int) *Bitset128
- func (bs *Bitset128) Next(n int) int
- func (bs *Bitset128) Num() (count int)
- func (bs *Bitset128) Or(mask Bitset128) *Bitset128
- func (bs *Bitset128) Pack(idx []int) *Bitset128
- func (bs *Bitset128) Res(n int) *Bitset128
- func (bs *Bitset128) Set(n int) *Bitset128
- func (bs *Bitset128) SetNum(count, from int) *Bitset128
- func (bs *Bitset128) Toggle(n int) *Bitset128
- func (bs *Bitset128) Xor(mask Bitset128) *Bitset128
- type Bitset64
- func (bs *Bitset64) And(mask Bitset64) *Bitset64
- func (bs *Bitset64) AndNot(mask Bitset64) *Bitset64
- func (bs Bitset64) Bits() iter.Seq[int]
- func (bs *Bitset64) Expand() []int
- func (bs Bitset64) Is(n int) bool
- func (bs Bitset64) IsZero() bool
- func (bs Bitset64) Next(n int) int
- func (bs Bitset64) Num() int
- func (bs *Bitset64) Or(mask Bitset64) *Bitset64
- func (bs *Bitset64) Pack(idx []int) *Bitset64
- func (bs *Bitset64) Res(n int) *Bitset64
- func (bs *Bitset64) Set(n int) *Bitset64
- func (bs *Bitset64) SetNum(count, from int) *Bitset64
- func (bs *Bitset64) Toggle(n int) *Bitset64
- func (bs *Bitset64) Xor(mask Bitset64) *Bitset64
- type Cache
- func (c *Cache[K, T]) Free(n int)
- func (c *Cache[K, T]) Get(key K) (ret T, ok bool)
- func (c *Cache[K, T]) Has(key K) (ok bool)
- func (c *Cache[K, T]) Items() iter.Seq2[K, T]
- func (c *Cache[K, T]) Len() int
- func (c *Cache[K, T]) OnRemove(efn func(K, T))
- func (c *Cache[K, T]) Peek(key K) (ret T, ok bool)
- func (c *Cache[K, T]) Poke(key K, val T)
- func (c *Cache[K, T]) Remove(key K) (ok bool)
- func (c *Cache[K, T]) Set(key K, val T)
- func (c *Cache[K, T]) ToLimit(limit int)
- func (c *Cache[K, T]) Until(f func(K, T) bool)
- type RWMap
- func (rwm *RWMap[K, T]) Delete(key K)
- func (rwm *RWMap[K, T]) Get(key K) (ret T, ok bool)
- func (rwm *RWMap[K, T]) GetAndDelete(key K) (ret T, ok bool)
- func (rwm *RWMap[K, T]) Has(key K) (ok bool)
- func (rwm *RWMap[K, T]) Init(capacity int)
- func (rwm *RWMap[K, T]) Items() iter.Seq2[K, T]
- func (rwm *RWMap[K, T]) Len() int
- func (rwm *RWMap[K, T]) Set(key K, val T)
- type Sizer
- type SqlBuf
Examples ¶
Constants ¶
const PathSeparator = string(os.PathSeparator)
OS-specific path separator string
Variables ¶
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.
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 CacheSize ¶
func CacheSize[K comparable, T Sizer](cache *Cache[K, T]) (size int64)
CacheSize returns size of given cache.
func Envfmt ¶
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 ¶
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 ¶
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 ¶
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 ToID ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
DeleteDir deletes key-value pair pointed by key, and returns deleted value.
func (*Bimap[K, T]) DeleteRev ¶
DeleteRev deletes key-value pair pointed by value, and returns deleted key.
type Bitset128 ¶ added in v0.4.0
type Bitset128 [2]uint64
Bitset128 is bitset on 128 bites.
func MakeBitNum128 ¶ added in v0.4.0
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
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) Expand ¶ added in v0.4.0
Expand returns bitset converted to slice with integer indexes.
func (*Bitset128) Next ¶ added in v0.4.0
Next helps iterate bits with no allocations as followed:
for n := bs.Next(-1); n != -1; n = bs.Next(n) {}
type Bitset64 ¶ added in v0.4.0
type Bitset64 uint64
Bitset64 is bitset on 64 bites.
func MakeBitNum64 ¶ added in v0.4.0
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
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) Expand ¶ added in v0.4.0
Expand returns bitset converted to slice with integer indexes.
func (Bitset64) Next ¶ added in v0.4.0
Next helps iterate bits with no allocations as followed:
for n := bs.Next(-1); n != -1; n = bs.Next(n) {}
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]) Get ¶
Get returns value pointed by given key, and brings the pair to top of cache.
func (*Cache[K, T]) Items ¶ added in v0.4.0
Items returns iterator for all key-value pairs. Iterator makes copy of the state and then yields for each pair.
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.
type RWMap ¶
type RWMap[K comparable, T any] struct { // contains filtered or unexported fields }
RWMap is threads safe map.
func (*RWMap[K, T]) GetAndDelete ¶
GetAndDelete removes pair from map and returns the value if it was.
type Sizer ¶
type Sizer interface {
Size() int64
}
Sizer is interface that determine structure size itself.