nanohtml2text

Lightweight html to text converter in Rust
git clone git://git.alexwennerberg.com/nanohtml2text
Log | Files | Refs | README | LICENSE

commit 6e3fd37e736d69aa8afbd657b5a8e5e12527a934
parent f5f435a8c5d404656a71ca3328e771cdb68cbb80
Author: Johann150 <johann.galle@protonmail.com>
Date:   Wed, 12 Jan 2022 22:19:57 +0100

clean up decode_named_entity

Update the code style to current Rust.
Add the proper license notice to the entity.rs file.

Diffstat:
Msrc/entity.rs | 25+++++++++++++++++++++++++
Msrc/lib.rs | 12++++--------
2 files changed, 29 insertions(+), 8 deletions(-)

diff --git 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 <pazaconyoman@gmail.com> + +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 @@ -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<char> { - 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"];