2019-01-21 02:17:20 +03:00
|
|
|
package xlib
|
2018-11-06 00:40:56 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2020-08-25 15:30:07 +03:00
|
|
|
"strconv"
|
2018-11-06 00:40:56 +03:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
2021-01-23 00:11:25 +03:00
|
|
|
// Secret - возвращает строку с вырезанной серединой, удобно для отображения токенов и паролей
|
|
|
|
func Secret(s string) string {
|
|
|
|
if len(s) < 4 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s[:2] + "..." + s[len(s)-2:]
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:30:07 +03:00
|
|
|
// AtoI - convert string to int, if error occure, return def value
|
|
|
|
func AtoI(s string, def int) int {
|
|
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
return int(n)
|
|
|
|
}
|
|
|
|
|
2018-11-06 00:40:56 +03:00
|
|
|
//StrContainBackSlash - return true if input string contain '\'
|
|
|
|
func StrContainBackSlash(s string) bool {
|
|
|
|
return strings.ContainsRune(s, 0x005C)
|
|
|
|
}
|
|
|
|
|
|
|
|
//StrIsPrintRune - return true if input string consists of printable rune
|
|
|
|
func StrIsPrintRune(s string) bool {
|
|
|
|
for _, r := range s {
|
|
|
|
if !unicode.IsPrint(r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:17:20 +03:00
|
|
|
//ChangeFileExt - change in path string file name extention
|
2019-04-04 01:23:01 +03:00
|
|
|
//newExt must start from '.' sample '.xyz'
|
2018-11-06 00:40:56 +03:00
|
|
|
func ChangeFileExt(iFileName, newExt string) string {
|
|
|
|
return strings.TrimSuffix(iFileName, filepath.Ext(iFileName)) + newExt
|
|
|
|
}
|
2019-01-21 02:17:20 +03:00
|
|
|
|
2019-08-19 02:58:01 +03:00
|
|
|
//ContainsOtherRune - if sting s contains any other rune not in runes then return true and position of first this rune
|
2019-08-14 17:55:02 +03:00
|
|
|
//on empty parameters - return false, -1
|
|
|
|
func ContainsOtherRune(s string, runes ...rune) (bool, int) {
|
|
|
|
var (
|
|
|
|
i int
|
|
|
|
r rune
|
|
|
|
)
|
|
|
|
if (len(s) == 0) || (len(runes) == 0) {
|
|
|
|
return false, -1
|
|
|
|
}
|
|
|
|
for i, r = range s {
|
|
|
|
res := true
|
|
|
|
for _, sr := range runes {
|
|
|
|
res = (res && (r != sr))
|
|
|
|
}
|
|
|
|
if res {
|
|
|
|
return res, i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
//StrCopyStop - return s, stop on rune in stopRune
|
|
|
|
func StrCopyStop(s string, stopRune ...rune) (string, int) {
|
|
|
|
var (
|
|
|
|
i int
|
|
|
|
r rune
|
|
|
|
)
|
|
|
|
if len(stopRune) > 0 {
|
|
|
|
for i, r = range s {
|
|
|
|
for _, sr := range stopRune {
|
|
|
|
if r == sr {
|
|
|
|
return s[:i], i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
//ReplaceAllSpace - return string with one space
|
|
|
|
func ReplaceAllSpace(s string) string {
|
2020-08-06 09:46:15 +03:00
|
|
|
for strings.Contains(s, " ") {
|
2019-08-14 17:55:02 +03:00
|
|
|
s = strings.ReplaceAll(s, " ", " ")
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
//ReplaceSeparators - return string with one separator rune
|
2020-02-17 00:12:34 +03:00
|
|
|
// ' .' >> '.'
|
|
|
|
// '. ' >> '.'
|
|
|
|
// ' :' >> ':'
|
2019-11-26 23:06:36 +03:00
|
|
|
// ': ' >> ':'
|
2019-08-14 17:55:02 +03:00
|
|
|
func ReplaceSeparators(s string) string {
|
|
|
|
type TSeparatorsReplacement struct {
|
|
|
|
old string
|
|
|
|
new string
|
|
|
|
}
|
|
|
|
var SeparatorsList = []TSeparatorsReplacement{
|
|
|
|
{" .", "."},
|
|
|
|
{". ", "."},
|
|
|
|
{" :", ":"},
|
|
|
|
{": ", ":"},
|
2020-05-18 23:25:15 +03:00
|
|
|
//{":.", ":"},
|
|
|
|
//{".:", "."},
|
2019-08-14 17:55:02 +03:00
|
|
|
}
|
|
|
|
for _, sep := range SeparatorsList {
|
|
|
|
s = strings.ReplaceAll(s, sep.old, sep.new)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|