Skip to content

Commit

Permalink
refactor word lists
Browse files Browse the repository at this point in the history
this commit adds some meta data to each word list: the author of the
list, the license, the origin etc. the data is also shown in the -dump
output.

the encoding of the russian word list is converted to utf8.
  • Loading branch information
mgumz committed Oct 8, 2016
1 parent 223cd6e commit 7c0a0e4
Show file tree
Hide file tree
Showing 13 changed files with 7,943 additions and 7,872 deletions.
28 changes: 21 additions & 7 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@ package main

import (
"fmt"
"io"
"os"
"text/tabwriter"
)

func doDumpList(list string) {
r := internalListReader(list)
if r == nil {
fmt.Fprintf(os.Stderr, "error: list %q does not exist\n", list)
func doDumpList(name string) {
list, exists := internalLists[name]
if !exists {
fmt.Fprintf(os.Stderr, "error: list %q does not exist\n", name)
return
}
defer r.Close()
io.Copy(os.Stdout, r)

w := &tabwriter.Writer{}
w.Init(os.Stdout, 0, 8, 2, ' ', 0)
defer w.Flush()

fmt.Fprintln(w, "Name:", "\t", list.Name)
fmt.Fprintln(w, "Author:", "\t", list.Author)
fmt.Fprintln(w, "Origin:", "\t", list.Origin)
fmt.Fprintln(w, "License:", "\t", list.License)

fmt.Fprintln(w, "---")

for i, word := range linesFromInternalList(name) {
fmt.Fprintln(w, list.Index(i), "\t", word)
}

}
3 changes: 3 additions & 0 deletions index_stringer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

type IndexStringer func(int) string
12 changes: 6 additions & 6 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package main

import "fmt"

func doLookupRolls(rolls, lines []string, printer *wordPrinter) error {
func doLookupRolls(rolls, words []string, printer *wordPrinter) error {

words := make([]wordLine, len(rolls))
selected := make([]wordLine, len(rolls))

for i, chain := range rolls {
index, err := parseDiceChain(chain)
if err != nil {
return fmt.Errorf("%q not a valid dicechain\n", chain)
}
if index < 0 || index >= len(lines) {
if index < 0 || index >= len(words) {
return fmt.Errorf("%q not in the list\n", chain)
}

word := lines[index]
words[i].Set(word, index)
word := words[index]
selected[i].Set(word, index)
}

printer.Print(words)
printer.Print(selected)

return nil
}
59 changes: 0 additions & 59 deletions wordline.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package main

import (
"bufio"
"io"
"io/ioutil"
"sort"
"strings"
)

//
// ========================================================================
//
Expand All @@ -21,54 +13,3 @@ func (wl *wordLine) Set(line string, index int) {
wl.word = line
wl.index = index
}

//
// ========================================================================
//

var internalLists = make(map[string]string)

func linesFromInternalList(list string) []string {
r := internalListReader(list)
if r == nil {
return nil
}
defer r.Close()
return readerToLines(r)
}

func readerToLines(r io.Reader) []string {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
lines := make([]string, 0)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

return lines
}

func internalListReader(name string) io.ReadCloser {
list, exists := internalLists[name]
if !exists {
return nil
}

return listReaderFromString(list)
}

func listReaderFromString(list string) io.ReadCloser {
r := strings.NewReader(strings.TrimSpace(list))
return ioutil.NopCloser(r)
}

func internalListNames() []string {
names := make([]string, len(internalLists))
i := 0
for name, _ := range internalLists {
names[i] = name
i += 1
}
sort.Strings(names)
return names
}
65 changes: 65 additions & 0 deletions words.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"bufio"
"io"
"io/ioutil"
"sort"
"strings"
)

type WordList struct {
Name string
Author string
License string
Origin string
Index IndexStringer
Words string
}

var internalLists = make(map[string]*WordList)

func linesFromInternalList(list string) []string {
r := internalListReader(list)
if r == nil {
return nil
}
defer r.Close()
return readerToLines(r)
}

func readerToLines(r io.Reader) []string {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
lines := make([]string, 0)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

return lines
}

func internalListReader(name string) io.ReadCloser {
list, exists := internalLists[name]
if !exists {
return nil
}

return listReaderFromString(list.Words)
}

func listReaderFromString(list string) io.ReadCloser {
r := strings.NewReader(strings.TrimSpace(list))
return ioutil.NopCloser(r)
}

func internalListNames() []string {
names := make([]string, len(internalLists))
i := 0
for name, _ := range internalLists {
names[i] = name
i += 1
}
sort.Strings(names)
return names
}
13 changes: 9 additions & 4 deletions words_beale.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package main

func init() {

// base64(gzip('lists/beale.wordlist.asc'))
internalLists["beale"] = `a
internalLists["beale"] = &WordList{
Name: "beale",
Author: "Alan Beale",
License: "",
Origin: "http://world.std.com/~reinhold/beale.wordlist.asc",
Index: indexToDiceChain,
Words: `a
a's
a-1
a-z
Expand Down Expand Up @@ -7778,6 +7783,6 @@ $$
=
?
??
@`

@`,
}
}
13 changes: 9 additions & 4 deletions words_diceware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package main

func init() {

// base64(gzip('lists/diceware.wordlist.asc'))
internalLists["diceware"] = `a
internalLists["diceware"] = &WordList{
Name: "diceware",
Author: "Arnold Reinhold",
License: "",
Origin: "http://world.std.com/~reinhold/diceware.wordlist.asc",
Index: indexToDiceChain,
Words: `a
a&p
a's
aa
Expand Down Expand Up @@ -7778,6 +7783,6 @@ $$
=
?
??
@`

@`,
}
}
13 changes: 10 additions & 3 deletions words_diceware8k.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package main

import "strconv"

func init() {
internalLists["diceware8k"] = `a
internalLists["diceware8k"] = &WordList{
Name: "Diceware 8k List",
Author: "Arnold Reinhold",
License: "",
Index: strconv.Itoa,
Words: `a
a&p
a's
a2
Expand Down Expand Up @@ -8192,6 +8199,6 @@ $$
=
?
??
@`

@`,
}
}
14 changes: 11 additions & 3 deletions words_eff2016.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package main

