~dancek/twinwiki-devel

Cleanup edit function v2 APPLIED

Antti Keränen: 1
 Cleanup edit function

 1 files changed, 9 insertions(+), 8 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~dancek/twinwiki-devel/patches/11528/mbox | git am -3
Learn more about email & git

[PATCH v2] Cleanup edit function Export this patch

Use boxed iterators instead of Vecs. This should also reduce a few
memory allocations.
---
Reworded the commit message.

Sorry for sending this multiple times, still learning the email
workflow.

 src/sed/edit.rs | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/sed/edit.rs b/src/sed/edit.rs
index 695778a..c80cde1 100644
--- a/src/sed/edit.rs
+++ b/src/sed/edit.rs
@@ -20,7 +20,7 @@ use super::parser::parse_substitute;
use super::types::{Address, Command, Substitute};

use itertools::Itertools;
use std::iter::repeat;
use std::iter::{empty, once, repeat};

fn regex_replace<'a, I>(lines: I, arg: &str) -> Result<String, String>
where
@@ -59,14 +59,14 @@ pub fn edit(text: &str, cmd: Command) -> Result<String, String> {
    let repeating_arg = repeat(arg.as_ref()).take(count);

    let tmp;
    let edited = match command {
        'a' => editing_range.interleave_shortest(repeating_arg).collect(),
        'c' => vec![arg.as_ref()],
        'd' => vec![],
        'i' => repeating_arg.interleave_shortest(editing_range).collect(),
    let edited: Box<dyn Iterator<Item = &str>> = match command {
        'a' => Box::new(editing_range.interleave_shortest(repeating_arg)),
        'c' => Box::new(once(arg.as_ref())),
        'd' => Box::new(empty()),
        'i' => Box::new(repeating_arg.interleave_shortest(editing_range)),
        's' => {
            tmp = regex_replace(editing_range, &arg)?;
            vec![tmp.as_ref()]
            Box::new(once(tmp.as_ref()))
        }
        _ => return Err(format!("Sed command `{}` not implemented!", command)),
    };
@@ -76,5 +76,6 @@ pub fn edit(text: &str, cmd: Command) -> Result<String, String> {
        .take(start)
        .chain(edited)
        .chain(text.split('\n').skip(start + count))
        .join("\n"))
        .intersperse("\n")
        .collect())
}
-- 
2.27.0
Thanks for the patch! I'll push this later.