/// <summary> /// Initializes a new instance of the <see cref="NativeString"/> struct. /// </summary> /// <param name="span">The span to copy.</param> /// <param name="allocator">The allocator.</param> public NativeString(ReadOnlySpan <char> span, Allocator allocator) { if (span.IsEmpty) { this = default; return; } char *src = (char *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span)); this = new NativeString(src, span.Length, allocator); }
/// <summary> /// Initializes a new instance of the <see cref="NativeString"/> struct. /// </summary> /// <param name="str">The string to copy.</param> /// <param name="allocator">The allocator.</param> public NativeString(string str, Allocator allocator) { if (string.IsNullOrEmpty(str)) { this = default; return; } fixed(char *p = str) { int length = str.Length; this = new NativeString(p, length, allocator); } }
private NativeString(ref NativeString str) { if (!str.IsValid) { throw new ArgumentException("string is invalid"); } Allocator allocator = str.GetAllocator() !; char * buffer = allocator.Allocate <char>(str._length); Unsafe.CopyBlockUnaligned(buffer, str._buffer, (uint)(sizeof(char) * str._length)); _buffer = buffer; _length = str._length; _allocatorID = str._allocatorID; }