summaryrefslogtreecommitdiff
path: root/src/tokenizer.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2025-02-12 20:15:04 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2025-02-12 20:15:04 +0900
commit84e2262ad63df2112f16b2a80fc661294c3da45e (patch)
tree803f4bf41de9d0011efcc2e29f788ac990fc7c73 /src/tokenizer.go
parent378137d34a2a11b16c66dff2bf4309c7ce232a94 (diff)
downloadfzf-84e2262ad63df2112f16b2a80fc661294c3da45e.tar.gz
Make --accept-nth and --with-nth support templates
Diffstat (limited to 'src/tokenizer.go')
-rw-r--r--src/tokenizer.go35
1 files changed, 11 insertions, 24 deletions
diff --git a/src/tokenizer.go b/src/tokenizer.go
index 057d7405..aaddd17d 100644
--- a/src/tokenizer.go
+++ b/src/tokenizer.go
@@ -6,6 +6,7 @@ import (
"regexp"
"strconv"
"strings"
+ "unicode"
"github.com/junegunn/fzf/src/util"
)
@@ -211,32 +212,18 @@ func Tokenize(text string, delimiter Delimiter) []Token {
return withPrefixLengths(tokens, 0)
}
-// StripLastDelimiter removes the trailing delimiter and whitespaces from the
-// last token.
-func StripLastDelimiter(tokens []Token, delimiter Delimiter) []Token {
- if len(tokens) == 0 {
- return tokens
- }
-
- lastToken := tokens[len(tokens)-1]
-
- if delimiter.str == nil && delimiter.regex == nil {
- lastToken.text.TrimTrailingWhitespaces()
- } else {
- if delimiter.str != nil {
- lastToken.text.TrimSuffix([]rune(*delimiter.str))
- } else if delimiter.regex != nil {
- str := lastToken.text.ToString()
- locs := delimiter.regex.FindAllStringIndex(str, -1)
- if len(locs) > 0 {
- lastLoc := locs[len(locs)-1]
- lastToken.text.SliceRight(lastLoc[0])
- }
+// StripLastDelimiter removes the trailing delimiter and whitespaces
+func StripLastDelimiter(str string, delimiter Delimiter) string {
+ if delimiter.str != nil {
+ str = strings.TrimSuffix(str, *delimiter.str)
+ } else if delimiter.regex != nil {
+ locs := delimiter.regex.FindAllStringIndex(str, -1)
+ if len(locs) > 0 {
+ lastLoc := locs[len(locs)-1]
+ str = str[:lastLoc[0]]
}
- lastToken.text.TrimTrailingWhitespaces()
}
-
- return tokens
+ return strings.TrimRightFunc(str, unicode.IsSpace)
}
// JoinTokens concatenates the tokens into a single string