From 377d041b99ed5ecd2eba99ca7181e3d8e7f52eb3 Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Sat, 17 Feb 2024 20:27:19 -0300 Subject: [PATCH] handling hyphenated queries and names --- go/rf.go | 24 ++++++++++++++++++++---- go/rf_test.go | 9 ++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/go/rf.go b/go/rf.go index da50206..04a82ce 100644 --- a/go/rf.go +++ b/go/rf.go @@ -9,9 +9,25 @@ import ( "golang.org/x/text/unicode/runenames" ) -const first, last = ' ', unicode.MaxRune +const default_first, default_last = ' ', unicode.MaxRune -func find(words ...string) { +func tokenize(text string) []string { + return strings.Fields(strings.ReplaceAll(text, "-", " ")) +} + +func find(text string, firstLast ...rune) { + first, last := default_first, default_last + switch len(firstLast) { + case 0: + // done + case 1: + first = firstLast[0] + case 2: + first, last = firstLast[0], firstLast[1] + default: + panic("find: firstLast must be 0, 1 or 2 runes") + } + words := tokenize(text) query := []string{} for _, word := range words { query = append(query, strings.ToUpper(word)) @@ -22,7 +38,7 @@ func find(words ...string) { if len(name) == 0 { continue } - if containsAll(strings.Fields(name), query) { + if containsAll(tokenize(name), query) { fmt.Printf("%U\t%c\t%v\n", char, char, name) count++ } @@ -32,7 +48,7 @@ func find(words ...string) { func main() { if len(os.Args) > 1 { - find(os.Args[1:]...) + find(strings.Join(os.Args[1:], " ")) } else { fmt.Println("Please provide words to find.") } diff --git a/go/rf_test.go b/go/rf_test.go index 0dfaf4a..cbec5cf 100644 --- a/go/rf_test.go +++ b/go/rf_test.go @@ -12,13 +12,20 @@ func Example_find() { } func Example_find_two_results() { - find("hexagram", "completion") + find("completion hexagram") // Output: // U+4DFE ䷾ HEXAGRAM FOR AFTER COMPLETION // U+4DFF ䷿ HEXAGRAM FOR BEFORE COMPLETION // (2 found) } +func Example_find_hyphenated_name() { + find("hyphen", ' ', '\x80') + // Output: + // U+002D - HYPHEN-MINUS + // (1 found) +} + func Example() { oldArgs := os.Args defer func() { os.Args = oldArgs }()