123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package pin
- import (
- "fmt"
- //"strconv"
- //"log"
- "vultras.su/core/cli/mtool"
- "os"
- )
- var (
- Lflag bool
- lval int
- delim rune = '\n'
- nVal int
- chrs []rune
- Tool = mtool.T("pin").Func(Run).Desc(
- "print all the possible PIN combinations made of custom characters",
- ).Usage(
- "<tmpl_chars>",
- )
- )
- func Pow(x, p int) int {
- ret := 1
- for i:=0 ; i<p ; i++ {
- ret *= x
- }
- return ret
- }
- func GetPin(s []rune, l int, i int) string {
- ret := ""
- slen := len(s)
- for j:=0 ; j<l ; j++ {
- ret = string(s[ ( i/Pow(slen, j) ) % slen] ) + ret
- }
- return ret
- }
- func printPins(s []rune, l int) {
- n := Pow(len(s), l)
- for i:=0 ; i<n ; i++ {
- pin := GetPin(s, l, i)
- if Fits([]rune(pin)) {
- fmt.Println(GetPin(s, l, i))
- }
- }
- }
- func Fits(s []rune) bool {
- a := make([]int, len(chrs))
- for i, v1 := range chrs {
- for _, v2 := range s {
- if v1 == v2 {
- a[i]++
- if a[i] > nVal {
- return false
- }
- }
- }
- }
- return true
- }
- func Run(flags *mtool.Flags) {
- var (
- length int
- rFlag bool
- )
- flags.IntVar(
- &lval, "min", 0,
- "min length of the output pins",
- )
- flags.BoolVar(
- &Lflag, "m", false,
- "set the '-min' flag value to 1 (overrides it)",
- )
- flags.IntVar(
- &length, "max", 0,
- "max length of the output pins",
- )
- flags.IntVar(&nVal, "rep", 1, "max repeats of the rune." )
- flags.BoolVar(&rFlag, "R", false, "make the maximum repeat equal to the length of input chars" )
- args := flags.Parse()
- if len(args) == 0 {
- flags.Usage()
- os.Exit(1)
- }
- chrs = []rune(args[0])
- if length == 0 {
- length = len(chrs)
- }
- if Lflag {
- lval = 1
- }
- if rFlag {
- nVal = len(chrs)
- }
- if lval != 0 {
- if lval > length {
- flags.Usage()
- os.Exit(1)
- }
- for i := lval ; i<=length ; i++ {
- printPins(chrs, i)
- }
- } else {
- printPins(chrs, length)
- }
- }
|