Skip to content
Myr

tokenizer.go

This file

Path

src/tokenizer.go

318 lines · 7.0 KiB · Go

Last changed by

Don't trim last field when delimiter is regex (#4266)phaniumbefore adoption77568e1

History of this file

Take it

The walk stopped after 200 commits: lines older than 3b0f976 point at it.

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 1–8why, one step back
1package fzf
23import (4 "bytes"5 "fmt"6 "regexp"7 "strconv"8 "strings"

Make --accept-nth and --with-nth support templatesJunegunn Choibefore adoption84e2262

line 9why, one step back
9 "unicode"

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 10–21why, one step back
10
11 "github.com/junegunn/fzf/src/util"12)1314const rangeEllipsis = 01516// Range represents nth-expression17type Range struct {18 begin int19 end int20}21

Do not apply nth style when the whole range is coveredJunegunn Choibefore adoption1313510

lines 22–25why, one step back
22func (r Range) IsFull() bool {
23 return r.begin == rangeEllipsis && r.end == rangeEllipsis24}25

Make click-header export $FZF_CLICK_HEADER_{NTH,WORD}Junegunn Choibefore adoptiond658454

lines 26–37why, one step back
26func compareRanges(r1 []Range, r2 []Range) bool {
27 if len(r1) != len(r2) {28 return false29 }30 for idx := range r1 {31 if r1[idx] != r2[idx] {32 return false33 }34 }35 return true36}37

Export the current nth value as $FZF_NTHJunegunn Choibefore adoptionb712f2b

lines 38–63why, one step back
38func RangesToString(ranges []Range) string {
39 strs := []string{}40 for _, r := range ranges {41 s := ""42 if r.begin == rangeEllipsis && r.end == rangeEllipsis {43 s = ".."44 } else if r.begin == r.end {45 s = strconv.Itoa(r.begin)46 } else {47 if r.begin != rangeEllipsis {48 s += strconv.Itoa(r.begin)49 }5051 if r.begin != -1 {52 s += ".."53 if r.end != rangeEllipsis {54 s += strconv.Itoa(r.end)55 }56 }57 }58 strs = append(strs, s)59 }6061 return strings.Join(strs, ",")62}63

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 64–80why, one step back
64// Token contains the tokenized part of the strings and its prefix length
65type Token struct {66 text *util.Chars67 prefixLength int3268}6970// String returns the string representation of a Token.71func (t Token) String() string {72 return fmt.Sprintf("Token{text: %s, prefixLength: %d}", t.text, t.prefixLength)73}7475// Delimiter for tokenizing the input76type Delimiter struct {77 regex *regexp.Regexp78 str *string79}80

Allow suffix match on --nth with custom --delimiterJunegunn Choibefore adoption9abf2c8

lines 81–85why, one step back
81// IsAwk returns true if the delimiter is an AWK-style delimiter
82func (d Delimiter) IsAwk() bool {83 return d.regex == nil && d.str == nil84}85

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 86–91why, one step back
86// String returns the string representation of a Delimiter.
87func (d Delimiter) String() string {88 return fmt.Sprintf("Delimiter{regex: %v, str: &%q}", d.regex, *d.str)89}9091func newRange(begin int, end int) Range {

Export the current nth value as $FZF_NTHJunegunn Choibefore adoptionb712f2b

line 92why, one step back
92 if begin == 1 && end != 1 {

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 93–123why, one step back
93 begin = rangeEllipsis
94 }95 if end == -1 {96 end = rangeEllipsis97 }98 return Range{begin, end}99}100101// ParseRange parses nth-expression and returns the corresponding Range object102func ParseRange(str *string) (Range, bool) {103 if (*str) == ".." {104 return newRange(rangeEllipsis, rangeEllipsis), true105 } else if strings.HasPrefix(*str, "..") {106 end, err := strconv.Atoi((*str)[2:])107 if err != nil || end == 0 {108 return Range{}, false109 }110 return newRange(rangeEllipsis, end), true111 } else if strings.HasSuffix(*str, "..") {112 begin, err := strconv.Atoi((*str)[:len(*str)-2])113 if err != nil || begin == 0 {114 return Range{}, false115 }116 return newRange(begin, rangeEllipsis), true117 } else if strings.Contains(*str, "..") {118 ns := strings.Split(*str, "..")119 if len(ns) != 2 {120 return Range{}, false121 }122 begin, err1 := strconv.Atoi(ns[0])123 end, err2 := strconv.Atoi(ns[1])

Disallow incorrect wrapping range expression for --nthJunegunn Choibefore adoption717562b

