forked from g3n/engine
/
str.go
66 lines (55 loc) · 1.32 KB
/
str.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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package text
// StrCount returns the number of runes in the specified string
func StrCount(s string) int {
count := 0
for range s {
count++
}
return count
}
// StrFind returns the start and length of the rune at the
// specified position in the string
func StrFind(s string, pos int) (start, length int) {
count := 0
for index := range s {
if count == pos {
start = index
count++
continue
}
if count == pos+1 {
length = index - start
break
}
count++
}
if length == 0 {
length = len(s) - start
}
return start, length
}
// StrRemove removes the rune from the specified string and position
func StrRemove(s string, col int) string {
start, length := StrFind(s, col)
return s[:start] + s[start+length:]
}
// StrInsert inserts a string at the specified character position
func StrInsert(s, data string, col int) string {
start, _ := StrFind(s, col)
return s[:start] + data + s[start:]
}
// StrPrefix returns the prefix of the specified string up to
// the specified character position
func StrPrefix(text string, pos int) string {
count := 0
for index := range text {
if count == pos {
return text[:index]
}
count++
}
return text
}