Beispiel #1
0
        internal (HeaderEnumerator Headers, bool IsHeader, Memory <char> PushBack) Read()
        {
            var madeProgress = true;

            while (true)
            {
                var available = Buffer.Read(Inner.Value, madeProgress);
                if (available == 0)
                {
                    if (BuilderBacking.Length > 0)
                    {
                        var inEscapedValue = ReaderStateMachine.IsInEscapedValue(StateMachine.CurrentState);
                        PushPendingCharactersToValue(inEscapedValue);
                    }
                    break;
                }
                else
                {
                    AddToPushback(Buffer.Buffer.Span.Slice(0, available));
                }

                if (AdvanceWork(available, out madeProgress))
                {
                    break;
                }
            }

            return(IsHeaderResult());
        }
Beispiel #2
0
        internal ValueTask <(HeaderEnumerator Headers, bool IsHeader, Memory <char> PushBack)> ReadAsync(CancellationToken cancellationToken)
        {
            var madeProgress = true;

            while (true)
            {
                var availableTask = Buffer.ReadAsync(InnerAsync.Value, madeProgress, cancellationToken);
                if (!availableTask.IsCompletedSuccessfully(this))
                {
                    return(ReadAsync_ContinueAfterReadAsync(this, availableTask, cancellationToken));
                }

                var available = availableTask.Result;
                if (available == 0)
                {
                    if (BuilderBacking.Length > 0)
                    {
                        var inEscapedValue = ReaderStateMachine.IsInEscapedValue(StateMachine.CurrentState);
                        PushPendingCharactersToValue(inEscapedValue);
                    }
                    break;
                }
                else
                {
                    AddToPushback(Buffer.Buffer.Span.Slice(0, available));
                }

                if (AdvanceWork(available, out madeProgress))
                {
                    break;
                }
            }

            return(new ValueTask <(HeaderEnumerator Headers, bool IsHeader, Memory <char> PushBack)>(IsHeaderResult()));
Beispiel #3
0
        protected ReaderBase(BoundConfigurationBase <T> config, object context)
        {
            RowNumber     = 0;
            Configuration = config;
            Context       = context;

            var bufferSize = config.ReadBufferSizeHint;

            if (bufferSize == 0)
            {
                bufferSize = DEFAULT_BUFFER_SIZE;
            }

            Buffer  = new BufferWithPushback(config.MemoryPool, bufferSize);
            Partial = new Partial <T>(config.MemoryPool);

            SharedCharacterLookup =
                ReaderStateMachine.MakeCharacterLookup(
                    config.MemoryPool,
                    config.EscapedValueStartAndStop,
                    config.ValueSeparator,
                    config.EscapeValueEscapeChar,
                    config.CommentChar
                    );
        }
Beispiel #4
0
        internal HeadersReader(
            BoundConfigurationBase <T> config,
            ReaderStateMachine.CharacterLookup charLookup,
            TextReader inner,
            BufferWithPushback buffer
            )
        {
            MemoryPool     = config.MemoryPool;
            BufferSizeHint = config.ReadBufferSizeHint;
            Columns        = config.DeserializeColumns;
            Inner          = inner;

            StateMachine =
                new ReaderStateMachine(
                    charLookup,
                    config.EscapedValueStartAndStop,
                    config.EscapeValueEscapeChar,
                    config.RowEnding,
                    ReadHeaders.Never
                    );

            Buffer = buffer;

            HeaderCount    = 0;
            PushBackLength = 0;
        }
        private RowEndingDetector(ReaderStateMachine stateMachine, Options options, MemoryPool <char> memPool, CharacterLookup charLookup, IReaderAdapter?inner, IAsyncReaderAdapter?innerAsync, ReadOnlyMemory <char> valueSeparatorMemory)
        {
            Inner.SetAllowNull(inner);
            InnerAsync.SetAllowNull(innerAsync);

            State = stateMachine;
            stateMachine.Initialize(
                charLookup,
                options.EscapedValueStartAndEnd,
                options.EscapedValueEscapeCharacter,
Beispiel #6
0
 internal HeadersReader(
     ReaderStateMachine stateMachine,
     BoundConfigurationBase <T> config,
     CharacterLookup charLookup,
     IAsyncReaderAdapter inner,
     BufferWithPushback buffer,
     ReadRowEnding rowEndingOverride
     )
     : this(stateMachine, config, charLookup, null, inner, buffer, rowEndingOverride)
 {
 }
Beispiel #7
0
        private HeadersReader(
            ReaderStateMachine stateMachine,
            BoundConfigurationBase <T> config,
            CharacterLookup charLookup,
            IReaderAdapter?inner,
            IAsyncReaderAdapter?innerAsync,
            BufferWithPushback buffer,
            ReadRowEnding rowEndingOverride
            )
        {
            LogHelper.StateTransition_NewHeadersReader();

            Configuration = config;

            Inner.SetAllowNull(inner);
            InnerAsync.SetAllowNull(innerAsync);

            var options = Configuration.Options;

            MemoryPool     = Configuration.MemoryPool;
            BufferSizeHint = options.ReadBufferSizeHint;
            Columns        = config.DeserializeColumns;

            StateMachine = stateMachine;
            stateMachine.Initialize(
                charLookup,
                options.EscapedValueStartAndEnd,
                options.EscapedValueEscapeCharacter,
                rowEndingOverride,                      // this can be something OTHER than what was provided, due to RowEnding.Detect
                ReadHeader.Never,
                false,
                options.WhitespaceTreatment.HasFlag(WhitespaceTreatments.TrimBeforeValues),
                options.WhitespaceTreatment.HasFlag(WhitespaceTreatments.TrimAfterValues)
                );

            Buffer = buffer;

            HeaderCount         = 0;
            PushBackLength      = 0;
            WhitespaceTreatment = options.WhitespaceTreatment;
        }
Beispiel #8
0
        protected ReaderBase(BoundConfigurationBase <T> config, object?context, IRowConstructor <T> rowBuilder, ExtraColumnTreatment extraTreatment)
        {
            RowNumber     = 0;
            Configuration = config;
            Context       = context;

            var options = config.Options;

            var bufferSize = options.ReadBufferSizeHint;

            if (bufferSize == 0)
            {
                bufferSize = Utils.DEFAULT_BUFFER_SIZE;
            }

            // always need to be able to store a whole value separator "character"
            var bufferMinSize = config.ValueSeparatorMemory.Length * 2;

            if (bufferSize < bufferMinSize)
            {
                bufferSize = bufferMinSize;
            }

            var memPool = config.MemoryPool;

            Buffer =
                new BufferWithPushback(
                    memPool,
                    bufferSize
                    );
            Partial = new Partial(memPool);

            SharedCharacterLookup = CharacterLookup.MakeCharacterLookup(options, out _);
            StateMachine          = new ReaderStateMachine();
            RowBuilder            = rowBuilder;

            ExtraColumnTreatment = extraTreatment;
        }
Beispiel #9
0
        internal RowEndingDetector(BoundConfigurationBase <T> config, ReaderStateMachine.CharacterLookup charLookup, TextReader inner)
        {
            Inner = inner;

            State =
                new ReaderStateMachine(
                    charLookup,
                    config.EscapedValueStartAndStop,
                    config.EscapeValueEscapeChar,
                    RowEndings.None,
                    ReadHeaders.Never
                    );

            MemoryPool = config.MemoryPool;

            BufferSizeHint = config.ReadBufferSizeHint;
            if (BufferSizeHint == 0)
            {
                BufferSizeHint = ReaderBase <T> .DEFAULT_BUFFER_SIZE;
            }

            BufferOwner = MemoryPool.Rent(BufferSizeHint);
            BufferStart = 0;
        }
Beispiel #10
0
            // wait for read to complete, then continue async
            static async ValueTask <(HeaderEnumerator Headers, bool IsHeader, Memory <char> PushBack)> ReadAsync_ContinueAfterReadAsync(
                HeadersReader <T> self,
                ValueTask <int> waitFor,
                CancellationToken cancellationToken)
            {
                bool madeProgress;

                int available;

                {
                    available = await ConfigureCancellableAwait(self, waitFor, cancellationToken);
                }

                // handle the in flight task
                if (available == 0)
                {
                    if (self.BuilderBacking.Length > 0)
                    {
                        var inEscapedValue = ReaderStateMachine.IsInEscapedValue(self.StateMachine.CurrentState);
                        self.PushPendingCharactersToValue(inEscapedValue);
                    }

                    return(self.IsHeaderResult());
                }
                else
                {
                    self.AddToPushback(self.Buffer.Buffer.Span.Slice(0, available));
                }

                if (self.AdvanceWork(available, out madeProgress))
                {
                    return(self.IsHeaderResult());
                }

                // go back into the loop
                while (true)
                {
                    var readTask = self.Buffer.ReadAsync(self.InnerAsync.Value, madeProgress, cancellationToken);
                    {
                        available = await ConfigureCancellableAwait(self, readTask, cancellationToken);
                    }

                    if (available == 0)
                    {
                        if (self.BuilderBacking.Length > 0)
                        {
                            var inEscapedValue = ReaderStateMachine.IsInEscapedValue(self.StateMachine.CurrentState);
                            self.PushPendingCharactersToValue(inEscapedValue);
                        }
                        break;
                    }
                    else
                    {
                        self.AddToPushback(self.Buffer.Buffer.Span.Slice(0, available));
                    }

                    if (self.AdvanceWork(available, out madeProgress))
                    {
                        break;
                    }
                }

                return(self.IsHeaderResult());
            }
 internal RowEndingDetector(ReaderStateMachine stateMachine, Options options, MemoryPool <char> memPool, CharacterLookup charLookup, IAsyncReaderAdapter innerAsync, ReadOnlyMemory <char> valueSeparatorMemory)
     : this(stateMachine, options, memPool, charLookup, null, innerAsync, valueSeparatorMemory)
 {
 }