diff --git a/comp.go b/comp.go index 7d4d3745..e8cc184f 100644 --- a/comp.go +++ b/comp.go @@ -151,7 +151,7 @@ func matchFile(s string) (matches []string, longest string) { dir = wd + string(filepath.Separator) + dir } - dir = filepath.Dir(dir) + dir = unescape(filepath.Dir(dir)) fi, err := ioutil.ReadDir(dir) if err != nil { @@ -166,11 +166,12 @@ func matchFile(s string) (matches []string, longest string) { } _, last := filepath.Split(s) - if strings.HasPrefix(f.Name(), last) { + if strings.HasPrefix(escape(f.Name()), last) { name := f.Name() if isRoot(s) || filepath.Base(s) != s { name = filepath.Join(filepath.Dir(s), f.Name()) } + name = escape(name) item := f.Name() if f.Mode().IsDir() { item += string(filepath.Separator) @@ -197,7 +198,7 @@ func matchFile(s string) (matches []string, longest string) { func compCmd(acc []rune) (matches []string, longestAcc []rune) { s := string(acc) - f := strings.Fields(s) + f := tokenize(s) if len(f) == 0 || s[len(s)-1] == ' ' { f = append(f, "") diff --git a/misc.go b/misc.go index 0304d93a..db3d926f 100644 --- a/misc.go +++ b/misc.go @@ -50,6 +50,81 @@ func runeSliceWidthRange(rs []rune, beg int, end int) []rune { return nil } +// This function is used to escape whitespaces with backlashes in a given +// string. If a whitespace is already escaped then it is not escaped again. +func escape(s string) string { + esc := false + buf := make([]rune, 0, len(s)) + for _, r := range s { + if r == '\\' { + esc = true + continue + } + if esc { + esc = false + buf = append(buf, '\\', r) + continue + } + if unicode.IsSpace(r) { + buf = append(buf, '\\') + } + buf = append(buf, r) + } + return string(buf) +} + +// This function is used to remove backlashes that are used to escape +// whitespaces in a given string. +func unescape(s string) string { + esc := false + buf := make([]rune, 0, len(s)) + for _, r := range s { + if r == '\\' { + esc = true + continue + } + if esc { + esc = false + if unicode.IsSpace(r) { + buf = append(buf, r) + } else { + buf = append(buf, '\\', r) + } + continue + } + buf = append(buf, r) + } + return string(buf) +} + +// This function splits the given string by whitespaces. It is aware of escaped +// whitespaces so that they are not splitted unintentionally. +func tokenize(s string) []string { + esc := false + var buf []rune + var toks []string + for _, r := range s { + if r == '\\' { + esc = true + buf = append(buf, r) + continue + } + if esc { + esc = false + buf = append(buf, r) + continue + } + if !unicode.IsSpace(r) { + buf = append(buf, r) + } else { + toks = append(toks, string(buf)) + buf = nil + } + } + toks = append(toks, string(buf)) + return toks +} + // This function splits the first word of a string delimited by whitespace from // the rest. This is used to tokenize a string one by one without touching the // rest. Whitespace on the left side of both the word and the rest are trimmed. diff --git a/misc_test.go b/misc_test.go index 7ebc8b54..a76a9bb0 100644 --- a/misc_test.go +++ b/misc_test.go @@ -91,6 +91,63 @@ func TestRuneSliceWidthRange(t *testing.T) { } } +func TestEscape(t *testing.T) { + strs := []struct { + s string + e string + }{ + {"", ""}, + {"foo", "foo"}, + {"foo bar", `foo\ bar`}, + {"foo bar", `foo\ \ bar`}, + {`foo\ bar`, `foo\ bar`}, + {`foo\bar`, `foo\bar`}, + } + + for _, str := range strs { + if e := escape(str.s); !reflect.DeepEqual(e, str.e) { + t.Errorf("at input '%v' expected '%v' but got '%v'", str.s, str.e, e) + } + } +} + +func TestUnescape(t *testing.T) { + strs := []struct { + s string + u string + }{ + {"", ""}, + {"foo", "foo"}, + {`foo\bar`, `foo\bar`}, + {`foo\ bar`, "foo bar"}, + {`foo\ \ bar`, "foo bar"}, + } + + for _, str := range strs { + if u := unescape(str.s); !reflect.DeepEqual(u, str.u) { + t.Errorf("at input '%v' expected '%v' but got '%v'", str.s, str.u, u) + } + } +} + +func TestTokenize(t *testing.T) { + strs := []struct { + s string + toks []string + }{ + {"", []string{""}}, + {"foo", []string{"foo"}}, + {"foo bar", []string{"foo", "bar"}}, + {`:rename foo\ bar`, []string{":rename", `foo\ bar`}}, + } + + for _, str := range strs { + if toks := tokenize(str.s); !reflect.DeepEqual(toks, str.toks) { + t.Errorf("at input '%v' expected '%v' but got '%v'", str.s, str.toks, toks) + } + } +} + func TestSplitWord(t *testing.T) { strs := []struct { s string