From 0ff50e3291bf07cda3d9b622c4387227c67622a0 Mon Sep 17 00:00:00 2001 From: David Hall Date: Wed, 8 May 2019 09:23:04 -0600 Subject: [PATCH] Added CopyTo extension that specifies a start offset. --- Core/Extensions/InteropExtensions.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Core/Extensions/InteropExtensions.cs b/Core/Extensions/InteropExtensions.cs index 01fa06f9..64cc3ccf 100644 --- a/Core/Extensions/InteropExtensions.cs +++ b/Core/Extensions/InteropExtensions.cs @@ -15,12 +15,23 @@ namespace Vanara.Extensions /// The allocated memory pointer. /// The allocated memory pointer to copy to. /// The number of bytes to copy from to . - public static unsafe void CopyTo(this IntPtr ptr, IntPtr dest, long length) + public static void CopyTo(this IntPtr ptr, IntPtr dest, long length) => CopyTo(ptr, 0L, dest, length); + + /// Copies the number of specified bytes from one unmanaged memory block to another. + /// The allocated memory pointer. + /// The offset from at which to start the copying. + /// The allocated memory pointer to copy to. + /// The number of bytes to copy from to . + public static void CopyTo(this IntPtr source, long start, IntPtr dest, long length) { - var psrc = (byte*)ptr; - var pdest = (byte*)dest; - for (var i = 0; i < length; i++, psrc++, pdest++) - *pdest = *psrc; + if (start < 0 || length < 0) throw new ArgumentOutOfRangeException(); + if (source == IntPtr.Zero || dest == IntPtr.Zero) throw new ArgumentNullException(); + unsafe + { + byte* psrc = (byte*)source + start, pdest = (byte*)dest; + for (long i = 0; i < length; i++, psrc++, pdest++) + *pdest = *psrc; + } } ///