1ffe3c632Sopenharmony_ci#region Copyright notice and license
2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
3ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc.  All rights reserved.
4ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
5ffe3c632Sopenharmony_ci//
6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
8ffe3c632Sopenharmony_ci// met:
9ffe3c632Sopenharmony_ci//
10ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
12ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
15ffe3c632Sopenharmony_ci// distribution.
16ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
18ffe3c632Sopenharmony_ci// this software without specific prior written permission.
19ffe3c632Sopenharmony_ci//
20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31ffe3c632Sopenharmony_ci#endregion
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ciusing System;
34ffe3c632Sopenharmony_ciusing System.Buffers;
35ffe3c632Sopenharmony_ciusing System.Collections.Generic;
36ffe3c632Sopenharmony_ciusing System.Linq;
37ffe3c632Sopenharmony_ciusing System.Text;
38ffe3c632Sopenharmony_ciusing System.Threading.Tasks;
39ffe3c632Sopenharmony_ci
40ffe3c632Sopenharmony_cinamespace Google.Protobuf
41ffe3c632Sopenharmony_ci{
42ffe3c632Sopenharmony_ci    internal static class ReadOnlySequenceFactory
43ffe3c632Sopenharmony_ci    {
44ffe3c632Sopenharmony_ci        /// <summary>
45ffe3c632Sopenharmony_ci        /// Create a sequence from the specified data. The data will be divided up into segments in the sequence.
46ffe3c632Sopenharmony_ci        /// </summary>
47ffe3c632Sopenharmony_ci        public static ReadOnlySequence<byte> CreateWithContent(byte[] data, int segmentSize = 1, bool addEmptySegmentDelimiters = true)
48ffe3c632Sopenharmony_ci        {
49ffe3c632Sopenharmony_ci            var segments = new List<byte[]>();
50ffe3c632Sopenharmony_ci
51ffe3c632Sopenharmony_ci            if (addEmptySegmentDelimiters)
52ffe3c632Sopenharmony_ci            {
53ffe3c632Sopenharmony_ci                segments.Add(new byte[0]);
54ffe3c632Sopenharmony_ci            }
55ffe3c632Sopenharmony_ci
56ffe3c632Sopenharmony_ci            var currentIndex = 0;
57ffe3c632Sopenharmony_ci            while (currentIndex < data.Length)
58ffe3c632Sopenharmony_ci            {
59ffe3c632Sopenharmony_ci                var segment = new List<byte>();
60ffe3c632Sopenharmony_ci                while (segment.Count < segmentSize && currentIndex < data.Length)
61ffe3c632Sopenharmony_ci                {
62ffe3c632Sopenharmony_ci                    segment.Add(data[currentIndex++]);
63ffe3c632Sopenharmony_ci                }
64ffe3c632Sopenharmony_ci                segments.Add(segment.ToArray());
65ffe3c632Sopenharmony_ci
66ffe3c632Sopenharmony_ci                if (addEmptySegmentDelimiters)
67ffe3c632Sopenharmony_ci                {
68ffe3c632Sopenharmony_ci                    segments.Add(new byte[0]);
69ffe3c632Sopenharmony_ci                }
70ffe3c632Sopenharmony_ci            }
71ffe3c632Sopenharmony_ci
72ffe3c632Sopenharmony_ci            return CreateSegments(segments.ToArray());
73ffe3c632Sopenharmony_ci        }
74ffe3c632Sopenharmony_ci
75ffe3c632Sopenharmony_ci        /// <summary>
76ffe3c632Sopenharmony_ci        /// Originally from corefx, and has been contributed to Protobuf
77ffe3c632Sopenharmony_ci        /// https://github.com/dotnet/corefx/blob/e99ec129cfd594d53f4390bf97d1d736cff6f860/src/System.Memory/tests/ReadOnlyBuffer/ReadOnlySequenceFactory.byte.cs
78ffe3c632Sopenharmony_ci        /// </summary>
79ffe3c632Sopenharmony_ci        private static ReadOnlySequence<byte> CreateSegments(params byte[][] inputs)
80ffe3c632Sopenharmony_ci        {
81ffe3c632Sopenharmony_ci            if (inputs == null || inputs.Length == 0)
82ffe3c632Sopenharmony_ci            {
83ffe3c632Sopenharmony_ci                throw new InvalidOperationException();
84ffe3c632Sopenharmony_ci            }
85ffe3c632Sopenharmony_ci
86ffe3c632Sopenharmony_ci            int i = 0;
87ffe3c632Sopenharmony_ci
88ffe3c632Sopenharmony_ci            BufferSegment last = null;
89ffe3c632Sopenharmony_ci            BufferSegment first = null;
90ffe3c632Sopenharmony_ci
91ffe3c632Sopenharmony_ci            do
92ffe3c632Sopenharmony_ci            {
93ffe3c632Sopenharmony_ci                byte[] s = inputs[i];
94ffe3c632Sopenharmony_ci                int length = s.Length;
95ffe3c632Sopenharmony_ci                int dataOffset = length;
96ffe3c632Sopenharmony_ci                var chars = new byte[length * 2];
97ffe3c632Sopenharmony_ci
98ffe3c632Sopenharmony_ci                for (int j = 0; j < length; j++)
99ffe3c632Sopenharmony_ci                {
100ffe3c632Sopenharmony_ci                    chars[dataOffset + j] = s[j];
101ffe3c632Sopenharmony_ci                }
102ffe3c632Sopenharmony_ci
103ffe3c632Sopenharmony_ci                // Create a segment that has offset relative to the OwnedMemory and OwnedMemory itself has offset relative to array
104ffe3c632Sopenharmony_ci                var memory = new Memory<byte>(chars).Slice(length, length);
105ffe3c632Sopenharmony_ci
106ffe3c632Sopenharmony_ci                if (first == null)
107ffe3c632Sopenharmony_ci                {
108ffe3c632Sopenharmony_ci                    first = new BufferSegment(memory);
109ffe3c632Sopenharmony_ci                    last = first;
110ffe3c632Sopenharmony_ci                }
111ffe3c632Sopenharmony_ci                else
112ffe3c632Sopenharmony_ci                {
113ffe3c632Sopenharmony_ci                    last = last.Append(memory);
114ffe3c632Sopenharmony_ci                }
115ffe3c632Sopenharmony_ci                i++;
116ffe3c632Sopenharmony_ci            } while (i < inputs.Length);
117ffe3c632Sopenharmony_ci
118ffe3c632Sopenharmony_ci            return new ReadOnlySequence<byte>(first, 0, last, last.Memory.Length);
119ffe3c632Sopenharmony_ci        }
120ffe3c632Sopenharmony_ci
121ffe3c632Sopenharmony_ci        private class BufferSegment : ReadOnlySequenceSegment<byte>
122ffe3c632Sopenharmony_ci        {
123ffe3c632Sopenharmony_ci            public BufferSegment(Memory<byte> memory)
124ffe3c632Sopenharmony_ci            {
125ffe3c632Sopenharmony_ci                Memory = memory;
126ffe3c632Sopenharmony_ci            }
127ffe3c632Sopenharmony_ci
128ffe3c632Sopenharmony_ci            public BufferSegment Append(Memory<byte> memory)
129ffe3c632Sopenharmony_ci            {
130ffe3c632Sopenharmony_ci                var segment = new BufferSegment(memory)
131ffe3c632Sopenharmony_ci                {
132ffe3c632Sopenharmony_ci                    RunningIndex = RunningIndex + Memory.Length
133ffe3c632Sopenharmony_ci                };
134ffe3c632Sopenharmony_ci                Next = segment;
135ffe3c632Sopenharmony_ci                return segment;
136ffe3c632Sopenharmony_ci            }
137ffe3c632Sopenharmony_ci        }
138ffe3c632Sopenharmony_ci    }
139ffe3c632Sopenharmony_ci}