示例#1
0
        /// <summary>
        /// Set how to treat whitespace when parsing.
        /// </summary>
        public OptionsBuilder WithWhitespaceTreatment(WhitespaceTreatments whitespaceTreatment)
        {
            // WhitespaceTreatment not recognized
            if (!Utils.IsLegalFlagEnum(whitespaceTreatment))
            {
                Throw.ArgumentException($"{nameof(WhitespaceTreatment)} has an unexpected value, '{WhitespaceTreatment}'", nameof(whitespaceTreatment));
            }

            return(WithWhitespaceTreatmentInternal(whitespaceTreatment));
        }
示例#2
0
        /// <summary>
        /// Create the Options object that has been configured
        /// by this builder.
        /// </summary>
        public Options ToOptions()
        {
            if (string.IsNullOrEmpty(ValueSeparator))
            {
                Throw.InvalidOperationException($"{nameof(ValueSeparator)} cannot be empty");
            }

            // can't distinguish between the start of a value and an empty value
            if (ValueSeparator[0] == EscapedValueStartAndEnd)
            {
                Throw.InvalidOperationException($"{nameof(ValueSeparator)} cannot equal {nameof(EscapedValueStartAndEnd)}, both are '{ValueSeparator}'");
            }
            // can't distinguish between the start of a comment and an empty value
            if (ValueSeparator[0] == CommentCharacter)
            {
                Throw.InvalidOperationException($"{nameof(ValueSeparator)} cannot equal {nameof(CommentCharacter)}, both are '{ValueSeparator}'");
            }
            // can't distinguish between the start of an escaped value and a comment
            if (EscapedValueStartAndEnd != null && EscapedValueStartAndEnd == CommentCharacter)
            {
                Throw.InvalidOperationException($"{nameof(EscapedValueStartAndEnd)} cannot equal {nameof(CommentCharacter)}, both are '{EscapedValueStartAndEnd}'");
            }
            // can't have an escape char if you can't start an escape sequence
            if (EscapedValueStartAndEnd == null && EscapedValueEscapeCharacter != null)
            {
                Throw.InvalidOperationException($"{nameof(EscapedValueEscapeCharacter)} cannot be set if, {nameof(EscapedValueStartAndEnd)} isn't set");
            }
            // ReadRowEnding not recognized
            if (!Enum.IsDefined(Types.ReadRowEnding, ReadRowEnding))
            {
                Throw.InvalidOperationException($"{nameof(ReadRowEnding)} has an unexpected value, '{ReadRowEnding}'");
            }
            // WriteRowEnding not recognized
            if (!Enum.IsDefined(Types.WriteRowEnding, WriteRowEnding))
            {
                Throw.InvalidOperationException($"{nameof(WriteRowEnding)} has an unexpected value, '{WriteRowEnding}'");
            }
            // ReadHeader not recognized
            if (!Enum.IsDefined(Types.ReadHeader, ReadHeader))
            {
                Throw.InvalidOperationException($"{nameof(ReadHeader)} has an unexpected value, '{ReadHeader}'");
            }
            // WriteHeader not recognized
            if (!Enum.IsDefined(Types.WriteHeader, WriteHeader))
            {
                Throw.InvalidOperationException($"{nameof(WriteHeader)} has an unexpected value, '{WriteHeader}'");
            }
            // TypeDescriber not configured
            if (TypeDescriber == null)
            {
                Throw.InvalidOperationException($"{nameof(TypeDescriber)} has not been set");
            }
            // WriteTrailingNewLine not recognized
            if (!Enum.IsDefined(Types.WriteTrailingRowEnding, WriteTrailingRowEnding))
            {
                Throw.InvalidOperationException($"{nameof(WriteTrailingRowEnding)} has an unexpected value, '{WriteTrailingRowEnding}'");
            }
            // MemoryPoolProvider not configured
            if (MemoryPoolProvider == null)
            {
                Throw.InvalidOperationException($"{nameof(MemoryPoolProvider)} has not been set");
            }
            // WriteBufferSizeHint < 0
            if (WriteBufferSizeHint.HasValue && WriteBufferSizeHint.Value < 0)
            {
                Throw.InvalidOperationException($"{nameof(WriteBufferSizeHint)} cannot be less than 0, is '{WriteBufferSizeHint}'");
            }
            // ReadBufferSizeHint < 0
            if (ReadBufferSizeHint < 0)
            {
                Throw.InvalidOperationException($"{nameof(ReadBufferSizeHint)} cannot be less than 0, is '{ReadBufferSizeHint}'");
            }
            // DynamicRowDisposal not recognized
            if (!Enum.IsDefined(Types.DynamicRowDisposal, DynamicRowDisposal))
            {
                Throw.InvalidOperationException($"{nameof(DynamicRowDisposal)} has an unexpected value, '{DynamicRowDisposal}'");
            }
            // WhitespaceTreatment not recognized
            if (!Utils.IsLegalFlagEnum(WhitespaceTreatment))
            {
                Throw.InvalidOperationException($"{nameof(WhitespaceTreatment)} has an unexpected value, '{WhitespaceTreatment}'");
            }
            // ExtraColumnTreatment not recognized
            if (!Enum.IsDefined(Types.ExtraColumnTreatment, ExtraColumnTreatment))
            {
                Throw.InvalidOperationException($"{nameof(ExtraColumnTreatment)} has an unexpected value, '{ExtraColumnTreatment}'");
            }

            // if any of the special characters are whitespace, we can't use them with any of the trimming... parse is ambiguous
            if (WhitespaceTreatment != WhitespaceTreatments.Preserve)
            {
                if (CommentCharacter.HasValue && char.IsWhiteSpace(CommentCharacter.Value))
                {
                    Throw.InvalidOperationException($"Cannot use a whitespace removing {nameof(WhitespaceTreatment)} ({WhitespaceTreatment}) with a whitespace {nameof(CommentCharacter)} ('{CommentCharacter}')");
                }

                if (EscapedValueEscapeCharacter.HasValue && char.IsWhiteSpace(EscapedValueEscapeCharacter.Value))
                {
                    Throw.InvalidOperationException($"Cannot use a whitespace removing {nameof(WhitespaceTreatment)} ({WhitespaceTreatment}) with a whitespace {nameof(EscapedValueEscapeCharacter)} ('{EscapedValueEscapeCharacter}')");
                }

                if (EscapedValueStartAndEnd.HasValue && char.IsWhiteSpace(EscapedValueStartAndEnd.Value))
                {
                    Throw.InvalidOperationException($"Cannot use a whitespace removing {nameof(WhitespaceTreatment)} ({WhitespaceTreatment}) with a whitespace {nameof(EscapedValueStartAndEnd)} ('{EscapedValueStartAndEnd}')");
                }

                if (ValueSeparator.Any(c => char.IsWhiteSpace(c)))
                {
                    Throw.InvalidOperationException($"Cannot use a whitespace removing {nameof(WhitespaceTreatment)} ({WhitespaceTreatment}) with a whitespace {nameof(ValueSeparator)} ('{ValueSeparator}')");
                }
            }

            // ValueSeparator can't be the same as the expected row ending
            var valSepFirstChar   = ValueSeparator[0];
            var valSepIsCR        = valSepFirstChar == '\r';
            var valSepIsLF        = valSepFirstChar == '\n';
            var valSepIsRowEnding = valSepIsCR || valSepIsLF;

            var valueSeparatorMatchesRowEnding =
                (ReadRowEnding == ReadRowEnding.CarriageReturn && valSepIsCR) ||
                (ReadRowEnding == ReadRowEnding.CarriageReturnLineFeed && valSepIsCR) ||    // only the first character matters, so valSepIsCR is what we want here
                (ReadRowEnding == ReadRowEnding.LineFeed && valSepIsLF) ||
                (ReadRowEnding == ReadRowEnding.Detect && valSepIsRowEnding);

            if (valueSeparatorMatchesRowEnding)
            {
                Throw.InvalidOperationException($"{nameof(ValueSeparator)} cannot match the expected (or potential for {nameof(ReadRowEnding.Detect)}) {nameof(ReadRowEnding)}");
            }

            return(BuildInternal());
        }