How to eliminate up to n spaces at the beginning of each line?
For example, when trim 4 space:
" 5"->" 5"" 4"->"4"" 3"->"3"
const INPUT:&str = " 4\n 2\n0\n\n 6\n";
const OUTPUT:&str = "4\n2\n0\n\n 6\n";
#[test]
fn main(){
assert_eq!(&trim_deindent(INPUT,4), OUTPUT)
}
I was about to comment
textwrap::dedent, but then I noticed"2", which has less than 4 spaces. So you wanted it to keep removing spaces, if there is any up until 4.Just writing a quick solution, it could look something like this:
Your assert will pass, but note that lines ending in
\r\nwill be converted to\n, aslinesdoes not provide a way to differentiate between\nand\r\n.If you want to retain both
\nand\r\nthen you can go a more complex route of scanning through the string, and thus avoiding usinglines.