cbecd9caec
FIXED: * XML, YAML marshaling/unmarshaling of pwgenerator.CharSet.
149 lines
2.4 KiB
Go
149 lines
2.4 KiB
Go
package pwgenerator
|
|
|
|
/*
|
|
Has returns true if a CharSet contains char.
|
|
|
|
char can be one of: Char, byte, rune, or a string (of length 1).
|
|
*/
|
|
func (c *CharSet) Has(char interface{}) (contains bool, err error) {
|
|
|
|
var chkChar Char
|
|
|
|
switch t := char.(type) {
|
|
case Char:
|
|
chkChar = t
|
|
case byte:
|
|
chkChar = Char(t)
|
|
case rune:
|
|
chkChar = Char(t)
|
|
case string:
|
|
if len(t) != 1 {
|
|
err = ErrBadType
|
|
return
|
|
}
|
|
chkChar = Char(t[0])
|
|
default:
|
|
err = ErrBadType
|
|
return
|
|
}
|
|
|
|
for _, i := range *c {
|
|
if chkChar == i {
|
|
contains = true
|
|
break
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// MarshalText conforms to encoding/TextUnmarshaler.
|
|
func (c *CharSet) MarshalText() (text []byte, err error) {
|
|
|
|
if c == nil {
|
|
return
|
|
}
|
|
|
|
text = []byte(c.String())
|
|
|
|
return
|
|
}
|
|
|
|
// MarshalYAML conforms to (github.com/goccy/go-yaml)yaml.BytesMarshaler. (For some reason, this only works as direct, not pointer.)
|
|
func (c CharSet) MarshalYAML() (b []byte, err error) {
|
|
|
|
if c == nil {
|
|
return
|
|
}
|
|
|
|
b = []byte(c.String())
|
|
|
|
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)); 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) {
|
|
|
|
(*c)[j], (*c)[i] = (*c)[i], (*c)[j]
|
|
|
|
return
|
|
}
|
|
|
|
// UnmarshalText conforms to an encoding.TextUnmarshaler.
|
|
func (c *CharSet) UnmarshalText(text []byte) (err error) {
|
|
|
|
var cs []Char
|
|
|
|
if text == nil {
|
|
return
|
|
}
|
|
|
|
for _, i := range string(text) {
|
|
cs = append(cs, Char(i))
|
|
}
|
|
|
|
*c = cs
|
|
|
|
return
|
|
}
|
|
|
|
// UnmarshalYAML conforms to (github.com/goccy/go-yaml)yaml.BytesUnmarshaler.
|
|
func (c *CharSet) UnmarshalYAML(text []byte) (err error) {
|
|
|
|
var cs []Char
|
|
|
|
if text == nil {
|
|
return
|
|
}
|
|
|
|
for _, i := range string(text) {
|
|
cs = append(cs, Char(i))
|
|
}
|
|
|
|
*c = cs
|
|
|
|
return
|
|
}
|