Ejemplo n.º 1
0
        public T GetCurrentRecordDynamic()
        {
            var obj = TCreatorFunc();

            _mapping.Map(obj, _data.Span, _currentValueIndices);
            return(obj);
        }
Ejemplo n.º 2
0
        // TODO Use GetPosition instead of nextpositionrelative
        private void ProcessLine(ReadOnlySequence <byte> readOnlySequence)
        {
            if (_recordLength == 0)
            {
                _recordLength = CalculateRecordLength(readOnlySequence);
            }

            Span <int> valueIndices = stackalloc int[_recordLength * 2];

            SequencePosition[] positions = ArrayPool <SequencePosition> .Shared.Rent(_recordLength + 1);

            SequencePosition currentValuePosition = readOnlySequence.Start;

            for (int i = 0; i < _recordLength; i++)
            {
                byte delim             = i == _recordLength - 1 ? (byte)'\r' : (byte)',';
                var  nextValuePosition = readOnlySequence.Slice(readOnlySequence.GetPosition(1, currentValuePosition)).PositionOf(delim);
                if (nextValuePosition == null)
                {
                    if (i == _recordLength - 1)
                    {
                        // the rest of the string is the last value
                        //var recordValue = readOnlySequence.Slice(currentValuePosition);
                        nextValuePosition = currentValuePosition;
                    }
                    else
                    {
                        throw new InvalidOperationException("CSV was invalid, unexpected EOF");
                    }
                }

                positions[i]         = currentValuePosition;
                currentValuePosition = readOnlySequence.GetPosition(1, nextValuePosition.Value);
            }

            positions[_recordLength] = readOnlySequence.End;

            var currentPos = 0;

            for (int i = 0; i < _recordLength; i++)
            {
                // Because we already skipped over the delimiter, need to 'unskip' over it so length doesn't include it
                var segmentLength = (int)readOnlySequence.Slice(positions[i], positions[i + 1]).Length - 1;
                valueIndices[i * 2]     = currentPos;
                valueIndices[i * 2 + 1] = segmentLength;
                currentPos += segmentLength + 1; // skip over the delim
            }

            // This is disgusting, find a better way to do this
            // this happens because .End doesn't have a delim to skip over
            // and we assume that we do in the for loop
            valueIndices[(_recordLength - 1) * 2 + 1] += 1;

            var entity = _mapper.Map(readOnlySequence, valueIndices);

            _list.Add(entity);
        }