| | 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 | |
|
| 3369 | 18 | | public List<string> HeaderTextLines { get; } = new List<string>(); |
| | 19 | |
|
| 1779 | 20 | | private char[] _lineBuffer = new char[256]; |
| | 21 | | private int _lineBufferPosition = 0; |
| | 22 | |
|
| | 23 | | private void SetLineBufferLength(int length) |
| | 24 | | { |
| 7911 | 25 | | if (length <= _lineBuffer.Length) |
| | 26 | | { |
| 7902 | 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 | | { |
| 7890 | 38 | | SetLineBufferLength(line.Length); |
| 7890 | 39 | | line.CopyTo(_lineBuffer); |
| 7890 | 40 | | _lineBufferPosition = line.Length; |
| 7890 | 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 | |
|
| 1779 | 57 | | private char[] _lineReadBuffer = new char[256]; |
| | 58 | | private int _lineReadBufferPosition = 0; |
| | 59 | |
|
| | 60 | | private void SetLineReadBufferLength(int length) |
| | 61 | | { |
| 45888 | 62 | | if (length <= _lineReadBuffer.Length) |
| | 63 | | { |
| 45870 | 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 | | { |
| 45888 | 82 | | SetLineReadBufferLength(_lineReadBufferPosition + 1); |
| 45888 | 83 | | _lineReadBuffer[_lineReadBufferPosition] = c; |
| 45888 | 84 | | _lineReadBufferPosition++; |
| 45888 | 85 | | } |
| | 86 | |
|
| | 87 | | private void ReadLine9(StreamReader reader) |
| | 88 | | { |
| 4659 | 89 | | _lineReadBufferPosition = 0; |
| | 90 | |
|
| 45888 | 91 | | while (true) |
| | 92 | | { |
| 50547 | 93 | | int r = reader.Read(); |
| | 94 | |
|
| 50547 | 95 | | if (r == -1) |
| | 96 | | { |
| | 97 | | break; |
| | 98 | | } |
| | 99 | |
|
| 49014 | 100 | | char c = (char)r; |
| | 101 | |
|
| 49014 | 102 | | if (c == '\r') |
| | 103 | | { |
| | 104 | | continue; |
| | 105 | | } |
| | 106 | |
|
| 49014 | 107 | | if (c == '\n') |
| | 108 | | { |
| | 109 | | break; |
| | 110 | | } |
| | 111 | |
|
| 45888 | 112 | | AddLineReadBufferChar(c); |
| | 113 | | } |
| 4659 | 114 | | } |
| | 115 | |
|
| | 116 | | public IEnumerable<(char[] Buffer, int Length)> Read9(Stream stream) |
| | 117 | | { |
| 1779 | 118 | | using var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true); |
| 1779 | 119 | | _lineBufferPosition = 0; |
| 1779 | 120 | | bool isMultiLine = false; |
| 1779 | 121 | | bool readHeaderText = true; |
| | 122 | |
|
| 5505 | 123 | | while (!reader.EndOfStream) |
| | 124 | | { |
| 4659 | 125 | | ReadLine9(reader); |
| | 126 | |
|
| 4659 | 127 | | if (_lineReadBufferPosition == 0 || MemoryExtensions.IsWhiteSpace(_lineReadBuffer.AsSpan()[.._lineReadBu |
| | 128 | | { |
| 717 | 129 | | if (readHeaderText) |
| | 130 | | { |
| 717 | 131 | | this.HeaderTextLines.Add(string.Empty); |
| | 132 | | } |
| | 133 | |
|
| 717 | 134 | | continue; |
| | 135 | | } |
| | 136 | |
|
| 3942 | 137 | | if (isMultiLine) |
| | 138 | | { |
| 21 | 139 | | SetLineBufferSlice(_lineBuffer.AsSpan()[..(_lineBufferPosition - 1)]); |
| 21 | 140 | | AppendLineBufferSlice(_lineReadBuffer.AsSpan()[.._lineReadBufferPosition]); |
| | 141 | | } |
| | 142 | | else |
| | 143 | | { |
| 3921 | 144 | | SetLineBufferSlice(_lineReadBuffer.AsSpan()[.._lineReadBufferPosition]); |
| | 145 | | } |
| | 146 | |
|
| 3942 | 147 | | SetLineBufferSlice(MemoryExtensions.Trim(_lineBuffer.AsSpan()[.._lineBufferPosition])); |
| | 148 | |
|
| 3942 | 149 | | isMultiLine = MemoryExtensions.EndsWith(_lineBuffer.AsSpan()[.._lineBufferPosition], "\\".AsSpan()); |
| | 150 | |
|
| 3942 | 151 | | if (isMultiLine) |
| | 152 | | { |
| | 153 | | continue; |
| | 154 | | } |
| | 155 | |
|
| 3921 | 156 | | int commentIndex = MemoryExtensions.IndexOf(_lineBuffer.AsSpan()[.._lineBufferPosition], '#'); |
| | 157 | |
|
| 3921 | 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 | |
|
| 3888 | 168 | | if (readHeaderText) |
| | 169 | | { |
| 1776 | 170 | | readHeaderText = false; |
| | 171 | | } |
| | 172 | |
|
| 3888 | 173 | | if (commentIndex != -1) |
| | 174 | | { |
| 6 | 175 | | SetLineBufferSlice(_lineBuffer.AsSpan()[0..commentIndex]); |
| | 176 | | } |
| | 177 | |
|
| 3888 | 178 | | yield return (_lineBuffer, _lineBufferPosition); |
| 2955 | 179 | | _lineBufferPosition = 0; |
| | 180 | | } |
| 846 | 181 | | } |
| | 182 | | } |
| | 183 | | } |
| | 184 | |
|
| | 185 | | #endif |
| | 186 | |
|