Minor optimizations

This commit is contained in:
Zachary Yedidia
2018-01-29 16:02:15 -05:00
parent fddf1690e3
commit 015fcf5fec
5 changed files with 42 additions and 8 deletions

View File

@@ -37,6 +37,40 @@ func toRunes(b []byte) []rune {
return runes
}
func sliceStart(slc []byte, index int) []byte {
len := len(slc)
i := 0
totalSize := 0
for totalSize < len {
if i >= index {
return slc[totalSize:]
}
_, size := utf8.DecodeRune(slc[totalSize:])
totalSize += size
i++
}
return slc[totalSize:]
}
func sliceEnd(slc []byte, index int) []byte {
len := len(slc)
i := 0
totalSize := 0
for totalSize < len {
if i >= index {
return slc[:totalSize]
}
_, size := utf8.DecodeRune(slc[totalSize:])
totalSize += size
i++
}
return slc[:totalSize]
}
// NumOccurrences counts the number of occurrences of a byte in a string
func NumOccurrences(s string, c byte) int {
var n int
@@ -144,7 +178,7 @@ func GetLeadingWhitespace(str string) string {
}
// IsSpaces checks if a given string is only spaces
func IsSpaces(str string) bool {
func IsSpaces(str []byte) bool {
for _, c := range str {
if c != ' ' {
return false