| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | namespace System.Buffers.Binary |
| | | 8 | | { |
| | | 9 | | public static partial class BinaryPrimitives |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Writes an Int16 into a span of bytes as little endian. |
| | | 13 | | /// </summary> |
| | | 14 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 15 | | public static void WriteInt16LittleEndian(Span<byte> buffer, short value) |
| | 1 | 16 | | { |
| | 1 | 17 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 18 | | { |
| | 0 | 19 | | value = ReverseEndianness(value); |
| | 0 | 20 | | } |
| | 1 | 21 | | WriteMachineEndian(buffer, ref value); |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Writes an Int32 into a span of bytes as little endian. |
| | | 26 | | /// </summary> |
| | | 27 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 28 | | public static void WriteInt32LittleEndian(Span<byte> buffer, int value) |
| | 1 | 29 | | { |
| | 1 | 30 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 31 | | { |
| | 0 | 32 | | value = ReverseEndianness(value); |
| | 0 | 33 | | } |
| | 1 | 34 | | WriteMachineEndian(buffer, ref value); |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Writes an Int64 into a span of bytes as little endian. |
| | | 39 | | /// </summary> |
| | | 40 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 41 | | public static void WriteInt64LittleEndian(Span<byte> buffer, long value) |
| | 1 | 42 | | { |
| | 1 | 43 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 44 | | { |
| | 0 | 45 | | value = ReverseEndianness(value); |
| | 0 | 46 | | } |
| | 1 | 47 | | WriteMachineEndian(buffer, ref value); |
| | 1 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Write a UInt16 into a span of bytes as little endian. |
| | | 52 | | /// </summary> |
| | | 53 | | [CLSCompliant(false)] |
| | | 54 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 55 | | public static void WriteUInt16LittleEndian(Span<byte> buffer, ushort value) |
| | 1 | 56 | | { |
| | 1 | 57 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 58 | | { |
| | 0 | 59 | | value = ReverseEndianness(value); |
| | 0 | 60 | | } |
| | 1 | 61 | | WriteMachineEndian(buffer, ref value); |
| | 1 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Write a UInt32 into a span of bytes as little endian. |
| | | 66 | | /// </summary> |
| | | 67 | | [CLSCompliant(false)] |
| | | 68 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 69 | | public static void WriteUInt32LittleEndian(Span<byte> buffer, uint value) |
| | 1 | 70 | | { |
| | 1 | 71 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 72 | | { |
| | 0 | 73 | | value = ReverseEndianness(value); |
| | 0 | 74 | | } |
| | 1 | 75 | | WriteMachineEndian(buffer, ref value); |
| | 1 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Write a UInt64 into a span of bytes as little endian. |
| | | 80 | | /// </summary> |
| | | 81 | | [CLSCompliant(false)] |
| | | 82 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 83 | | public static void WriteUInt64LittleEndian(Span<byte> buffer, ulong value) |
| | 1 | 84 | | { |
| | 1 | 85 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 86 | | { |
| | 0 | 87 | | value = ReverseEndianness(value); |
| | 0 | 88 | | } |
| | 1 | 89 | | WriteMachineEndian(buffer, ref value); |
| | 1 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Writes an Int16 into a span of bytes as little endian. |
| | | 94 | | /// <returns>If the span is too small to contain the value, return false.</returns> |
| | | 95 | | /// </summary> |
| | | 96 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 97 | | public static bool TryWriteInt16LittleEndian(Span<byte> buffer, short value) |
| | 1 | 98 | | { |
| | 1 | 99 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 100 | | { |
| | 0 | 101 | | value = ReverseEndianness(value); |
| | 0 | 102 | | } |
| | 1 | 103 | | return TryWriteMachineEndian(buffer, ref value); |
| | 1 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Writes an Int32 into a span of bytes as little endian. |
| | | 108 | | /// <returns>If the span is too small to contain the value, return false.</returns> |
| | | 109 | | /// </summary> |
| | | 110 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 111 | | public static bool TryWriteInt32LittleEndian(Span<byte> buffer, int value) |
| | 1 | 112 | | { |
| | 1 | 113 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 114 | | { |
| | 0 | 115 | | value = ReverseEndianness(value); |
| | 0 | 116 | | } |
| | 1 | 117 | | return TryWriteMachineEndian(buffer, ref value); |
| | 1 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Writes an Int64 into a span of bytes as little endian. |
| | | 122 | | /// <returns>If the span is too small to contain the value, return false.</returns> |
| | | 123 | | /// </summary> |
| | | 124 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 125 | | public static bool TryWriteInt64LittleEndian(Span<byte> buffer, long value) |
| | 1 | 126 | | { |
| | 1 | 127 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 128 | | { |
| | 0 | 129 | | value = ReverseEndianness(value); |
| | 0 | 130 | | } |
| | 1 | 131 | | return TryWriteMachineEndian(buffer, ref value); |
| | 1 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Write a UInt16 into a span of bytes as little endian. |
| | | 136 | | /// <returns>If the span is too small to contain the value, return false.</returns> |
| | | 137 | | /// </summary> |
| | | 138 | | [CLSCompliant(false)] |
| | | 139 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 140 | | public static bool TryWriteUInt16LittleEndian(Span<byte> buffer, ushort value) |
| | 1 | 141 | | { |
| | 1 | 142 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 143 | | { |
| | 0 | 144 | | value = ReverseEndianness(value); |
| | 0 | 145 | | } |
| | 1 | 146 | | return TryWriteMachineEndian(buffer, ref value); |
| | 1 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Write a UInt32 into a span of bytes as little endian. |
| | | 151 | | /// <returns>If the span is too small to contain the value, return false.</returns> |
| | | 152 | | /// </summary> |
| | | 153 | | [CLSCompliant(false)] |
| | | 154 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 155 | | public static bool TryWriteUInt32LittleEndian(Span<byte> buffer, uint value) |
| | 1 | 156 | | { |
| | 1 | 157 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 158 | | { |
| | 0 | 159 | | value = ReverseEndianness(value); |
| | 0 | 160 | | } |
| | 1 | 161 | | return TryWriteMachineEndian(buffer, ref value); |
| | 1 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Write a UInt64 into a span of bytes as little endian. |
| | | 166 | | /// <returns>If the span is too small to contain the value, return false.</returns> |
| | | 167 | | /// </summary> |
| | | 168 | | [CLSCompliant(false)] |
| | | 169 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 170 | | public static bool TryWriteUInt64LittleEndian(Span<byte> buffer, ulong value) |
| | 1 | 171 | | { |
| | 1 | 172 | | if (!BitConverter.IsLittleEndian) |
| | 0 | 173 | | { |
| | 0 | 174 | | value = ReverseEndianness(value); |
| | 0 | 175 | | } |
| | 1 | 176 | | return TryWriteMachineEndian(buffer, ref value); |
| | 1 | 177 | | } |
| | | 178 | | } |
| | | 179 | | } |