<commit_before>// Copyright 2014 <NAME>. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// 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.

//! Contains mixed-up tools for SMTP
//!
//! TODO : Clean up and split this file

use std::io::net::ip::Port;
use std::string::String;

/// Default SMTP port
pub static SMTP_PORT: Port = 25;

// Maximum length of an SMTP command line
//pub static MAX_SMTP_LINE_LENGTH: uint = 1034;

/// The word separator for SMTP transactions
pub static SP: &'static str = " ";

/// The line ending for SMTP transactions
pub static CRLF: &'static str = "\r\n";

/// Adds quotes to emails if needed
pub fn quote_email_address(address: String) -> String {
    match address.len() {
        0 ... 1 => format!("<{:s}>", address),
        _   => match (address.as_slice().slice_to(1),
                      address.as_slice().slice_from(address.len() - 1)) {
                   ("<", ">") => address,
                   _          => format!("<{:s}>", address)
               }
    }
}

/// Removes quotes from emails if needed
pub fn unquote_email_address(address: String) -> String {
    match address.len() {
        0 ... 1 => address,
        _    => match (address.as_slice().slice_to(1),
                       address.as_slice().slice_from(address.len() - 1)) {
                    ("<", ">") => address.as_slice().slice(1, address.len() - 1).to_string(),
                    _          => address
                }
    }
}

/// Removes the trailing line return at the end of a string
pub fn remove_trailing_crlf(string: String) -> String {
    if string.len() > 1 && string.as_slice().slice_from(string.len() - 2) == CRLF {
        string.as_slice().slice_to(string.len() - 2).to_string()
    } else if string.len() > 0 && string.as_slice().slice_from(string.len() - 1) == "\r" {
        string.as_slice().slice_to(string.len() - 1).to_string()
    } else {
        string
    }
}

/// Returns the first word of a string, or the string if it contains no space
pub fn get_first_word(string: String) -> String {
    string.as_slice().split_str(CRLF).next().unwrap().splitn(1, ' ').next().unwrap().to_string()
}

/// TODO
pub fn escape_crlf(string: String) -> String {
    string.replace(CRLF, "<CRLF>")
}

#[cfg(test)]
mod test {
    #[test]
    fn test_quote_email_address() {
        assert_eq!(super::quote_email_address("address".to_string()), "<address>".to_string());
        assert_eq!(super::quote_email_address("<address>".to_string()), "<address>".to_string());
        assert_eq!(super::quote_email_address("a".to_string()), "<a>".to_string());
        assert_eq!(super::quote_email_address("".to_string()), "<>".to_string());
    }

    #[test]
    fn test_unquote_email_address() {
        assert_eq!(super::unquote_email_address("<address>".to_string()), "address".to_string());
        assert_eq!(super::unquote_email_address("address".to_string()), "address".to_string());
        assert_eq!(super::unquote_email_address("<address".to_string()), "<address".to_string());
        assert_eq!(super::unquote_email_address("<>".to_string()), "".to_string());
        assert_eq!(super::unquote_email_address("a".to_string()), "a".to_string());
        assert_eq!(super::unquote_email_address("".to_string()), "".to_string());
    }

    #[test]
    fn test_remove_trailing_crlf() {
        assert_eq!(super::remove_trailing_crlf("word".to_string()), "word".to_string());
        assert_eq!(super::remove_trailing_crlf("word\r\n".to_string()), "word".to_string());
        assert_eq!(super::remove_trailing_crlf("word\r\n ".to_string()), "word\r\n ".to_string());
        assert_eq!(super::remove_trailing_crlf("word\r".to_string()), "word".to_string());
        assert_eq!(super::remove_trailing_crlf("\r\n".to_string()), "".to_string());
        assert_eq!(super::remove_trailing_crlf("\r".to_string()), "".to_string());
        assert_eq!(super::remove_trailing_crlf("a".to_string()), "a".to_string());
        assert_eq!(super::remove_trailing_crlf("".to_string()), "".to_string());
    }

    #[test]
    fn test_get_first_word() {
        assert_eq!(super::get_first_word("first word".to_string()), "first".to_string());
        assert_eq!(super::get_first_word("first word\r\ntest".to_string()), "first".to_string());
        assert_eq!(super::get_first_word("first".to_string()), "first".to_string());
        assert_eq!(super::get_first_word("".to_string()), "".to_string());
        assert_eq!(super::get_first_word("\r\n".to_string()), "".to_string());
        assert_eq!(super::get_first_word("a\r\n".to_string()), "a".to_string());
        // Manage cases of empty line, spaces at the beginning
        //assert_eq!(super::get_first_word(" a".to_string()), "a".to_string());
        //assert_eq!(super::get_first_word("\r\n a".to_string()), "a".to_string());
        assert_eq!(super::get_first_word(" \r\n".to_string()), "".to_string());
        assert_eq!(super::get_first_word("\r\n ".to_string()), "".to_string());
    }
}<commit_msg>Add tests for the escape_crlf function<commit_after>// Copyright 2014 <NAME>. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// 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.

//! Contains mixed-up tools for SMTP
//!
//! TODO : Clean up and split this file

use std::io::net::ip::Port;
use std::string::String;

/// Default SMTP port
pub static SMTP_PORT: Port = 25;

// Maximum length of an SMTP command line
//pub static MAX_SMTP_LINE_LENGTH: uint = 1034;

