Пример #1
0
            /// <summary>
            /// Builds the <see cref="Delimiters"/> object.
            /// </summary>
            /// <returns>Returns a <see cref="Delimiters"/> instance.</returns>
            public Delimiters Build()
            {
                Delimiters instance = this.instance;

                this.instance = null;
                instance.checkForExceptions();
                return(instance);
            }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of <see cref="DatParser"/>.
 /// </summary>
 public DatParser()
 {
     this.delimiters = Delimiters.CONCORDANCE;
 }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of <see cref="DatParser"/>.
 /// </summary>
 /// <param name="delimiters">The delimiters used for parsing.</param>
 public DatParser(Delimiters delimiters)
 {
     this.delimiters = delimiters;
 }
Пример #4
0
        /// <summary>
        /// Parses a single field from a string of delimited fields based on
        /// the position of startIndex.
        /// </summary>
        /// <param name="line">The line that is being parsed.</param>
        /// <param name="startIndex">The current position in the line.</param>
        /// <param name="delimiters">The delimiters used to parse the line.
        /// </param>
        /// <returns>Returns the next field in the line.</returns>
        protected string parseField(string line, ref int startIndex, Delimiters delimiters)
        {
            StringBuilder fieldValue   = new StringBuilder();
            int           currentIndex = startIndex;
            char          currentChar  = line[currentIndex];

            // if this is an empty field return an empty value
            if (currentChar == delimiters.FieldSeparator && delimiters.TextQualifier != Delimiters.Null)
            {
                currentIndex++;
            }
            // if this is not a qualified field then scan through to the next
            // field separator
            else if (currentChar != delimiters.TextQualifier)
            {
                int endIndex = line.IndexOf(
                    delimiters.FieldSeparator, currentIndex);
                endIndex = (endIndex < 0) ? line.Length : endIndex;
                fieldValue.Append(
                    line.Substring(currentIndex, endIndex - currentIndex));
                currentIndex = endIndex;
            }
            // otherwise this field must be text qualified
            else
            {
                for (currentIndex = currentIndex + 1; currentIndex < line.Length; currentIndex++)
                {
                    currentChar = line[currentIndex];
                    char?nextChar = null;

                    if (currentIndex + 1 < line.Length)
                    {
                        nextChar = line[currentIndex + 1];
                    }

                    // current character is an escape character for a text qualifier,
                    // append text qualifier as part of the field value
                    if (currentChar == delimiters.EscapeCharacter && nextChar == delimiters.TextQualifier)
                    {
                        fieldValue.Append(nextChar);
                        currentIndex++;
                        continue;
                    }
                    // if current character is text qualifer then next
                    // character is the field separator of doesn't exist then
                    // we're done with the field
                    else if (currentChar == delimiters.TextQualifier &&
                             (nextChar == null || nextChar == delimiters.FieldSeparator))
                    {
                        currentIndex++;
                        break;
                    }
                    // append to field value
                    else
                    {
                        fieldValue.Append(currentChar);
                    }
                }
            }
            // update start position to the character after the last one examined
            startIndex = currentIndex + 1;
            return(fieldValue.ToString());
        }
Пример #5
0
 private Builder()
 {
     instance = new Delimiters();
 }