LiangliangSui commented on code in PR #1566: URL: https://github.com/apache/incubator-fury/pull/1566#discussion_r1578816915
########## go/fury/meta/meta_string_decoder.go: ########## @@ -0,0 +1,143 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package meta + +import ( + "fmt" + "strings" +) + +type Decoder struct { + specialChar1 byte + specialChar2 byte +} + +func NewDecoder(specialCh1 byte, specialCh2 byte) *Decoder { + return &Decoder{ + specialChar1: specialCh1, + specialChar2: specialCh2, + } +} + +// Decode +// Accept an encoded byte array, and the encoding method +// We also need to accept numBits, because we don't know how many characters are in the byte array, +// and numBits are the actual valid bits +func (d *Decoder) Decode(data []byte, encoding Encoding, numBits int) string { + switch encoding { + case LOWER_SPECIAL: + return d.decodeGeneric(data, encoding, numBits) + case LOWER_UPPER_DIGIT_SPECIAL: + return d.decodeGeneric(data, encoding, numBits) + case FIRST_TO_LOWER_SPECIAL: + return strings.ToTitle(d.decodeGeneric(data, LOWER_SPECIAL, numBits)) + case ALL_TO_LOWER_SPECIAL: + return d.decodeRepAllToLowerSpecial(data, LOWER_SPECIAL, numBits) + case UTF_8: + return string(data) + default: + panic("Unexpected encoding flag: {encoding}") + } +} + +// DecodeGeneric +// algorithm is LowerSpecial or LowerUpperDigit +func (d *Decoder) decodeGeneric(data []byte, algorithm Encoding, numBits int) string { + bitsPerChar := 5 + if algorithm == LOWER_UPPER_DIGIT_SPECIAL { + bitsPerChar = 6 + } + // Retrieve 5 bits every iteration from data, convert them to characters, and save them to chars + // "abc" encoded as [00000] [000,01] [00010] [0, corresponding to three bytes, which are 0, 68, 0 (68 = 64 + 4) + // In order, take the highest digit first, then the lower + chars := make([]byte, 0) + bitPos, bitCount := 7, 0 + for bitCount+bitsPerChar <= numBits { + var val byte = 0 + for i := 0; i < bitsPerChar; i++ { + val <<= 1 + if data[bitCount/8]&(1<<bitPos) > 0 { + val += 1 + } + bitPos = (bitPos - 1 + 8) % 8 + bitCount++ + } + chars = append(chars, d.decodeChar(val, algorithm)) Review Comment: ```suggestion var val byte = 0 for i := 0; i < bitsPerChar; i++ { if data[bitCount/8]&(1<<bitPos) > 0 { val |= 1 << (bitsPerChar - i) } bitPos = (bitPos - 1 + 8) % 8 bitCount++ } chars = append(chars, d.decodeChar(val, algorithm)) ``` In this way, when `data[bitCount/8]&(1<<bitPos) > 0` is false, we can save a shift operation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
