MCPcopy Create free account
hub / github.com/apache/fory / EncodeGeneric

Method EncodeGeneric

go/fory/meta/meta_string_encoder.go:127–157  ·  view source on GitHub ↗
(chars []byte, bitsPerChar int)

Source from the content-addressed store, hash-verified

125}
126
127func (e *Encoder) EncodeGeneric(chars []byte, bitsPerChar int) (result []byte, err error) {
128 totBits := len(chars)*bitsPerChar + 1
129 result = make([]byte, (totBits+7)/8)
130 currentBit := 1
131 for _, c := range chars {
132 var value byte
133 if bitsPerChar == 5 {
134 value, err = e.charToValueLowerSpecial(c)
135 } else if bitsPerChar == 6 {
136 value, err = e.charToValueLowerUpperDigitSpecial(c)
137 }
138 if err != nil {
139 return nil, err
140 }
141 // Use currentBit to figure out where the result should be filled
142 // abc encodedBytes as [00000] [000,01] [00010] [0, corresponding to three bytes, which are 0, 68, 0 (68 = 64 + 4)
143 // In order, put the highest bit first, then the lower
144 for i := bitsPerChar - 1; i >= 0; i-- {
145 if (value & (1 << i)) > 0 {
146 bytePos := currentBit / 8
147 bitPos := currentBit % 8
148 result[bytePos] |= 1 << (7 - bitPos)
149 }
150 currentBit++
151 }
152 }
153 if totBits+bitsPerChar <= len(result)*8 {
154 result[0] |= byte(0x80)
155 }
156 return
157}
158
159func (e *Encoder) ComputeEncoding(input string) Encoding {
160 allEncodings := []Encoding{LOWER_SPECIAL, LOWER_UPPER_DIGIT_SPECIAL, FIRST_TO_LOWER_SPECIAL, ALL_TO_LOWER_SPECIAL, UTF_8}

Callers 4

EncodeLowerSpecialMethod · 0.95

Tested by

no test coverage detected