This commit is contained in:
Junegunn Choi 2015-08-02 23:54:53 +09:00
parent 7833fa7396
commit ae87f6548a
3 changed files with 19 additions and 22 deletions

View File

@ -161,6 +161,7 @@ func SuffixMatch(caseSensitive bool, input []rune, pattern []rune) (int, int) {
return trimmedLen - len(pattern), trimmedLen return trimmedLen - len(pattern), trimmedLen
} }
// EqualMatch performs equal-match
func EqualMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) { func EqualMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
if len(runes) != len(pattern) { if len(runes) != len(pattern) {
return -1, -1 return -1, -1

View File

@ -37,14 +37,14 @@ type Rank struct {
var rankTiebreak tiebreak var rankTiebreak tiebreak
// Rank calculates rank of the Item // Rank calculates rank of the Item
func (i *Item) Rank(cache bool) Rank { func (item *Item) Rank(cache bool) Rank {
if cache && (i.rank.matchlen > 0 || i.rank.tiebreak > 0) { if cache && (item.rank.matchlen > 0 || item.rank.tiebreak > 0) {
return i.rank return item.rank
} }
matchlen := 0 matchlen := 0
prevEnd := 0 prevEnd := 0
minBegin := math.MaxUint16 minBegin := math.MaxUint16
for _, offset := range i.offsets { for _, offset := range item.offsets {
begin := int(offset[0]) begin := int(offset[0])
end := int(offset[1]) end := int(offset[1])
if prevEnd > begin { if prevEnd > begin {
@ -64,21 +64,21 @@ func (i *Item) Rank(cache bool) Rank {
switch rankTiebreak { switch rankTiebreak {
case byLength: case byLength:
// It is guaranteed that .transformed in not null in normal execution // It is guaranteed that .transformed in not null in normal execution
if i.transformed != nil { if item.transformed != nil {
lenSum := 0 lenSum := 0
for _, token := range i.transformed { for _, token := range item.transformed {
lenSum += len(token.text) lenSum += len(token.text)
} }
tiebreak = uint16(lenSum) tiebreak = uint16(lenSum)
} else { } else {
tiebreak = uint16(len(i.text)) tiebreak = uint16(len(item.text))
} }
case byBegin: case byBegin:
// We can't just look at i.offsets[0][0] because it can be an inverse term // We can't just look at item.offsets[0][0] because it can be an inverse term
tiebreak = uint16(minBegin) tiebreak = uint16(minBegin)
case byEnd: case byEnd:
if prevEnd > 0 { if prevEnd > 0 {
tiebreak = uint16(1 + len(i.text) - prevEnd) tiebreak = uint16(1 + len(item.text) - prevEnd)
} else { } else {
// Empty offsets due to inverse terms. // Empty offsets due to inverse terms.
tiebreak = 1 tiebreak = 1
@ -86,23 +86,23 @@ func (i *Item) Rank(cache bool) Rank {
case byIndex: case byIndex:
tiebreak = 1 tiebreak = 1
} }
rank := Rank{uint16(matchlen), tiebreak, i.index} rank := Rank{uint16(matchlen), tiebreak, item.index}
if cache { if cache {
i.rank = rank item.rank = rank
} }
return rank return rank
} }
// AsString returns the original string // AsString returns the original string
func (i *Item) AsString() string { func (item *Item) AsString() string {
return *i.StringPtr() return *item.StringPtr()
} }
// StringPtr returns the pointer to the original string // StringPtr returns the pointer to the original string
func (i *Item) StringPtr() *string { func (item *Item) StringPtr() *string {
runes := i.text runes := item.text
if i.origText != nil { if item.origText != nil {
runes = *i.origText runes = *item.origText
} }
str := string(runes) str := string(runes)
return &str return &str

View File

@ -20,7 +20,7 @@ func Max(first int, items ...int) int {
return max return max
} }
// Max32 returns the smallest 32-bit integer // Min32 returns the smallest 32-bit integer
func Min32(first int32, second int32) int32 { func Min32(first int32, second int32) int32 {
if first <= second { if first <= second {
return first return first
@ -70,10 +70,6 @@ func DurWithin(
return val return val
} }
func Between(val int, min int, max int) bool {
return val >= min && val <= max
}
// IsTty returns true is stdin is a terminal // IsTty returns true is stdin is a terminal
func IsTty() bool { func IsTty() bool {
return int(C.isatty(C.int(os.Stdin.Fd()))) != 0 return int(C.isatty(C.int(os.Stdin.Fd()))) != 0