56 lines
1003 B
Go
56 lines
1003 B
Go
package pwgenerator
|
|
|
|
// Len returns the length of a CharSet (needed for sort.Interface).
|
|
func (c *CharSet) Len() (l int) {
|
|
|
|
l = len(*c)
|
|
|
|
return
|
|
}
|
|
|
|
// Less returns true if item at index i is "less than" (sorts before) item at index j (needed for sort.Interface).
|
|
func (c *CharSet) Less(i, j int) (isBefore bool) {
|
|
|
|
if (*c)[i] < (*c)[j] {
|
|
isBefore = true
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// RandChar returns a random character from a CharSet.
|
|
func (c *CharSet) RandChar() (char Char, err error) {
|
|
|
|
var selectIdx int
|
|
|
|
if selectIdx, err = saferRandInt(len(*c) - 1); err != nil {
|
|
return
|
|
}
|
|
|
|
char = (*c)[selectIdx]
|
|
|
|
return
|
|
}
|
|
|
|
// String returns a string from a CharSet.
|
|
func (c *CharSet) String() (s string) {
|
|
|
|
for _, i := range *c {
|
|
s += string(i)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Swap will swap the position of the item at index i and the item at index j in a CharSet (needed for sort.Interface).
|
|
func (c *CharSet) Swap(i, j int) {
|
|
|
|
var iVal Char = (*c)[i]
|
|
var jVal Char = (*c)[j]
|
|
|
|
(*c)[i] = jVal
|
|
(*c)[j] = iVal
|
|
|
|
return
|
|
}
|