Пример #1
0
        private void HandleLineEndings()
        {
            if (Configuration.RowEnding != Cesil.RowEndings.Detect)
            {
                RowEndings = Configuration.RowEnding;
                TryMakeStateMachine();
                return;
            }

            using (var detector = new RowEndingDetector <T>(Configuration, SharedCharacterLookup, Inner))
            {
                var res = detector.Detect();
                HandleLineEndingsDetectionResult(res);
            }
        }
Пример #2
0
        private void HandleLineEndings()
        {
            var options = Configuration.Options;

            if (options.ReadRowEnding != ReadRowEnding.Detect)
            {
                RowEndings = options.ReadRowEnding;
                TryMakeStateMachine();
                return;
            }

            using (var detector = new RowEndingDetector(StateMachine, options, Configuration.MemoryPool, SharedCharacterLookup, Inner, Configuration.ValueSeparatorMemory))
            {
                var res = detector.Detect();
                HandleLineEndingsDetectionResult(res);
            }
        }
Пример #3
0
            static async ValueTask <(RowEndings Ending, Memory <char> PushBack)?> DetectAsync_ContinueAfterReadAsync(RowEndingDetector <T> self, ValueTask <int> waitFor, CancellationToken cancel)
            {
                var end = await waitFor;

                // handle the results that were in flight
                var continueScan = true;

                var buffMem = self.BufferOwner.Memory;

                if (end == 0)
                {
                    if (self.BufferStart > 0)
                    {
                        switch (buffMem.Span[0])
                        {
                        case '\r': self.Ending = RowEndings.CarriageReturn; break;

                        case '\n': self.Ending = RowEndings.LineFeed; break;
                        }
                    }
                    goto end;
                }
                else
                {
                    self.AddToPushback(buffMem.Slice(self.BufferStart, end));

                    var len = end + self.BufferStart;
                    var res = self.Advance(buffMem.Span.Slice(0, len));
                    switch (res)
                    {
                    case AdvanceResult.Continue:
                        self.BufferStart = 0;
                        goto loopStart;

                    case AdvanceResult.Finished:
                        continueScan = false;
                        goto loopStart;

                    case AdvanceResult.Continue_PushBackOne:
                        buffMem.Span[0]  = buffMem.Span[len - 1];
                        self.BufferStart = 1;
                        goto loopStart;

                    default:
                        return(default);
                    }
                }


                // resume the loop
loopStart:
                while (continueScan)
                {
                    var mem = self.BufferOwner.Memory.Slice(self.BufferStart, self.BufferOwner.Memory.Length - self.BufferStart);
                    end = await self.Inner.ReadAsync(mem, cancel);

                    if (end == 0)
                    {
                        buffMem = self.BufferOwner.Memory;

                        if (self.BufferStart > 0)
                        {
                            switch (buffMem.Span[0])
                            {
                            case '\r': self.Ending = RowEndings.CarriageReturn; break;

                            case '\n': self.Ending = RowEndings.LineFeed; break;
                            }
                        }
                        break;
                    }
                    else
                    {
                        self.AddToPushback(buffMem.Slice(self.BufferStart, end));

                        var len = end + self.BufferStart;
                        var res = self.Advance(buffMem.Span.Slice(0, len));
                        switch (res)
                        {
                        case AdvanceResult.Continue:
                            self.BufferStart = 0;
                            continue;

                        case AdvanceResult.Finished:
                            continueScan = false;
                            continue;

                        case AdvanceResult.Continue_PushBackOne:
                            buffMem.Span[0]  = buffMem.Span[len - 1];
                            self.BufferStart = 1;
                            continue;

                        default:
                            return(default);
                        }
                    }
                }

end:
                if (self.Ending == RowEndings.None)
                {
                    self.Ending = RowEndings.CarriageReturnLineFeed;
                }

                return(self.Ending, self.Pushback.Slice(0, self.PushbackLength));
            }