chomp_nl/
lib.rs

1/*
2 * Copyright (c) 2016 Boucher, Antoni <bouanto@zoho.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22//! chomp-nl provides traits and functions for removing newline characters at the end of strings.
23
24#![warn(missing_docs)]
25
26/// Check if the specified character is a newline character.
27fn is_newline(c: char) -> bool {
28    c == '\r' || c == '\n'
29}
30
31/// Trait for specifying how to remove the trailing newline characters (\r, \n).
32pub trait Chomp<'a> {
33    /// Return a string slice that does not contain the trailing newline characters.
34    fn chomp(Self) -> &'a str;
35}
36
37impl<'a> Chomp<'a> for &'a str {
38    fn chomp(string: Self) -> &'a str {
39        string.trim_right_matches(is_newline)
40    }
41}
42
43impl<'a> Chomp<'a> for &'a String {
44    fn chomp(string: Self) -> &'a str {
45        string.trim_right_matches(is_newline)
46    }
47}
48
49/// Trait for specifying how to remove (in-place) the trailing newline characters (\r, \n).
50pub trait ChompInPlace {
51    /// Remove the trailing newline characters from the string.
52    fn chomp(&mut self);
53}
54
55impl ChompInPlace for String {
56    fn chomp(&mut self) {
57        let len = self.len();
58        let newline_count = self.chars().rev()
59            .take_while(|&c| is_newline(c))
60            .count();
61        self.truncate(len - newline_count);
62    }
63}
64
65/// Remove the newline characters at the end of the `string`.
66pub fn chomp<'a, C: Chomp<'a>>(string: C) -> &'a str {
67    Chomp::chomp(string)
68}
69
70#[cfg(test)]
71mod tests {
72    use super::{ChompInPlace, chomp};
73
74    #[test]
75    fn test_chomp() {
76        assert_eq!("", chomp(""));
77        assert_eq!("test", chomp("test"));
78        assert_eq!("test", chomp("test\n"));
79        assert_eq!("   test    ", chomp("   test    \n"));
80        assert_eq!("   test    ", chomp("   test    \r"));
81        assert_eq!("   test    ", chomp("   test    \r\n"));
82        assert_eq!("\r\n   test    ", chomp("\r\n   test    \r\n"));
83        assert_eq!("\r\n   test   \r\n ", chomp("\r\n   test   \r\n \r\n"));
84
85        assert_eq!("", chomp(&"".to_string()));
86        assert_eq!("test", chomp(&"test".to_string()));
87        assert_eq!("test", chomp(&"test\n".to_string()));
88        assert_eq!("   test    ", chomp(&"   test    \n".to_string()));
89        assert_eq!("   test    ", chomp(&"   test    \r".to_string()));
90        assert_eq!("   test    ", chomp(&"   test    \r\n".to_string()));
91        assert_eq!("\r\n   test    ", chomp(&"\r\n   test    \r\n".to_string()));
92        assert_eq!("\r\n   test   \r\n ", chomp(&"\r\n   test   \r\n \r\n".to_string()));
93    }
94
95    #[test]
96    fn test_chomp_inplace() {
97        let mut string = "".to_string();
98        string.chomp();
99        assert_eq!("", string);
100
101        let mut string = "test".to_string();
102        string.chomp();
103        assert_eq!("test", string);
104
105        let mut string = "test\n".to_string();
106        string.chomp();
107        assert_eq!("test", string);
108
109        let mut string = "   test    \n".to_string();
110        string.chomp();
111        assert_eq!("   test    ", string);
112
113        let mut string = "   test    \r".to_string();
114        string.chomp();
115        assert_eq!("   test    ", string);
116
117        let mut string = "   test    \r\n".to_string();
118        string.chomp();
119        assert_eq!("   test    ", string);
120
121        let mut string = "\r\n   test    \r\n".to_string();
122        string.chomp();
123        assert_eq!("\r\n   test    ", string);
124
125        let mut string = "\r\n   test   \r\n \r\n".to_string();
126        string.chomp();
127        assert_eq!("\r\n   test   \r\n ", string);
128    }
129}