xlib/xStrUtil.go

57 строки
1.3 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"
2019-01-21 02:17:20 +03:00
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
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
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
//ConvertStrCodePage - convert string from one code page to another
func ConvertStrCodePage(s string, fromCP, toCP int64) (string, error) {
if len(s) == 0 {
return "", nil
}
2019-01-29 01:05:41 +03:00
if fromCP == toCP {
return s, nil
}
2019-01-21 02:17:20 +03:00
var err error
switch fromCP {
case Cp866:
s, _, err = transform.String(charmap.CodePage866.NewDecoder(), s)
case CpWindows1251:
s, _, err = transform.String(charmap.Windows1251.NewDecoder(), s)
}
switch toCP {
case Cp866:
s, _, err = transform.String(charmap.CodePage866.NewEncoder(), s)
case CpWindows1251:
s, _, err = transform.String(charmap.Windows1251.NewEncoder(), s)
}
return s, err
}