Browse Source

Convert to multi if blocks with in-place reverse

master
jimi 9 years ago
parent
commit
c57e2466e3
  1. 27
      secret_handshake.go

27
secret_handshake.go

@ -25,19 +25,22 @@ func Handshake(i int) (h []string) {
return h
}
// Pre-define the ordered key sets.
keys := []int{wnk, bli, clo, jmp}
bkwd := []int{jmp, clo, bli, wnk}
// Use the reversed key set if the 5th bit is set.
if i&rev != 0 {
keys = bkwd
if i&wnk != 0 {
h = append(h, m[wnk])
}
if i&bli != 0 {
h = append(h, m[bli])
}
if i&clo != 0 {
h = append(h, m[clo])
}
if i&jmp != 0 {
h = append(h, m[jmp])
}
// Build the string slice based on the bitmask.
for _, k := range keys {
if i&k != 0 {
h = append(h, m[k])
if i&rev != 0 {
for i, j := 0, len(h)-1; i < j; i, j = i+1, j-1 {
h[i], h[j] = h[j], h[i]
}
}
@ -47,4 +50,4 @@ func Handshake(i int) (h []string) {
// Revision 1 benchmark:
//
// PASS
// BenchmarkHandshake-12 2000000 718 ns/op
// BenchmarkHandshake-12 2000000 734 ns/op
Loading…
Cancel
Save