Exemple #1
0
        /// <summary>
        /// Decodes the ASCII encoded bytes in the <see cref="ReadOnlyBuffer"/> into a <see cref="string"/>
        /// </summary>
        /// <param name="buffer">The buffer to decode</param>
        public unsafe static string GetAsciiString(this ReadOnlyBuffer buffer)
        {
            if (buffer.IsEmpty)
            {
                return(null);
            }

            var asciiString = new string('\0', (int)Math.Min(int.MaxValue, buffer.Length));

            fixed(char *outputStart = asciiString)
            {
                int offset = 0;
                var output = outputStart;

                foreach (var memory in buffer)
                {
                    fixed(byte *source = &MemoryMarshal.GetReference(memory.Span))
                    {
                        if (!AsciiUtilities.TryGetAsciiString(source, output + offset, memory.Length))
                        {
                            ThrowInvalidOperation();
                        }
                    }

                    offset += memory.Length;
                }
            }

            return(asciiString);
        }
        /// <summary>
        /// Decodes the ASCII encoded bytes in the <see cref="ReadableBuffer"/> into a <see cref="string"/>
        /// </summary>
        /// <param name="buffer">The buffer to decode</param>
        public unsafe static string GetAsciiString(this ReadableBuffer buffer)
        {
            if (buffer.IsEmpty)
            {
                return(null);
            }

            var asciiString = new string('\0', buffer.Length);

            fixed(char *outputStart = asciiString)
            {
                int offset = 0;
                var output = outputStart;

                foreach (var memory in buffer)
                {
                    fixed(byte *source = &memory.Span.DangerousGetPinnableReference())
                    {
                        if (!AsciiUtilities.TryGetAsciiString(source, output + offset, memory.Length))
                        {
                            ThrowInvalidOperation();
                        }
                    }

                    offset += memory.Length;
                }
            }

            return(asciiString);
        }
Exemple #3
0
        /// <summary>
        /// Decodes the ASCII encoded bytes in the <see cref="ReadableBuffer"/> into a <see cref="string"/>
        /// </summary>
        /// <param name="buffer">The buffer to decode</param>
        public unsafe static string GetAsciiString(this ReadableBuffer buffer)
        {
            if (buffer.IsEmpty)
            {
                return(null);
            }

            var asciiString = new string('\0', buffer.Length);

            fixed(char *outputStart = asciiString)
            {
                int offset = 0;
                var output = outputStart;

                foreach (var memory in buffer)
                {
                    void *pointer;
                    if (memory.TryGetPointer(out pointer))
                    {
                        if (!AsciiUtilities.TryGetAsciiString((byte *)pointer, output + offset, memory.Length))
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    else
                    {
                        ArraySegment <byte> data;
                        if (memory.TryGetArray(out data))
                        {
                            fixed(byte *ptr = &data.Array[0])
                            {
                                if (!AsciiUtilities.TryGetAsciiString(ptr + data.Offset, output + offset, memory.Length))
                                {
                                    throw new InvalidOperationException();
                                }
                            }
                        }
                    }

                    offset += memory.Length;
                }
            }

            return(asciiString);
        }
Exemple #4
0
        public unsafe static string GetAsciiString(this Span <byte> span)
        {
            var len = span.Length;

            if (len == 0)
            {
                return(null);
            }

            var asciiString = new string('\0', len);

            fixed(char *destination = asciiString)
            fixed(byte *source = &MemoryMarshal.GetReference(span))
            {
                if (!AsciiUtilities.TryGetAsciiString(source, destination, len))
                {
                    ThrowInvalidOperation();
                }
            }

            return(asciiString);
        }
Exemple #5
0
        public unsafe static string GetAsciiString(this Span <byte> span)
        {
            if (span.IsEmpty)
            {
                return(null);
            }

            var asciiString = new string('\0', span.Length);

            fixed(char *outputStart = asciiString)
            {
                var output = outputStart;

                fixed(byte *buffer = &span.DangerousGetPinnableReference())
                {
                    if (!AsciiUtilities.TryGetAsciiString(buffer, output, span.Length))
                    {
                        throw new InvalidOperationException();
                    }
                }
            }

            return(asciiString);
        }