diff --git a/src/entity.rs b/src/entity.rs index b111e05..d5efe07 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -1,3 +1,28 @@ +/* +The below piece of code was taken from: +https://github.com/veddan/rust-htmlescape/blob/master/src/entities.rs + +Copyright 2016 Viktor Dahl + +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. +*/ + pub static ENTITIES: &'static [(&'static str, char)] = &[ ("AElig", '\u{00C6}'), ("AMP", '\u{000026}'), diff --git a/src/lib.rs b/src/lib.rs index ef1e4b5..a8fa021 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,15 +3,11 @@ mod entity; const LBR: &str = "\r\n"; -// stolen from https://github.com/veddan/rust-htmlescape/blob/master/src/decode.rs fn decode_named_entity(entity: &str) -> Option { - match entity::ENTITIES.binary_search_by(|&(ent, _)| ent.cmp(entity)) { - Err(..) => None, - Ok(idx) => { - let (_, c) = entity::ENTITIES[idx]; - Some(c) - } - } + entity::ENTITIES + .binary_search_by_key(&entity, |t| t.0) + .map(|idx| entity::ENTITIES[idx].1) + .ok() } const BAD_TAGS: [&str; 4] = ["head", "script", "style", "a"];