public override string ToString() { if (TryGetChars(out var chars)) { if (chars.IsSingleSegment) { if (MemoryMarshal.TryGetString(chars.First, out var s, out var start, out var length)) { return(s.Substring(start, length)); } return(chars.First.Span.CreateString()); } } if (TryGetBytes(out var bytes)) { if (bytes.IsSingleSegment) { return(UTF8.GetString(bytes.First.Span)); } } switch (_state.Storage) { case StorageKind.Empty: case StorageKind.Null: return(""); case StorageKind.InlinedBytes: return(_state.InlinedBytesToString(UTF8)); case StorageKind.InlinedInt64: return(_state.Int64.ToString(NumberFormatInfo.InvariantInfo)); case StorageKind.InlinedUInt32: return(_state.UInt32.ToString(NumberFormatInfo.InvariantInfo)); case StorageKind.InlinedDouble: var r64 = _state.Double; if (double.IsInfinity(r64)) { if (double.IsPositiveInfinity(r64)) { return("+inf"); } if (double.IsNegativeInfinity(r64)) { return("-inf"); } } return(r64.ToString("G17", NumberFormatInfo.InvariantInfo)); default: ThrowHelper.StorageKindNotImplemented(_state.Storage); return(default !); } }
/// <summary> /// Initializes a new instance of the <see cref="ReadOnlyMemory2D{T}"/> struct. /// </summary> /// <param name="memory">The target <see cref="ReadOnlyMemory{T}"/> to wrap.</param> /// <param name="offset">The initial offset within <paramref name="memory"/>.</param> /// <param name="height">The height of the resulting 2D area.</param> /// <param name="width">The width of each row in the resulting 2D area.</param> /// <param name="pitch">The pitch in the resulting 2D area.</param> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown when one of the input parameters is out of range. /// </exception> /// <exception cref="ArgumentException"> /// Thrown when the requested area is outside of bounds for <paramref name="memory"/>. /// </exception> internal ReadOnlyMemory2D(ReadOnlyMemory <T> memory, int offset, int height, int width, int pitch) { if ((uint)offset > (uint)memory.Length) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForOffset(); } if (height < 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight(); } if (width < 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth(); } if (pitch < 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch(); } if (width == 0 || height == 0) { this = default; return; } int area = OverflowHelper.ComputeInt32Area(height, width, pitch), remaining = memory.Length - offset; if (area > remaining) { ThrowHelper.ThrowArgumentException(); } // Check the supported underlying objects, like in Memory2D<T> if (typeof(T) == typeof(char) && MemoryMarshal.TryGetString(Unsafe.As <ReadOnlyMemory <T>, ReadOnlyMemory <char> >(ref memory), out string?text, out int textStart, out _)) { ref char r0 = ref text.DangerousGetReferenceAt(textStart + offset); this.instance = text; this.offset = text.DangerousGetObjectDataByteOffset(ref r0); }
public static string ConvertToString(this ReadOnlyMemory <char> memory) { if (memory.IsEmpty) { return(string.Empty); } if (MemoryMarshal.TryGetString(memory, out var text, out var start, out var length)) { // If the memory is only a part of the string we had to substring anyway. if (start == 0 && length == text.Length) { return(text); } } var result = new string('\0', memory.Length); var resultAsMemory = MemoryMarshal.AsMemory(result.AsMemory()); memory.CopyTo(resultAsMemory); return(result); }