<commit_before>﻿using System.Diagnostics.Contracts;
using Text.Scanning;

namespace Http.Grammar.Rfc7230
{
    public class HeaderFieldLexer : Lexer<HeaderField>
    {
        private readonly ILexer<FieldName> fieldNameLexer;
        private readonly ILexer<OptionalWhiteSpace> owsLexer;
        private readonly ILexer<FieldValue> fieldValueLexer;

        public HeaderFieldLexer()
            : this(new FieldNameLexer(), new OptionalWhiteSpaceLexer(), new FieldValueLexer())
        {
        }

        public HeaderFieldLexer(ILexer<FieldName> fieldNameLexer, ILexer<OptionalWhiteSpace> owsLexer, ILexer<FieldValue> fieldValueLexer)
        {
            Contract.Requires(fieldNameLexer != null);
            Contract.Requires(owsLexer != null);
            Contract.Requires(fieldValueLexer != null);
            this.fieldNameLexer = fieldNameLexer;
            this.owsLexer = owsLexer;
            this.fieldValueLexer = fieldValueLexer;
        }

        public override HeaderField Read(ITextScanner scanner)
        {
            var context = scanner.GetContext();
            HeaderField element;
            if (this.TryRead(scanner, out element))
            {
                return element;
            }

            throw new SyntaxErrorException(context, "Expected 'header-field'");
        }

        public override bool TryRead(ITextScanner scanner, out HeaderField element)
        {
            if (scanner.EndOfInput)
            {
                element = default(HeaderField);
                return false;
            }

            var context = scanner.GetContext();
            FieldName fieldName;
            if (!this.fieldNameLexer.TryRead(scanner, out fieldName))
            {
                element = default(HeaderField);
                return false;
            }

            if (!scanner.TryMatch(':'))
            {
                this.fieldNameLexer.PutBack(scanner, fieldName);
                element = default(HeaderField);
                return false;
            }

            OptionalWhiteSpace ows1;
            if (!this.owsLexer.TryRead(scanner, out ows1))
            {
                scanner.PutBack(':');
                this.fieldNameLexer.PutBack(scanner, fieldName);
                element = default(HeaderField);
                return false;
            }

            FieldValue fieldValue;
            if (!this.fieldValueLexer.TryRead(scanner, out fieldValue))
            {
                this.owsLexer.PutBack(scanner, ows1);
                scanner.PutBack(':');
                this.fieldNameLexer.PutBack(scanner, fieldName);
                element = default(HeaderField);
                return false;
            }

            OptionalWhiteSpace ows2;
            if (!this.owsLexer.TryRead(scanner, out ows2))
            {
                this.fieldValueLexer.PutBack(scanner, fieldValue);
                this.owsLexer.PutBack(scanner, ows1);
                scanner.PutBack(':');
                this.fieldNameLexer.PutBack(scanner, fieldName);
                element = default(HeaderField);
                return false;
            }

            element = new HeaderField(fieldName, ows1, fieldValue, ows2, context);
            return true;
        }

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.fieldNameLexer != null);
            Contract.Invariant(this.owsLexer != null);
            Contract.Invariant(this.fieldValueLexer != null);
        }

    }
}<commit_msg>Refactoring 'HeaderFieldLexer'<commit_after>﻿using System.Diagnostics.Contracts;
using Text.Scanning;

namespace Http.Grammar.Rfc7230
{
    public class HeaderFieldLexer : Lexer<HeaderField>
    {
        private readonly ILexer<FieldName> fieldNameLexer;
        private readonly ILexer<OptionalWhiteSpace> optionalWhiteSpaceLexer;
        private readonly ILexer<FieldValue> fieldValueLexer;

        public HeaderFieldLexer()
            : this(new FieldNameLexer(), new OptionalWhiteSpaceLexer(), new FieldValueLexer())
        {
        }

        public HeaderFieldLexer(ILexer<FieldName> fieldNameLexer, ILexer<OptionalWhiteSpace> optionalWhiteSpaceLexer, ILexer<FieldValue> fieldValueLexer)
        {
            Contract.Requires(fieldNameLexer != null);
            Contract.Requires(optionalWhiteSpaceLexer != null);
            Contract.Requires(fieldValueLexer != null);
            this.fieldNameLexer = fieldNameLexer;
            this.optionalWhiteSpaceLexer = optionalWhiteSpaceLexer;
            this.fieldValueLexer = fieldValueLexer;
        }

        public override HeaderField Read(ITextScanner scanner)
        {
            var context = scanner.GetContext();
            HeaderField element;
            if (this.TryRead(scanner, out element))
            {
                return element;
            }

            throw new SyntaxErrorException(context, "Expected 'header-field'");
        }

        public override bool TryRead(ITextScanner scanner, out HeaderField element)
        {
            if (scanner.EndOfInput)
            {
                element = default(HeaderField);
                return false;
            }

            var context = scanner.GetContext();
            FieldName fieldName;
            if (!this.fieldNameLexer.TryRead(scanner, out fieldName))
            {
                element = default(HeaderField);
                return false;
            }

            if (!scanner.TryMatch(':'))
            {
                this.fieldNameLexer.PutBack(scanner, fieldName);
                element = default(HeaderField);
                return false;
            }

            OptionalWhiteSpace optionalWhiteSpace1;
            if (!this.optionalWhiteSpaceLexer.TryRead(scanner, out optionalWhiteSpace1))
            {
                scanner.PutBack(':');
                this.fieldNameLexer.PutBack(scanner, fieldName);
                element = default(HeaderField);
                return false;
            }

            FieldValue fieldValue;
            if (!this.fieldValueLexer.TryRead(scanner, out fieldValue))
            {
                this.optionalWhiteSpaceLexer.PutBack(scanner, optionalWhiteSpace1);
                scanner.PutBack(':');
                this.fieldNameLexer.PutBack(scanner, fieldName);
                element = default(HeaderField);
                return false;
            }

            OptionalWhiteSpace optionalWhiteSpace2;
            if (!this.optionalWhiteSpaceLexer.TryRead(scanner, out optionalWhiteSpace2))
            {
                this.fieldValueLexer.PutBack(scanner, fieldValue);
                this.optionalWhiteSpaceLexer.PutBack(scanner, optionalWhiteSpace1);
                scanner.PutBack(':');
                this.fieldNameLexer.PutBack(scanner, fieldName);
                element = default(HeaderField);
                return false;
            }

            element = new HeaderField(fieldName, optionalWhiteSpace1, fieldValue, optionalWhiteSpace2, context);
            return true;
        }

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.fieldNameLexer != null);
            Contract.Invariant(this.optionalWhiteSpaceLexer != null);
            Contract.Invariant(this.fieldValueLexer != null);
        }

    }
}
{'fraction_non_alphanumeric': 0.08388760689561559, 'fraction_numerical': 0.003257771141577304, 'mean_word_length': 1.827321565617805, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1681069', 'n_tokens_mistral': 2011, 'n_tokens_neox': 1856, 'n_words': 339}