Skip to content

Commit

Permalink
handling hyphenated queries and names
Browse files Browse the repository at this point in the history
  • Loading branch information
ramalho committed Feb 17, 2024
1 parent 0c4ae63 commit 377d041
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
24 changes: 20 additions & 4 deletions go/rf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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++
}
Expand All @@ -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.")
}
Expand Down
9 changes: 8 additions & 1 deletion go/rf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }()
Expand Down

0 comments on commit 377d041

Please sign in to comment.