From 0dcca6125eb4b7ca2a8a85e82647829735958542 Mon Sep 17 00:00:00 2001 From: Alexander Zaytsev Date: Fri, 19 Dec 2014 20:26:21 +1300 Subject: [PATCH] Use RegexOptions.Compiled if available --- src/Humanizer/Humanizer.csproj | 1 + src/Humanizer/InflectorExtensions.cs | 2 +- src/Humanizer/RegexOptionsUtil.cs | 21 +++++++++++++++++++++ src/Humanizer/RomanNumeralExtensions.cs | 2 +- src/Humanizer/StringHumanizeExtensions.cs | 4 ++-- 5 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/Humanizer/RegexOptionsUtil.cs diff --git a/src/Humanizer/Humanizer.csproj b/src/Humanizer/Humanizer.csproj index dadba6555..048cef089 100644 --- a/src/Humanizer/Humanizer.csproj +++ b/src/Humanizer/Humanizer.csproj @@ -109,6 +109,7 @@ + True diff --git a/src/Humanizer/InflectorExtensions.cs b/src/Humanizer/InflectorExtensions.cs index e0ea8bde6..52155269c 100644 --- a/src/Humanizer/InflectorExtensions.cs +++ b/src/Humanizer/InflectorExtensions.cs @@ -127,7 +127,7 @@ private class Rule public Rule(string pattern, string replacement) { - _regex = new Regex(pattern, RegexOptions.IgnoreCase); + _regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptionsUtil.Compiled); _replacement = replacement; } diff --git a/src/Humanizer/RegexOptionsUtil.cs b/src/Humanizer/RegexOptionsUtil.cs new file mode 100644 index 000000000..655a7627e --- /dev/null +++ b/src/Humanizer/RegexOptionsUtil.cs @@ -0,0 +1,21 @@ +using System; +using System.Text.RegularExpressions; + +namespace Humanizer +{ + internal static class RegexOptionsUtil + { + private static readonly RegexOptions _compiled; + + static RegexOptionsUtil() + { + RegexOptions compiled; + _compiled = Enum.TryParse("Compiled", out compiled) ? compiled : RegexOptions.None; + } + + public static RegexOptions Compiled + { + get { return _compiled; } + } + } +} \ No newline at end of file diff --git a/src/Humanizer/RomanNumeralExtensions.cs b/src/Humanizer/RomanNumeralExtensions.cs index d6e72b772..9ae801178 100644 --- a/src/Humanizer/RomanNumeralExtensions.cs +++ b/src/Humanizer/RomanNumeralExtensions.cs @@ -34,7 +34,7 @@ public static class RomanNumeralExtensions private static readonly Regex ValidRomanNumeral = new Regex( "^(?i:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$", - RegexOptions.None); + RegexOptionsUtil.Compiled); /// /// Converts Roman numbers into integer diff --git a/src/Humanizer/StringHumanizeExtensions.cs b/src/Humanizer/StringHumanizeExtensions.cs index 3a95a8816..d438222f0 100644 --- a/src/Humanizer/StringHumanizeExtensions.cs +++ b/src/Humanizer/StringHumanizeExtensions.cs @@ -15,8 +15,8 @@ public static class StringHumanizeExtensions static StringHumanizeExtensions() { PascalCaseWordPartsRegex = new Regex(@"[A-Z]?[a-z]+|[0-9]+|[A-Z]+(?=[A-Z][a-z]|[0-9]|\b)", - RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture); - FreestandingSpacingCharRegex = new Regex(@"\s[-_]|[-_]\s"); + RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptionsUtil.Compiled); + FreestandingSpacingCharRegex = new Regex(@"\s[-_]|[-_]\s", RegexOptionsUtil.Compiled); } static string FromUnderscoreDashSeparatedWords (string input)