|
5 | 5 | "io/fs" |
6 | 6 | "log" |
7 | 7 | "math/bits" |
8 | | - "math/rand" |
9 | 8 | "os" |
10 | 9 | "strconv" |
11 | 10 | "strings" |
@@ -69,16 +68,7 @@ var nameCmd = &cli.Command{ |
69 | 68 | if err != nil { |
70 | 69 | return errors.Wrapf(err, "couldn't load wordlists for language '%s", c.String("lang")) |
71 | 70 | } |
72 | | - shuffleWordlist(first, 0) |
73 | | - shuffleWordlist(second, 1) |
74 | | - firstSize := uint32(len(first)) |
75 | | - secondSize := uint32(len(second)) |
76 | | - quotient := shuffle(parsed) |
77 | | - firstIndex := quotient % firstSize |
78 | | - quotient /= firstSize |
79 | | - secondIndex := quotient % secondSize |
80 | | - quotient /= secondSize |
81 | | - fmt.Printf("%s-%s-%d\n", first[firstIndex], second[secondIndex], quotient) |
| 71 | + fmt.Println(computeName(parsed, first, second)) |
82 | 72 | return nil |
83 | 73 | }, |
84 | 74 | } |
@@ -127,55 +117,63 @@ func loadWordlist(lang fs.FS, file string) ([]string, error) { |
127 | 117 | return words, nil |
128 | 118 | } |
129 | 119 |
|
130 | | -func shuffleWordlist(wordlist []string, randSeed int64) { |
131 | | - rng := rand.New(rand.NewSource(randSeed)) //nolint:gosec // We need a PRNG to shuffle reproducibly |
132 | | - rng.Shuffle(len(wordlist), func(i, j int) { |
133 | | - atI := wordlist[i] |
134 | | - atJ := wordlist[j] |
135 | | - wordlist[i] = atJ |
136 | | - wordlist[j] = atI |
137 | | - }) |
| 120 | +func computeName(sn uint32, first []string, second []string) string { |
| 121 | + firstSize := uint32(len(first)) |
| 122 | + secondSize := uint32(len(second)) |
| 123 | + quotient := shuffle(sn) |
| 124 | + firstIndex := quotient % firstSize |
| 125 | + quotient /= firstSize |
| 126 | + secondIndex := quotient % secondSize |
| 127 | + quotient /= secondSize |
| 128 | + return fmt.Sprintf("%s-%s-%d\n", first[firstIndex], second[secondIndex], quotient) |
138 | 129 | } |
139 | 130 |
|
| 131 | +const ( |
| 132 | + shuffleShift8 = 8 |
| 133 | + shuffleShift4 = 4 |
| 134 | + shuffleShift2 = 2 |
| 135 | + shuffleShift1 = 1 |
| 136 | + shuffleMask8 = 0x0000ff00 |
| 137 | + shuffleMask4 = 0x00f000f0 |
| 138 | + shuffleMask2 = 0x0c0c0c0c |
| 139 | + shuffleMask1 = 0x22222222 |
| 140 | +) |
| 141 | + |
140 | 142 | // shuffle performs a one-to-one mapping of the serial number so that consecutive numbers are no |
141 | 143 | // longer close to each other. |
142 | | -// |
143 | | -//nolint:gomnd // We need a bunch of well-defined bit shifts and masks for this algorithm |
144 | 144 | func shuffle(x uint32) uint32 { |
145 | 145 | // This code was copied from the Hacker's Delight website at |
146 | 146 | // https://web.archive.org/web/20160405214331/http://hackersdelight.org/hdcodetxt/shuffle.c.txt |
147 | 147 | // which is licensed released to the public domain - for details, refer to |
148 | 148 | // https://web.archive.org/web/20160309224818/http://www.hackersdelight.org/permissions.htm |
149 | | - t := (x ^ (x >> 8)) & 0x0000ff00 |
150 | | - x = x ^ t ^ (t << 8) |
151 | | - t = (x ^ (x >> 4)) & 0x00f000f0 |
152 | | - x = x ^ t ^ (t << 4) |
153 | | - t = (x ^ (x >> 2)) & 0x0c0c0c0c |
154 | | - x = x ^ t ^ (t << 2) |
155 | | - t = (x ^ (x >> 1)) & 0x22222222 |
156 | | - x = x ^ t ^ (t << 1) |
| 149 | + t := (x ^ (x >> shuffleShift8)) & shuffleMask8 |
| 150 | + x = x ^ t ^ (t << shuffleShift8) |
| 151 | + t = (x ^ (x >> shuffleShift4)) & shuffleMask4 |
| 152 | + x = x ^ t ^ (t << shuffleShift4) |
| 153 | + t = (x ^ (x >> shuffleShift2)) & shuffleMask2 |
| 154 | + x = x ^ t ^ (t << shuffleShift2) |
| 155 | + t = (x ^ (x >> shuffleShift1)) & shuffleMask1 |
| 156 | + x = x ^ t ^ (t << shuffleShift1) |
157 | 157 | x = bits.Reverse32(x) |
158 | 158 | return x |
159 | 159 | } |
160 | 160 |
|
161 | 161 | /* |
162 | 162 | // unshuffle inverts the shuffle operation. |
163 | | -// |
164 | | -//nolint:gomnd // We need a bunch of well-defined bit shifts and masks for this algorithm |
165 | 163 | func unshuffle(x uint32) uint32 { |
166 | 164 | // This code was copied from the Hacker's Delight website at |
167 | 165 | // https://web.archive.org/web/20160405214331/http://hackersdelight.org/hdcodetxt/shuffle.c.txt |
168 | 166 | // which is licensed released to the public domain - for details, refer to |
169 | 167 | // https://web.archive.org/web/20160309224818/http://www.hackersdelight.org/permissions.htm |
170 | 168 | x = bits.Reverse32(x) |
171 | | - t := (x ^ (x >> 1)) & 0x22222222 |
172 | | - x = x ^ t ^ (t << 1) |
173 | | - t = (x ^ (x >> 2)) & 0x0c0c0c0c |
174 | | - x = x ^ t ^ (t << 2) |
175 | | - t = (x ^ (x >> 4)) & 0x00f000f0 |
176 | | - x = x ^ t ^ (t << 4) |
177 | | - t = (x ^ (x >> 8)) & 0x0000ff00 |
178 | | - x = x ^ t ^ (t << 8) |
| 169 | + t := (x ^ (x >> shuffleShift1)) & shuffleMask1 |
| 170 | + x = x ^ t ^ (t << shuffleShift1) |
| 171 | + t = (x ^ (x >> shuffleShift2)) & shuffleMask2 |
| 172 | + x = x ^ t ^ (t << shuffleShift2) |
| 173 | + t = (x ^ (x >> shuffleShift4)) & shuffleMask4 |
| 174 | + x = x ^ t ^ (t << shuffleShift4) |
| 175 | + t = (x ^ (x >> shuffleShift8)) & shuffleMask8 |
| 176 | + x = x ^ t ^ (t << shuffleShift8) |
179 | 177 | return x |
180 | 178 | } |
181 | 179 | */ |
0 commit comments