-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
35 lines (29 loc) · 987 Bytes
/
sequence.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
// Copyright 2018 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package hprose_go_nats
import "strings"
type Sequence uint64
var hexTable [16]byte = [16]byte{0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46}
var byteTable map[byte]byte = map[byte]byte{0x30: 0, 0x31: 1, 0x32: 2, 0x33: 3, 0x34: 4, 0x35: 5, 0x36: 6, 0x37: 7, 0x38: 8, 0x39: 9, 0x41: 10, 0x42: 11, 0x43: 12, 0x44: 13, 0x45: 14, 0x46: 15}
func NewSequence(s string) Sequence {
pos := strings.LastIndexByte(s, byte('.'))
seq := uint64(0)
for _, v := range s[pos+1:] {
seq <<= 4
seq += uint64(byteTable[byte(v)])
}
return Sequence(seq)
}
func (s Sequence) String() string {
if 0 == s {
return ".0"
}
var buf [20]byte
var pos = 19
for ; s > 0; s >>= 4 {
buf[pos] = hexTable[s&15]
pos--
}
buf[pos] = byte('.')
return string(buf[pos:])
}