| | | 1 | | // <copyright file="LineReader9.cs" company="Jérémy Ansel"> |
| | | 2 | | // Copyright (c) 2017, 2019 Jérémy Ansel |
| | | 3 | | // </copyright> |
| | | 4 | | // <license> |
| | | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | | 6 | | // </license> |
| | | 7 | | |
| | | 8 | | #if NET6_0_OR_GREATER |
| | | 9 | | |
| | | 10 | | using System.Text; |
| | | 11 | | |
| | | 12 | | namespace JeremyAnsel.Media.WavefrontObj |
| | | 13 | | { |
| | | 14 | | internal class LineReader9 |
| | | 15 | | { |
| | 3 | 16 | | public static readonly char[] LineSeparators = new char[] { ' ', '\t' }; |
| | | 17 | | |
| | 3255 | 18 | | public List<string> HeaderTextLines { get; } = new List<string>(); |
| | | 19 | | |
| | 1782 | 20 | | private char[] _lineBuffer = new char[256]; |
| | | 21 | | private int _lineBufferPosition = 0; |
| | | 22 | | |
| | | 23 | | private void SetLineBufferLength(int length) |
| | | 24 | | { |
| | 7917 | 25 | | if (length <= _lineBuffer.Length) |
| | | 26 | | { |
| | 7908 | 27 | | return; |
| | | 28 | | } |
| | | 29 | | |
| | 9 | 30 | | int newLength = Math.Max(length, _lineBuffer.Length * 2); |
| | 9 | 31 | | var newBuffer = new char[newLength]; |
| | 9 | 32 | | Array.Copy(_lineBuffer, newBuffer, _lineBuffer.Length); |
| | 9 | 33 | | _lineBuffer = newBuffer; |
| | 9 | 34 | | } |
| | | 35 | | |
| | | 36 | | private void SetLineBufferSlice(ReadOnlySpan<char> line) |
| | | 37 | | { |
| | 7896 | 38 | | SetLineBufferLength(line.Length); |
| | 7896 | 39 | | line.CopyTo(_lineBuffer); |
| | 7896 | 40 | | _lineBufferPosition = line.Length; |
| | 7896 | 41 | | } |
| | | 42 | | |
| | | 43 | | private void AppendLineBufferSlice(ReadOnlySpan<char> line) |
| | | 44 | | { |
| | 21 | 45 | | SetLineBufferLength(_lineBufferPosition + line.Length); |
| | 21 | 46 | | line.CopyTo(_lineBuffer.AsSpan()[_lineBufferPosition..]); |
| | 21 | 47 | | _lineBufferPosition += line.Length; |
| | 21 | 48 | | } |
| | | 49 | | |
| | | 50 | | //private void AddLineBufferChar(char c) |
| | | 51 | | //{ |
| | | 52 | | // SetLineBufferLength(_lineBufferPosition + 1); |
| | | 53 | | // _lineBuffer[_lineBufferPosition] = c; |
| | | 54 | | // _lineBufferPosition++; |
| | | 55 | | //} |
| | | 56 | | |
| | 1782 | 57 | | private char[] _lineReadBuffer = new char[256]; |
| | | 58 | | private int _lineReadBufferPosition = 0; |
| | | 59 | | |
| | | 60 | | private void SetLineReadBufferLength(int length) |
| | | 61 | | { |
| | 46560 | 62 | | if (length <= _lineReadBuffer.Length) |
| | | 63 | | { |
| | 46542 | 64 | | return; |
| | | 65 | | } |
| | | 66 | | |
| | 18 | 67 | | int newLength = Math.Max(length, _lineReadBuffer.Length * 2); |
| | 18 | 68 | | var newBuffer = new char[newLength]; |
| | 18 | 69 | | Array.Copy(_lineReadBuffer, newBuffer, _lineReadBuffer.Length); |
| | 18 | 70 | | _lineReadBuffer = newBuffer; |
| | 18 | 71 | | } |
| | | 72 | | |
| | | 73 | | //private void SetLineReadBufferSlice(ReadOnlySpan<char> line) |
| | | 74 | | //{ |
| | | 75 | | // SetLineReadBufferLength(line.Length); |
| | | 76 | | // line.CopyTo(_lineReadBuffer); |
| | | 77 | | // _lineReadBufferPosition = line.Length; |
| | | 78 | | //} |
| | | 79 | | |
| | | 80 | | private void AddLineReadBufferChar(char c) |
| | | 81 | | { |
| | 46560 | 82 | | SetLineReadBufferLength(_lineReadBufferPosition + 1); |
| | 46560 | 83 | | _lineReadBuffer[_lineReadBufferPosition] = c; |
| | 46560 | 84 | | _lineReadBufferPosition++; |
| | 46560 | 85 | | } |
| | | 86 | | |
| | | 87 | | private void ReadLine9(StreamReader reader) |
| | | 88 | | { |
| | 4542 | 89 | | _lineReadBufferPosition = 0; |
| | | 90 | | |
| | 46560 | 91 | | while (true) |
| | | 92 | | { |
| | 51102 | 93 | | int r = reader.Read(); |
| | | 94 | | |
| | 51102 | 95 | | if (r == -1) |
| | | 96 | | { |
| | | 97 | | break; |
| | | 98 | | } |
| | | 99 | | |
| | 49566 | 100 | | char c = (char)r; |
| | | 101 | | |
| | 49566 | 102 | | if (c == '\r') |
| | | 103 | | { |
| | | 104 | | continue; |
| | | 105 | | } |
| | | 106 | | |
| | 49566 | 107 | | if (c == '\n') |
| | | 108 | | { |
| | | 109 | | break; |
| | | 110 | | } |
| | | 111 | | |
| | 46560 | 112 | | AddLineReadBufferChar(c); |
| | | 113 | | } |
| | 4542 | 114 | | } |
| | | 115 | | |
| | | 116 | | public IEnumerable<(char[] Buffer, int Length)> Read9(Stream stream) |
| | | 117 | | { |
| | 1782 | 118 | | using var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true); |
| | 1782 | 119 | | _lineBufferPosition = 0; |
| | 1782 | 120 | | bool isMultiLine = false; |
| | 1782 | 121 | | bool readHeaderText = true; |
| | | 122 | | |
| | 5391 | 123 | | while (!reader.EndOfStream) |
| | | 124 | | { |
| | 4542 | 125 | | ReadLine9(reader); |
| | | 126 | | |
| | 4542 | 127 | | if (_lineReadBufferPosition == 0 || MemoryExtensions.IsWhiteSpace(_lineReadBuffer.AsSpan()[.._lineReadBu |
| | | 128 | | { |
| | 597 | 129 | | if (readHeaderText) |
| | | 130 | | { |
| | 597 | 131 | | this.HeaderTextLines.Add(string.Empty); |
| | | 132 | | } |
| | | 133 | | |
| | 597 | 134 | | continue; |
| | | 135 | | } |
| | | 136 | | |
| | 3945 | 137 | | if (isMultiLine) |
| | | 138 | | { |
| | 21 | 139 | | SetLineBufferSlice(_lineBuffer.AsSpan()[..(_lineBufferPosition - 1)]); |
| | 21 | 140 | | AppendLineBufferSlice(_lineReadBuffer.AsSpan()[.._lineReadBufferPosition]); |
| | | 141 | | } |
| | | 142 | | else |
| | | 143 | | { |
| | 3924 | 144 | | SetLineBufferSlice(_lineReadBuffer.AsSpan()[.._lineReadBufferPosition]); |
| | | 145 | | } |
| | | 146 | | |
| | 3945 | 147 | | SetLineBufferSlice(MemoryExtensions.Trim(_lineBuffer.AsSpan()[.._lineBufferPosition])); |
| | | 148 | | |
| | 3945 | 149 | | isMultiLine = MemoryExtensions.EndsWith(_lineBuffer.AsSpan()[.._lineBufferPosition], "\\".AsSpan()); |
| | | 150 | | |
| | 3945 | 151 | | if (isMultiLine) |
| | | 152 | | { |
| | | 153 | | continue; |
| | | 154 | | } |
| | | 155 | | |
| | 3924 | 156 | | int commentIndex = MemoryExtensions.IndexOf(_lineBuffer.AsSpan()[.._lineBufferPosition], '#'); |
| | | 157 | | |
| | 3924 | 158 | | if (commentIndex == 0) |
| | | 159 | | { |
| | 33 | 160 | | if (readHeaderText) |
| | | 161 | | { |
| | 27 | 162 | | this.HeaderTextLines.Add(_lineBuffer.AsSpan()[1.._lineBufferPosition].ToString()); |
| | | 163 | | } |
| | | 164 | | |
| | 27 | 165 | | continue; |
| | | 166 | | } |
| | | 167 | | |
| | 3891 | 168 | | if (readHeaderText) |
| | | 169 | | { |
| | 1779 | 170 | | readHeaderText = false; |
| | | 171 | | } |
| | | 172 | | |
| | 3891 | 173 | | if (commentIndex != -1) |
| | | 174 | | { |
| | 6 | 175 | | SetLineBufferSlice(_lineBuffer.AsSpan()[0..commentIndex]); |
| | | 176 | | } |
| | | 177 | | |
| | 3891 | 178 | | yield return (_lineBuffer, _lineBufferPosition); |
| | 2958 | 179 | | _lineBufferPosition = 0; |
| | | 180 | | } |
| | 849 | 181 | | } |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | #endif |
| | | 186 | | |