/// <summary>
        /// Formats a single field for writing.
        /// </summary>
        /// <param name="value">The value to format.</param>
        /// <param name="field">The descriptor for the current field.</param>
        /// <returns>The formatted string.</returns>
        private string FormatField(string value, FixedWidthField field)
        {
            if (value == null)
            {
                value = string.Empty;
            }

            if (value.Length < field.Length)
            {
                FieldAlignment alignment    = field.Alignment ?? Options.DefaultAlignment;
                char           padCharacter = field.PadCharacter ?? Options.DefaultPadCharacter;
                if (alignment == FieldAlignment.Left)
                {
                    value = value.PadRight(field.Length, padCharacter);
                }
                else // FieldAlignment.Right
                {
                    value = value.PadLeft(field.Length, padCharacter);
                }
            }
            else if (value.Length > field.Length)
            {
                if (Options.ThrowOverflowException)
                {
                    throw new FixedWidthOverflowException(value, field.Length);
                }
                value = value.Substring(0, field.Length);
            }
            return(value);
        }
예제 #2
0
        /// <summary>
        /// Reads the specified field from the given line.
        /// </summary>
        /// <param name="line">The line that contains all the fields.</param>
        /// <param name="field">Field descriptor.</param>
        /// <param name="position">The current position within the liine.</param>
        /// <returns>The extracted field.</returns>
        private string ReadField(string line, FixedWidthField field, ref int position)
        {
            Debug.Assert(line != null);
            Debug.Assert(field != null);
            Debug.Assert(position >= 0);

            // Extract field
            string result;

            if (line.Length < position + field.Length)
            {
                if (Options.ThrowOutOfRangeException)
                {
                    throw new FixedWidthOutOfRangeException();
                }
#if NETSTANDARD2_0
                result = (position <= line.Length) ? line.Substring(position) : string.Empty;
#else
                result = (position <= line.Length) ? line[position..] : string.Empty;