forked from infinilabs/crawler
/
stringutil.go
137 lines (116 loc) · 2.79 KB
/
stringutil.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright 2016 Medcl (m AT medcl.net)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"bytes"
"encoding/json"
"errors"
"runtime"
"strconv"
. "strings"
"time"
"unicode"
"unicode/utf16"
)
func ContainStr(s, substr string) bool {
return Index(s, substr) != -1
}
func StringToUTF16(s string) []uint16 {
return utf16.Encode([]rune(s + "\x00"))
}
func SubStringWithSuffix(str string, length int, suffix string) string {
if len(str) > length {
str = SubString(str, 0, length) + suffix
}
return str
}
func UnicodeIndex(str, substr string) int {
// 子串在字符串的字节位置
result := Index(str, substr)
if result >= 0 {
// 获得子串之前的字符串并转换成[]byte
prefix := []byte(str)[0:result]
// 将子串之前的字符串转换成[]rune
rs := []rune(string(prefix))
// 获得子串之前的字符串的长度,便是子串在字符串的字符位置
result = len(rs)
}
return result
}
func SubString(str string, begin, length int) (substr string) {
rs := []rune(str)
lth := len(rs)
if begin < 0 {
begin = 0
}
if begin >= lth {
begin = lth
}
end := begin + length
if end > lth {
end = lth
}
return string(rs[begin:end])
}
func NoWordBreak(in string) string {
return Replace(in, "\n", " ", -1)
}
// Removes all unnecessary whitespaces
func MergeSpace(in string) (out string) {
var buffer bytes.Buffer
white := false
for _, c := range in {
if unicode.IsSpace(c) {
if !white {
buffer.WriteString(" ")
}
white = true
} else {
buffer.WriteRune(c)
white = false
}
}
return TrimSpace(buffer.String())
}
func ToJson(in interface{}, indent bool) string {
var b []byte
if indent {
b, _ = json.MarshalIndent(in, " ", " ")
} else {
b, _ = json.Marshal(in)
}
return string(b)
}
func ToInt(str string) (int, error) {
if IndexAny(str, ".") > 0 {
nonFractionalPart := Split(str, ".")
return strconv.Atoi(nonFractionalPart[0])
} else {
return strconv.Atoi(str)
}
}
func FormatTime(date *time.Time) string {
return date.Format("2006-01-02 15:04:05")
}
func GetRuntimeErrorMessage(r runtime.Error) string {
if r != nil {
return r.Error()
}
panic(errors.New("nil runtime error"))
return "nil runtime error"
}
func XSSHandle(src string) string {
src = Replace(src, ">", "< ", -1)
src = Replace(src, ">", "> ", -1)
return src
}