]> git.scottworley.com Git - tablify/commitdiff
Move the trim_end() down
authorScott Worley <scottworley@scottworley.com>
Thu, 29 Aug 2024 09:34:41 +0000 (02:34 -0700)
committerScott Worley <scottworley@scottworley.com>
Wed, 2 Oct 2024 09:40:47 +0000 (02:40 -0700)
We don't want to covert the String to a &str prematurely.

src/lib.rs

index 199b8abd2732705484cc770b5ee948af71da332c..7292f0237d6765abed9e3d1b8d30143744aa3263 100644 (file)
@@ -117,18 +117,13 @@ impl<'a, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Re
     type Item = Result<RowInput<'a>, std::io::Error>;
     fn next(&mut self) -> Option<Self::Item> {
         loop {
-            match self
-                .input
-                .next()
-                // TODO: Don't leak
-                .map(|(n, r)| (n, r.map(|line| String::from(line).leak().trim_end())))
-            {
+            match self.input.next() {
                 None => return Ok(std::mem::take(&mut self.row)).transpose(),
                 Some((_, Err(e))) => return Some(Err(e)),
-                Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
+                Some((_, Ok(line))) if line.trim_end().is_empty() && self.row.is_some() => {
                     return Ok(std::mem::take(&mut self.row)).transpose()
                 }
-                Some((_, Ok(line))) if line.is_empty() => {}
+                Some((_, Ok(line))) if line.trim_end().is_empty() => {}
                 Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row {
                     None => {
                         return Some(Err(std::io::Error::other(format!(
@@ -136,12 +131,14 @@ impl<'a, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Re
                             n + 1
                         ))))
                     }
-                    Some(ref mut row) => row.entries.push(Entry::from(line.trim())),
+                    // TODO: Don't leak
+                    Some(ref mut row) => row.entries.push(Entry::from(line.leak().trim())),
                 },
                 Some((_, Ok(line))) => {
                     let prev = std::mem::take(&mut self.row);
                     self.row = Some(RowInput {
-                        label: line,
+                        // TODO: Don't leak
+                        label: line.leak().trim_end(),
                         entries: vec![],
                     });
                     if prev.is_some() {