/// <summary>Copy a slice into the buffer, with optional alignement, and return a new identical slice.</summary> /// <param name="data">Data to copy in the buffer</param> /// <param name="aligned">If true, align the index of first byte of the slice with a multiple of 8 bytes</param> /// <returns>Slice that is the equivalent of <paramref name="data"/>, backed by the buffer.</returns> public Slice Intern(Slice data, bool aligned = false) { if (data.Count == 0) { // transform into the corresponding Slice.Nil / Slice.Empty singleton return(data.Memoize()); } data.EnsureSliceIsValid(); // allocate the slice var slice = Allocate(data.Count, aligned); UnsafeHelpers.CopyUnsafe(slice.Array, slice.Offset, data.Array, data.Offset, data.Count); return(slice); }
/// <summary>Copy a slice into the buffer, immediately followed by a suffix, and return a new slice that is the concatenation of the two.</summary> /// <param name="data">Data to copy in the buffer</param> /// <param name="suffix">Suffix to copy immediately after <paramref name="data"/>.</param> /// <param name="aligned">If true, align the index of first byte of the slice with a multiple of 8 bytes</param> /// <returns>Slice that is the equivalent of <paramref name="data"/> plus <paramref name="suffix"/>, backed by the buffer.</returns> /// <remarks>When <paramref name="data"/> is empty, <paramref name="suffix"/> is returned without being copied to the buffer itself.</remarks> internal Slice Intern(Slice data, Slice suffix, bool aligned = false) { if (data.Count == 0) { // note: we don't memoize the suffix, because in most case, it comes from a constant, and it would be a waste to copy it other and other again... return(suffix.Count > 0 ? suffix : data.Array == null ? Slice.Nil : Slice.Empty); } data.EnsureSliceIsValid(); suffix.EnsureSliceIsValid(); var slice = Allocate(data.Count + suffix.Count, aligned); UnsafeHelpers.CopyUnsafe(slice.Array, slice.Offset, data.Array, data.Offset, data.Count); UnsafeHelpers.CopyUnsafe(slice.Array, slice.Offset + data.Count, suffix.Array, suffix.Offset, suffix.Count); return(slice); }