1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#![cfg_attr(feature = "nightly", feature(pattern))]
#![warn(missing_docs)]

//! This crate allows you to `Display::fmt` strings that include replacements, without actually doing any replacement until format-time and totally avoiding allocation.
//!
//! This is useful when you do `.replace` and then immediately pass the result to `format!` - it will prevent the intermediate allocation from happening. You can even use the result in another `.lazy_replace` call and it will still avoid allocation, although it may do the inner replacement multiple times. The work of memoizing the result of `Display::fmt` to avoid duplicating work can be done in a generic way by an external crate and requires allocation, so is out of the scope of this crate.

extern crate memchr;

use std::{
    fmt::{self, Write},
    ops::Deref,
};

#[cfg(not(feature = "nightly"))]
pub mod pattern;

#[cfg(feature = "nightly")]
pub use std::str::pattern;

use self::pattern::{Pattern, SearchStep, Searcher};

/// A type to lazily replace strings in any type that implements `Display`
pub struct ReplaceDisplay<'a, H, R> {
    haystack: H,
    needle: &'a str,
    replacement: R,
}

impl<'a, H, R> ReplaceDisplay<'a, H, R> {
    /// Create a new instance of this type
    pub fn new(haystack: H, needle: &'a str, replacement: R) -> Self {
        ReplaceDisplay {
            haystack,
            needle,
            replacement,
        }
    }
}

impl<'a, D, R> fmt::Display for ReplaceDisplay<'a, D, R>
where
    D: fmt::Display,
    R: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            ReplaceWriter::new(f, self.needle, &self.replacement),
            "{}",
            self.haystack
        )
    }
}

/// A wrapper around a `fmt::Write` that does string replacement on anything that is written to it
/// before passing it to the underlying writer.
pub struct ReplaceWriter<'a, W, R> {
    writer: W,
    needle_pos: usize,
    needle: &'a str,
    replacement: R,
    buffer: String,
}

impl<'a, W, R> ReplaceWriter<'a, W, R>
where
    W: fmt::Write,
    R: fmt::Display,
{
    /// Create a new instance of this type
    pub fn new(writer: W, needle: &'a str, replacement: R) -> Self {
        ReplaceWriter {
            writer,
            needle_pos: 0,
            needle,
            replacement,
            buffer: String::new(),
        }
    }
}

impl<'a, W, R> fmt::Write for ReplaceWriter<'a, W, R>
where
    W: fmt::Write,
    R: fmt::Display,
{
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let rest_needle = &self.needle[self.needle_pos..];

        if s.len() < rest_needle.len() && s.starts_with(&rest_needle[..s.len()]) {
            self.needle_pos += s.len();
            self.buffer.push_str(s);
        } else {
            self.needle_pos = 0;

            if s == rest_needle {
                self.buffer.clear();
                write!(self.writer, "{}", self.replacement)?;
            } else {
                self.writer.write_str(&self.buffer)?;
                self.buffer.clear();

                if let Some(first_char) = self.needle.chars().next() {
                    let mut s = s;

                    while let Some(i) = s.find(first_char) {
                        self.writer.write_str(&s[..i])?;
                        s = &s[i..];
                        dbg!(s);

                        let mut len = first_char.len_utf8();
                        let needle_bytes = self.needle.as_bytes();
                        let s_bytes = s.as_bytes();

                        while needle_bytes
                            .get(len)
                            .and_then(|needle| s_bytes.get(len).map(|haystack| haystack == needle))
                            .unwrap_or(false)
                        {
                            len += 1;
                        }

                        if len == self.needle.len() {
                            write!(self.writer, "{}", self.replacement)?;
                            s = &s[len..];
                        } else if len == s.len() {
                            self.buffer.push_str(&s[i..]);
                            self.needle_pos = len;

                            return Ok(());
                        } else {
                            self.writer.write_str(&s[..len])?;
                            s = &s[len..];
                        }
                    }

                    dbg!(s);
                    self.writer.write_str(s)?;
                }
            }
        }

        Ok(())
    }
}

