lbry.go/extras/util/strings.go

54 lines
1.1 KiB
Go
Raw Normal View History

2020-09-01 20:36:59 +02:00
package util
2021-06-18 05:59:06 +02:00
import (
"encoding/hex"
"golang.org/x/text/cases"
"golang.org/x/text/unicode/norm"
"strings"
)
2020-09-01 20:36:59 +02:00
func StringSplitArg(stringToSplit, separator string) []interface{} {
split := strings.Split(stringToSplit, separator)
splitInterface := make([]interface{}, len(split))
for i, s := range split {
splitInterface[i] = s
}
return splitInterface
}
2021-06-18 05:59:06 +02:00
// NormalizeName Normalize names to remove weird characters and account to capitalization
func NormalizeName(s string) string {
2021-06-18 05:59:06 +02:00
c := cases.Fold()
return c.String(norm.NFD.String(s))
}
2021-10-05 04:21:59 +02:00
// ReverseBytesInPlace reverse the bytes. thanks, Satoshi 😒
func ReverseBytesInPlace(s []byte) {
2021-06-18 05:59:06 +02:00
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
// TxIdToTxHash convert the txid to a hash for returning from the hub
func TxIdToTxHash(txid string) []byte {
2021-06-18 05:59:06 +02:00
t, err := hex.DecodeString(txid)
if err != nil {
return nil
}
ReverseBytesInPlace(t)
2021-06-18 05:59:06 +02:00
return t
}
// TxHashToTxId convert the txHash from the response format back to an id
func TxHashToTxId(txHash []byte) string {
2021-06-18 05:59:06 +02:00
t := make([]byte, len(txHash))
copy(t, txHash)
ReverseBytesInPlace(t)
2021-06-18 05:59:06 +02:00
return hex.EncodeToString(t)
}