func init() {
internalLists["eff2016"] = `abacus
internalLists["eff2016"] = &WordList{
Name: "Electronic Frontier Foundation Word List, 2006er version",
// see .Origin, Joseph writes about
// "We took all words between 3 and 9 characters from the list"
Author: "Joseph Bonneau et al.",
License: "Creative Commons Attribution License", // https://www.eff.org/copyright
Origin: "https://www.eff.org/deeplinks/2016/07/new-wordlists-random-passphrases",
Index: indexToDiceChain,
Words: `abacus
abdomen
abdominal
abide
Expand Down Expand Up @@ -7776,6 +7784,6 @@ zoning
zookeeper
zoologist
zoology
zoom`

zoom`,
}
}
12 changes: 9 additions & 3 deletions words_german.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

func init() {
internalLists["de"] = `0
internalLists["de"] = &WordList{
Name: "German Diceware List",
Author: "Benjamin Tenne",
License: "GNU General Public License",
Origin: "http://world.std.com/~reinhold/diceware_german.txt",
Index: indexToDiceChain,
Words: `0
1
2
3
Expand Down Expand Up @@ -7776,6 +7782,6 @@ zyste
zz
zzgl
zzz
zzzz`

zzzz`,
}
}
12 changes: 9 additions & 3 deletions words_italian.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

func init() {
internalLists["it"] = `0
internalLists["it"] = &WordList{
Name: "Italian Word List",
License: "",
Author: "",
Origin: "",
Index: indexToDiceChain,
Words: `0
1
2
3
Expand Down Expand Up @@ -7776,6 +7782,6 @@ zx
zy
zz
zzz
zzzz`

zzzz`,
}
}
12 changes: 9 additions & 3 deletions words_japanese.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

func init() {
internalLists["jp"] = `0
internalLists["jp"] = &WordList{
Name: "Japanese Word List",
Author: "Hiroshi Yuki",
License: "",
Origin: "http://s3.amazonaws.com/dotclue.org/diceware_jp.txt",
Index: indexToDiceChain,
Words: `0
1
10
100
Expand Down Expand Up @@ -7776,6 +7782,6 @@ zx
zy
zz
zzz
zzzz`

zzzz`,
}
}
Loading

0 comments on commit 7c0a0e4

Please sign in to comment.