コード例 #1
0
        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 span in buffer)
                {
                    if (!AsciiUtilities.TryGetAsciiString((byte *)span.UnsafePointer, output + offset, span.Length))
                    {
                        throw new InvalidOperationException();
                    }

                    offset += span.Length;
                }
            }

            return(asciiString);
        }
コード例 #2
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);
        }