Add stringmanip package

This commit is contained in:
Matt Baer
2018-10-16 22:58:06 -04:00
parent 567e0c6ef0
commit 3d47537e1f
2 changed files with 113 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
package stringmanip
// Substring provides a safe way to extract a substring from a UTF-8 string.
// From this discussion:
// https://groups.google.com/d/msg/golang-nuts/cGq1Irv_5Vs/0SKoj49BsWQJ
func Substring(s string, p, l int) string {
if p < 0 || l <= 0 {
return ""
}
c := []rune(s)
if p > len(c) {
return ""
} else if p+l > len(c) || p+l < p {
return string(c[p:])
}
return string(c[p : p+l])
}