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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#![no_std]
use core::{mem, slice, str};

/// Error that can occur during [`concat`](fn.concat.html).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
    /// The passed strs are not adjacent.
    NotAdjacent,
    /// The first str is too long for concatenation.
    TooLong,
}

/// Concatenate two string slices if they are adjacent.
///
/// If two strs are adjacent to each other in memory, this function
/// concatenates both, creating a single str.
///
/// # Errors
///
/// Returns `Err` if the two slices aren't adjacent, `a` is after `b`, or if
/// `a` is too long for proper concatenation (longer than `isize::MAX`).
///
/// # Safety
///
/// The provided slices must come from the same underlying allocation. The adjacency test can not
/// reliably differentiate between the one-past-the-end pointer of one allocation and the start of
/// another. However, all slices must be within a single allocation.
///
/// # Examples
///
/// Correct usage:
///
/// ```rust
/// # use str_concat::concat;
/// let s = "0123456789";
/// unsafe {
///     // SAFETY: slices from the same str originally.
///     assert_eq!("0123456", concat(&s[..5], &s[5..7]).unwrap());
/// }
/// ```
///
/// Non-adjacent string slices:
///
/// ```rust
/// # use str_concat::{concat, Error};
/// let s = "0123456789";
/// unsafe {
///     // SAFETY: slices from the same str originally.
///     assert_eq!(Err(Error::NotAdjacent), concat(&s[..5], &s[6..7]))
/// }
/// ```
pub unsafe fn concat<'a>(a: &'a str, b: &'a str) -> Result<&'a str, Error> {
    let slice = concat_slice(a.as_bytes(), b.as_bytes())?;

    // * concatenating two valid UTF8 strings will produce a valid UTF8 string
    // * a BOM in `b` is still valid:
    //   > It is important to understand that the character U+FEFF appearing at
    //   > any position other than the beginning of a stream MUST be interpreted
    //   > with the semantics for the zero-width non-breaking space, and MUST
    //   > NOT be interpreted as a signature.
    // * the grapheme *clusters* (and thus potentially the semantics of the string
    //   might change if the first code point of `b` is a combining character,
    //   a zero width joiner or similar.
    //   This does not affect the correctness of UTF-8.
    Ok(str::from_utf8_unchecked(slice))
}

/// Concatenate two slices if they are adjacent.
///
/// If two slices are adjacent to each other in memory, this function
/// concatenates both, creating a single longer slice. Note that slices of
/// zero-sized types (ZST) are never considered adjacent. Otherwise it would be
/// possible to concatenate a slice to itself.
///
/// # Errors
///
/// Returns `Err` if the two slices aren't adjacent, `a` is after `b`, or if the
/// result is too long to be represented as a slice (size in bytes is larger
/// than `isize::MAX`).
///
/// When T is a zero-sized type (ZST) then always returns `Err(NotAdjacent)` otherwise. This is
/// because ZST-slices are [extra weird][zst-str-concat] and [their safety][zst-unsafe-wg1] is not
/// yet [fully determined][zst-unsafe-wg2].
///
/// [zst-str-concat]: https://github.com/oberien/str-concat/issues/5
/// [zst-unsafe-wg1]: https://github.com/rust-lang/unsafe-code-guidelines/issues/93
/// [zst-unsafe-wg2]: https://github.com/rust-lang/unsafe-code-guidelines/issues/168
///
/// # Safety
///
/// The provided slices must come from the same underlying allocation. The adjacency test can not
/// reliably differentiate between the one-past-the-end pointer of one allocation and the start of
/// another. However, all slices must be within a single allocation.
///
/// # Examples
///
/// Correct usage:
///
/// ```rust
/// # use str_concat::concat_slice;
/// let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
/// unsafe {
///     // SAFETY: slices from the same bytes originally.
///     assert_eq!(
///         [0, 1, 2, 3, 4, 5, 6], 
///         concat_slice(&s[..5], &s[5..7]).unwrap());
/// }
/// ```
///
/// Non-adjacent byte slices:
///
/// ```rust
/// # use str_concat::{concat_slice, Error};
/// let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
/// unsafe {
///     // SAFETY: slices from the same bytes originally.
///     assert_eq!(Err(Error::NotAdjacent), concat_slice(&s[..5], &s[6..7]))
/// }
/// ```
///
pub unsafe fn concat_slice<'a, T>(a: &'a [T], b: &'a [T]) -> Result<&'a [T], Error> {
    let a_ptr = a.as_ptr();
    let b_ptr = b.as_ptr();

    let a_len = a.len();
    let b_len = b.len();

    if mem::size_of::<T>() == 0 {
        // NOTE(HeroicKatora)
        // Never consider ZST slices adjacent through this function. You could
        // infinitely duplicate a non-zero length slice by concatenating it to
        // itself as opposed to non-ZST slice types. That would just be weird.
        //
        // It is however safe.
        // See: https://github.com/rust-lang/unsafe-code-guidelines/issues/93
        // and https://github.com/rust-lang/unsafe-code-guidelines/issues/168
        // Issue: https://github.com/oberien/str-concat/issues/5
        return Err(Error::NotAdjacent)
    }

    // `max_len <= isize::max_value()`
    let max_len = isize::max_value() as usize / mem::size_of::<T>();

    // These should be guaranteed for the slices.
    assert!(a_len <= max_len as usize);
    assert!(b_len <= max_len as usize);

    // https://doc.rust-lang.org/std/primitive.pointer.html#safety-1
    // * starting pointer in-bounds obviously
    // * ending pointer one byte past the end of an allocated object
    // * explicit isize overflow check above
    // * no wraparound required
    // why: this is the one byte past the end pointer for the input slice `a`
    if a_ptr.offset(a_len as isize) != b_ptr {
        return Err(Error::NotAdjacent);
    }
    // UNWRAP: both smaller than isize, can't wrap in usize.
    // This is because in rust `usize` and `isize` are both guaranteed to have
    // the same number of bits as a pointer [1]. As `isize` is signed, a `usize`
    // can always store the sum of two positive `isize`.
    // [1]: https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types
    let new_len = a_len.checked_add(b_len).unwrap();
    // Ensure the length is bounded. The bound is strict from the definition of `max_len`
    // `new_len <= max_len` <=> `new_len * mem::size_of::<T>() <= isize::max_value()`
    if !(new_len <= max_len) {
        return Err(Error::TooLong);
    }
    // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety
    // * slices are adjacent (checked above)
    // * no double-free / leak because we work on borrowed data
    // * no use-after-free because `a` and `b` have same lifetime
    // * the total size is smaller than `isize::MAX` bytes, as max_len is rounded down
    Ok(slice::from_raw_parts(a_ptr, new_len))
}

