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
|
|
|
|
2019-08-19 02:58:01 +03:00
|
|
|
"github.com/softlandia/xlib/internal/cp"
|
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
|
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-14 17:55:02 +03:00
|
|
|
//StrConvertCodePage - convert string from one code page to another
|
2019-08-19 02:58:01 +03:00
|
|
|
func StrConvertCodePage(s string, fromCP, toCP uint16) (string, error) {
|
2019-01-21 02:17:20 +03:00
|
|
|
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 {
|
2019-08-19 02:58:01 +03:00
|
|
|
case cp.IBM866:
|
2019-01-21 02:17:20 +03:00
|
|
|
s, _, err = transform.String(charmap.CodePage866.NewDecoder(), s)
|
2019-08-19 02:58:01 +03:00
|
|
|
case cp.Windows1251:
|
2019-01-21 02:17:20 +03:00
|
|
|
s, _, err = transform.String(charmap.Windows1251.NewDecoder(), s)
|
|
|
|
}
|
|
|
|
switch toCP {
|
2019-08-19 02:58:01 +03:00
|
|
|
case cp.IBM866:
|
2019-01-21 02:17:20 +03:00
|
|
|
s, _, err = transform.String(charmap.CodePage866.NewEncoder(), s)
|
2019-08-19 02:58:01 +03:00
|
|
|
case cp.Windows1251:
|
2019-01-21 02:17:20 +03:00
|
|
|
s, _, err = transform.String(charmap.Windows1251.NewEncoder(), s)
|
|
|
|
}
|
|
|
|
return s, err
|
|
|
|
}
|
2019-04-04 01:23:01 +03:00
|
|
|
|
2019-08-19 02:58:01 +03:00
|
|
|
// CodePageAsString - return name of char set with id codepage
|
|
|
|
// if codepage not exist - return ""
|
|
|
|
func CodePageAsString(codepage uint16) string {
|
|
|
|
return cp.Name[codepage]
|
2019-04-04 01:23:01 +03:00
|
|
|
}
|
2019-08-14 17:55:02 +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 {
|
|
|
|
for strings.Index(s, " ") >= 0 {
|
|
|
|
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
|
|
|
|
}
|