You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
943 B
59 lines
943 B
package qurl
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type QURL struct {
|
|
ID uint64
|
|
URL string
|
|
Created time.Time
|
|
IP []byte
|
|
Browser string
|
|
}
|
|
|
|
const alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
const alphalen = uint64(len(alpha))
|
|
|
|
func ToString(v uint64) string {
|
|
var r strings.Builder
|
|
var i uint64
|
|
for i = v; i >= alphalen; i = (i / alphalen) - 1 {
|
|
r.WriteByte(alpha[i%alphalen])
|
|
}
|
|
r.WriteByte(alpha[i])
|
|
return r.String()
|
|
}
|
|
|
|
func FromString(s string) (uint64, error) {
|
|
var r uint64
|
|
vlen := uint64(len(s))
|
|
|
|
for i := uint64(0); i < vlen; i++ {
|
|
x := strings.IndexByte(alpha, s[i])
|
|
if x == -1 {
|
|
return 0, fmt.Errorf("Invalid character [%s]", s[i])
|
|
}
|
|
|
|
if i == 0 {
|
|
r += uint64(x)
|
|
} else {
|
|
r += (uint64(x+1) * pow(alphalen, i))
|
|
}
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func pow(a, b uint64) uint64 {
|
|
p := uint64(1)
|
|
for b > 0 {
|
|
if b&1 != 0 {
|
|
p *= a
|
|
}
|
|
b >>= 1
|
|
a *= a
|
|
}
|
|
return p
|
|
}
|