Skip to main content

Module pct

Module pct 

Source
Expand description

The percent-encoding primitive: validate, decode into a caller buffer, and encode with a named set and hex case.

These are the operations parse-net’s own grammars need (RFC 8187 values, RFC 9651 Display Strings, RFC 7578 file names). Everything URL-specific (component encode sets, + as space, normalization) belongs to url-lib, which builds its sets with EncodeSet::add and EncodeSet::remove.

§Examples

use parse_net::{self as pn, pct::{self, EncodeSet, HexCase}, class::Class};

let mut buf = [0u8; 16];
assert_eq!(pct::decode_utf8_into(b"caf%C3%A9", &mut buf)?, "caf\u{e9}");
assert_eq!(pct::validate(b"%4").unwrap_err().kind(), pn::ErrorKind::InvalidPercentEncoding);
assert!(pct::validate_with(b"/a/b?c=%20", Class::URI_TARGET).is_ok());

let mut out = pn::SliceSink::new(&mut buf);
pct::encode(b"a b/c", &EncodeSet::ATTR_CHAR_COMPLEMENT, HexCase::Upper, &mut out).expect("room");
assert_eq!(out.written(), b"a%20b%2Fc");

Structs§

EncodeSet
A set of bytes that must be percent-encoded: a 256-bit bitmap.

Enums§

HexCase
The hex case encode writes.

Functions§

decode_into
Decodes %XX triples (either case) into out, copying every other byte, and returns the written part.
decode_lower_only_into
As decode_into, refusing uppercase hex (RFC 9651 section 4.2.10).
decode_utf8_into
As decode_into, and the decoded bytes must be UTF-8 (overlong forms and surrogates refused).
decoded_len
Validates input and returns its decoded length.
encode
Writes input with every byte in set, and every byte above 0x7F, as %XX.
encoded_len
The length of input once encoded with set.
validate
Checks that every % is followed by two hex digits (either case).
validate_with
As validate, and every byte other than a %XX triple is in allowed.