reduce default short values

This commit is contained in:
Gustavo Maronato 2023-08-19 12:04:28 -03:00
parent 34853c0dbd
commit e0e1a775bc
Signed by: maronato
SSH Key Fingerprint: SHA256:2Gw7kwMz/As+2UkR1qQ/qYYhn+WNh3FGv6ozhoRrLcs
3 changed files with 12 additions and 9 deletions

View File

@ -15,11 +15,11 @@ import (
const (
// DefaultShortLength is the default length of the short URL.
DefaultShortLength = 8
DefaultShortLength = 5
// MinShortLength is the minimum length of the short URL.
MinShortLength = 4
// MaxShortLength is the maximum length of the short URL.
MaxShortLength = 32
MaxShortLength = 20
)
type ShortService struct {

View File

@ -1,3 +0,0 @@
package shortutil
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" // 64 possibilities

View File

@ -3,16 +3,22 @@ package shortutil
import (
"math/rand"
"strconv"
"strings"
"time"
)
const (
letterIdxBits = 7 // 7 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
// These are the characters that can be used in shorts
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789" // 36 possibilities
const alphabetLen = int64(len(alphabet))
var (
letterIdxBits = len(strconv.FormatInt(alphabetLen, 2)) // number of bits to represent alphabetLen
letterIdxMask = int64(1<<letterIdxBits - 1) // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
// src is the random source for generating shorts
var src = rand.NewSource(time.Now().UnixNano())
func GenerateRandomShort(n int) string {