phpify/string/lcfirst.rs
1// Copyright (c) 2020 DarkWeb Design
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16// SOFTWARE.
17
18/// Make a string's first character lowercase
19///
20/// # Description
21///
22/// Returns a string with the first character of str lowercased if that character is alphabetic.
23///
24/// Note that 'alphabetic' is determined by the current locale. For instance, in the default "C"
25/// locale characters such as umlaut-a (ä) will not be converted.
26///
27/// # Examples
28///
29/// Example #1 lcfirst() example
30///
31/// ```
32/// use phpify::string::lcfirst;
33///
34/// let foo = "HelloWorld";
35/// assert_eq!(lcfirst(foo), "helloWorld");
36///
37/// let bar = "HELLO WORLD!";
38/// assert_eq!(lcfirst(bar), "hELLO WORLD!");
39/// ```
40pub fn lcfirst<S>(str: S) -> String
41 where
42 S: AsRef<str> {
43
44 let mut chars = str.as_ref().chars();
45
46 match chars.next() {
47 None => String::new(),
48 Some(char) => char.to_lowercase().collect::<String>() + chars.as_str(),
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use crate::string::lcfirst;
55
56 #[test]
57 fn test() {
58 assert_eq!(lcfirst("ABC"), "aBC");
59 assert_eq!(lcfirst("ÄBC"), "äBC");
60 assert_eq!(lcfirst("ΔΞΠ"), "δΞΠ");
61 }
62}