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
}
// EqualMatch performs equal-match
func EqualMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
if len(runes) != len(pattern) {
return -1, -1

View File

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

View File

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