util

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: MIT Imports: 2 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 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/bot/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/bot/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/bot/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/bot/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/bot/util"
)

func main() {
	fmt.Println(util.ToID("Joker Dolphin"))
	fmt.Println(util.ToID("Lucky Lady's Charm"))
}
Output:

jokerdolphin
luckyladyscharm

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/bot/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/bot/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/bot/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/bot/util"
)

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

C:\WINDOWS\TEMP

Types

This section is empty.

Jump to

Keyboard shortcuts

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