MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / percent_encode_path_segment

Function percent_encode_path_segment

crates/openshell-core/src/secrets.rs:584–618  ·  view source on GitHub ↗

Percent-encode a string for safe use in URL path segments. RFC 3986 §3.3: pchar = unreserved / pct-encoded / sub-delims / ":" / "@" sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" Must encode: `/`, `?`, `#`, space, and other non-pchar characters.

(input: &str)

Source from the content-addressed store, hash-verified

582///
583/// Must encode: `/`, `?`, `#`, space, and other non-pchar characters.
584fn percent_encode_path_segment(input: &str) -> String {
585 let mut encoded = String::with_capacity(input.len());
586 for byte in input.bytes() {
587 match byte {
588 // unreserved + sub-delims + ":" + "@"
589 b'A'..=b'Z'
590 | b'a'..=b'z'
591 | b'0'..=b'9'
592 | b'-'
593 | b'.'
594 | b'_'
595 | b'~'
596 | b'!'
597 | b'$'
598 | b'&'
599 | b'\''
600 | b'('
601 | b')'
602 | b'*'
603 | b'+'
604 | b','
605 | b';'
606 | b'='
607 | b':'
608 | b'@' => {
609 encoded.push(byte as char);
610 }
611 _ => {
612 use fmt::Write;
613 let _ = write!(encoded, "%{byte:02X}");
614 }
615 }
616 }
617 encoded
618}
619
620/// Percent-decode a URL-encoded string.
621fn percent_decode(input: &str) -> String {

Callers 1

rewrite_uri_pathFunction · 0.85

Calls 2

lenMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected