forked from gorpher/gone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconv.go
More file actions
50 lines (45 loc) · 1.05 KB
/
Copy pathconv.go
File metadata and controls
50 lines (45 loc) · 1.05 KB
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
package gone
import (
"bytes"
"strings"
"unsafe"
)
// BytesToStr []byte转string.
func BytesToStr(b []byte) string {
return *(*string)(unsafe.Pointer(&b)) //nolint
}
// StrToBytes string转[]byte.
func StrToBytes(str string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&str)) //nolint
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h)) //nolint
}
// Contains 判断字符串是否存在切片中.
func Contains(str string, s []string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
// StringReplaceIgnoreCase 忽略大小写替换字符串.
func StringReplaceIgnoreCase(text, source, target string) string {
buf := &bytes.Buffer{}
textLower := strings.ToLower(text)
searchStrLower := strings.ToLower(source)
searchStrLen := len(source)
var end int
for {
idx := strings.Index(textLower, searchStrLower)
if idx <= 0 {
break
}
buf.WriteString(text[:idx])
buf.WriteString(target)
end = idx + searchStrLen
textLower = textLower[end:]
}
buf.WriteString(text[end:])
return buf.String()
}