diff options
author | Andinus <andinus@nand.sh> | 2020-04-06 23:52:41 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-04-06 23:52:41 +0530 |
commit | 30f7ea8ddf1077ddc3bc289f56dff39c41258827 (patch) | |
tree | 51a8660b368d8a5dc67e059973e52d45a2a4aef1 /search/anagrams.go | |
parent | 1e6075d11caa9565e82707fff06e64a7c648464f (diff) | |
download | grus-30f7ea8ddf1077ddc3bc289f56dff39c41258827.tar.gz |
Add Anagrams function
Diffstat (limited to 'search/anagrams.go')
-rw-r--r-- | search/anagrams.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/search/anagrams.go b/search/anagrams.go new file mode 100644 index 0000000..e04dfd7 --- /dev/null +++ b/search/anagrams.go @@ -0,0 +1,35 @@ +package search + +import "tildegit.org/andinus/grus/storage" + +// Anagrams will search for unjumbled words in database, given sorted +// word along with all the anagrams. +func Anagrams(sorted string, db *storage.DB) (anagrams []string, err error) { + db.Mu.RLock() + defer db.Mu.RUnlock() + + stmt, err := db.Conn.Prepare("SELECT word FROM words WHERE sorted = ?") + if err != nil { + return + } + defer stmt.Close() + + rows, err := stmt.Query(sorted) + if err != nil { + return + } + defer rows.Close() + for rows.Next() { + var word string + err = rows.Scan(&word) + if err != nil { + return + } + anagrams = append(anagrams, word) + } + err = rows.Err() + if err != nil { + return + } + return +} |