@@ -59,6 +59,26 @@ func HasSuffix(s string, suffix string) bool {
5959 return false
6060}
6161
62+ func Count(s string, substr string) int {
63+ lenS := len(s)
64+ lenSub := len(substr)
65+
66+ if lenSub == 0 {
67+ return lenS + 1
68+ }
69+ c := 0
70+
71+ for i := 0; i < lenS; {
72+ if HasPrefix(s[i:], substr) {
73+ c++
74+ i += lenSub
75+ } else {
76+ i++
77+ }
78+ }
79+ return c
80+ }
81+
6282func Split(s string, sep string) []string {
6383 sLen := len(s)
6484 sepLen := len(sep)
@@ -101,3 +121,140 @@ func Split(s string, sep string) []string {
101121 }
102122 return elems
103123}
124+
125+ func Repeat(s string, count int) string {
126+ new := s
127+
128+ for i := 1; i < count; i++ {
129+ new += s
130+ }
131+ return new
132+ }
133+
134+ func Replace(s string, old string, new string, n int) string {
135+ res := ""
136+ rep := 0
137+ i := 0
138+ lenOld := len(old)
139+ lenNew := len(new)
140+
141+ if lenOld == 0 {
142+ res = new
143+ rep++
144+ }
145+
146+ for i < len(s) && (rep < n || n < 0) {
147+ c := s[i]
148+
149+ if lenOld == 0 {
150+ res += c + new
151+ i++
152+ } else if HasPrefix(s[i:], old) {
153+ res += new
154+ i += lenOld
155+ } else {
156+ res += c
157+ i++
158+ continue
159+ }
160+ rep++
161+ }
162+ return res + s[i:]
163+ }
164+
165+ func ReplaceAll(s string, old string, new string) string {
166+ return Replace(s, old, new, -1)
167+ }
168+
169+ func CutPrefix(s string, prefix string) (string, bool) {
170+ if HasPrefix(s, prefix) {
171+ return s[len(prefix):], true
172+ }
173+ return s, false
174+ }
175+
176+ func CutSuffix(s string, suffix string) (string, bool) {
177+ if HasSuffix(s, suffix) {
178+ return s[0 : len(s)-len(suffix)], true
179+ }
180+ return s, false
181+ }
182+
183+ func Cut(s string, sep string) (string, string, bool) {
184+ lenS := len(s)
185+
186+ if lenS > 0 {
187+ for i := 0; i < lenS; i++ {
188+ if HasPrefix(s[i:], sep) {
189+ return s[0:i], s[i+len(sep):], true
190+ }
191+ }
192+ }
193+ return s, "", false
194+ }
195+
196+ func TrimPrefix(s string, prefix string) string {
197+ s, c := CutPrefix(s, prefix)
198+ return s
199+ }
200+
201+ func TrimSuffix(s string, suffix string) string {
202+ s, c := CutSuffix(s, suffix)
203+ return s
204+ }
205+
206+ func TrimLeft(s string, cutset string) string {
207+ lenCS := len(cutset)
208+
209+ if len(s) > 0 && lenCS > 0 {
210+ for {
211+ trimmed := false
212+
213+ for i := 0; i < lenCS; i++ {
214+ lenS := len(s)
215+ s, cut := CutPrefix(s, cutset[i])
216+
217+ if cut {
218+ trimmed = true
219+ }
220+ }
221+
222+ if !trimmed {
223+ break
224+ }
225+ }
226+ }
227+ return s
228+ }
229+
230+ func TrimRight(s string, cutset string) string {
231+ lenCS := len(cutset)
232+
233+ if len(s) > 0 && lenCS > 0 {
234+ for {
235+ trimmed := false
236+
237+ for i := 0; i < lenCS; i++ {
238+ lenS := len(s)
239+ s, cut := CutSuffix(s, cutset[i])
240+
241+ if cut {
242+ trimmed = true
243+ }
244+ }
245+
246+ if !trimmed {
247+ break
248+ }
249+ }
250+ }
251+ return s
252+ }
253+
254+ func Trim(s string, cutset string) string {
255+ return TrimRight(TrimLeft(s, cutset), cutset)
256+ }
257+
258+ func TrimSpace(s string) string {
259+ return Trim(s, "\t\n\v\f\r ")
260+ }
0 commit comments