Skip to content

Commit 1134499

Browse files
committed
Add join convenience function
1 parent 95ef696 commit 1134499

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ pub fn quote(in_str: &str) -> Cow<str> {
181181
}
182182
}
183183

184+
/// Convenience function that consumes an iterable of words and turns it into a single string,
185+
/// quoting words when necessary. Consecutive words will be separated by a single space.
186+
pub fn join<'a, I: IntoIterator<Item = &'a str>>(words: I) -> String {
187+
words.into_iter()
188+
.map(quote)
189+
.collect::<Vec<_>>()
190+
.join(" ")
191+
}
192+
184193
#[cfg(test)]
185194
static SPLIT_TEST_ITEMS: &'static [(&'static str, Option<&'static [&'static str]>)] = &[
186195
("foo$baz", Some(&["foo$baz"])),
@@ -227,3 +236,11 @@ fn test_quote() {
227236
assert_eq!(quote("\""), "\"\\\"\"");
228237
assert_eq!(quote(""), "\"\"");
229238
}
239+
240+
#[test]
241+
fn test_join() {
242+
assert_eq!(join(vec![]), "");
243+
assert_eq!(join(vec![""]), "\"\"");
244+
assert_eq!(join(vec!["a", "b"]), "a b");
245+
assert_eq!(join(vec!["foo bar", "baz"]), "\"foo bar\" baz");
246+
}

0 commit comments

Comments
 (0)