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
//! This crate provides methods for finding the overlap between two string slices.
//!
//! An overlap is here defined as the largest substring contained both at the end of one string
//! slice and the beginning of another string slice.
//!
//! The implementation is provided through the [`Overlap`] trait, which is implemented on [`str`].
//! This allows the user to simply pull the trait into scope and use its methods:
//!
//! ```
//! use str_overlap::Overlap;
//!
//! assert_eq!("bcd".overlap_start("abc"), "bc");
//! assert_eq!("abc".overlap_end("bcd"), "bc");
//! ```
//!
//! The trait provides two methods: [`overlap_start`] and [`overlap_end`], which find the overlap
//! at the beginning and end of the first value respectively. The reason for these two methods is
//! to allow the user to specify ownership of the resulting subvalue, regardless of its overlap
//! position.
//!
//! [`overlap_end`]: Overlap::overlap_end
//! [`overlap_start`]: Overlap::overlap_start

// Since the MSRV is 1.0.0, allowing usage of deprecated items is ok, as the replacements are likely
// not available in early versions.
#![allow(deprecated)]
#![cfg_attr(rustc_1_6, no_std)]

#[cfg(not(rustc_1_6))]
extern crate std as core;

/// Shared logic for finding the index at which two strings overlap.
///
/// The `left` and `right` parameters are, conceptually, defined as follows:
/// - `left` is the parameter whose suffix will be overlapping
/// - `right` is the parameter whose prefix will be overlapping
///
/// If no overlap exists, the returned index will be the length of `left`. This allows the result to
/// be used to create an empty slice.
#[inline]
#[must_use]
fn string_overlap_index(left: &str, right: &str) -> usize {
    left.char_indices()
        .map(|(index, _)| index)
        .find(|index| {
            let slice_len = left.len() - index;
            slice_len <= right.len()
                && unsafe {
                    // SAFETY: `index` is obtained from `left`'s `CharIndices`, so it will always be
                    // within the bounds of `left`. Additionally, `index` will also always be on
                    // UTF-8 character bounds of `left`.
                    left.slice_unchecked(*index, left.len())
                    // SAFETY: Since `slice_len - index` is less than or equal to `right.len()`,
                    // `slice_len` will always be within the bounds of `right`. Additionally, since
                    // the string slice is simply cast to bytes when checking equality, we don't
                    // need to worry about whether the slice occurs on a valid UTF-8 character
                    // bound.
                        == right.slice_unchecked(0, slice_len)
                }
        })
        .unwrap_or_else(|| left.len())
}

/// Provides methods for finding overlaps between values.
///
/// This trait provides methods for finding overlaps at both the start and end of `self`. This
/// allows for returning overlapping values that are owned by `self`, regardless of which side of
/// `self` the overlap is occurring.
///
/// This trait is made available by pulling it into scope:
///
/// ```
/// use str_overlap::Overlap;
/// ```
///
/// `Overlap` is implemented on [`str`], which means its methods are usable by `str` and any types
/// which implement [`Deref<Target = str>`], such as [`String`].
///
/// [`Deref<Target = str>`]: core::ops::Deref
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
pub trait Overlap {
    /// Returns the overlap found at the start of `self` and the end of `other`.
    ///
    /// # Example
    /// This method can be used through its implementation on [`str`], like so:
    ///
    /// ```
    /// use str_overlap::Overlap;
    ///
    /// assert_eq!("bcd".overlap_start("abc"), "bc");
    /// ```
    fn overlap_start(&self, other: &Self) -> &Self;
    /// Returns the overlap found at the end of `self` and the start of `other`.
    ///
    /// # Example
    /// This method can be used through its implementation on `str`, like so:
    ///
    /// ```
    /// use str_overlap::Overlap;
    ///
    /// assert_eq!("abc".overlap_end("bcd"), "bc");
    /// ```
    fn overlap_end(&self, other: &Self) -> &Self;
}