/// Concatenate two adjacent string slices no matter their order.
///
/// This is the same as [`concat`] except that it also concatenates
/// `b` to `a` if `b` is in front of `a` (in which case [`concat`] errors).
///
/// # Safety
///
/// The provided slices must come from the same underlying allocation. The adjacency test can not
/// reliably differentiate between the one-past-the-end pointer of one allocation and the start of
/// another. However, all slices must be within a single allocation.
///
/// # Examples
///
/// Reversed order:
///
/// ```rust
/// # use str_concat::concat_unordered;
/// let s = "0123456789";
/// unsafe {
///     // SAFETY: slices from the same str originally.
///     assert_eq!("0123456", concat_unordered(&s[5..7], &s[..5]).unwrap());
/// }
/// ```
///
/// Normal order:
///
/// ```rust
/// # use str_concat::{concat_unordered, Error};
/// let s = "0123456789";
/// unsafe {
///     // SAFETY: slices from the same str originally.
///     assert_eq!("0123456", concat_unordered(&s[..5], &s[5..7]).unwrap())
/// }
/// ```
///
/// [`concat`]: fn.concat.html
pub unsafe fn concat_unordered<'a>(a: &'a str, b: &'a str) -> Result<&'a str, Error> {
    // add lengths to handle empty-string cases correctly
    let a_ptr = a.as_bytes().as_ptr() as usize;
    let a_end_ptr = a_ptr + a.len();
    let b_ptr = b.as_bytes().as_ptr() as usize;

    // make the order of `a` and `b` not matter
    let (a, b) = if a_ptr <= b_ptr && a_end_ptr <= b_ptr {
        (a, b)
    } else {
        (b, a)
    };

    concat(a, b)
}

/// Concatenate two adjacent slices no matter their order.
///
/// This is the same as [`concat_slice`] except that it also concatenates `b` to
/// `a` if `b` is in front of `a` (in which case of [`concat_slice`] errors).
/// Keep in mind that slices of zero-sized types (ZST) will still not be concatenated.
///
/// # Safety
///
/// The provided slices must come from the same underlying allocation. The adjacency test can not
/// reliably differentiate between the one-past-the-end pointer of one allocation and the start of
/// another. However, all slices must be within a single allocation.
///
/// # Examples
///
/// Reversed order:
///
/// ```rust
/// # use str_concat::concat_slice_unordered;
/// let s = [0, 1, 2, 3, 4, 5, 6];
/// unsafe {
///     // SAFETY: slices from the same bytes originally.
///     assert_eq!(
///         [0, 1, 2, 3, 4, 5, 6],
///         concat_slice_unordered(&s[5..7], &s[..5]).unwrap());
/// }
/// ```
///
/// Normal order:
///
/// ```rust
/// # use str_concat::{concat_slice_unordered, Error};
/// let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
/// unsafe {
///     // SAFETY: slices from the same bytes originally.
///     assert_eq!(
///         [0, 1, 2, 3, 4, 5, 6],
///         concat_slice_unordered(&s[..5], &s[5..7]).unwrap())
/// }
/// ```
///
/// [`concat_slice`]: fn.concat_slice.html
pub unsafe fn concat_slice_unordered<'a, T>(a: &'a [T], b: &'a [T]) -> Result<&'a [T], Error> {
    // add lengths to handle empty cases correctly
    let a_ptr = a.as_ptr() as usize;
    let a_end_ptr = a_ptr + a.len() * mem::size_of::<T>();
    let b_ptr = b.as_ptr() as usize;

    // make the order of `a` and `b` not matter
    let (a, b) = if a_ptr <= b_ptr && a_end_ptr <= b_ptr {
        (a, b)
    } else {
        (b, a)
    };

    concat_slice(a, b)
}

