< Summary

Line coverage
100%
Covered lines: 68
Uncovered lines: 0
Coverable lines: 68
Total lines: 186
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_HeaderTextLines()100%11100%
.ctor()100%11100%
SetLineBufferLength(...)100%22100%
SetLineBufferSlice(...)100%11100%
AppendLineBufferSlice(...)100%11100%
SetLineReadBufferLength(...)100%22100%
AddLineReadBufferChar(...)100%11100%
ReadLine9(...)100%66100%
Read9()100%2020100%

File(s)

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.Media.WavefrontObj/636b700b450d7d3262bf9218a7cc67966be4ced8/JeremyAnsel.Media.WavefrontObj/JeremyAnsel.Media.WavefrontObj/LineReader9.cs

#LineLine coverage
 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
 10using System.Text;
 11
 12namespace JeremyAnsel.Media.WavefrontObj
 13{
 14    internal class LineReader9
 15    {
 316        public static readonly char[] LineSeparators = new char[] { ' ', '\t' };
 17
 336918        public List<string> HeaderTextLines { get; } = new List<string>();
 19
 177920        private char[] _lineBuffer = new char[256];
 21        private int _lineBufferPosition = 0;
 22
 23        private void SetLineBufferLength(int length)
 24        {
 791125            if (length <= _lineBuffer.Length)
 26            {
 790227                return;
 28            }
 29
 930            int newLength = Math.Max(length, _lineBuffer.Length * 2);
 931            var newBuffer = new char[newLength];
 932            Array.Copy(_lineBuffer, newBuffer, _lineBuffer.Length);
 933            _lineBuffer = newBuffer;
 934        }
 35
 36        private void SetLineBufferSlice(ReadOnlySpan<char> line)
 37        {
 789038            SetLineBufferLength(line.Length);
 789039            line.CopyTo(_lineBuffer);
 789040            _lineBufferPosition = line.Length;
 789041        }
 42
 43        private void AppendLineBufferSlice(ReadOnlySpan<char> line)
 44        {
 2145            SetLineBufferLength(_lineBufferPosition + line.Length);
 2146            line.CopyTo(_lineBuffer.AsSpan()[_lineBufferPosition..]);
 2147            _lineBufferPosition += line.Length;
 2148        }
 49
 50        //private void AddLineBufferChar(char c)
 51        //{
 52        //    SetLineBufferLength(_lineBufferPosition + 1);
 53        //    _lineBuffer[_lineBufferPosition] = c;
 54        //    _lineBufferPosition++;
 55        //}
 56
 177957        private char[] _lineReadBuffer = new char[256];
 58        private int _lineReadBufferPosition = 0;
 59
 60        private void SetLineReadBufferLength(int length)
 61        {
 4588862            if (length <= _lineReadBuffer.Length)
 63            {
 4587064                return;
 65            }
 66
 1867            int newLength = Math.Max(length, _lineReadBuffer.Length * 2);
 1868            var newBuffer = new char[newLength];
 1869            Array.Copy(_lineReadBuffer, newBuffer, _lineReadBuffer.Length);
 1870            _lineReadBuffer = newBuffer;
 1871        }
 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        {
 4588882            SetLineReadBufferLength(_lineReadBufferPosition + 1);
 4588883            _lineReadBuffer[_lineReadBufferPosition] = c;
 4588884            _lineReadBufferPosition++;
 4588885        }
 86
 87        private void ReadLine9(StreamReader reader)
 88        {
 465989            _lineReadBufferPosition = 0;
 90
 4588891            while (true)
 92            {
 5054793                int r = reader.Read();
 94
 5054795                if (r == -1)
 96                {
 97                    break;
 98                }
 99
 49014100                char c = (char)r;
 101
 49014102                if (c == '\r')
 103                {
 104                    continue;
 105                }
 106
 49014107                if (c == '\n')
 108                {
 109                    break;
 110                }
 111
 45888112                AddLineReadBufferChar(c);
 113            }
 4659114        }
 115
 116        public IEnumerable<(char[] Buffer, int Length)> Read9(Stream stream)
 117        {
 1779118            using var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true);
 1779119            _lineBufferPosition = 0;
 1779120            bool isMultiLine = false;
 1779121            bool readHeaderText = true;
 122
 5505123            while (!reader.EndOfStream)
 124            {
 4659125                ReadLine9(reader);
 126
 4659127                if (_lineReadBufferPosition == 0 || MemoryExtensions.IsWhiteSpace(_lineReadBuffer.AsSpan()[.._lineReadBu
 128                {
 717129                    if (readHeaderText)
 130                    {
 717131                        this.HeaderTextLines.Add(string.Empty);
 132                    }
 133
 717134                    continue;
 135                }
 136
 3942137                if (isMultiLine)
 138                {
 21139                    SetLineBufferSlice(_lineBuffer.AsSpan()[..(_lineBufferPosition - 1)]);
 21140                    AppendLineBufferSlice(_lineReadBuffer.AsSpan()[.._lineReadBufferPosition]);
 141                }
 142                else
 143                {
 3921144                    SetLineBufferSlice(_lineReadBuffer.AsSpan()[.._lineReadBufferPosition]);
 145                }
 146
 3942147                SetLineBufferSlice(MemoryExtensions.Trim(_lineBuffer.AsSpan()[.._lineBufferPosition]));
 148
 3942149                isMultiLine = MemoryExtensions.EndsWith(_lineBuffer.AsSpan()[.._lineBufferPosition], "\\".AsSpan());
 150
 3942151                if (isMultiLine)
 152                {
 153                    continue;
 154                }
 155
 3921156                int commentIndex = MemoryExtensions.IndexOf(_lineBuffer.AsSpan()[.._lineBufferPosition], '#');
 157
 3921158                if (commentIndex == 0)
 159                {
 33160                    if (readHeaderText)
 161                    {
 27162                        this.HeaderTextLines.Add(_lineBuffer.AsSpan()[1.._lineBufferPosition].ToString());
 163                    }
 164
 27165                    continue;
 166                }
 167
 3888168                if (readHeaderText)
 169                {
 1776170                    readHeaderText = false;
 171                }
 172
 3888173                if (commentIndex != -1)
 174                {
 6175                    SetLineBufferSlice(_lineBuffer.AsSpan()[0..commentIndex]);
 176                }
 177
 3888178                yield return (_lineBuffer, _lineBufferPosition);
 2955179                _lineBufferPosition = 0;
 180            }
 846181        }
 182    }
 183}
 184
 185#endif
 186