helper

package
v1.17.10 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package helper is a set common helper functions

Package helper is a set common helper functions

Package helper is a set common helper functions

Package helper is a set common helper functions

Index

Constants

View Source
const InvalidFileNameChars = "\"'`:*?><|$}{@&^;%/\\" // invalid or dangerous file name or URL characters
View Source
const InvalidFilePathChars = "\"'`:*?><|$}{@&^;%" // invalid or dangerous file path or URL characters
View Source
const TimeStampLength = len("2012_08_17_16_04_59_123") // length of timestap string

Variables

View Source
var (
	Utf8bom    = []byte{0xEF, 0xBB, 0xBF}
	Utf16LEbom = []byte{0xFF, 0xFE}
	Utf16BEbom = []byte{0xFE, 0xFF}
	Utf32LEbom = []byte{0xFF, 0xFE, 0x00, 0x00}
	Utf32BEbom = []byte{0x00, 0x00, 0xFE, 0xFF}
)

byte order mark bytes

Functions

func CleanFileName added in v1.17.3

func CleanFileName(src string) string

replace special file name characters: "'`:*?><|$}{@&^;/\ by _ underscore

func CleanFilePath added in v1.17.4

func CleanFilePath(src string) string

replace special file name characters: "'`:*?><|$}{@&^; by _ underscore

func DeepCopy

func DeepCopy(src interface{}, dst interface{}) error

DeepCopy using gob to make a deep copy from src into dst, both src and dst expected to be a pointers

func DirStat added in v1.17.10

func DirStat(dirPath string) (fs.FileInfo, error)

return file Stat if this is a directory

func ErrorFmt added in v1.17.10

func ErrorFmt(format string, msg ...any) error

short form of: errors.New(helper.Fmt(format, msg))

func ErrorFmtL added in v1.17.10

func ErrorFmtL(lang, format string, msg ...any) error

short form of: errors.New(helper.FmtL(lang, format, msg))

func ErrorNew added in v1.17.10

func ErrorNew(msg ...any) error

short form of: errors.New(helper.Msg(msg))

func ErrorNewL added in v1.17.10

func ErrorNewL(lang string, msg ...any) error

short form of: errors.New(helper.MsgL(lang, msg))

func FileStat added in v1.17.10

func FileStat(filePath string) (fs.FileInfo, error)

return file Stat if this is a regular file

func FileToUtf8

func FileToUtf8(filePath string, encodingName string) (string, error)

FileToUtf8 read file content and convert it to UTF-8 string.

If file starts with BOM (utf-8 utf-16LE utf-16BE utf-32LE utf-32BE) then BOM is used. If no BOM and encodingName is "" empty then file content probed to see is it already utf-8. If encodingName explicitly specified then it is used to convert file content to string. If none of above then assume default encoding: "windows-1252" on Windows and "utf-8" on Linux.

func Fmt added in v1.17.10

func Fmt(format string, msg ...any) string

return string formatted by Sprintf translate format string using message.ini content, use language specific printer instead of fmt by default

func FmtL added in v1.17.10

func FmtL(lang, format string, msg ...any) string

return string formatted by Sprintf translate format string using message.ini content, use language specific printer instead of fmt by default

func FromJson

func FromJson(srcJson []byte, dst interface{}) (bool, error)

FromJson restore from json string bytes and convert to destination pointer.

func FromJsonFile

func FromJsonFile(jsonPath string, dst interface{}) (bool, error)

FromJsonFile reads read from json file and convert to destination pointer.

func FromUnderscoreTimeStamp added in v1.11.0

func FromUnderscoreTimeStamp(src string) string

FromUnderscoreTimeStamp converts timestamp string to date-time string: 2021_07_16_13_40_53_882 into 2021-07-16 13:40:53.882 If source string is not a date-time string then return empty "" string

func Gcd added in v1.11.0

func Gcd(src []int) int

return greatest common divisor of all src[]

func Gcd2 added in v1.11.0

func Gcd2(a, b int) int

greatest common divisor of two integers

func IniSectionList added in v1.17.10

func IniSectionList(eaIni []IniEntry) []string

return ini-file sections

func IsDir added in v1.17.10

func IsDir(dirPath string) (bool, error)

return true if path exists and it is directory, return false if path not exists, return error if path is not a directory or not accessible

func IsDirExist added in v1.17.0

func IsDirExist(dirPath string) bool

return true if path exists and it is directory, return false if path is not a directory or not accessible

func IsFileExist added in v1.17.10

func IsFileExist(filePath string) bool

fileExist return error if file not exist, not accessible or it is not a regular file

func IsTimeStamp added in v1.13.0

func IsTimeStamp(src string) bool

IsTimeStamp return true if src is timestamp string: 2021-07-16 13:40:53.882

func IsUnderscoreTimeStamp added in v1.11.0

func IsUnderscoreTimeStamp(src string) bool

IsUnderscoreTimeStamp return true if src is underscore timestamp string: 2021_07_16_13_40_53_882

func LT added in v1.17.10

func LT(src string) string

return translated string, if translation not found then retrun source string

func LTL added in v1.17.10

func LTL(lang, src string) string

return translated string, if translation not found then retrun source string

func MakeDateTime

func MakeDateTime(t time.Time) string

MakeDateTime return date-time string, ie: 2012-08-17 16:04:59.148

func MakeTimeStamp

func MakeTimeStamp(t time.Time) string

MakeTimeStamp return timestamp string as: 2012_08_17_16_04_59_148

func Msg added in v1.17.10

func Msg(msg ...any) string

Join items by space, use Sprint for each item, translate first msg[0] item using message.ini content, use language specific printer instead of fmt by default

func MsgL added in v1.17.10

func MsgL(lang string, msg ...any) string

Join items by space, use Sprint for each item, translate first msg[0] item using message.ini content, use language specific printer instead of fmt by default

func MsgNoLT added in v1.17.10

func MsgNoLT(msg ...any) string

Join msg items by space this function should NOT use anything from message store

func PackZip

func PackZip(srcPath string, isCleanDstDir bool, dstDir string) (string, error)

PackZip create new (overwrite) zip archive from specified file or directory and all subdirs. If dstDir is "" empty then result located in source base directory.

func ParseCsvLine added in v1.15.2

func ParseCsvLine(src string, comma rune) []string

ParseCsvLine comma separated string: " value ", value, ' value '. Value can be empty and can be escaped with "double" or 'single' quotes. If comma is zero rune then , comma used by default.

func ParseKeyValue

func ParseKeyValue(src string) (map[string]string, error)

ParseKeyValue string of multiple key = value; pairs separated by semicolon. Key cannot be empty, value can be. Value can be escaped with "double" or 'single' quotes

func QuoteForIni added in v1.15.6

func QuoteForIni(src string) string

Escape src value for ini-file writing: add 'single' or "double" quotes around if src contains ; semicolon or # hash.

func SaveTo added in v1.9.7

func SaveTo(outPath string, rd io.Reader) error

SaveTo copy all from source reader into new outPath file. File truncated if already exists.

func SetMsg added in v1.17.10

func SetMsg(userLang string, msgIni []IniEntry) (string, []string)

update user language name and translation map

func ToAlphaNumeric

func ToAlphaNumeric(src string) string

ToAlphaNumeric replace all non [A-Z,a-z,0-9] by _ underscore and remove repetitive underscores

func ToIntValue added in v1.9.8

func ToIntValue(src interface{}) (int, bool)

ToIntValue cast src to int if it not nil and type is one of integer or float types. Return int value and true on success or 0 and false if src is nil or invalid type.

func ToJsonFile

func ToJsonFile(jsonPath string, src interface{}) error

ToJsonFile convert source to json and write into jsonPath file.

func ToJsonIndent

func ToJsonIndent(src interface{}) (string, error)

ToJsonIndent return source conveted to json indeneted string.

func ToJsonIndentFile added in v1.10.0

func ToJsonIndentFile(jsonPath string, src interface{}) error

ToJsonIndentFile convert source to json and write into jsonPath file.

func ToUnderscoreTimeStamp added in v1.11.0

func ToUnderscoreTimeStamp(src string) string

ToUnderscoreTimeStamp converts date-time string to timestamp string: 2021-07-16 13:40:53.882 into 2021_07_16_13_40_53_882 If source string is not a date-time string then return empty "" string

func UnQuote

func UnQuote(src string) string

UnQuote trim spaces and remove "double" or 'single' quotes around string

func UnpackZip

func UnpackZip(zipPath string, isCleanDstDir bool, dstDir string) error

UnpackZip unpack zip archive into specified directory, creating it if not exist. If dstDir is "" empty then result located in source base directory.

func Utf8Reader

func Utf8Reader(f *os.File, encodingName string) (io.Reader, error)

Utf8Reader return a reader to transform file content to utf-8.

If file starts with BOM (utf-8 utf-16LE utf-16BE utf-32LE utf-32BE) then BOM is used. If no BOM and encodingName is "" empty then file content probed to see is it already utf-8. If encodingName explicitly specified then it is used to convert file content to string. If none of above then assume default encoding: "windows-1252" on Windows and "utf-8" on Linux.

Types

type IniEntry added in v1.17.10

type IniEntry struct {
	Section string // section: in lower case, without [], space trimed
	Key     string // key: in lower case, without =, space trimed
	Val     string // value: space trimed and unquoted ("quotes" or 'apostrophes' removed)
}

Ini-file entry: section, key and value

func AddIniEntry added in v1.17.10

func AddIniEntry(isUpdate bool, eaIni []IniEntry, section, key, val string) []IniEntry

Insert new or update existing ini file entry: search by (section, key) in source []IniEntry if not found then insert new entry if found and isUpdate true then update existing entry with new value

func InsertIniEntry added in v1.17.10

func InsertIniEntry(eaIni []IniEntry, section, key, val string) []IniEntry

Insert new ini file entry if not already exists: search by (section, key) in source []IniEntry if not found then insert new entry

func MergeIniEntry added in v1.17.10

func MergeIniEntry(eaIni []IniEntry, section, key, val string) []IniEntry

Update existing or insert new ini file entry: search by (section, key) in source []IniEntry if not found then insert new entry if found then update existing entry with new value

func MergeSectionKeyIniEntry added in v1.17.10

func MergeSectionKeyIniEntry(eaIni []IniEntry, sectionKey, val string) []IniEntry

Update existing or insert new ini file entry: sectionKey expected to be: section.key if not then exit and do nothing. find section and key in source []IniEntry if not found then insert new entry if found then update existing entry with new value

Jump to

Keyboard shortcuts

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