#[cfg(test)]
mod tests {
    use super::{concat, concat_unordered, concat_slice, concat_slice_unordered, Error};

    #[test]
    fn simple_success() {
        let s = "0123456789";
        unsafe {
            assert_eq!(Ok("0123456"), concat(&s[..5], &s[5..7]));
            assert_eq!(Ok("0123456"), concat_unordered(&s[..5], &s[5..7]));
        }
    }

    #[test]
    fn unordered() {
        let s = "0123456789";
        unsafe {
            assert_eq!(Err(Error::NotAdjacent), concat(&s[5..7], &s[..5]));
            assert_eq!(Ok("0123456"), concat_unordered(&s[5..7], &s[..5]));
        }
    }

    #[test]
    fn simple_fail() {
        let s = "0123456789";
        unsafe {
            assert_eq!(Err(Error::NotAdjacent), concat(&s[..5], &s[6..7]))
        }
    }

    #[test]
    fn zero_width_joiner() {
        let s = "0\u{200d}1";
        unsafe {
            assert_eq!(Ok("0\u{200d}1"), concat(&s[..1], &s[1..5]));
        }
    }

    #[test]
    fn zero_width_joiner_combining_grave() {
        let s = "0\u{200d}̀1";
        unsafe {
            assert_eq!(Ok("0\u{200d}\u{300}1"), concat(&s[..1], &s[1..7]));
        }
    }

    #[test]
    fn bom() {
        let s = "0\u{FEFF}1";
        unsafe {
            assert_eq!(Ok("0\u{FEFF}1"), concat(&s[..1], &s[1..5]));
        }
    }

    #[test]
    fn empty_str() {
        let s = "0123";
        unsafe {
            assert_eq!(Ok("0123"), concat(&s[..0], s));
            assert_eq!(Ok("0123"), concat_unordered(&s[..0], s));
            assert_eq!(Ok("0123"), concat_unordered(s, &s[..0]));
            assert_eq!(Ok("0123"), concat(s, &s[4..]));
            assert_eq!(Ok("0123"), concat_unordered(s, &s[4..]));
            assert_eq!(Ok("0123"), concat_unordered(&s[4..], s));
        }
    }

    #[test]
    fn typed_slices() {
        #[derive(Debug, PartialEq)]
        struct T(usize);

        let s: &[T] = &[T(0), T(1), T(2), T(3)][..];
        unsafe {
            assert_eq!(Ok(s), concat_slice(&s[..2], &s[2..]));
            assert_eq!(Ok(s), concat_slice_unordered(&s[..2], &s[2..]));
            assert_eq!(Ok(s), concat_slice_unordered(&s[2..], &s[..2]));

            // One slice empty
            assert_eq!(Ok(s), concat_slice(&s[..0], s));
            assert_eq!(Ok(s), concat_slice_unordered(&s[..0], s));
            assert_eq!(Ok(s), concat_slice_unordered(s, &s[..0]));
            assert_eq!(Ok(s), concat_slice(s, &s[4..]));
            assert_eq!(Ok(s), concat_slice_unordered(s, &s[4..]));
            assert_eq!(Ok(s), concat_slice_unordered(&s[4..], s));
        }
    }

    #[test]
    fn typed_fail() {
        #[derive(Debug, PartialEq)]
        struct T(usize);

        let s: &[T] = &[T(0), T(1), T(2), T(3)][..];
        unsafe {
            assert_eq!(Err(Error::NotAdjacent), concat_slice(&s[..1], &s[2..]));
            assert_eq!(Err(Error::NotAdjacent), concat_slice_unordered(&s[..1], &s[2..]));
            assert_eq!(Err(Error::NotAdjacent), concat_slice(&s[2..], &s[..2]));
        }
    }

    #[test]
    fn zst_fail() {
        #[derive(Clone, Copy, Debug, PartialEq)]
        struct Zst;

        let s: &[Zst] = &[Zst; 4];
        unsafe {
            assert_eq!(Err(Error::NotAdjacent), concat_slice(&s[..1], &s[1..]));
            assert_eq!(Err(Error::NotAdjacent), concat_slice_unordered(&s[..1], &s[1..]));
        }
    }
}