<commit_before>using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Routing;

namespace Magurany.Web.Routing.RegularExpressions
{
	public sealed class RegexRoute : Route
	{
		private readonly Regex m_Pattern;

		public static bool UseLegacy { get; set; }

		public RegexRoute(string url, string pattern, IRouteHandler routeHandler) : base(url, routeHandler)
		{
			if(pattern == null)
			{
				throw new ArgumentNullException("pattern");
			}

			m_Pattern = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
		}

		public override RouteData GetRouteData(HttpContextBase httpContext)
		{
			if(httpContext == null)
			{
				throw new ArgumentNullException("httpContext");
			}

			string url = httpContext.Request.AppRelativeCurrentExecutionFilePath;
			if (!UseLegacy)
			{
				url = url.Substring(2) + httpContext.Request.PathInfo;
			}
			Match match = m_Pattern.Match(url);

			if(match.Success)
			{
				RouteData data = new RouteData(this, RouteHandler);

				foreach(string groupName in m_Pattern.GetGroupNames())
				{
					if(!data.Values.ContainsKey(groupName))
					{
						Group group = match.Groups[groupName];

						if((group != null) && group.Success)
						{
							data.Values.Add(groupName, group.Value);
						}
					}
				}

				foreach(KeyValuePair<string, object> pair in Defaults)
				{
					if(!data.Values.ContainsKey(pair.Key))
					{
						data.Values.Add(pair.Key, pair.Value);
					}
				}

				if (DataTokens != null)
				{
					foreach (KeyValuePair<string, object> pair in DataTokens)
					{
						data.DataTokens[pair.Key] = pair.Value;
					}
				}

				return data;
			}

			return null;
		}
	}
}
<commit_msg>Added support for AutoConstraints generation<commit_after>using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Routing;

namespace Magurany.Web.Routing.RegularExpressions
{
	public sealed class RegexRoute : Route
	{
		private readonly Regex m_Pattern;

		public static bool UseLegacy { get; set; }

		public static bool AutoConstraints { get; set; }

		public RegexRoute(string url, string pattern, IRouteHandler routeHandler) : this(url, pattern, null, routeHandler)
		{
		}

		public RegexRoute(string url, string pattern, object constraints, IRouteHandler routeHandler) : base(url, routeHandler)
		{
			if (pattern == null)
			{
				throw new ArgumentNullException("pattern");
			}

			m_Pattern = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
			Constraints = new RouteValueDictionary(constraints);
			if (AutoConstraints)
			{
				GenerateConstraintsFromPattern(pattern);
			}
		}

		public override RouteData GetRouteData(HttpContextBase httpContext)
		{
			if(httpContext == null)
			{
				throw new ArgumentNullException("httpContext");
			}

			string url = httpContext.Request.AppRelativeCurrentExecutionFilePath;
			if (!UseLegacy)
			{
				url = url.Substring(2) + httpContext.Request.PathInfo;
			}
			Match match = m_Pattern.Match(url);

			if(match.Success)
			{
				RouteData data = new RouteData(this, RouteHandler);

				foreach(string groupName in m_Pattern.GetGroupNames())
				{
					if(!data.Values.ContainsKey(groupName))
					{
						Group group = match.Groups[groupName];

						if((group != null) && group.Success)
						{
							data.Values.Add(groupName, group.Value);
						}
					}
				}

				foreach(KeyValuePair<string, object> pair in Defaults)
				{
					if(!data.Values.ContainsKey(pair.Key))
					{
						data.Values.Add(pair.Key, pair.Value);
					}
				}

				if (DataTokens != null)
				{
					foreach (KeyValuePair<string, object> pair in DataTokens)
					{
						data.DataTokens[pair.Key] = pair.Value;
					}
				}

				return data;
			}

			return null;
		}

		private void GenerateConstraintsFromPattern(string pattern) {
			Regex names = new Regex(@"{(\w+)}");
			MatchCollection matches = names.Matches(Url);

			foreach (Match match in matches)
			{
				string key = match.Groups[1].ToString();
				if (!this.Constraints.ContainsKey(key))
				{
					// Using the amazing pattern found here: http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396452.aspx
					Regex r = new Regex(String.Format(@"\(\?<{0}>([^\(\)]*(((?<Open>\()[^\(\)]*)+((?<Close-Open>\))[^\(\)]*)+)*(?(Open)(?!)))\)", key));
					MatchCollection constraint = r.Matches(pattern);
					if (constraint.Count == 1) {
						this.Constraints[match.Groups[1].ToString()] = constraint[0].Groups[1].ToString();
					}
					else
					{
						throw new InvalidOperationException("constraints");
					}
				}
			}
		}
	}
}

{'fraction_non_alphanumeric': 0.12586058519793458, 'fraction_numerical': 0.004733218588640275, 'mean_word_length': 3.253430924062214, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '5033510', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1441, 'n_words': 293}