pin.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package pin
  2. import (
  3. "fmt"
  4. //"strconv"
  5. //"log"
  6. "vultras.su/core/cli/mtool"
  7. "os"
  8. )
  9. var (
  10. Lflag bool
  11. lval int
  12. delim rune = '\n'
  13. nVal int
  14. chrs []rune
  15. Tool = mtool.T("pin").Func(Run).Desc(
  16. "print all the possible PIN combinations made of custom characters",
  17. ).Usage(
  18. "<tmpl_chars>",
  19. )
  20. )
  21. func Pow(x, p int) int {
  22. ret := 1
  23. for i:=0 ; i<p ; i++ {
  24. ret *= x
  25. }
  26. return ret
  27. }
  28. func GetPin(s []rune, l int, i int) string {
  29. ret := ""
  30. slen := len(s)
  31. for j:=0 ; j<l ; j++ {
  32. ret = string(s[ ( i/Pow(slen, j) ) % slen] ) + ret
  33. }
  34. return ret
  35. }
  36. func printPins(s []rune, l int) {
  37. n := Pow(len(s), l)
  38. for i:=0 ; i<n ; i++ {
  39. pin := GetPin(s, l, i)
  40. if Fits([]rune(pin)) {
  41. fmt.Println(GetPin(s, l, i))
  42. }
  43. }
  44. }
  45. func Fits(s []rune) bool {
  46. a := make([]int, len(chrs))
  47. for i, v1 := range chrs {
  48. for _, v2 := range s {
  49. if v1 == v2 {
  50. a[i]++
  51. if a[i] > nVal {
  52. return false
  53. }
  54. }
  55. }
  56. }
  57. return true
  58. }
  59. func Run(flags *mtool.Flags) {
  60. var (
  61. length int
  62. rFlag bool
  63. )
  64. flags.IntVar(
  65. &lval, "min", 0,
  66. "min length of the output pins",
  67. )
  68. flags.BoolVar(
  69. &Lflag, "m", false,
  70. "set the '-min' flag value to 1 (overrides it)",
  71. )
  72. flags.IntVar(
  73. &length, "max", 0,
  74. "max length of the output pins",
  75. )
  76. flags.IntVar(&nVal, "rep", 1, "max repeats of the rune." )
  77. flags.BoolVar(&rFlag, "R", false, "make the maximum repeat equal to the length of input chars" )
  78. args := flags.Parse()
  79. if len(args) == 0 {
  80. flags.Usage()
  81. os.Exit(1)
  82. }
  83. chrs = []rune(args[0])
  84. if length == 0 {
  85. length = len(chrs)
  86. }
  87. if Lflag {
  88. lval = 1
  89. }
  90. if rFlag {
  91. nVal = len(chrs)
  92. }
  93. if lval != 0 {
  94. if lval > length {
  95. flags.Usage()
  96. os.Exit(1)
  97. }
  98. for i := lval ; i<=length ; i++ {
  99. printPins(chrs, i)
  100. }
  101. } else {
  102. printPins(chrs, length)
  103. }
  104. }