123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package lib
- import (
- "math"
- "strings"
- "git.sfnt.net/sfnt/cnlink/conf"
- )
- // Int2String converts an unsigned 64bit integer to a string.
- func Int2String(seq uint64) (shortURL string) {
- charSeq := []rune{}
- if seq != 0 {
- for seq != 0 {
- mod := seq % conf.Conf.Common.BaseStringLength
- div := seq / conf.Conf.Common.BaseStringLength
- charSeq = append(charSeq, rune(conf.Conf.Common.BaseString[mod]))
- seq = div
- }
- } else {
- charSeq = append(charSeq, rune(conf.Conf.Common.BaseString[seq]))
- }
- tmpShortURL := string(charSeq)
- shortURL = reverse(tmpShortURL)
- return
- }
- // String2Int converts a short URL string to an unsigned 64bit integer.
- func String2Int(shortURL string) (seq uint64) {
- shortURL = reverse(shortURL)
- for index, char := range shortURL {
- base := uint64(math.Pow(float64(conf.Conf.Common.BaseStringLength), float64(index)))
- seq += uint64(strings.Index(conf.Conf.Common.BaseString, string(char))) * base
- }
- return
- }
- func reverse(s string) string {
- r := []rune(s)
- for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
- r[i], r[j] = r[j], r[i]
- }
- return string(r)
- }
|