Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed unnecessary Regex constructors for #353 #354

Closed
wants to merge 5 commits into from
Closed

Removed unnecessary Regex constructors for #353 #354

wants to merge 5 commits into from

Conversation

dlras2
Copy link
Contributor

@dlras2 dlras2 commented Nov 29, 2014

As per #353, StringHumanizeExtensions.Humanize and StringHumanizeExtensions.FromPascalCase no longer call the Regex constructor on each call. Humanize uses the static Regex.IsMatch method, and FromPascalCase instantiates its needed Regex once during the static constructor. See the new call traces below (taken from running the unit tests.)

image
image

@@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not replace this one with a static version too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's definitely an option, for consistency. I didn't just because it was a shorter regex to inline.

I've made that change in my branch, as well as a few others for even more performance improvements. Should I make a new pull request with those, or can they be added here somehow? (Still pretty new to the whole pull request thing.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By simply pushing more commits to your PR branch adds them here.
You already got managed this.

static StringHumanizeExtensions()
{
PascalCaseWordPartsRegex = new Regex(@"[A-Z]?[a-z]+|[0-9]+|[A-Z]+(?=[A-Z][a-z]|[0-9]|\b)",
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be RegexOptions.Compiled as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but that option is not supported in portable class libraries, so we're stuck with interpreted expressions. This could be worked around by removing the regex object entirely and "pre-compiling" the expression to a function ourselves, but I'm not sure the added complexity would be worth the performance. Perhaps I'll give it a try, just to see what benefit there is.

@MehdiK
Copy link
Member

MehdiK commented Dec 11, 2014

Thanks for the fix. This is merged now.

In the future, please rebase your code before submitting a PR. Also you should add your PR to the release-notes file which I did for this PR.

@MehdiK MehdiK closed this Dec 11, 2014
@MehdiK
Copy link
Member

MehdiK commented Dec 18, 2014

Thanks for the contribution. This is now available on NuGet as v1.32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants