xlib/xStrUtil.go

101 строка
2.0 KiB
Go
Исходник Обычный вид История

2019-01-21 02:17:20 +03:00
package xlib
2018-11-06 00:40:56 +03:00
import (
"path/filepath"
"strings"
"unicode"
)
//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-02-17 00:12:34 +03:00
for strings.Contains(s, " ") { //strings.Index(s, " ") >= 0 {
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
}