Decode Accept an encodedBytes byte array, and the encoding method
(data []byte, encoding Encoding)
| 36 | // Decode |
| 37 | // Accept an encodedBytes byte array, and the encoding method |
| 38 | func (d *Decoder) Decode(data []byte, encoding Encoding) (result string, err error) { |
| 39 | // we prepend one bit at the start to indicate whether strip last char |
| 40 | // so checking empty here will be convenient for decoding procedure |
| 41 | if data == nil { |
| 42 | return "", err |
| 43 | } |
| 44 | var chars []byte |
| 45 | switch encoding { |
| 46 | case LOWER_SPECIAL: |
| 47 | chars, err = d.decodeGeneric(data, encoding) |
| 48 | case LOWER_UPPER_DIGIT_SPECIAL: |
| 49 | chars, err = d.decodeGeneric(data, encoding) |
| 50 | case FIRST_TO_LOWER_SPECIAL: |
| 51 | chars, err = d.decodeGeneric(data, LOWER_SPECIAL) |
| 52 | if err == nil { |
| 53 | chars[0] = chars[0] - 'a' + 'A' |
| 54 | } |
| 55 | case ALL_TO_LOWER_SPECIAL: |
| 56 | chars, err = d.decodeRepAllToLowerSpecial(data, LOWER_SPECIAL) |
| 57 | case UTF_8: |
| 58 | chars = data |
| 59 | default: |
| 60 | err = fmt.Errorf("Unexpected encoding flag: %v\n", encoding) |
| 61 | } |
| 62 | if err != nil { |
| 63 | return "", err |
| 64 | } |
| 65 | return string(chars), err |
| 66 | } |
| 67 | |
| 68 | // DecodeGeneric |
| 69 | // algorithm is LowerSpecial or LowerUpperDigit |