-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconciseStringCompress.go
More file actions
159 lines (124 loc) · 3.12 KB
/
conciseStringCompress.go
File metadata and controls
159 lines (124 loc) · 3.12 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package conciseStringCompress
import (
"errors"
"fmt"
"math"
"sync"
bitutil "github.com/valorwing/ConciseStringCompress/internal/bitUtil"
"github.com/valorwing/ConciseStringCompress/internal/constants"
)
type Compressor struct {
alphabet []rune
alphabetLock sync.RWMutex
alphabetMap map[rune]uint8
}
func NewCustomAlphabetCompressor(alphabet []rune) *Compressor {
retVal := &Compressor{
alphabet: nil,
alphabetLock: sync.RWMutex{},
alphabetMap: nil,
}
retVal.SetAlphabet(alphabet)
return retVal
}
func NewDefaultCompressor() *Compressor {
return NewCustomAlphabetCompressor(constants.DefaultAlphabet)
}
func (c *Compressor) SetAlphabet(alphabet []rune) error {
if len(alphabet) != 64 {
return errors.New(constants.ErrInvalidAlphabetLength + fmt.Sprint(len(alphabet)))
}
newAlphabetMap := map[rune]uint8{}
for i := range alphabet {
newAlphabetMap[alphabet[i]] = uint8(i)
}
c.alphabetLock.Lock()
c.alphabet = alphabet
c.alphabetMap = newAlphabetMap
c.alphabetLock.Unlock()
return nil
}
func (c *Compressor) GetAlphabet() []rune {
c.alphabetLock.RLock()
retVal := c.alphabet
c.alphabetLock.RUnlock()
return retVal
}
func (c *Compressor) isInAlphabet(r rune) bool {
c.alphabetLock.RLock()
_, ok := c.alphabetMap[r]
c.alphabetLock.RUnlock()
return ok
}
func (c *Compressor) getIndex(r rune) uint8 {
v, ok := c.alphabetMap[r]
if !ok {
panic("not in alphabet")
}
return v
}
func (c *Compressor) CompressString(input string) ([]byte, error) {
if len(input) == 0 {
return []byte{}, nil
}
var retVal []byte = make([]uint8, ((len([]byte(input))*6)/8)+1)
retValPtr := &retVal
byteOffset := uint64(0)
bitOffset := uint8(0)
writedRunes := 0
c.alphabetLock.RLock()
for _, r := range input {
if !c.isInAlphabet(r) {
return nil, errors.New(string(r) + constants.ErrInvalidString)
}
bitutil.WriteBits(byte(c.getIndex(r)), 6, retValPtr, byteOffset, bitOffset)
writedRunes++
bitOffset += 6
if bitOffset >= 8 {
bitOffset -= 8
byteOffset += 1
}
}
c.alphabetLock.RUnlock()
outLenBits := writedRunes * 6
outLen := int(math.Floor(float64(outLenBits) / float64(8)))
if outLenBits%8 != 0 {
outLen += 1
}
retVal = retVal[:outLen]
if retVal[len(retVal)-1]&1 == 0 || retVal[len(retVal)-1] == constants.NetworkFixByte {
retVal = append(retVal, constants.NetworkFixByte)
}
return retVal, nil
}
func (c *Compressor) DecompressString(input []byte) string {
if len(input) == 0 {
return ""
}
retVal := make([]rune, 0, len(input)*2)
currentReadBitsOffset := uint8(0)
currentReadByteOffset := uint64(0)
if input[len(input)-1] == constants.NetworkFixByte {
input = input[:len(input)-1]
}
inputPtr := &input
c.alphabetLock.RLock()
for {
bitByte, readBitsOk := bitutil.ReadBits(inputPtr, 6, currentReadByteOffset, currentReadBitsOffset)
if readBitsOk {
retVal = append(retVal, c.alphabet[bitByte])
} else {
break
}
currentReadBitsOffset += 6
if currentReadBitsOffset >= 8 {
currentReadBitsOffset -= 8
currentReadByteOffset += 1
if currentReadByteOffset == uint64(len(input)) {
break
}
}
}
c.alphabetLock.RUnlock()
return string(retVal)
}