Skip to content

Commit

Permalink
Removed unnecessary Regex constructors for Humanizr#353
Browse files Browse the repository at this point in the history
  • Loading branch information
dlras2 authored and Borzoo committed Dec 23, 2014
1 parent 0ef938e commit e15c2b7
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Humanizer/StringHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ namespace Humanizer
/// </summary>
public static class StringHumanizeExtensions
{
static string FromUnderscoreDashSeparatedWords (string input)
{
return String.Join(" ", input.Split(new[] {'_', '-'}));
}
private static readonly Regex PascalCaseWordBoundaryRegex;

static string FromPascalCase(string input)
static StringHumanizeExtensions()
{
var pascalCaseWordBoundaryRegex = new Regex(@"
PascalCaseWordBoundaryRegex = new Regex(@"
(?# word to word, number or acronym)
(?<=[a-z])(?=[A-Z0-9])|
(?# number to word or acronym)
Expand All @@ -28,8 +25,16 @@ static string FromPascalCase(string input)
(?# words/acronyms/numbers separated by space)
(?<=[^\s])(?=[\s])
", RegexOptions.IgnorePatternWhitespace);
}

var result = pascalCaseWordBoundaryRegex
static string FromUnderscoreDashSeparatedWords (string input)
{
return String.Join(" ", input.Split(new[] {'_', '-'}));
}

static string FromPascalCase(string input)
{
var result = PascalCaseWordBoundaryRegex
.Split(input)
.Select(word =>
word.Trim().ToCharArray().All(Char.IsUpper) && word.Length > 1
Expand All @@ -55,8 +60,7 @@ public static string Humanize(this string input)

// if input contains a dash or underscore which preceeds or follows a space (or both, i.g. free-standing)
// remove the dash/underscore and run it through FromPascalCase
Regex r = new Regex(@"[\s]{1}[-_]|[-_][\s]{1}", RegexOptions.IgnoreCase);
if (r.IsMatch(input))
if (Regex.IsMatch(input, @"[\s]{1}[-_]|[-_][\s]{1}", RegexOptions.IgnoreCase))
return FromPascalCase(FromUnderscoreDashSeparatedWords(input));

if (input.Contains("_") || input.Contains("-"))
Expand Down

0 comments on commit e15c2b7

Please sign in to comment.