diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2016-08-14 04:23:37 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2016-08-14 04:30:55 +0900 |
commit | d9c8a9a880dd9fc28c9fb466bd1c81cd6a8a159c (patch) | |
tree | 98a2018200734947aa6fa40552f3ccc7463404a2 /src/util/chars.go | |
parent | ddc7bb9064042a0d5da9546eaf6ff888dca63f0c (diff) |
[perf] Remove memory copy when using string delimiter
Diffstat (limited to 'src/util/chars.go')
-rw-r--r-- | src/util/chars.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/util/chars.go b/src/util/chars.go index 6034ee53..12417c66 100644 --- a/src/util/chars.go +++ b/src/util/chars.go @@ -118,3 +118,39 @@ func (chars *Chars) Slice(b int, e int) Chars { } return Chars{bytes: chars.bytes[b:e]} } + +func (chars *Chars) Split(delimiter string) []Chars { + delim := []rune(delimiter) + numChars := chars.Length() + numDelim := len(delim) + begin := 0 + ret := make([]Chars, 0, 1) + + for index := 0; index < numChars; { + if index+numDelim <= numChars { + match := true + for off, d := range delim { + if chars.Get(index+off) != d { + match = false + break + } + } + // Found the delimiter + if match { + incr := Max(numDelim, 1) + ret = append(ret, chars.Slice(begin, index+incr)) + index += incr + begin = index + continue + } + } else { + // Impossible to find the delimiter in the remaining substring + break + } + index++ + } + if begin < numChars || len(ret) == 0 { + ret = append(ret, chars.Slice(begin, numChars)) + } + return ret +} |