fzf/src/util/chars.go

189 lines
3.7 KiB
Go
Raw Normal View History

package util
import (
"unicode"
"unicode/utf8"
2017-07-16 14:31:19 +00:00
"unsafe"
)
type Chars struct {
2017-07-16 14:31:19 +00:00
slice []byte // or []rune
inBytes bool
trimLengthKnown bool
trimLength uint16
// XXX Piggybacking item index here is a horrible idea. But I'm trying to
// minimize the memory footprint by not wasting padded spaces.
Index int32
}
// ToChars converts byte array into rune array
2017-07-16 14:31:19 +00:00
func ToChars(bytes []byte) Chars {
var runes []rune
2017-07-16 14:31:19 +00:00
inBytes := true
numBytes := len(bytes)
for i := 0; i < numBytes; {
2017-07-16 14:31:19 +00:00
if bytes[i] < utf8.RuneSelf {
if !inBytes {
runes = append(runes, rune(bytes[i]))
}
i++
} else {
2017-07-16 14:31:19 +00:00
if inBytes {
inBytes = false
runes = make([]rune, i, numBytes)
for j := 0; j < i; j++ {
2017-07-16 14:31:19 +00:00
runes[j] = rune(bytes[j])
}
}
2017-07-16 14:31:19 +00:00
r, sz := utf8.DecodeRune(bytes[i:])
i += sz
runes = append(runes, r)
}
}
2017-07-16 14:31:19 +00:00
if inBytes {
return Chars{slice: bytes, inBytes: inBytes}
}
2017-07-16 14:31:19 +00:00
return RunesToChars(runes)
}
func RunesToChars(runes []rune) Chars {
2017-07-16 14:31:19 +00:00
return Chars{slice: *(*[]byte)(unsafe.Pointer(&runes)), inBytes: false}
}
func (chars *Chars) optionalRunes() []rune {
if chars.inBytes {
return nil
}
return *(*[]rune)(unsafe.Pointer(&chars.slice))
}
func (chars *Chars) Get(i int) rune {
2017-07-16 14:31:19 +00:00
if runes := chars.optionalRunes(); runes != nil {
return runes[i]
}
2017-07-16 14:31:19 +00:00
return rune(chars.slice[i])
}
func (chars *Chars) Length() int {
2017-07-16 14:31:19 +00:00
if runes := chars.optionalRunes(); runes != nil {
return len(runes)
}
2017-07-16 14:31:19 +00:00
return len(chars.slice)
}
// TrimLength returns the length after trimming leading and trailing whitespaces
2017-07-16 14:31:19 +00:00
func (chars *Chars) TrimLength() uint16 {
if chars.trimLengthKnown {
return chars.trimLength
}
chars.trimLengthKnown = true
var i int
len := chars.Length()
for i = len - 1; i >= 0; i-- {
char := chars.Get(i)
if !unicode.IsSpace(char) {
break
}
}
// Completely empty
if i < 0 {
return 0
}
var j int
for j = 0; j < len; j++ {
char := chars.Get(j)
if !unicode.IsSpace(char) {
break
}
}
2017-07-16 14:31:19 +00:00
chars.trimLength = AsUint16(i - j + 1)
return chars.trimLength
}
func (chars *Chars) TrailingWhitespaces() int {
whitespaces := 0
for i := chars.Length() - 1; i >= 0; i-- {
char := chars.Get(i)
if !unicode.IsSpace(char) {
break
}
whitespaces++
}
return whitespaces
}
func (chars *Chars) ToString() string {
2017-07-16 14:31:19 +00:00
if runes := chars.optionalRunes(); runes != nil {
return string(runes)
}
2017-07-16 14:31:19 +00:00
return string(chars.slice)
}
func (chars *Chars) ToRunes() []rune {
2017-07-16 14:31:19 +00:00
if runes := chars.optionalRunes(); runes != nil {
return runes
}
2017-07-16 14:31:19 +00:00
bytes := chars.slice
runes := make([]rune, len(bytes))
for idx, b := range bytes {
runes[idx] = rune(b)
}
return runes
}
2017-07-16 14:31:19 +00:00
func (chars *Chars) CopyRunes(dest []rune) {
if runes := chars.optionalRunes(); runes != nil {
copy(dest, runes)
return
}
for idx, b := range chars.slice {
dest[idx] = rune(b)
}
return
}
func (chars *Chars) Slice(b int, e int) Chars {
2017-07-16 14:31:19 +00:00
if runes := chars.optionalRunes(); runes != nil {
return RunesToChars(runes[b:e])
}
2017-07-16 14:31:19 +00:00
return Chars{slice: chars.slice[b:e], inBytes: true}
}
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
}