/// The word separator for SMTP transactions
pub static SP: &'static str = " ";

/// The line ending for SMTP transactions
pub static CRLF: &'static str = "\r\n";

/// Adds quotes to emails if needed
pub fn quote_email_address(address: String) -> String {
    match address.len() {
        0 ... 1 => format!("<{:s}>", address),
        _   => match (address.as_slice().slice_to(1),
                      address.as_slice().slice_from(address.len() - 1)) {
                   ("<", ">") => address,
                   _          => format!("<{:s}>", address)
               }
    }
}

/// Removes quotes from emails if needed
pub fn unquote_email_address(address: String) -> String {
    match address.len() {
        0 ... 1 => address,
        _    => match (address.as_slice().slice_to(1),
                       address.as_slice().slice_from(address.len() - 1)) {
                    ("<", ">") => address.as_slice().slice(1, address.len() - 1).to_string(),
                    _          => address
                }
    }
}

/// Removes the trailing line return at the end of a string
pub fn remove_trailing_crlf(string: String) -> String {
    if string.len() > 1 && string.as_slice().slice_from(string.len() - 2) == CRLF {
        string.as_slice().slice_to(string.len() - 2).to_string()
    } else if string.len() > 0 && string.as_slice().slice_from(string.len() - 1) == "\r" {
        string.as_slice().slice_to(string.len() - 1).to_string()
    } else {
        string
    }
}

/// Returns the first word of a string, or the string if it contains no space
pub fn get_first_word(string: String) -> String {
    string.as_slice().split_str(CRLF).next().unwrap().splitn(1, ' ').next().unwrap().to_string()
}

/// TODO
pub fn escape_crlf(string: String) -> String {
    string.replace(CRLF, "<CRLF>")
}

#[cfg(test)]
mod test {
    use super::CRLF;

    #[test]
    fn test_quote_email_address() {
        assert_eq!(super::quote_email_address("address".to_string()), "<address>".to_string());
        assert_eq!(super::quote_email_address("<address>".to_string()), "<address>".to_string());
        assert_eq!(super::quote_email_address("a".to_string()), "<a>".to_string());
        assert_eq!(super::quote_email_address("".to_string()), "<>".to_string());
    }

    #[test]
    fn test_unquote_email_address() {
        assert_eq!(super::unquote_email_address("<address>".to_string()), "address".to_string());
        assert_eq!(super::unquote_email_address("address".to_string()), "address".to_string());
        assert_eq!(super::unquote_email_address("<address".to_string()), "<address".to_string());
        assert_eq!(super::unquote_email_address("<>".to_string()), "".to_string());
        assert_eq!(super::unquote_email_address("a".to_string()), "a".to_string());
        assert_eq!(super::unquote_email_address("".to_string()), "".to_string());
    }

    #[test]
    fn test_remove_trailing_crlf() {
        assert_eq!(super::remove_trailing_crlf("word".to_string()), "word".to_string());
        assert_eq!(super::remove_trailing_crlf("word\r\n".to_string()), "word".to_string());
        assert_eq!(super::remove_trailing_crlf("word\r\n ".to_string()), "word\r\n ".to_string());
        assert_eq!(super::remove_trailing_crlf("word\r".to_string()), "word".to_string());
        assert_eq!(super::remove_trailing_crlf("\r\n".to_string()), "".to_string());
        assert_eq!(super::remove_trailing_crlf("\r".to_string()), "".to_string());
        assert_eq!(super::remove_trailing_crlf("a".to_string()), "a".to_string());
        assert_eq!(super::remove_trailing_crlf("".to_string()), "".to_string());
    }

    #[test]
    fn test_get_first_word() {
        assert_eq!(super::get_first_word("first word".to_string()), "first".to_string());
        assert_eq!(super::get_first_word("first word\r\ntest".to_string()), "first".to_string());
        assert_eq!(super::get_first_word("first".to_string()), "first".to_string());
        assert_eq!(super::get_first_word("".to_string()), "".to_string());
        assert_eq!(super::get_first_word("\r\n".to_string()), "".to_string());
        assert_eq!(super::get_first_word("a\r\n".to_string()), "a".to_string());
        // Manage cases of empty line, spaces at the beginning
        //assert_eq!(super::get_first_word(" a".to_string()), "a".to_string());
        //assert_eq!(super::get_first_word("\r\n a".to_string()), "a".to_string());
        assert_eq!(super::get_first_word(" \r\n".to_string()), "".to_string());
        assert_eq!(super::get_first_word("\r\n ".to_string()), "".to_string());
    }

    #[test]
    fn test_escape_crlf() {
        assert_eq!(super::escape_crlf(format!("{}", CRLF)), "<CRLF>".to_string());
        assert_eq!(super::escape_crlf(format!("EHLO my_name{}", CRLF)), "EHLO my_name<CRLF>".to_string());
        assert_eq!(super::escape_crlf(format!("EHLO my_name{}SIZE 42{}", CRLF, CRLF)), "EHLO my_name<CRLF>SIZE 42<CRLF>".to_string());
    }
}
{'fraction_non_alphanumeric': 0.1928447567764899, 'fraction_numerical': 0.006023546591220224, 'mean_word_length': 3.4580960130187144, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7607984', 'n_tokens_mistral': 3638, 'n_tokens_neox': 3543, 'n_words': 644}