6 个版本 (3 个稳定版)
1.0.2 | 2024年7月11日 |
---|---|
1.0.0 | 2024年7月10日 |
0.3.0 | 2024年7月10日 |
0.2.0 | 2024年7月9日 |
0.1.0 | 2024年7月9日 |
#257 在 文本处理
每月414次下载
19KB
213 行
url_encor 🌐
一个小巧轻量的🪶 rust 库,用于编码和解码 URL!
目标 ✅
url_encor 的目标是提供快速 🚀 的 URL 编码 和 解码
它通过使用 预处理 数据来实现这一点。
以下内容被 预处理:
- 十进制到十六进制的转换
- 十六进制到十进制的转换
- 决定字符是否需要编码
查看 此文件,看看什么被 预处理!
使用 ⚙️
编码 字符串非常简单
use url_encor::Encoder;
fn main() {
let string_to_encode = String::from("Hello, World!");
println!("{}", string_to_encode.url_encode());
//OUTPUT: Hello%2C%20World%21
assert_eq!(string_to_encode.url_encode(), "Hello%2C%20World%21")
}
解码 同样简单
use url_encor::Encoder;
fn main() {
let string_to_decode = String::from("Hello%2C%20World%21");
println!("{}", string_to_decode.url_decode());
//OUTPUT: Hello, World!
assert_eq!(string_to_decode.url_decode(), "Hello, World!")
}
实现自定义 编码 逻辑也很简单
use std::fmt::{Debug, Formatter};
use url_encor::{Encoder, encode};
fn main() {
let custom_type_to_encode = CharVector(vec!['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' ]);
println!("{:?}", custom_type_to_encode.url_encode());
//OUTPUT: ['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1']
assert_eq!(custom_type_to_encode.url_encode().0, vec!['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1'])
}
pub struct CharVector(Vec<char>);
impl Debug for CharVector {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Encoder<CharVector> for CharVector {
fn url_encode(&self) -> CharVector {
CharVector(encode(&self.0.iter().collect::<String>()).chars().collect())
}
fn url_decode(&self) -> CharVector {
todo!()
}
}
实现自定义 解码 逻辑
use std::fmt::{Debug, Formatter};
use url_encor::{Encoder, decode};
fn main() {
let custom_type_to_decode = CharVector(vec!['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1']);
println!("{:?}", custom_type_to_decode.url_decode());
//OUTPUT: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
assert_eq!(custom_type_to_decode.url_decode().0, vec!['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' ])
}
pub struct CharVector(Vec<char>);
impl Debug for CharVector {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Encoder<CharVector> for CharVector {
fn url_encode(&self) -> CharVector {
todo!()
}
fn url_decode(&self) -> CharVector {
CharVector(decode(&self.0.iter().collect::<String>()).chars().collect())
}
}
相关链接 🔗
问题 ⁉️
如果您遇到任何问题、错误或问题,请打开一个新的 问题