unsafe void AddTag([NotNull] string key, [NotNull] string value) { // Optimization - avoid allocating on the heap Span <byte> keyBytes = stackalloc byte[Encoding.ASCII.GetMaxByteCount(key.Length) + 1]; #if NETSTANDARD2_0 fixed(char *keyAddress = key) Encoding.ASCII.GetBytes( keyAddress, key.Length, (byte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(keyBytes)), keyBytes.Length); #else Encoding.ASCII.GetBytes(key, keyBytes); #endif // Use heap allocations for comments > 256kB (usually pictures) var valueMaxByteCount = Encoding.UTF8.GetMaxByteCount(value.Length) + 1; var valueBytes = valueMaxByteCount < 0x40000 ? stackalloc byte[valueMaxByteCount] : new byte[valueMaxByteCount]; #if NETSTANDARD2_0 fixed(char *valueAddress = value) fixed(byte *valueBytesAddress = valueBytes) Encoding.UTF8.GetBytes(valueAddress, value.Length, valueBytesAddress, valueMaxByteCount); #else Encoding.ASCII.GetBytes(value, valueBytes); #endif fixed(byte *valueBytesAddress = valueBytes) SafeNativeMethods.VorbisCommentAddTag(_comment, ref MemoryMarshal.GetReference(keyBytes), valueBytesAddress); _unmanagedMemoryAllocated = true; }
unsafe void AddTag([NotNull] string key, ReadOnlySpan <byte> value) { // Optimization - avoid allocating on the heap Span <byte> keyBytes = stackalloc byte[Encoding.ASCII.GetMaxByteCount(key.Length) + 1]; #if NETSTANDARD2_0 fixed(char *keyAddress = key) Encoding.ASCII.GetBytes( keyAddress, key.Length, (byte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(keyBytes)), keyBytes.Length); #else Encoding.ASCII.GetBytes(key, keyBytes); #endif fixed(byte *valueAddress = value) SafeNativeMethods.VorbisCommentAddTag(_comment, ref MemoryMarshal.GetReference(keyBytes), valueAddress); _unmanagedMemoryAllocated = true; }
unsafe void AddTag(string key, string value) { // Optimization - avoid allocating on the heap Span <byte> keyBytes = stackalloc byte[Encoding.ASCII.GetMaxByteCount(key.Length) + 1]; #if NETSTANDARD2_0 fixed(char *keyAddress = key) Encoding.ASCII.GetBytes( keyAddress, key.Length, (byte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(keyBytes)), keyBytes.Length); #else var keyLength = Encoding.ASCII.GetBytes(key, keyBytes); #endif // Use heap allocations for comments > 256kB var valueMaxByteCount = Encoding.UTF8.GetMaxByteCount(value.Length) + 1; var valueBytes = valueMaxByteCount < 0x40000 ? stackalloc byte[valueMaxByteCount] : new byte[valueMaxByteCount]; #if NETSTANDARD2_0 fixed(char *valueAddress = value) fixed(byte *valueBytesAddress = valueBytes) Encoding.UTF8.GetBytes(valueAddress, value.Length, valueBytesAddress, valueMaxByteCount); #else var valueLength = Encoding.UTF8.GetBytes(value, valueBytes); // Since SkipLocalsInit is set, make sure the strings are null-terminated keyBytes[keyLength] = 0; valueBytes[valueLength] = 0; #endif fixed(byte *valueBytesAddress = valueBytes) SafeNativeMethods.VorbisCommentAddTag(_comment, ref MemoryMarshal.GetReference(keyBytes), valueBytesAddress); _unmanagedMemoryAllocated = true; }