<commit_before>﻿namespace AngleSharp.Html.InputTypes
{
    using AngleSharp.Dom;
    using AngleSharp.Dom.Html;
    using AngleSharp.Extensions;
    using System;
    using System.Globalization;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Base type for the all input field types. Primarely from:
    /// http://www.w3.org/TR/html5/forms.html#range-state-(type=range)
    /// </summary>
    abstract class BaseInputType
    {
        #region Fields

        protected static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        protected static readonly Regex Number = new Regex("^\\-?\\d+(\\.\\d+)?([eE][\\-\\+]?\\d+)?$");

        private readonly IHtmlInputElement _input;
        private readonly Boolean _validate;
        private readonly String _name;

        #endregion

        #region ctor

        public BaseInputType(IHtmlInputElement input, String name, Boolean validate)
        {
            _input = input;
            _validate = validate;
            _name = name;
        }

        #endregion

        #region Properties

        public String Name
        {
            get { return _name; }
        }

        public Boolean CanBeValidated
        {
            get { return _validate; }
        }

        public IHtmlInputElement Input
        {
            get { return _input; }
        }

        #endregion

        #region Methods

        public virtual Boolean IsAppendingData(IHtmlElement submitter)
        {
            return true;
        }

        public virtual void Check(ValidityState state)
        {
        }

        public virtual Double? ConvertToNumber(String value)
        {
            return null;
        }

        public virtual String ConvertFromNumber(Double value)
        {
            throw new DomException(DomError.InvalidState);
        }

        public virtual DateTime? ConvertToDate(String value)
        {
            return null;
        }

        public virtual String ConvertFromDate(DateTime value)
        {
            throw new DomException(DomError.InvalidState);
        }

        public virtual void ConstructDataSet(FormDataSet dataSet)
        {
            dataSet.Append(_input.Name, _input.Value, _input.Type);
        }

        public virtual void DoStep(Int32 n)
        {
            throw new DomException(DomError.InvalidState);
        }

        #endregion

        #region Step

        protected Boolean IsStepMismatch()
        {
            var step = GetStep();
            var value = ConvertToNumber(_input.Value);
            var offset = GetStepBase();
            return step != 0.0 && (value - offset) % step != 0.0;
        }

        protected Double GetStep()
        {
            var step = _input.Step;

            if (String.IsNullOrEmpty(step))
            {
                return GetDefaultStep() * GetStepScaleFactor();
            }
            else if (step.Isi(Keywords.Any))
            {
                return 0.0;
            }

            var num = ToNumber(step);

            if (num.HasValue == false || num <= 0.0)
            {
                return GetDefaultStep() * GetStepScaleFactor();
            }

            return num.Value * GetStepScaleFactor();
        }

        private Double GetStepBase()
        {
            var num = ConvertToNumber(_input.Minimum);

            if (num.HasValue)
            {
                return num.Value;
            }

            num = ConvertToNumber(_input.DefaultValue);

            if (num.HasValue)
            {
                return num.Value;
            }

            return GetDefaultStepBase();
        }

        protected virtual Double GetDefaultStepBase()
        {
            return 0.0;
        }

        protected virtual Double GetDefaultStep()
        {
            return 1.0;
        }

        protected virtual Double GetStepScaleFactor()
        {
            return 1.0;
        }

        #endregion

        #region Helper

        protected static Boolean IsInvalidPattern(String pattern, String value)
        {
            if (!String.IsNullOrEmpty(pattern) && !String.IsNullOrEmpty(value))
            {
                try
                {
                    var regex = new Regex(pattern, RegexOptions.ECMAScript);
                    return !regex.IsMatch(value);
                }
                catch { }
            }

            return false;
        }

        protected static Double? ToNumber(String value)
        {
            if (!String.IsNullOrEmpty(value) && Number.IsMatch(value))
            {
                return Double.Parse(value);
            }

            return null;<commit_msg>Fix for tests when running with a different culture: Use CultureInfo.InvariantCulture for parsing doubles<commit_after>﻿namespace AngleSharp.Html.InputTypes
{
    using AngleSharp.Dom;
    using AngleSharp.Dom.Html;
    using AngleSharp.Extensions;
    using System;
    using System.Globalization;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Base type for the all input field types. Primarely from:
    /// http://www.w3.org/TR/html5/forms.html#range-state-(type=range)
    /// </summary>
    abstract class BaseInputType
    {
        #region Fields

        protected static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        protected static readonly Regex Number = new Regex("^\\-?\\d+(\\.\\d+)?([eE][\\-\\+]?\\d+)?$");

        private readonly IHtmlInputElement _input;
        private readonly Boolean _validate;
        private readonly String _name;

        #endregion

        #region ctor

        public BaseInputType(IHtmlInputElement input, String name, Boolean validate)
        {
            _input = input;
            _validate = validate;
            _name = name;
        }

        #endregion

        #region Properties

        public String Name
        {
            get { return _name; }
        }

        public Boolean CanBeValidated
        {
            get { return _validate; }
        }

        public IHtmlInputElement Input
        {
            get { return _input; }
        }

        #endregion

        #region Methods

        public virtual Boolean IsAppendingData(IHtmlElement submitter)
        {
            return true;
        }

        public virtual void Check(ValidityState state)
        {
        }

        public virtual Double? ConvertToNumber(String value)
        {
            return null;
        }

        public virtual String ConvertFromNumber(Double value)
        {
            throw new DomException(DomError.InvalidState);
        }

        public virtual DateTime? ConvertToDate(String value)
        {
            return null;
        }

        public virtual String ConvertFromDate(DateTime value)
        {
            throw new DomException(DomError.InvalidState);
        }

        public virtual void ConstructDataSet(FormDataSet dataSet)
        {
            dataSet.Append(_input.Name, _input.Value, _input.Type);
        }

        public virtual void DoStep(Int32 n)
        {
            throw new DomException(DomError.InvalidState);
        }

        #endregion

        #region Step

        protected Boolean IsStepMismatch()
        {
            var step = GetStep();
            var value = ConvertToNumber(_input.Value);
            var offset = GetStepBase();
            return step != 0.0 && (value - offset) % step != 0.0;
        }

        protected Double GetStep()
        {
            var step = _input.Step;

            if (String.IsNullOrEmpty(step))
            {
                return GetDefaultStep() * GetStepScaleFactor();
            }
            else if (step.Isi(Keywords.Any))
            {
                return 0.0;
            }

            var num = ToNumber(step);

            if (num.HasValue == false || num <= 0.0)
            {
                return GetDefaultStep() * GetStepScaleFactor();
            }

            return num.Value * GetStepScaleFactor();
        }

        private Double GetStepBase()
        {
            var num = ConvertToNumber(_input.Minimum);

            if (num.HasValue)
            {
                return num.Value;
            }

            num = ConvertToNumber(_input.DefaultValue);

            if (num.HasValue)
            {
                return num.Value;
            }

            return GetDefaultStepBase();
        }

        protected virtual Double GetDefaultStepBase()
        {
            return 0.0;
        }

        protected virtual Double GetDefaultStep()
        {
            return 1.0;
        }

        protected virtual Double GetStepScaleFactor()
        {
            return 1.0;
        }

        #endregion

        #region Helper

        protected static Boolean IsInvalidPattern(String pattern, String value)
        {
            if (!String.IsNullOrEmpty(pattern) && !String.IsNullOrEmpty(value))
            {
                try
                {
                    var regex = new Regex(pattern, RegexOptions.ECMAScript);
                    return !regex.IsMatch(value);
                }
                catch { }
            }

            return false;
        }

        protected static Double? ToNumber(String value)
        {
            if (!String.IsNullOrEmpty(value) && Number.IsMatch(value))
            {
                return Double.Parse(value, CultureInfo.InvariantCulture);
            }

            return null;
{'fraction_non_alphanumeric': 0.08438552188552188, 'fraction_numerical': 0.005681818181818182, 'mean_word_length': 1.4328128999232148, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5892798', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2347, 'n_words': 585}