Skip to content

Commit

Permalink
move percent_encoding to its own crate
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar authored and SimonSapin committed Jun 13, 2017
1 parent 52a45a1 commit 9d36a39
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ travis-ci = { repository = "servo/rust-url" }
appveyor = { repository = "servo/rust-url" }

[workspace]
members = [".", "idna", "url_serde"]
members = [".", "idna", "percent_encoding", "url_serde"]

[[test]]
name = "unit"
Expand All @@ -44,5 +44,6 @@ encoding = {version = "0.2", optional = true}
heapsize = {version = ">=0.1.1, <0.5", optional = true}
idna = { version = "0.1.0", path = "./idna" }
matches = "0.1"
percent_encoding = { version = "1.0.0", path = "./percent_encoding" }
rustc-serialize = {version = "0.3", optional = true}
serde = {version = ">=0.6.1, <0.9", optional = true}
17 changes: 17 additions & 0 deletions percent_encoding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "percent_encoding"
version = "1.0.0"
authors = ["The rust-url developers"]
description = "Percent encoding and decoding"
repository = "https://github.com/servo/rust-url/"
license = "MIT/Apache-2.0"

[lib]
doctest = false
test = false

[dev-dependencies]
rustc-test = "0.1"
rustc-serialize = "0.3"

[dependencies]
28 changes: 23 additions & 5 deletions src/percent_encoding.rs → percent_encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
//! assert_eq!(utf8_percent_encode("foo bar?", DEFAULT_ENCODE_SET).to_string(), "foo%20bar%3F");
//! ```
use encoding;
use std::ascii::AsciiExt;
use std::borrow::Cow;
use std::fmt;
Expand Down Expand Up @@ -70,8 +69,8 @@ pub trait EncodeSet: Clone {
/// =======
///
/// ```rust
/// #[macro_use] extern crate url;
/// use url::percent_encoding::{utf8_percent_encode, SIMPLE_ENCODE_SET};
/// #[macro_use] extern crate percent_encoding;
/// use percent_encoding::{utf8_percent_encode, SIMPLE_ENCODE_SET};
/// define_encode_set! {
/// /// This encode set is used in the URL parser for query strings.
/// pub QUERY_ENCODE_SET = [SIMPLE_ENCODE_SET] | {' ', '"', '#', '<', '>'}
Expand All @@ -88,7 +87,7 @@ macro_rules! define_encode_set {
#[allow(non_camel_case_types)]
pub struct $name;

impl $crate::percent_encoding::EncodeSet for $name {
impl $crate::EncodeSet for $name {
#[inline]
fn contains(&self, byte: u8) -> bool {
match byte as char {
Expand Down Expand Up @@ -419,6 +418,25 @@ impl<'a> PercentDecode<'a> {
/// Invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD,
/// the replacement character.
pub fn decode_utf8_lossy(self) -> Cow<'a, str> {
encoding::decode_utf8_lossy(self.clone().into())
decode_utf8_lossy(self.clone().into())
}
}

fn decode_utf8_lossy(input: Cow<[u8]>) -> Cow<str> {
match input {
Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
Cow::Owned(bytes) => {
let raw_utf8: *const [u8];
match String::from_utf8_lossy(&bytes) {
Cow::Borrowed(utf8) => raw_utf8 = utf8.as_bytes(),
Cow::Owned(s) => return s.into(),
}
// from_utf8_lossy returned a borrow of `bytes` unchanged.
debug_assert!(raw_utf8 == &*bytes as *const [u8]);
// Reuse the existing `Vec` allocation.
unsafe { String::from_utf8_unchecked(bytes) }.into()
}
}
}


47 changes: 46 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ assert_eq!(css_url.as_str(), "http://servo.github.io/rust-url/main.css");
#[cfg(feature="heapsize")] #[macro_use] extern crate heapsize;

pub extern crate idna;
pub extern crate percent_encoding;

use encoding::EncodingOverride;
#[cfg(feature = "heapsize")] use heapsize::HeapSizeOf;
Expand Down Expand Up @@ -145,7 +146,6 @@ mod parser;
mod slicing;

pub mod form_urlencoded;
pub mod percent_encoding;
pub mod quirks;

/// A parsed URL record.
Expand Down Expand Up @@ -2103,3 +2103,48 @@ impl<'a> Drop for UrlQuery<'a> {
self.url.restore_already_parsed_fragment(self.fragment.take())
}
}


/// Define a new struct
/// that implements the [`EncodeSet`](percent_encoding/trait.EncodeSet.html) trait,
/// for use in [`percent_decode()`](percent_encoding/fn.percent_encode.html)
/// and related functions.
///
/// Parameters are characters to include in the set in addition to those of the base set.
/// See [encode sets specification](http://url.spec.whatwg.org/#simple-encode-set).
///
/// Example
/// =======
///
/// ```rust
/// #[macro_use] extern crate url;
/// use url::percent_encoding::{utf8_percent_encode, SIMPLE_ENCODE_SET};
/// define_encode_set! {
/// /// This encode set is used in the URL parser for query strings.
/// pub QUERY_ENCODE_SET = [SIMPLE_ENCODE_SET] | {' ', '"', '#', '<', '>'}
/// }
/// # fn main() {
/// assert_eq!(utf8_percent_encode("foo bar", QUERY_ENCODE_SET).collect::<String>(), "foo%20bar");
/// # }
/// ```
#[macro_export]
macro_rules! define_encode_set {
($(#[$attr: meta])* pub $name: ident = [$base_set: expr] | {$($ch: pat),*}) => {
$(#[$attr])*
#[derive(Copy, Clone)]
#[allow(non_camel_case_types)]
pub struct $name;

impl $crate::percent_encoding::EncodeSet for $name {
#[inline]
fn contains(&self, byte: u8) -> bool {
match byte as char {
$(
$ch => true,
)*
_ => $base_set.contains(byte)
}
}
}
}
}

0 comments on commit 9d36a39

Please sign in to comment.