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
// str.rs
//! String-related utilities.
// `StrExt` -------------------------------------------------------------------------------------------------
/// An extension trait for strings.
///
/// This is included in the crate's [prelude](crate::prelude).
pub trait StrExt {
/// Creates a new [`String`] by enclosing this string in back ticks.
///
/// # Examples
///
/// ```
/// use meadows::prelude::*;
///
/// assert_eq!("name".bt(), "`name`");
/// ```
#[must_use]
fn bt(&self) -> String;
/// Creates a new [`String`] by converting the first [`char`] of this string to uppercase.
///
/// # Examples
///
/// ```
/// use meadows::prelude::*;
///
/// assert_eq!("übermut".capitalize(), "Übermut");
/// ```
#[must_use]
fn capitalize(&self) -> String;
/// Creates a new [`String`] by putting this string, which may be a multi-line string, into a fence that is
/// made up of `c` and `text_width` - 1 characters wide.
///
/// # Examples
///
/// ```
/// use meadows::prelude::*;
///
/// assert_eq!("1st line\n2nd line".fence('*', 8), "*******\n*\n* 1st line\n* 2nd line\n*\n*******");
/// ```
#[must_use]
fn fence(&self, c: char, text_width: usize) -> String;
/// Creates a new [`String`] by converting the first [`char`] of this string to lowercase.
///
/// # Examples
///
/// ```
/// use meadows::prelude::*;
///
/// assert_eq!("Übermut".uncapitalize(), "übermut");
/// ```
#[must_use]
fn uncapitalize(&self) -> String;
}
impl StrExt for str {
#[inline]
fn bt(&self) -> String { format!("`{self}`") }
fn capitalize(&self) -> String {
let mut it = self.chars();
match it.next() {
None => String::new(),
Some(c) => c.to_uppercase().collect::<String>() + it.as_str(),
}
}
fn fence(&self, c: char, text_width: usize) -> String {
let mut ret = String::new();
let row = c.to_string().repeat(text_width - 1);
ret.push_str(&row);
ret.push('\n');
ret.push(c);
ret.push('\n');
for line in self.lines() {
ret.push(c);
ret.push(' ');
ret.push_str(line);
ret.push('\n');
}
ret.push(c);
ret.push('\n');
ret.push_str(&row);
ret
}
fn uncapitalize(&self) -> String {
let mut it = self.chars();
match it.next() {
None => String::new(),
Some(c) => c.to_lowercase().collect::<String>() + it.as_str(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
// `StrExt` -----------------------------------------------------------------------------------------------
#[test]
fn test_str_ext_bt() {
assert_eq!("a".bt(), "`a`");
assert_eq!("äöü".to_owned().bt(), "`äöü`");
}
#[test]
fn test_str_ext_capitalize() {
assert_eq!("".capitalize(), "");
assert_eq!("abc".capitalize(), "Abc");
assert_eq!("äöü".capitalize(), "Äöü");
assert_eq!("€".capitalize(), "€");
}
#[test]
fn test_str_ext_uncapitalize() {
assert_eq!("".uncapitalize(), "");
assert_eq!("Abc".uncapitalize(), "abc");
assert_eq!("Äöü".uncapitalize(), "äöü");
assert_eq!("€".uncapitalize(), "€");
}
}
// EOF