line 124why, one step back
124 if err1 != nil || err2 != nil || begin == 0 || end == 0 || begin < 0 && end > 0 {

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 125–219why, one step back
125 return Range{}, false
126 }127 return newRange(begin, end), true128 }129130 n, err := strconv.Atoi(*str)131 if err != nil || n == 0 {132 return Range{}, false133 }134 return newRange(n, n), true135}136137func withPrefixLengths(tokens []string, begin int) []Token {138 ret := make([]Token, len(tokens))139140 prefixLength := begin141 for idx := range tokens {142 chars := util.ToChars(stringBytes(tokens[idx]))143 ret[idx] = Token{&chars, int32(prefixLength)}144 prefixLength += chars.Length()145 }146 return ret147}148149const (150 awkNil = iota151 awkBlack152 awkWhite153)154155func awkTokenizer(input string) ([]string, int) {156 // 9, 32157 ret := []string{}158 prefixLength := 0159 state := awkNil160 begin := 0161 end := 0162 for idx := 0; idx < len(input); idx++ {163 r := input[idx]164 white := r == 9 || r == 32165 switch state {166 case awkNil:167 if white {168 prefixLength++169 } else {170 state, begin, end = awkBlack, idx, idx+1171 }172 case awkBlack:173 end = idx + 1174 if white {175 state = awkWhite176 }177 case awkWhite:178 if white {179 end = idx + 1180 } else {181 ret = append(ret, input[begin:end])182 state, begin, end = awkBlack, idx, idx+1183 }184 }185 }186 if begin < end {187 ret = append(ret, input[begin:end])188 }189 return ret, prefixLength190}191192// Tokenize tokenizes the given string with the delimiter193func Tokenize(text string, delimiter Delimiter) []Token {194 if delimiter.str == nil && delimiter.regex == nil {195 // AWK-style (\S+\s*)196 tokens, prefixLength := awkTokenizer(text)197 return withPrefixLengths(tokens, prefixLength)198 }199200 if delimiter.str != nil {201 return withPrefixLengths(strings.SplitAfter(text, *delimiter.str), 0)202 }203204 // FIXME performance205 var tokens []string206 if delimiter.regex != nil {207 locs := delimiter.regex.FindAllStringIndex(text, -1)208 begin := 0209 for _, loc := range locs {210 tokens = append(tokens, text[begin:loc[1]])211 begin = loc[1]212 }213 if begin < len(text) {214 tokens = append(tokens, text[begin:])215 }216 }217 return withPrefixLengths(tokens, 0)218}219

Make --accept-nth and --with-nth support templatesJunegunn Choibefore adoption84e2262

lines 220–227why, one step back
220// StripLastDelimiter removes the trailing delimiter and whitespaces
221func StripLastDelimiter(str string, delimiter Delimiter) string {222 if delimiter.str != nil {223 str = strings.TrimSuffix(str, *delimiter.str)224 } else if delimiter.regex != nil {225 locs := delimiter.regex.FindAllStringIndex(str, -1)226 if len(locs) > 0 {227 lastLoc := locs[len(locs)-1]

Don't trim last field when delimiter is regex (#4266)phaniumbefore adoption77568e1

lines 228–230why, one step back
228 if lastLoc[1] == len(str) {
229 str = str[:lastLoc[0]]230 }

Add --accept-nth option to transform the outputJunegunn Choibefore adoption2b58458

lines 231–232why, one step back
231 }
232 }

Make --accept-nth and --with-nth support templatesJunegunn Choibefore adoption84e2262

line 233why, one step back
233 return strings.TrimRightFunc(str, unicode.IsSpace)

Add --accept-nth option to transform the outputJunegunn Choibefore adoption2b58458

lines 234–237why, one step back
234}
235236// JoinTokens concatenates the tokens into a single string237func JoinTokens(tokens []Token) string {

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 238–254why, one step back
238 var output bytes.Buffer
239 for _, token := range tokens {240 output.WriteString(token.text.ToString())241 }242 return output.String()243}244245// Transform is used to transform the input when --with-nth option is given246func Transform(tokens []Token, withNth []Range) []Token {247 transTokens := make([]Token, len(withNth))248 numTokens := len(tokens)249 for idx, r := range withNth {250 parts := []*util.Chars{}251 minIdx := 0252 if r.begin == r.end {253 idx := r.begin254 if idx == rangeEllipsis {

Add --accept-nth option to transform the outputJunegunn Choibefore adoption2b58458

line 255why, one step back
255 chars := util.ToChars(stringBytes(JoinTokens(tokens)))

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 256–318why, one step back
256 parts = append(parts, &chars)
257 } else {258 if idx < 0 {259 idx += numTokens + 1260 }261 if idx >= 1 && idx <= numTokens {262 minIdx = idx - 1263 parts = append(parts, tokens[idx-1].text)264 }265 }266 } else {267 var begin, end int268 if r.begin == rangeEllipsis { // ..N269 begin, end = 1, r.end270 if end < 0 {271 end += numTokens + 1272 }273 } else if r.end == rangeEllipsis { // N..274 begin, end = r.begin, numTokens275 if begin < 0 {276 begin += numTokens + 1277 }278 } else {279 begin, end = r.begin, r.end280 if begin < 0 {281 begin += numTokens + 1282 }283 if end < 0 {284 end += numTokens + 1285 }286 }287 minIdx = util.Max(0, begin-1)288 for idx := begin; idx <= end; idx++ {289 if idx >= 1 && idx <= numTokens {290 parts = append(parts, tokens[idx-1].text)291 }292 }293 }294 // Merge multiple parts295 var merged util.Chars296 switch len(parts) {297 case 0:298 merged = util.ToChars([]byte{})299 case 1:300 merged = *parts[0]301 default:302 var output bytes.Buffer303 for _, part := range parts {304 output.WriteString(part.ToString())305 }306 merged = util.ToChars(output.Bytes())307 }308309 var prefixLength int32310 if minIdx < numTokens {311 prefixLength = tokens[minIdx].prefixLength312 } else {313 prefixLength = 0314 }315 transTokens[idx] = Token{&merged, prefixLength}316 }317 return transTokens318}