rfc2047-0.1.1/.gitignore010064400017500001750000000001651354540032200130110ustar0000000000000000/target **/*.rs.bk #Added by cargo # #already existing elements are commented out #/target #**/*.rs.bk Cargo.lock rfc2047-0.1.1/.travis.yml010064400017500001750000000000651354540477000131440ustar0000000000000000language: rust rust: - stable - beta - nightly rfc2047-0.1.1/Cargo.toml.orig010064400017500001750000000005761354541021200137150ustar0000000000000000[package] name = "rfc2047" version = "0.1.1" authors = ["Vincent Breitmoser ", "Nora Widdecke "] edition = "2018" license = "MIT" description = "A simple encoder for RFC2047 encoded words" repository = "https://github.com/Valodim/rust-rfc2047" keywords = ["rfc2047", "mail", "encoder"] readme = "readme.md" [dependencies] # no deps! woop woop rfc2047-0.1.1/Cargo.toml0000644000000015660000000000000101670ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "rfc2047" version = "0.1.1" authors = ["Vincent Breitmoser ", "Nora Widdecke "] description = "A simple encoder for RFC2047 encoded words" readme = "readme.md" keywords = ["rfc2047", "mail", "encoder"] license = "MIT" repository = "https://github.com/Valodim/rust-rfc2047" [dependencies] rfc2047-0.1.1/LICENSE010064400017500001750000000020631354540704000120310ustar0000000000000000MIT License Copyright (c) 2019 Vincent Breitmoser 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. rfc2047-0.1.1/readme.md010064400017500001750000000011241354540777100126130ustar0000000000000000# RFC 2047 Encoder [![Build Status](https://travis-ci.org/Valodim/rust-rfc2047.svg?branch=master)](https://travis-ci.org/Valodim/rust-rfc2047) ![Crates.io](https://img.shields.io/crates/v/rfc2047) ![Crates.io](https://img.shields.io/crates/l/rfc2047) Offers an encoder for RFC 2047 encoded words. ``` rust use rfc2047::rfc2047_encode; #[test] fn test_encode_rfc2047() { assert_eq!( "Foo =?utf-8?q?a=C3=A4b?= =?utf-8?q?_=C3=A4?= bar", rfc2047_encode("Foo aäb ä bar"), ); } ``` Words are encoded or not encoded as a whole. Only quoted-printable encoding is used. rfc2047-0.1.1/src/encode.rs010064400017500001750000000115231354540202400134140ustar0000000000000000use std::borrow::Cow; const HEX_CHARS: [char; 16] = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', ]; pub fn rfc2047_encode(mut data: &str) -> Cow { if data.is_ascii() { return Cow::Borrowed(data); } let mut result = String::with_capacity(data.len() * 2); let mut previous_token_was_encoded = false; let is_non_whitespace = |c: char| !c.is_ascii_whitespace(); let is_whitespace = |c: char| c.is_ascii_whitespace(); while !data.is_empty() { let word_begin = data.find(is_non_whitespace).unwrap_or(data.len()); let word_end = word_begin + data[word_begin..] .find(is_whitespace) .unwrap_or(data.len() - word_begin); let word = &data[word_begin..word_end]; if word.is_ascii() { let word_with_ws_prefix = &data[..word_end]; result.push_str(word_with_ws_prefix); previous_token_was_encoded = false; } else { if previous_token_was_encoded { result.push(' '); let word_with_ws_prefix = &data[..word_end]; let encoded_word = rfc2047_encode_word(word_with_ws_prefix); result.push_str(&encoded_word); } else { let prefix = &data[..word_begin]; result.push_str(prefix); let encoded_word = rfc2047_encode_word(word); result.push_str(&encoded_word); } previous_token_was_encoded = true; } data = &data[word_end..]; } Cow::Owned(result) } const MAX_LEN_ENCODED_WORD: usize = 75; // as per rfc 2047 const LEN_ENCODED_WORD_PREFIX: usize = 10; // "=?utf-8?q?" const LEN_ENCODED_WORD_SUFFIX: usize = 2; // "?=" const LEN_ENCODED_WORD_BUFFER: usize = 4 * 3; // max. four bytes per encoded char const MAX_LEN_ENCODED_DATA: usize = MAX_LEN_ENCODED_WORD - LEN_ENCODED_WORD_PREFIX - LEN_ENCODED_WORD_SUFFIX - LEN_ENCODED_WORD_BUFFER; fn rfc2047_encode_word(word: &str) -> String { let mut charbuf = [0; 4]; let mut result = String::with_capacity(word.len() + 15); result.push_str("=?utf-8?q?"); let mut at = 0; for b in word.chars() { if at >= MAX_LEN_ENCODED_DATA { result.push_str("?= =?utf-8?q?"); at = 0; } if b == ' ' { result.push('_'); at += 1; } else if b.is_ascii() && b != '_' && b != '=' { result.push(b); at += 1; } else { for x in b.encode_utf8(&mut charbuf).as_bytes() { result.push('='); result.push(HEX_CHARS[(x >> 4) as usize]); result.push(HEX_CHARS[(x & 0xf) as usize]); at += 3; } } } result.push_str("?="); result } #[cfg(test)] mod tests { use super::*; #[test] fn just_ascii() { assert_eq!("foo \n bar", rfc2047_encode("foo \n bar")); } #[test] fn just_whitespace() { assert_eq!(" ", rfc2047_encode(" ")); } #[test] fn single_char() { assert_eq!("=?utf-8?q?=C3=A4?=", rfc2047_encode("ä")); } #[test] fn single_char_multi() { assert_eq!("=?utf-8?q?=F0=9F=9A=80?=", rfc2047_encode("🚀")); } #[test] fn encoded_word() { assert_eq!("=?utf-8?q?foo=C3=A1bar?=", rfc2047_encode("fooábar")); } #[test] fn encoded_at_start() { assert_eq!("=?utf-8?q?=C3=A4?= bar", rfc2047_encode("ä bar")); } #[test] fn encoded_at_end() { assert_eq!("Foo =?utf-8?q?=C3=A4?=", rfc2047_encode("Foo ä")); } #[test] fn encoded_in_middle() { assert_eq!( "Foo =?utf-8?q?=C3=A4?= bar", rfc2047_encode("Foo ä bar") ); } #[test] fn encoded_adjacent_in_middle() { assert_eq!( "Foo =?utf-8?q?=C3=A4?= =?utf-8?q?_=C3=A4?= bar", rfc2047_encode("Foo ä ä bar") ); } #[test] fn encoded_adjacent_with_whitespace() { assert_eq!( "=?utf-8?q?=C3=A4?= =?utf-8?q?__=C3=BC?=", rfc2047_encode("ä ü") ); } #[test] fn encoded_trailing_whitespace() { assert_eq!("=?utf-8?q?=C3=A4?= ", rfc2047_encode("ä ")); } #[test] fn encoded_leading_whitespace() { assert_eq!(" =?utf-8?q?=C3=A4?=", rfc2047_encode(" ä")); } #[test] fn encoded_surrounding_whitespace() { assert_eq!(" =?utf-8?q?=C3=A4?= ", rfc2047_encode(" ä ")); } #[test] fn encoded_long() { assert_eq!( "=?utf-8?q?=C3=A4012345678012345678012345678012345678012345678?= =?utf-8?q?0123456789999990123456789?=", rfc2047_encode("ä0123456780123456780123456780123456780123456780123456789999990123456789") ); } } rfc2047-0.1.1/src/lib.rs010064400017500001750000000000401354540202600127170ustar0000000000000000mod encode; pub use encode::*; rfc2047-0.1.1/.cargo_vcs_info.json0000644000000001120000000000000121530ustar00{ "git": { "sha1": "dce391e1937a742ff15339607dece35ad29c023a" } }