Skip to content

Add Cargo feature query_encoding_2 to use encoding_rs crate #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,37 @@ jobs:
- cargo update
# getopts is only used in tests. Its versions 0.2.16+ don’t build on 1.17.0
- cargo update -p getopts --precise 0.2.15
# data-url uses pub(crate) which is unstable in 1.17
script: cargo test --all-features -p url -p idna -p percent-encoding -p url_serde
script:
# test building potentially conflicting features
- cargo build
- cargo build --features query_encoding
- cargo build --features query_encoding_2
# data-url uses pub(crate) which is unstable in 1.17
- cargo test --all-features -p url -p idna -p percent-encoding -p url_serde

- rust: stable
script: cargo test --all-features --all
script:
# test building potentially conflicting features
- cargo build
- cargo build --features query_encoding
- cargo build --features query_encoding_2
- cargo test --all-features --all

- rust: beta
script: cargo test --all-features --all
script:
# test building potentially conflicting features
- cargo build
- cargo build --features query_encoding
- cargo build --features query_encoding_2
- cargo test --all-features --all

- rust: nightly
script: cargo test --all-features --all
script:
# test building potentially conflicting features
- cargo build
- cargo build --features query_encoding
- cargo build --features query_encoding_2
- cargo test --all-features --all

- rust: nightly
env: TARGET=WASM32 # For job list UI
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ rustc-serialize = "0.3"
serde_json = ">=0.6.1, <0.9"

[features]
query_encoding_2 = ["encoding_rs"]
query_encoding = ["encoding"]
heap_size = ["heapsize"]

[dependencies]
encoding = {version = "0.2", optional = true}
encoding_rs = {version = "0.7", optional = true}
heapsize = {version = ">=0.4.1, <0.5", optional = true}
idna = { version = "0.1.0", path = "./idna" }
matches = "0.1"
Expand All @@ -49,4 +51,4 @@ rustc-serialize = {version = "0.3", optional = true}
serde = {version = ">=0.6.1, <0.9", optional = true}

[package.metadata.docs.rs]
features = ["query_encoding"]
features = ["query_encoding_2", "query_encoding"]
146 changes: 0 additions & 146 deletions src/encoding.rs

This file was deleted.

125 changes: 125 additions & 0 deletions src/encoding/encoding_rs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2013-2018 The rust-url developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


//! Implementation using [encoding_rs](https://github.com/hsivonen/encoding_rs).
//! Only built with feature flag `query_encoding_2`.

extern crate encoding_rs;

use encoding::EncodingOverride;
use encoding::utf8_helpers::{decode_utf8_lossy, encode_utf8};

use std::borrow::Cow;
use std::fmt::{self, Debug, Formatter};

use self::encoding_rs::Encoding;

pub struct EncodingOverrideRs {
/// `None` means UTF-8.
encoding: Option<&'static Encoding>
}

impl EncodingOverrideRs {
fn from_encoding(encoding: &'static Encoding) -> Self {
Self {
encoding: if encoding.name() == "UTF-8" { None } else { Some(encoding) }
}
}
}

impl EncodingOverride for EncodingOverrideRs {
#[inline]
fn utf8() -> Self {
Self { encoding: None }
}

fn lookup(label: &[u8]) -> Option<Self> {
// Don't use String::from_utf8_lossy since no encoding label contains U+FFFD
// https://encoding.spec.whatwg.org/#names-and-labels
Encoding::for_label(label)
.map(Self::from_encoding)
}

fn is_utf8(&self) -> bool {
self.encoding.is_none()
}

fn name(&self) -> &'static str {
match self.encoding {
Some(encoding) => encoding.name(),
None => encoding_rs::UTF_8.name(),
}
}

fn decode<'a>(&self, input: Cow<'a, [u8]>) -> Cow<'a, str> {
match self.encoding {
Some(encoding) => {
match input {
Cow::Borrowed(b) => {
let (cow, _) = encoding.decode_without_bom_handling(b);
cow
},
Cow::Owned(v) => {
{
let (cow, _) = encoding.decode_without_bom_handling(&v[..]);
match cow {
Cow::Owned(s) => {
// Free old heap buffer and return a new one.
return Cow::Owned(s);
},
Cow::Borrowed(_) => {},
}
}
// Reuse the old heap buffer.
Cow::Owned(unsafe { String::from_utf8_unchecked(v) })
},
}
},
None => decode_utf8_lossy(input),
}
}

fn encode<'a>(&self, input: Cow<'a, str>) -> Cow<'a, [u8]> {
match self.encoding {
Some(encoding) => {
match input {
Cow::Borrowed(s) => {
let (cow, _, _) = encoding.encode(s);
cow
},
Cow::Owned(s) => {
{
let (cow, _, _) = encoding.encode(&s[..]);
match cow {
Cow::Owned(v) => {
// Free old heap buffer and return a new one.
return Cow::Owned(v);
},
Cow::Borrowed(_) => {},
}
}
// Reuse the old heap buffer.
Cow::Owned(s.into_bytes())
},
}
},
None => encode_utf8(input),
}
}
}

impl Debug for EncodingOverrideRs {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "EncodingOverride {{ encoding: ")?;
match self.encoding {
Some(e) => write!(f, "{} }}", e.name()),
None => write!(f, "None }}")
}
}
}
53 changes: 53 additions & 0 deletions src/encoding/fallback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2013-2018 The rust-url developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


//! Implementation using UTF-8 only.
//! Used when building without any query encoding feature flags.

use std::borrow::Cow;

use encoding::EncodingOverride;
use encoding::utf8_helpers::{decode_utf8_lossy, encode_utf8};

#[derive(Copy, Clone, Debug)]
pub struct EncodingOverrideFallback;

impl EncodingOverrideFallback {
#[inline]
pub fn utf8() -> Self {
EncodingOverrideFallback
}
}

impl EncodingOverride for EncodingOverrideFallback {
fn utf8() -> Self {
Self {}
}

fn lookup(_label: &[u8]) -> Option<Self> {
// always return `None` which means UTF-8
None
}

fn is_utf8(&self) -> bool {
true
}

fn name(&self) -> &'static str {
"utf-8"
}

fn decode<'a>(&self, input: Cow<'a, [u8]>) -> Cow<'a, str> {
decode_utf8_lossy(input)
}

fn encode<'a>(&self, input: Cow<'a, str>) -> Cow<'a, [u8]> {
encode_utf8(input)
}
}
Loading