EncodeWithEncoding Encodes the input string to MetaString using specified encoding.
(input string, encoding Encoding)
| 51 | |
| 52 | // EncodeWithEncoding Encodes the input string to MetaString using specified encoding. |
| 53 | func (e *Encoder) EncodeWithEncoding(input string, encoding Encoding) (MetaString, error) { |
| 54 | if encoding != UTF_8 && !isASCII(input) { |
| 55 | return MetaString{}, errors.New("non-ASCII characters in meta string are not allowed") |
| 56 | } |
| 57 | if len(input) > 32767 { |
| 58 | return MetaString{}, errors.New("long meta string than 32767 is not allowed") |
| 59 | } |
| 60 | if len(input) == 0 { |
| 61 | // we prepend one bit at the start to indicate whether strip last char |
| 62 | // so checking empty here will be convenient for encoding procedure |
| 63 | return MetaString{ |
| 64 | inputString: input, |
| 65 | encoding: encoding, |
| 66 | specialChar1: e.specialChar1, |
| 67 | specialChar2: e.specialChar2, |
| 68 | encodedBytes: nil, |
| 69 | }, nil |
| 70 | } |
| 71 | // execute encoding algorithm according to the encoding mode |
| 72 | var encodedBytes []byte |
| 73 | var err error |
| 74 | switch encoding { |
| 75 | case LOWER_SPECIAL: |
| 76 | encodedBytes, err = e.EncodeLowerSpecial(input) |
| 77 | case LOWER_UPPER_DIGIT_SPECIAL: |
| 78 | encodedBytes, err = e.EncodeLowerUpperDigitSpecial(input) |
| 79 | case FIRST_TO_LOWER_SPECIAL: |
| 80 | encodedBytes, err = e.EncodeFirstToLowerSpecial(input) |
| 81 | case ALL_TO_LOWER_SPECIAL: |
| 82 | encodedBytes, err = e.EncodeAllToLowerSpecial(input) |
| 83 | default: |
| 84 | // UTF-8 Encoding, stay the same |
| 85 | encodedBytes = []byte(input) |
| 86 | } |
| 87 | return MetaString{ |
| 88 | inputString: input, |
| 89 | encoding: encoding, |
| 90 | specialChar1: e.specialChar1, |
| 91 | specialChar2: e.specialChar2, |
| 92 | encodedBytes: encodedBytes, |
| 93 | }, err |
| 94 | } |
| 95 | |
| 96 | func (e *Encoder) EncodeLowerSpecial(input string) ([]byte, error) { |
| 97 | return e.EncodeGeneric([]byte(input), 5) |