Skip to content

Commit 3d47537

Browse files
committed
Add stringmanip package
1 parent 567e0c6 commit 3d47537

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

stringmanip/runes.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package stringmanip
2+
3+
/*
4+
The MIT License (MIT)
5+
6+
Copyright (c) 2016 Sergey Kamardin
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
// Source: https://github.com/gobwas/glob/blob/master/util/runes/runes.go
27+
28+
func IndexRune(str string, r rune) int {
29+
s := []rune(str)
30+
for i, c := range s {
31+
if c == r {
32+
return i
33+
}
34+
}
35+
return -1
36+
}
37+
38+
func LastIndexRune(str string, needle rune) int {
39+
s := []rune(str)
40+
needles := []rune{needle}
41+
ls, ln := len(s), len(needles)
42+
43+
switch {
44+
case ln == 0:
45+
if ls == 0 {
46+
return 0
47+
}
48+
return ls
49+
case ln == 1:
50+
return IndexLastRune(s, needles[0])
51+
case ln == ls:
52+
if EqualRunes(s, needles) {
53+
return 0
54+
}
55+
return -1
56+
case ln > ls:
57+
return -1
58+
}
59+
60+
head:
61+
for i := ls - 1; i >= 0 && i >= ln; i-- {
62+
for y := ln - 1; y >= 0; y-- {
63+
if s[i-(ln-y-1)] != needles[y] {
64+
continue head
65+
}
66+
}
67+
68+
return i - ln + 1
69+
}
70+
71+
return -1
72+
}
73+
74+
func IndexLastRune(s []rune, r rune) int {
75+
for i := len(s) - 1; i >= 0; i-- {
76+
if s[i] == r {
77+
return i
78+
}
79+
}
80+
81+
return -1
82+
}
83+
84+
func EqualRunes(a, b []rune) bool {
85+
if len(a) == len(b) {
86+
for i := 0; i < len(a); i++ {
87+
if a[i] != b[i] {
88+
return false
89+
}
90+
}
91+
92+
return true
93+
}
94+
95+
return false
96+
}

stringmanip/strings.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package stringmanip
2+
3+
// Substring provides a safe way to extract a substring from a UTF-8 string.
4+
// From this discussion:
5+
// https://groups.google.com/d/msg/golang-nuts/cGq1Irv_5Vs/0SKoj49BsWQJ
6+
func Substring(s string, p, l int) string {
7+
if p < 0 || l <= 0 {
8+
return ""
9+
}
10+
c := []rune(s)
11+
if p > len(c) {
12+
return ""
13+
} else if p+l > len(c) || p+l < p {
14+
return string(c[p:])
15+
}
16+
return string(c[p : p+l])
17+
}

0 commit comments

Comments
 (0)