Expand description
§Generalized variable-length integers
This crate implements the “generalized variable-length integers” described in RFC 3492 section 3.3. This implementation is generic, so that it may be used outside of just RFC 3492 implementations.
This crate implements only little-endian encoding and decoding, as they are the only ones described in the RFC and associated litterature.
§Example
let parameters = gvli::Parameters::from_parts(&gvli::BASE_OCTAL, &[2, 3, 5]).unwrap();
let encoded = gvli::encode(&[145, 62], ¶meters);
assert_eq!(encoded, "734251");
let decoded = gvli::decode(&encoded, ¶meters).unwrap();
assert_eq!(decoded, [145, 62]);§Usage
§Parameters
GVLI encoding and decoding requires having:
- a base: a finite sequence of digits; a mapping of
chars to numbers; - a thresholds sequence: an infinite sequence of numbers which allows for discriminating terminating digits from non-terminating ones.
For one application, both of these settings are going to be fixed, so they
are grouped into one struct: Parameters.
A convenience constant is provided for the specific case of Punycode:
PUNYCODE.
§Encoding
Encoding takes a sequence of numbers and turns them into a sequence of
chars, the digits. Four encoding functions are provided:
encode_one()encodes a single number, providing a new String,encode()encodes a sequence of numbers, providing a new String,- their variants
encode_one_noalloc()andencode_noalloc()which extend and existing String.
Encoding never fails: all possible violations are checked by
Parameters::from_parts().
§Decoding
Decoding takes a sequence of chars, the digits, and turns them into a
sequence of numbers. Because it is impossible to know in advance how many
numbers are encoded, and if they are encoded correctly, only one function is
provided: decode().
Decoding may fail:
- if a digit that is not part of the base is encountered,
- if the sequence of
chars ends on a non-terminating digit.
Structs§
- Parameters
- The parameters of the GVLI encoding/decoding process: the base and thresholds.
Enums§
- GVLI
Error - Error type returned by the GVLI APIs.
Constants§
- BASE_
DECIMAL - Convenience
basearray for a base defining its digits as0to9. - BASE_
HEX - Convenience
basearray for a base defining its digits as0to9, thenAtoF. - BASE_
OCTAL - Convenience
basearray for a base defining its digits as0to7. - BASE_
PUNYCODE - Convenience
basearray for Punycode encoding:atoz, then0to9. - PUNYCODE
- Convenience
Parametersfor Punycode encoding. SeeBASE_PUNYCODEandTHRESHOLDS_PUNYCODE. - THRESHOLDS_
PUNYCODE - Convenience
thresholdsarray for Punicode encoding: 1 twice, then 26 repeated infinitely.
Functions§
- decode
- Decode a sequence of GVLI.
- encode
- Encode a sequence of numbers, creating a new String.
- encode_
noalloc - Encode a sequence of numbers, appending to an existing String.
- encode_
one - Encode a single number, creating a new String.
- encode_
one_ noalloc - Encode a single number, appending to an existing String.