strrand

package module
v0.0.0-...-5195340 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2018 License: MIT Imports: 6 Imported by: 12

README

strrand

Build Status Coverage Status MIT License GoDoc

Description

generate random strings from a pattern like regexp

It is golang porting of perl's String::Random (ported only randregex interface)

Synopsis

str, err := strrand.RandomString(`[1-9]{1,3}.?`)
fmt.Println(str) // 13h

OO interface

sr := strrand.New()
str, err := sr.Generate(`[あ-お]{15,18}`)

Factory Method

g, err := strrand.New().CreateGenerator(`\d{2,3}-\d{3,4}-\d{3,4}`)
str1 := g.Generate() // 11-2258-333
str2 := g.Generate() // 093-0033-3349

Supported Patterns

Please note that the pattern arguments are not real regular expressions. Only a small subset of regular expression syntax is actually supported. So far, the following regular expression elements are supported:

\w    Alphanumeric + "_".
\d    Digits.
\W    Printable characters other than those in \w.
\D    Printable characters other than those in \d.
\s    Whitespaces (whitespace and tab character)
\S    Ascii characters without whitespaces
.     Printable characters. (ascii only)
[]    Character classes. (Supported multibyte characters)
{}    Repetition.
*     Same as {0,}.
?     Same as {0,1}.
+     Same as {1,}.

Disclaimer

Seeding is naive and not secure. So, don't use this for creating password and so on.

Author

Songmu

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomString

func RandomString(pattern string) (string, error)

RandomString returns random string from pattern

Types

type Generator

type Generator interface {
	Generate() string
}

Generator generating random strings

type Strrand

type Strrand struct {
	Max uint
}

Strrand factory object of random string generator

func New

func New() *Strrand

New returns Strrand struct

func (*Strrand) CreateGenerator

func (sr *Strrand) CreateGenerator(pattern string) (Generator, error)

CreateGenerator returns random string generator

func (*Strrand) Generate

func (sr *Strrand) Generate(pattern string) (string, error)

Generate generates random string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL