Cleaned up regex used for literals matching and replacement. Changed literal patterns to use non-capturing groups for performance.

This commit is contained in:
Daniel Stolt
2014-07-28 17:12:25 +02:00
parent 4a10352ba6
commit 3afae3d538
3 changed files with 4 additions and 5 deletions

View File

@@ -218,7 +218,7 @@ namespace Orchard.Framework.Tests.Localization {
}
private DateParts GetExpectedDateParts(DateTime date, string format) {
var formatWithoutLiterals = Regex.Replace(format, @"((?<!\\)'(.*?)((?<!\\)')|(?<!\\)""(.*?)((?<!\\)""))", "");
var formatWithoutLiterals = Regex.Replace(format, @"(?<!\\)'(.*?)(?<!\\)'|(?<!\\)""(.*?)(?<!\\)""", "");
return new DateParts(
formatWithoutLiterals.Contains('y') ? date.Year : 0,
formatWithoutLiterals.Contains('M') ? date.Month : 0,
@@ -227,7 +227,7 @@ namespace Orchard.Framework.Tests.Localization {
}
private TimeParts GetExpectedTimeParts(DateTime time, string format) {
var formatWithoutLiterals = Regex.Replace(format, @"((?<!\\)'(.*?)((?<!\\)')|(?<!\\)""(.*?)((?<!\\)""))", "");
var formatWithoutLiterals = Regex.Replace(format, @"(?<!\\)'(.*?)(?<!\\)'|(?<!\\)""(.*?)(?<!\\)""", "");
return new TimeParts(
formatWithoutLiterals.Contains('H') || format.Contains('h') ? time.Hour : 0,
formatWithoutLiterals.Contains('m') ? time.Minute : 0,

View File

@@ -323,7 +323,8 @@ namespace Orchard.Framework.Localization.Services {
result = EscapeForRegex(result);
// Transform all literals to corresponding wildcard matches.
result = Regex.Replace(result, @"(?<!\\)'(.*?)((?<!\\)')", m => String.Format("(.{{{0}}})", m.Value.Replace("\\", "").Length - 2));
//result = Regex.Replace(result, @"(?<!\\)'(.*?)(?<!\\)'|(?<!\\)""(.*?)(?<!\\)""", m => EscapeForRegex(m.Value.Trim('\'', '"')));
result = Regex.Replace(result, @"(?<!\\)'(.*?)(?<!\\)'|(?<!\\)""(.*?)(?<!\\)""", m => String.Format("(?:.{{{0}}})", m.Value.Replace("\\", "").Length - 2));
// Transform all DateTime format specifiers into corresponding Regex captures.
result = result.ReplaceAll(replacements);

View File

@@ -42,9 +42,7 @@ struct DateLocalizationOptions {
TODO:
* Test for proper handling of fraction (f) format specifier - suspect it does not work properly in current state
* Literal parts in format strings should be transformed to corresponding literal in Regex, not just into a wildcard, otherwise the resulting wildcard in some cases will match subsequent non-literal parts of the date/time string.
* Rewrite DefaultDateLocalizationServices
* Write unit tests for DefaultDateLocalizationServices
* Add warning message when saving unsupported combination in settings
* Add support for the different Gregorian calendar types
* User ICultureManager and ICultureRepository to get the current CultureInfo in other classes