IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
MemoryMarshalExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Runtime.InteropServices;
3
5{
6
8 {
9
16 public static unsafe int GetIndexOfNull(byte* ptr, int max = int.MaxValue)
17 {
18 if (ptr is null)
19 throw new ArgumentNullException(nameof(ptr));
20 if (max < 0)
21 throw new ArgumentOutOfRangeException(nameof(max));
22
23 for (int i = 0; i < max; i++)
24 if (ptr[i] == 0)
25 return i;
26
27 return -1;
28 }
29
35 public static unsafe ReadOnlySpan<byte> CreateReadOnlySpanFromNullTerminated(byte* b)
36 {
37#if NETFRAMEWORK
38 return new ReadOnlySpan<byte>(b, GetIndexOfNull(b));
39#else
40 return MemoryMarshal.CreateReadOnlySpanFromNullTerminated(b);
41#endif
42 }
43
44 }
45
46}
static unsafe ReadOnlySpan< byte > CreateReadOnlySpanFromNullTerminated(byte *b)
Creates a ReadOnlySpan<byte> from the given pointer.
static unsafe int GetIndexOfNull(byte *ptr, int max=int.MaxValue)
Gets the length of a pointer to a C string.