/// Overlap methods for string slices.
///
/// This allows for the returned string slice to be a subset of either string slice from which an
/// overlap is obtained.
impl Overlap for str {
    /// Returns the substring which is both the prefix to `self` and the suffix to `other`.
    ///
    /// The returned string slice is a reference to the substring contained in `self`.
    ///
    /// # Example
    /// ```
    /// use str_overlap::Overlap;
    ///
    /// assert_eq!("bcd".overlap_start("abc"), "bc");
    /// ```
    #[inline]
    #[must_use]
    fn overlap_start(&self, other: &Self) -> &Self {
        unsafe {
            // SAFETY: The result of `string_overlap_index()` subtracted from `other.len()` will
            // always be on a character bound of `self`, since it is found by comparing directly the
            // bytes of the start of `self` and the end of `other`. Therefore, the range will be
            // within `self`'s bounds and also will uphold `str` invariants.
            self.slice_unchecked(0, other.len() - string_overlap_index(other, self))
        }
    }

    /// Returns the substring which is both the suffix to `self` and the prefix to `other`.
    ///
    /// The returned string slice is a reference to the substring contained in `self`.
    ///
    /// # Example
    /// ```
    /// use str_overlap::Overlap;
    ///
    /// assert_eq!("abc".overlap_end("bcd"), "bc");
    /// ```
    #[inline]
    #[must_use]
    fn overlap_end(&self, other: &Self) -> &Self {
        unsafe {
            // SAFETY: The result of `string_overlap_index()` will always be on a character bound of
            // `self`, since it is found from running over the CharIndices of `self`. Therefore, the
            // range will be within `self`'s bounds and also will uphold `str` invariants.
            self.slice_unchecked(string_overlap_index(self, other), self.len())
        }
    }
}

#[cfg(test)]
mod tests {
    use Overlap;

    #[test]
    fn partial_overlap_start() {
        assert_eq!("bcd".overlap_start("abc"), "bc");
    }

    #[test]
    fn partial_overlap_end() {
        assert_eq!("abc".overlap_end("bcd"), "bc");
    }

    #[test]
    fn full_overlap_start() {
        assert_eq!("abc".overlap_start("abc"), "abc");
    }

    #[test]
    fn full_overlap_end() {
        assert_eq!("abc".overlap_end("abc"), "abc");
    }

    #[test]
    fn no_overlap_start() {
        assert_eq!("abc".overlap_start("def"), "");
    }

    #[test]
    fn no_overlap_end() {
        assert_eq!("abc".overlap_end("def"), "");
    }

    #[test]
    fn other_substring_of_self_start() {
        assert_eq!("abcd".overlap_start("abc"), "abc");
    }

    #[test]
    fn other_substring_of_self_end() {
        assert_eq!("abcd".overlap_end("bcd"), "bcd");
    }

    #[test]
    fn self_substring_of_other_start() {
        assert_eq!("bcd".overlap_start("abcd"), "bcd");
    }

    #[test]
    fn self_substring_other_end() {
        assert_eq!("abc".overlap_end("abcd"), "abc");
    }

    #[test]
    fn only_checks_overlap_one_way_start() {
        assert_eq!("abc".overlap_start("bcd"), "");
    }

    #[test]
    fn only_checks_overlap_one_way_end() {
        assert_eq!("bcd".overlap_end("abc"), "");
    }

    #[test]
    fn self_empty_start() {
        assert_eq!("".overlap_start("abc"), "");
    }

    #[test]
    fn self_empty_end() {
        assert_eq!("".overlap_end("abc"), "");
    }

    #[test]
    fn other_empty_start() {
        assert_eq!("abc".overlap_start(""), "");
    }

    #[test]
    fn other_empty_end() {
        assert_eq!("abc".overlap_end(""), "");
    }

    #[test]
    fn all_empty_start() {
        assert_eq!("".overlap_end(""), "");
    }

    #[test]
    fn all_empty_end() {
        assert_eq!("".overlap_start(""), "");
    }

    #[test]
    fn multi_byte_start() {
        assert_eq!("語a日bc本".overlap_start("b日本語a"), "語a");
    }

    #[test]
    fn multi_byte_end() {
        assert_eq!("b日本語a".overlap_end("語a日bc本"), "語a");
    }
}