/// A lazily-replaced string - no work is done until you call `.to_string()` or use `format!`/`write!` and friends. This is useful when, for example, doing `format!("( {} )", my_string.replace(needle, some_replacement)`. Since it uses a `Display` for a replacement, you can even replace a string with a different lazily-replaced string, all without allocating. Of course, this will duplicate work when there is more than one match, but fixing this would require memoization of the `Display` result, which in turn would require allocation. A memoizing `Display` wrapper is out of scope for this crate.
pub struct ReplacedString<'a, P, R> {
    haystack: &'a str,
    needle: P,
    replacement: R,
}

impl<'a, P, R> ReplacedString<'a, P, R> {
    /// Create a struct implementing `Display` that will display the specified string with the specified pattern replaced with the specified replacement
    pub fn new(haystack: &'a str, needle: P, replacement: R) -> Self {
        ReplacedString {
            haystack,
            needle,
            replacement,
        }
    }
}

/// A convenience trait to allow you to call `.lazy_replace` on anything that can deref to a `&str`.
pub trait LazyReplace {
    /// Create a struct implementing `Display` that will display this string with the specified pattern replaced with the specified replacement
    fn lazy_replace<P, R>(&self, pat: P, replacement: R) -> ReplacedString<'_, P, R>;
}

impl<T> LazyReplace for T
where
    T: Deref<Target = str>,
{
    fn lazy_replace<P, R>(&self, needle: P, replacement: R) -> ReplacedString<'_, P, R> {
        ReplacedString {
            needle,
            replacement,
            haystack: &*self,
        }
    }
}

impl LazyReplace for str {
    fn lazy_replace<P, R>(&self, needle: P, replacement: R) -> ReplacedString<'_, P, R> {
        ReplacedString {
            needle,
            replacement,
            haystack: &*self,
        }
    }
}

/// A convenience trait to allow you to call `.replace_display` on anything that implements `fmt::Display`.
// TODO: Combine with `LazyReplace` once specialisation and GATs are stable
pub trait LazyReplaceDisplay: Sized {
    /// Create a struct implementing `Display` that will display this string with the specified pattern replaced with the specified replacement
    fn replace_display<'a, R>(self, pat: &'a str, replacement: R) -> ReplaceDisplay<'a, Self, R>;
}

impl<T> LazyReplaceDisplay for T
where
    T: fmt::Display,
{
    /// Create a struct implementing `Display` that will display this string with the specified pattern replaced with the specified replacement
    fn replace_display<'a, R>(self, pat: &'a str, replacement: R) -> ReplaceDisplay<'a, Self, R> {
        ReplaceDisplay::new(self, pat, replacement)
    }
}

impl<'a, P, R> fmt::Display for ReplacedString<'a, P, R>
where
    P: Pattern<'a> + Clone,
    R: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut searcher = self.needle.clone().into_searcher(self.haystack);
        loop {
            match searcher.next() {
                SearchStep::Match(_, _) => write!(f, "{}", self.replacement)?,
                SearchStep::Reject(start, end) => write!(f, "{}", &self.haystack[start..end])?,
                SearchStep::Done => break,
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::{LazyReplace, LazyReplaceDisplay};

    #[test]
    fn replace_string() {
        assert_eq!(
            "onetwothree",
            "one!HERE!three".lazy_replace("!HERE!", "two").to_string()
        );
        assert_eq!(
            "onetwothree",
            "onetwo!HERE!".lazy_replace("!HERE!", "three").to_string()
        );
        assert_eq!(
            "onetwothreethree",
            "onetwo!HERE!!HERE!"
                .lazy_replace("!HERE!", "three")
                .to_string()
        );
    }

    #[test]
    fn replace_display() {
        assert_eq!(
            "foobar",
            "foo!HERE!".replace_display("!HERE!", "bar").to_string()
        );
        assert_eq!(
            "foobar",
            "!HERE!bar".replace_display("!HERE!", "foo").to_string()
        );
        assert_eq!(
            "foobarbaz",
            format!(
                "{}{}",
                "foo!HERE!".replace_display("!HERE!", "ba"),
                "!HERE!baz".replace_display("!HERE!", "r")
            )
        );
        assert_eq!(
            "fooonetwothreebaz",
            format_args!(
                "{}{}",
                "foo!HERE!".replace_display("!HERE!", "ba"),
                "!HERE!baz".replace_display("!HERE!", "r")
            )
            .replace_display("bar", "one!HERE!three".replace_display("!HERE!", "two"))
            .to_string()
        );
    }
}