chomp-nl 0.1.2

chomp-nl provides a function to remove the trailing newline characters.
Documentation
/*
 * Copyright (c) 2016 Boucher, Antoni <bouanto@zoho.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.
 */

//! chomp-nl provides traits and functions for removing newline characters at the end of strings.

#![warn(missing_docs)]

/// Check if the specified character is a newline character.
fn is_newline(c: char) -> bool {
    c == '\r' || c == '\n'
}

/// Trait for specifying how to remove the trailing newline characters (\r, \n).
pub trait Chomp<'a> {
    /// Return a string slice that does not contain the trailing newline characters.
    fn chomp(Self) -> &'a str;
}

impl<'a> Chomp<'a> for &'a str {
    fn chomp(string: Self) -> &'a str {
        string.trim_right_matches(is_newline)
    }
}

impl<'a> Chomp<'a> for &'a String {
    fn chomp(string: Self) -> &'a str {
        string.trim_right_matches(is_newline)
    }
}

/// Trait for specifying how to remove (in-place) the trailing newline characters (\r, \n).
pub trait ChompInPlace {
    /// Remove the trailing newline characters from the string.
    fn chomp(&mut self);
}

impl ChompInPlace for String {
    fn chomp(&mut self) {
        let len = self.len();
        let newline_count = self.chars().rev()
            .take_while(|&c| is_newline(c))
            .count();
        self.truncate(len - newline_count);
    }
}

/// Remove the newline characters at the end of the `string`.
pub fn chomp<'a, C: Chomp<'a>>(string: C) -> &'a str {
    Chomp::chomp(string)
}

#[cfg(test)]
mod tests {
    use super::{ChompInPlace, chomp};

    #[test]
    fn test_chomp() {
        assert_eq!("", chomp(""));
        assert_eq!("test", chomp("test"));
        assert_eq!("test", chomp("test\n"));
        assert_eq!("   test    ", chomp("   test    \n"));
        assert_eq!("   test    ", chomp("   test    \r"));
        assert_eq!("   test    ", chomp("   test    \r\n"));
        assert_eq!("\r\n   test    ", chomp("\r\n   test    \r\n"));
        assert_eq!("\r\n   test   \r\n ", chomp("\r\n   test   \r\n \r\n"));

        assert_eq!("", chomp(&"".to_string()));
        assert_eq!("test", chomp(&"test".to_string()));
        assert_eq!("test", chomp(&"test\n".to_string()));
        assert_eq!("   test    ", chomp(&"   test    \n".to_string()));
        assert_eq!("   test    ", chomp(&"   test    \r".to_string()));
        assert_eq!("   test    ", chomp(&"   test    \r\n".to_string()));
        assert_eq!("\r\n   test    ", chomp(&"\r\n   test    \r\n".to_string()));
        assert_eq!("\r\n   test   \r\n ", chomp(&"\r\n   test   \r\n \r\n".to_string()));
    }

    #[test]
    fn test_chomp_inplace() {
        let mut string = "".to_string();
        string.chomp();
        assert_eq!("", string);

        let mut string = "test".to_string();
        string.chomp();
        assert_eq!("test", string);

        let mut string = "test\n".to_string();
        string.chomp();
        assert_eq!("test", string);

        let mut string = "   test    \n".to_string();
        string.chomp();
        assert_eq!("   test    ", string);

        let mut string = "   test    \r".to_string();
        string.chomp();
        assert_eq!("   test    ", string);

        let mut string = "   test    \r\n".to_string();
        string.chomp();
        assert_eq!("   test    ", string);

        let mut string = "\r\n   test    \r\n".to_string();
        string.chomp();
        assert_eq!("\r\n   test    ", string);

        let mut string = "\r\n   test   \r\n \r\n".to_string();
        string.chomp();
        assert_eq!("\r\n   test   \r\n ", string);
    }
}