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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! string

/// Returns the reversed version of the input string.
///
/// # Examples
///
/// ```
/// let text = "abcd";
/// let answer = jabba_lib::jstring::str_rev(text);
///
/// assert_eq!(answer, "dcba");
/// ```
pub fn str_rev(s: &str) -> String {
    s.chars().rev().collect::<String>()
}

/// Returns `true` if the given string is palindrome.
///
/// # Examples
///
/// ```
/// let yes = "abcba";
/// let no = "ABC";
///
/// assert_eq!(jabba_lib::jstring::is_palindrome(yes), true);
/// assert_eq!(jabba_lib::jstring::is_palindrome(no), false);
/// ```
pub fn is_palindrome(s: &str) -> bool {
    s == str_rev(s)
}

/// Removes the trailing newline of the given string.
///
/// It modifies the string in place.
///
/// It was inspired by Perl's `chomp()`.
///
/// # Examples
///
/// ```
/// let mut hello = String::from("hello\n");
/// let mut world = String::from("world\r\n");
///
/// jabba_lib::jstring::chomp(&mut hello);
/// jabba_lib::jstring::chomp(&mut world);
///
/// assert_eq!(hello, "hello");
/// assert_eq!(world, "world");
/// ```
pub fn chomp(text: &mut String) {
    if text.ends_with('\n') {
        text.pop();
        if text.ends_with('\r') {
            text.pop();
        }
    }
}

/// Returns a centered string of length `width`.
///
/// Padding is done with spaces.
///
/// It's similar to Python's `str.center()`.
///
/// # Examples
///
/// ```
/// let text = "*";
/// let result = jabba_lib::jstring::center(text, 3);
///
/// assert_eq!(result, " * ");
/// ```
pub fn center(s: &str, width: usize) -> String {
    format!("{s:^w$}", w = width)
}

/// Returns a capitalized version of the string.
///
/// More specifically, it makes the first character upper case and
/// the rest lower case.
///
/// It's like Python's `str.capitalize()`.
///
/// # Examples
///
/// ```
/// let name = "kAtE";
/// let result = jabba_lib::jstring::capitalize(name);
///
/// assert_eq!(result, "Kate");
/// ```
pub fn capitalize(s: &str) -> String {
    if s.is_empty() {
        String::new()
    } else {
        let chars: Vec<char> = s.chars().collect();
        let first = chars[0].to_uppercase();
        let rest = chars.into_iter().skip(1).collect::<String>().to_lowercase();
        format!("{}{}", first, rest)
    }
}

// ==========================================================================

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

    #[test]
    fn str_rev_test1() {
        assert_eq!(str_rev(""), "");
        assert_eq!(str_rev("a"), "a");
        assert_eq!(str_rev("ab"), "ba");
        assert_eq!(str_rev("anna"), "anna");
        assert_eq!(str_rev("abc"), "cba");
        assert_eq!(str_rev("AbCdE"), "EdCbA");
    }

    #[test]
    fn is_palindrome_test1() {
        assert!(is_palindrome(""));
        assert!(is_palindrome("a"));
        assert!(is_palindrome("aa"));
        assert!(is_palindrome("anna"));
        assert!(is_palindrome("görög"));
    }

    #[test]
    fn is_palindrome_test2() {
        assert_eq!(is_palindrome("ab"), false);
        assert_eq!(is_palindrome("Anna"), false);
    }

    #[test]
    fn chomp_test1() {
        let mut text = "".to_string();
        chomp(&mut text);
        assert_eq!(text, "");
        //
        let mut text = "abc".to_string();
        chomp(&mut text);
        assert_eq!(text, "abc");
        //
        let mut text = "\n".to_string();
        chomp(&mut text);
        assert_eq!(text, "");
        //
        let mut text = "\r\n".to_string();
        chomp(&mut text);
        assert_eq!(text, "");
        //
        let mut text = "\nend".to_string();
        chomp(&mut text);
        assert_eq!(text, "\nend");
        //
        let mut text = "\r\nend".to_string();
        chomp(&mut text);
        assert_eq!(text, "\r\nend");
        //
        let mut text = "longer\nstring\n".to_string();
        chomp(&mut text);
        assert_eq!(text, "longer\nstring");
    }

    #[test]
    fn center_test1() {
        assert_eq!(center("-", 0), "-");
        assert_eq!(center("-", 1), "-");
        assert_eq!(center("-", 2), "- ");
        assert_eq!(center("-", 3), " - ");
    }

    #[test]
    fn capitalize_test1() {
        assert_eq!(capitalize(""), "");
        assert_eq!(capitalize("a"), "A");
        assert_eq!(capitalize("aa"), "Aa");
        assert_eq!(capitalize("aNnA"), "Anna");
    }
}