-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathint_encoding.go
55 lines (43 loc) · 1.17 KB
/
int_encoding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package surl
import (
"encoding/base64"
"encoding/binary"
"strconv"
"github.com/itchyny/base58-go"
)
// intEncoding encodes integers into strings, and decodes strings into integers
type intEncoding interface {
Encode(int64) string
Decode(string) (int64, error)
}
type stdIntEncoding int
func (b stdIntEncoding) Encode(i int64) string {
return strconv.FormatInt(i, int(b))
}
func (b stdIntEncoding) Decode(s string) (int64, error) {
return strconv.ParseInt(s, int(b), 64)
}
type base58Encoding struct{}
func (base58Encoding) Encode(i int64) string {
return string(base58.FlickrEncoding.EncodeUint64(uint64(i)))
}
func (base58Encoding) Decode(s string) (int64, error) {
i, err := base58.FlickrEncoding.DecodeUint64([]byte(s))
if err != nil {
return 0, err
}
return int64(i), nil
}
type base64Encoding struct{}
func (base64Encoding) Encode(i int64) string {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(i))
return base64.RawURLEncoding.EncodeToString(b)
}
func (base64Encoding) Decode(s string) (int64, error) {
bytes, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
return 0, err
}
return int64(binary.BigEndian.Uint64(bytes)), nil
}