~dancek/twinwiki-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH v2] Cleanup edit function

Details
Message ID
<20200709145315.292907-1-detegr@rbx.email>
DKIM signature
missing
Download raw message
Patch: +9 -8
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
Details
Message ID
<CANmFPe=WugbrSOJ646M4j2cHtFb3seySOkDv64pVasouY7t4HQ@mail.gmail.com>
In-Reply-To
<20200709145315.292907-1-detegr@rbx.email> (view parent)
DKIM signature
missing
Download raw message
Thanks for the patch! I'll push this later.

-- 
Hannu Hartikainen
Reply to thread Export thread (mbox)