feat: add encode_percent function for percent-encoding strings

This commit is contained in:
Bakyt Niiazaliev 2025-06-13 17:30:48 +07:00
parent 68b6d569dc
commit 18047ea4b8
2 changed files with 22 additions and 1 deletions

View File

@ -3,5 +3,8 @@ use pyo3::prelude::*;
#[pymodule]
mod _httpx {
#[pymodule_export]
use crate::{urlparse::normalize_path, urls::QueryParams};
use crate::{
urlparse::{encode_percent, normalize_path},
urls::QueryParams,
};
}

View File

@ -23,3 +23,21 @@ pub fn normalize_path(path: &str) -> String {
normalized_components.join("/")
}
const UNRESERVED_CHARS: &[u8] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
#[pyfunction]
pub fn encode_percent(string: &str, safe: &str) -> String {
let safe = safe.as_bytes();
string
.bytes()
.map(|b| {
if UNRESERVED_CHARS.contains(&b) || safe.contains(&b) {
(b as char).to_string()
} else {
format!("%{:02X}", b)
}
})
.collect::<String>()
}