Documentation
¶
Index ¶
- Constants
- Variables
- func B2S(b []byte) string
- 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
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 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/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 ¶
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 ¶
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 ¶
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 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/bot/util"
)
func main() {
fmt.Println(util.ToID("Joker Dolphin"))
fmt.Println(util.ToID("Lucky Lady's Charm"))
}
Output: jokerdolphin luckyladyscharm
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/bot/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/bot/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/bot/util"
)
func main() {
fmt.Println(util.ToSlash("C:\\Windows\\Temp"))
}
Output: C:/Windows/Temp
Types ¶
This section is empty.