...
This commit is contained in:
@@ -81,3 +81,52 @@ pub fn dedent(text: &str) -> String {
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prefix a multiline string with a specified prefix.
|
||||
*
|
||||
* This function adds the specified prefix to the beginning of each line in the input text.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `text` - The multiline string to prefix
|
||||
* * `prefix` - The prefix to add to each line
|
||||
*
|
||||
* # Returns
|
||||
*
|
||||
* * `String` - The prefixed string
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* ```
|
||||
* let text = "line 1\nline 2\nline 3";
|
||||
* let prefixed = prefix(text, " ");
|
||||
* assert_eq!(prefixed, " line 1\n line 2\n line 3");
|
||||
* ```
|
||||
*/
|
||||
pub fn prefix(text: &str, prefix: &str) -> String {
|
||||
text.lines()
|
||||
.map(|line| format!("{}{}", prefix, line))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_dedent() {
|
||||
let indented = " line 1\n line 2\n line 3";
|
||||
let dedented = dedent(indented);
|
||||
assert_eq!(dedented, "line 1\nline 2\n line 3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_prefix() {
|
||||
let text = "line 1\nline 2\nline 3";
|
||||
let prefixed = prefix(text, " ");
|
||||
assert_eq!(prefixed, " line 1\n line 2\n line 3");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user