-
Notifications
You must be signed in to change notification settings - Fork 0
/
xStrUtil.go
138 lines (125 loc) · 2.95 KB
/
xStrUtil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package xlib
import (
"path/filepath"
"strconv"
"strings"
"unicode"
"time"
)
// Secret - возвращает строку с вырезанной серединой, удобно для отображения токенов и паролей
func Secret(s string) string {
if len(s) < 4 {
return s
}
return s[:2] + "..." + s[len(s)-2:]
}
// 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)
}
//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
}
//ChangeFileExt - change in path string file name extention
//newExt must start from '.' sample '.xyz'
func ChangeFileExt(iFileName, newExt string) string {
return strings.TrimSuffix(iFileName, filepath.Ext(iFileName)) + newExt
}
//ContainsOtherRune - if sting s contains any other rune not in runes then return true and position of first this rune
//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 {
for strings.Contains(s, " ") {
s = strings.ReplaceAll(s, " ", " ")
}
return s
}
//ReplaceSeparators - return string with one separator rune
// ' .' >> '.'
// '. ' >> '.'
// ' :' >> ':'
// ': ' >> ':'
func ReplaceSeparators(s string) string {
type TSeparatorsReplacement struct {
old string
new string
}
var SeparatorsList = []TSeparatorsReplacement{
{" .", "."},
{". ", "."},
{" :", ":"},
{": ", ":"},
//{":.", ":"},
//{".:", "."},
}
for _, sep := range SeparatorsList {
s = strings.ReplaceAll(s, sep.old, sep.new)
}
return s
}
// ParseBool - при ошибке возвращает значение по умолчанию
func ParseBool(b string, def bool) bool {
res, err := strconv.ParseBool(b)
if err != nil {
res = def
}
return res
}
// ParseDate - если не выйдет, вернёт заданную по умолчанию дату
func ParseDate(dt string, def time.Time) time.Time {
dtProcessed, err := time.Parse("2006-01-02", dt)
if err != nil {
return def
}
return dtProcessed
}