1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * 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);
    }
}