Просмотр исходного кода

Export RandomDigits. Remove NewRandom* functions.

Dmitry Chestnykh 14 лет назад
Родитель
Сommit
68d217e79d
4 измененных файлов с 8 добавлено и 26 удалено
  1. 0 9
      audio.go
  2. 4 4
      captcha.go
  3. 4 4
      cmd/main.go
  4. 0 9
      image.go

+ 0 - 9
audio.go

@@ -74,15 +74,6 @@ func NewAudio(digits []byte) *Audio {
 	return a
 }
 
-// NewRandomAudio generates a sequence of random digits with the given length,
-// and returns a new audio captcha with these digits, and the sequence of
-// digits itself.
-func NewRandomAudio(length int) (a *Audio, digits []byte) {
-	digits = randomDigits(length)
-	a = NewAudio(digits)
-	return
-}
-
 // WriteTo writes captcha audio in WAVE format into the given io.Writer, and
 // returns the number of bytes written and an error if any.
 func (a *Audio) WriteTo(w io.Writer) (n int64, err os.Error) {

+ 4 - 4
captcha.go

@@ -13,9 +13,9 @@ const StdLength = 6
 
 var globalStore = newStore()
 
-// randomDigits return a byte slice of the given length containing random
+// RandomDigits returns a byte slice of the given length containing random
 // digits in range 0-9.
-func randomDigits(length int) []byte {
+func RandomDigits(length int) []byte {
 	d := make([]byte, length)
 	if _, err := io.ReadFull(rand.Reader, d); err != nil {
 		panic(err)
@@ -30,7 +30,7 @@ func randomDigits(length int) []byte {
 // storage, and returns its id.
 func New(length int) (id string) {
 	id = uniuri.New()
-	globalStore.saveCaptcha(id, randomDigits(length))
+	globalStore.saveCaptcha(id, RandomDigits(length))
 	return
 }
 
@@ -45,7 +45,7 @@ func Reload(id string) bool {
 	if old == nil {
 		return false
 	}
-	globalStore.saveCaptcha(id, randomDigits(len(old)))
+	globalStore.saveCaptcha(id, RandomDigits(len(old)))
 	return true
 }
 

+ 4 - 4
cmd/main.go

@@ -35,16 +35,16 @@ func main() {
 	}
 	defer f.Close()
 	var w io.WriterTo
-	var ns []byte
+	d := captcha.RandomDigits(*flagLen)
 	switch {
 	case *flagAudio:
-		w, ns = captcha.NewRandomAudio(*flagLen)
+		w = captcha.NewAudio(d)
 	case *flagImage:
-		w, ns = captcha.NewRandomImage(*flagLen, *flagImgW, *flagImgH)
+		w = captcha.NewImage(d, *flagImgW, *flagImgH)
 	}
 	_, err = w.WriteTo(f)
 	if err != nil {
 		log.Fatalf("%s", err)
 	}
-	fmt.Println(ns)
+	fmt.Println(d)
 }

+ 0 - 9
image.go

@@ -59,15 +59,6 @@ func NewImage(digits []byte, width, height int) *Image {
 	return img
 }
 
-// NewRandomImage generates a sequence of random digits with the given length,
-// and returns a new captcha image of the given width and height with generated
-// digits printed on it, and the sequence of digits itself.
-func NewRandomImage(length, width, height int) (img *Image, digits []byte) {
-	digits = randomDigits(length)
-	img = NewImage(digits, width, height)
-	return
-}
-
 // WriteTo writes captcha image in PNG format into the given writer.
 //
 // Bug: while Image conforms to io.WriterTo interface, this function returns 0