github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/brutecheck/uint64switch.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/binary"
     5  )
     6  
     7  type Uint64Switch struct{}
     8  
     9  func (Uint64Switch) Generate(p Printer, keywords []string) {
    10  	minlen, _ := keywordBounds(keywords)
    11  	if minlen < 2 {
    12  		return
    13  	}
    14  
    15  	p.F("import \"encoding/binary\"\n")
    16  
    17  	p.FuncName("Uint64Switch")
    18  	p.F("func Uint64Switch(name string) bool {\n")
    19  	{
    20  		p.F("var key [8]byte\n")
    21  		p.F("copy(key[:], name)\n")
    22  		p.F("h := binary.LittleEndian.Uint64(key[:])\n")
    23  
    24  		p.F("switch h {\n")
    25  		for _, keyword := range keywords {
    26  			var key [8]byte
    27  			copy(key[:], keyword)
    28  			h := binary.LittleEndian.Uint64(key[:])
    29  
    30  			p.F("case %d:\n", h)
    31  			if len(keyword) <= 8 {
    32  				p.F("return len(name) == %d\n", len(keyword))
    33  			} else {
    34  				p.F("return name[8:] == %q\n", keyword[8:])
    35  			}
    36  		}
    37  		p.F("}\n")
    38  	}
    39  	p.F("return false\n")
    40  	p.F("}\n\n")
    41  }