github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/brutecheck/lengthsingle.go (about) 1 package main 2 3 import "strings" 4 5 // LengthSingle generates: 6 // 7 // func Len1Ch(name string) bool { 8 // switch len(name) { 9 // case 2: // go, if 10 // switch name[0] { 11 // case 'g': 12 // return name == "go" 13 // ... 14 type LengthSingle struct{} 15 16 func (LengthSingle) Generate(p Printer, keywords []string) { 17 bylen := map[int][]string{} 18 _, maxlen := keywordBounds(keywords) 19 for _, keyword := range keywords { 20 n := len(keyword) 21 bylen[n] = append(bylen[n], keyword) 22 } 23 uniquech := map[int]int{} 24 for namelen, names := range bylen { 25 chi := findUniqueChar(names) 26 if chi < 0 { 27 return 28 } 29 uniquech[namelen] = chi 30 } 31 32 p.FuncName("Len1Ch") 33 p.F("func Len1Ch(name string) bool {\n") 34 { 35 p.F("switch len(name) {") 36 for namelen := 0; namelen < maxlen; namelen++ { 37 names, ok := bylen[namelen] 38 if !ok { 39 continue 40 } 41 p.F("case %d: // %s\n", namelen, strings.Join(names, ", ")) 42 if len(names) == 1 { 43 p.F("return name == %q\n", names[0]) 44 continue 45 } 46 47 chi := uniquech[namelen] 48 p.F("switch name[%d] {\n", chi) 49 for _, name := range names { 50 p.F("case %q: return name == %q\n", name[chi], name) 51 } 52 p.F("}\n") 53 p.F("return false\n") 54 } 55 p.F("}\n") 56 } 57 p.F("return false\n") 58 p.F("}\n\n") 59 }