Initial commit
This commit is contained in:
commit
1f68250508
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
/target/
|
||||
**/*.rs.bk
|
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "urlify"
|
||||
version = "0.1.0"
|
||||
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "urlify"
|
||||
version = "0.1.0"
|
||||
authors = ["Kevin Cotugno <kevin@kevincotugno.com>"]
|
||||
|
||||
[dependencies]
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018, Kevin Cotugno
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
2
README.md
Normal file
2
README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# urlify
|
||||
A simple command line utility for encoding strings into RFC 3986 percent-encoding.
|
52
src/main.rs
Normal file
52
src/main.rs
Normal file
@ -0,0 +1,52 @@
|
||||
use std::env;
|
||||
|
||||
const LIMIT_8: u32 = 0x80;
|
||||
|
||||
static UNRESERVED: [u8; 66] = [
|
||||
0x2D, 0x2E, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
|
||||
0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,
|
||||
0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5F, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
|
||||
0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
|
||||
0x7A, 0x7E,
|
||||
];
|
||||
|
||||
fn main() {
|
||||
let content = parse_args();
|
||||
|
||||
let encoded = encode(&content);
|
||||
println!("{}", encoded)
|
||||
}
|
||||
|
||||
fn parse_args() -> String {
|
||||
let mut args: Vec<String> = env::args().collect();
|
||||
|
||||
if args.len() < 2 {
|
||||
panic!("Must supply an string to encode");
|
||||
}
|
||||
|
||||
args.remove(1)
|
||||
}
|
||||
|
||||
fn encode(value: &str) -> String {
|
||||
let mut encoded = String::new();
|
||||
|
||||
for s in value.chars() {
|
||||
let u = s as u32;
|
||||
|
||||
if u < LIMIT_8 {
|
||||
if UNRESERVED.contains(&(u as u8)) {
|
||||
encoded.push(s);
|
||||
} else {
|
||||
if u < 0x10 {
|
||||
encoded.push_str(format!("%0{:X}", u).as_str());
|
||||
} else {
|
||||
encoded.push_str(format!("%{:X}", u).as_str());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("Not valid ASCII");
|
||||
}
|
||||
}
|
||||
|
||||
encoded
|
||||
}
|
Loading…
Reference in New Issue
Block a user