Rust/encodeURIComponent: Difference between revisions
From Legoktm
(Created page with "<source lang="rust"> fn encode_uri_component(component: &str) -> String { use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; const COMPONENT: &AsciiSet =...") |
No edit summary |
||
Line 1: | Line 1: | ||
< |
<syntaxhighlight lang="rust"> |
||
fn encode_uri_component(component: &str) -> String { |
fn encode_uri_component(component: &str) -> String { |
||
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; |
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; |
||
Line 32: | Line 32: | ||
utf8_percent_encode(component, COMPONENT).to_string() |
utf8_percent_encode(component, COMPONENT).to_string() |
||
} |
} |
||
</syntaxhighlight> |
|||
</source> |
Latest revision as of 05:45, 31 January 2025
fn encode_uri_component(component: &str) -> String {
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
const COMPONENT: &AsciiSet = &CONTROLS
// query set
.add(b' ')
.add(b'"')
//.add(b'#')
.add(b'<')
.add(b'>')
// path set
.add(b'?')
.add(b'`')
.add(b'{')
.add(b'}')
// userinfo set
.add(b'/')
.add(b':')
.add(b';')
.add(b'=')
.add(b'@')
.add(b'[')
.add(b'\\')
.add(b']')
.add(b'^')
// component set
.add(b'$')
.add(b'%')
.add(b'&')
.add(b'+')
.add(b',');
utf8_percent_encode(component, COMPONENT).to_string()
}