< Summary

Information
Class: ValidateLib.TabularData.Parsing.Reader
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Parsing\Reader.cs
Line coverage
97%
Covered lines: 40
Uncovered lines: 1
Coverable lines: 41
Total lines: 105
Line coverage: 97.5%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBlocks covered Blocks not covered
Reader(...)360
IsNextCharsLineTerminator(...)321
IsEndOfFile(...)10

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Parsing\Reader.cs

#LineLine coverage
 1namespace ValidateLib.TabularData.Parsing
 2{
 3    /// <summary>
 4    /// Defines methods for more advanced readers for working with the CSV
 5    /// files.
 6    /// </summary>
 7    public abstract class Reader
 8    {
 9        protected Flags flags;
 110        protected int maximalLineTerminatorLength = 1;
 111        protected int quoteCharacterLength = 1;
 12        protected int bufferSize;
 13        protected bool CRLF_ending = false;
 14        protected bool LF_ending = false;
 115        protected byte CR_LF_length = 2;
 116        protected byte LF_length = 1;
 17        protected bool unusualLineTerminator = false;
 18
 119        public Reader(Flags flags)
 20        {
 121            this.flags = flags;
 122            maximalLineTerminatorLength = flags.lineTerminators.OrderByDescending(s => s.Length).First().Length;
 123            this.flags.lineTerminators = flags.lineTerminators.OrderBy(s => s.Length).ToList();
 124            quoteCharacterLength = flags.quoteCharacter.Length;
 125            bufferSize = Math.Max(ReaderConstants.BUFFER_MINIMAL_SIZE, Math.Max(maximalLineTerminatorLength, quoteCharac
 126            if (flags.lineTerminators.Contains("\r\n"))
 27            {
 128                CRLF_ending = true;
 29            }
 130            if (flags.lineTerminators.Contains("\n"))
 31            {
 132                LF_ending = true;
 33            }
 34
 135            foreach (var lineTerminator in flags.lineTerminators)
 36            {
 137                if (lineTerminator != "\r\n" && lineTerminator != "\n")
 38                {
 139                    unusualLineTerminator = true;
 40                }
 41            }
 42
 143        }
 144        public int MaximalLineTerminatorLength { get { return maximalLineTerminatorLength; } }
 45
 46        /// <summary>
 47        /// Checks whether next chars in a stream reader are newline strings (can be multiple chars)
 48        /// </summary>
 49        /// <param name="reader"></param>
 50        /// <returns>True when the next string is newline string</returns>
 51        protected bool IsNextCharsLineTerminator(CustomStreamReader reader)
 52        {
 153            char[] buffer = new char[maximalLineTerminatorLength];
 154            int bytesRead = reader.ReadChars(maximalLineTerminatorLength, buffer);
 55
 156            if (bytesRead == 0)
 57            {
 58                // End of stream reached
 059                return false;
 60            }
 61
 62
 163            if (CRLF_ending && bytesRead >= 2)
 64            {
 165                if (buffer[0] == '\r' && buffer[1] == '\n')
 66                {
 67                    // Match found
 168                    reader.MoveBack(bytesRead - CR_LF_length);
 169                    return true;
 70                }
 71            }
 72
 173            if (LF_ending && bytesRead >= 1)
 74            {
 175                if (buffer[0] == '\n')
 76                {
 77                    // Match found
 178                    reader.MoveBack(bytesRead - LF_length);
 179                    return true;
 80                }
 81            }
 82
 183            if (unusualLineTerminator)
 84            {
 185                string nextChars = new string(buffer, 0, bytesRead);
 86
 87                // Check if the next characters match any of the line terminators
 188                foreach (string terminator in flags.lineTerminators)
 89                {
 190                    if (nextChars.StartsWith(terminator))
 91                    {
 92                        // Match found
 193                        reader.MoveBack(bytesRead - terminator.Length);
 194                        return true;
 95                    }
 96                }
 97            }
 98
 199            reader.MoveBack(bytesRead);
 1100            return false;
 1101        }
 102
 1103        protected bool IsEndOfFile(int currentChar) => currentChar == ReaderConstants.END_OF_STREAM_CONSTANT;
 104    }
 105}