<commit_before>﻿#region License

// 
//  Copyright 2013 <NAME>
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 

#endregion

using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;

namespace Nova.Controls
{
    /// <summary>
    /// A textbox that accepts only numeric input.
    /// </summary>
    public class NumericTextBox : TextBox
    {
        private Regex _customValidNumberRegex;

        /// <summary>
        /// The valid number regex
        /// </summary>
        private static readonly Regex ValidNumberRegex;

        /// <summary>
        /// The numeric settings property
        /// </summary>
        public static readonly DependencyProperty NumericSettingsProperty;

        /// <summary>
        /// Gets or sets the numeric settings.
        /// </summary>
        /// <value>
        /// The numeric settings.
        /// </value>
        public NumericSettings NumericSettings
        {
            get { return (NumericSettings)GetValue(NumericSettingsProperty); }
            set { SetValue(NumericSettingsProperty, value); }
        }

        /// <summary>
        /// Initializes the <see cref="NumericTextBox" /> class.
        /// </summary>
        static NumericTextBox()
        {
            ValidNumberRegex = new Regex(@"^[-+]?[0-9]*([\.,][0-9]*)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant);

            var frameworkPropertyMetadata = new FrameworkPropertyMetadata
                {
                    CoerceValueCallback = CoerceTextValueCallback
                };

            TextProperty.AddOwner(typeof(NumericTextBox), frameworkPropertyMetadata);
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericTextBox), new FrameworkPropertyMetadata(typeof(NumericTextBox)));
            NumericSettingsProperty = DependencyProperty.Register("NumericSettings", typeof(NumericSettings), typeof(NumericTextBox), new PropertyMetadata(NumericSettingsChanged));
        }

        /// <summary>
        /// Coerces the text value callback.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="baseValue">The base value.</param>
        /// <returns></returns>
        private static object CoerceTextValueCallback(DependencyObject dependencyObject, object baseValue)
        {
            var numericTextBox = (NumericTextBox) dependencyObject;
            
            var regex = numericTextBox._customValidNumberRegex ?? ValidNumberRegex;

            var value = (string) baseValue;

            if (regex.IsMatch(value))
                return value;

            var text = ((NumericTextBox) dependencyObject).Text;

            if (regex.IsMatch(text))
                return text;
            
            return string.Empty;
        }

        /// <summary>
        /// Triggers when the numeric settings change.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
        private static void NumericSettingsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
		    var settings = e.NewValue as NumericSettings;
            
            var numericTextBox = (NumericTextBox) dependencyObject;

			if (settings == null)
			{
				numericTextBox._customValidNumberRegex = null;
				return;
			}
            
            var regexString = @"^[-+]?[0-9]{0," + settings.MaximumNumbers + "}";

			if (settings.MaximumDecimals > 0)
			{
				regexString += @"([\.,][0-9]{0," + settings.MaximumDecimals + "})?";
			}

			regexString += "$";

            numericTextBox._customValidNumberRegex = new Regex(regexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant);
        }
    }
}<commit_msg>Update NumericTextBox.cs<commit_after>﻿#region License

// 
//  Copyright 2013 <NAME>
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 

#endregion

using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;

namespace Nova.Controls
{
    /// <summary>
    /// A textbox that accepts only numeric input.
    /// </summary>
    public class NumericTextBox : TextBox
    {
        private Regex _customValidNumberRegex;

        /// <summary>
        /// The valid number regex
        /// </summary>
        private static readonly Regex ValidNumberRegex;

        /// <summary>
        /// The numeric settings property
        /// </summary>
        public static readonly DependencyProperty NumericSettingsProperty;

        /// <summary>
        /// Gets or sets the numeric settings.
        /// </summary>
        /// <value>
        /// The numeric settings.
        /// </value>
        public NumericSettings NumericSettings
        {
            get { return (NumericSettings)GetValue(NumericSettingsProperty); }
            set { SetValue(NumericSettingsProperty, value); }
        }

        /// <summary>
        /// Initializes the <see cref="NumericTextBox" /> class.
        /// </summary>
        static NumericTextBox()
        {
            ValidNumberRegex = new Regex(@"^[-+]?[0-9]*([\.,][0-9]*)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant);

            var frameworkPropertyMetadata = new FrameworkPropertyMetadata
                {
                    CoerceValueCallback = CoerceTextValueCallback
                };

            TextProperty.AddOwner(typeof(NumericTextBox), frameworkPropertyMetadata);
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericTextBox), new FrameworkPropertyMetadata(typeof(NumericTextBox)));
            NumericSettingsProperty = DependencyProperty.Register("NumericSettings", typeof(NumericSettings), typeof(NumericTextBox), new PropertyMetadata(NumericSettingsChanged));
        }

        /// <summary>
        /// Coerces the text value callback.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="baseValue">The base value.</param>
        /// <returns></returns>
        private static object CoerceTextValueCallback(DependencyObject dependencyObject, object baseValue)
        {
            var numericTextBox = (NumericTextBox) dependencyObject;
            
            var regex = numericTextBox._customValidNumberRegex ?? ValidNumberRegex;

            var value = (string) baseValue;

            if (regex.IsMatch(value))
                return value;

            var text = ((NumericTextBox) dependencyObject).Text;

            if (regex.IsMatch(text))
                return text;
            
            return string.Empty;
        }

        /// <summary>
        /// Triggers when the numeric settings change.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
        private static void NumericSettingsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
		    var settings = e.NewValue as NumericSettings;
            
            var numericTextBox = (NumericTextBox) dependencyObject;

			if (settings == null)
			{
				numericTextBox._customValidNumberRegex = null;
				return;
			}
            

			var regexString = settings.MaximumNumbers > 0
			                     ? @"^[-+]?[0-9]{0," + settings.MaximumNumbers + "}"
			                     : @"^[-+]?[0-9]*";

			if (settings.MaximumDecimals > 0)
			{
				regexString += @"([\.,][0-9]{0," + settings.MaximumDecimals + "})?";
			}

			regexString += "$";

            numericTextBox._customValidNumberRegex = new Regex(regexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant);
        }
    }
}

{'fraction_non_alphanumeric': 0.1107182320441989, 'fraction_numerical': 0.004530386740331491, 'mean_word_length': 2.3117453347969263, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 64, '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': '903242', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2192, 'n_words': 592}