Skip to content

Commit

Permalink
Use GeneratedRegexAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
SeriaWei committed Oct 9, 2023
1 parent 16a32e8 commit 9b07bd6
Show file tree
Hide file tree
Showing 24 changed files with 107 additions and 68 deletions.
11 changes: 6 additions & 5 deletions src/EasyFrameWork/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@

namespace Easy
{
public class Version
public partial class Version
{
static Regex regex = new Regex(@"^(\d+\.)?(\d+\.)?(\d+\.)?(\d+)$", RegexOptions.Compiled);

public int Major { get; set; }
public int Minor { get; set; }
public int Revision { get; set; }
public int Build { get; set; }

public static Version Parse(string version)
{
if (!regex.IsMatch(version))
if (!RegexVersion().IsMatch(version))
{
throw new Exception("Version format is not supported.");
}
Expand Down Expand Up @@ -161,7 +159,7 @@ public override bool Equals(object obj)
return false;
}

return this == (obj as Version);
return this == obj as Version;
}

public override int GetHashCode()
Expand All @@ -172,5 +170,8 @@ public override string ToString()
{
return $"{Major}.{Minor}.{Revision}.{Build}";
}

[GeneratedRegex("^(\\d+\\.)?(\\d+\\.)?(\\d+\\.)?(\\d+)$", RegexOptions.Compiled)]
private static partial Regex RegexVersion();
}
}
8 changes: 5 additions & 3 deletions src/ZKEACMS.EventAction/HttpParser/Helps/HttpBaseInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

namespace ZKEACMS.EventAction.HttpParser.Helps
{
class HttpBaseInfo
partial class HttpBaseInfo
{
private static Regex _httpVersionValidate = new Regex(@"HTTP/\d.\d", RegexOptions.Compiled);
private static readonly HashSet<string> validHttpVerbs = new HashSet<string> { "GET", "POST", "HEAD", "PUT", "DELETE", "MOVE", "TRACE", "CONNECT", "OPTIONS" };

public string Method { get; set; }
Expand Down Expand Up @@ -63,9 +62,12 @@ private static bool IsValidUri(string url)

private void SetHttpVersion(string version)
{
if (!_httpVersionValidate.IsMatch(version)) return;
if (!RegexHttpVersionValidate().IsMatch(version)) return;

HttpVersion = version.Trim();
}

[GeneratedRegex("HTTP/\\d.\\d", RegexOptions.Compiled)]
private static partial Regex RegexHttpVersionValidate();
}
}
8 changes: 5 additions & 3 deletions src/ZKEACMS.EventAction/Service/EventActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@

namespace ZKEACMS.EventAction.Service
{
public sealed class EventActionService : ServiceBase<Models.EventAction>, IEventActionService
public sealed partial class EventActionService : ServiceBase<Models.EventAction>, IEventActionService
{
private const string EventActionChanged = "EventActionChanged";

private readonly ICacheManager<EventActionService> _cacheManager;
private readonly ISignals _signals;
private readonly ILogger<EventActionService> _logger;
private readonly ILocalize _localize;
private static Regex _encoder = new Regex(@":( +)({{)([\w|\.| |\[|\]]+)(}})", RegexOptions.Compiled);

public EventActionService(IApplicationContext applicationContext, CMSDbContext dbContext,
ICacheManager<EventActionService> cacheManager, ILogger<EventActionService> logger, ILocalize localize, ISignals signals)
Expand Down Expand Up @@ -126,10 +125,13 @@ private ServiceResult<Models.EventAction> ValidateActions(Models.EventAction ite
}
private string Encode(string actions)
{
return _encoder.Replace(actions, evaluator =>
return RegexEncoder().Replace(actions, evaluator =>
{
return $":{ evaluator.Groups[1].Value }'{ evaluator.Groups[2].Value + evaluator.Groups[3].Value + evaluator.Groups[4].Value}'";
});
}

[GeneratedRegex(":( +)({{)([\\w|\\.| |\\[|\\]]+)(}})", RegexOptions.Compiled)]
private static partial Regex RegexEncoder();
}
}
8 changes: 5 additions & 3 deletions src/ZKEACMS.FormGenerator/Service/FormDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

namespace ZKEACMS.FormGenerator.Service
{
public class FormDataService : ServiceBase<FormData, CMSDbContext>, IFormDataService
public partial class FormDataService : ServiceBase<FormData, CMSDbContext>, IFormDataService
{
private readonly IFormService _formService;
private readonly IFormDataItemService _formDataItemService;
private readonly IEnumerable<IFormDataValidator> _formDataValidators;
private readonly IEventManager _eventManager;
private static Regex _nameRegex = new Regex(@"(\w+)\[(\d+)\]", RegexOptions.Compiled);

public FormDataService(IApplicationContext applicationContext,
CMSDbContext dbContext,
Expand Down Expand Up @@ -115,7 +114,7 @@ public ServiceResult<FormData> SaveForm(IFormCollection formCollection, string f

foreach (var item in formCollection.Keys)
{
string id = _nameRegex.Replace(item, evaluator =>
string id = RegexName().Replace(item, evaluator =>
{
return evaluator.Groups[1].Value;
});
Expand Down Expand Up @@ -216,5 +215,8 @@ public MemoryStream ExportByForm(string formId)
return excel.ToMemoryStream();
}
}

[GeneratedRegex("(\\w+)\\[(\\d+)\\]", RegexOptions.Compiled)]
private static partial Regex RegexName();
}
}
6 changes: 3 additions & 3 deletions src/ZKEACMS.Product/Views/Widget.ProductDetail.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@
@Html.LabelFor(m => m.Price)
@if (Model.RebatePrice.HasValue)
{
<del class="text-muted"><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((Model.Price ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</del>
<span class="price"><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((Model.RebatePrice ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</span>
<del class="text-muted"><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((Model.Price ?? 0).ToString("F2"), "<small>.$1</small>"))</del>
<span class="price"><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((Model.RebatePrice ?? 0).ToString("F2"), "<small>.$1</small>"))</span>
}
else
{
<span class="price"><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((Model.Price ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</span>
<span class="price"><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((Model.Price ?? 0).ToString("F2"), "<small>.$1</small>"))</span>
}
</p>
}
Expand Down
6 changes: 3 additions & 3 deletions src/ZKEACMS.Product/Views/Widget.ProductGallery.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
<div class="pull-left price">
@if (item.RebatePrice.HasValue)
{
<del class="text-muted"><small><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.Price ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</small></del>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.RebatePrice ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</strong>
<del class="text-muted"><small><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.Price ?? 0).ToString("F2"), "<small>.$1</small>"))</small></del>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.RebatePrice ?? 0).ToString("F2"), "<small>.$1</small>"))</strong>
}
else
{
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.Price ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</strong>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.Price ?? 0).ToString("F2"), "<small>.$1</small>"))</strong>
}
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/ZKEACMS.Product/Views/Widget.ProductList.A.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
<p class="price">
@if (item.RebatePrice.HasValue)
{
<del class="text-muted"><small><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.Price ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</small></del>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.RebatePrice ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</strong>
<del class="text-muted"><small><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.Price ?? 0).ToString("F2"), "<small>.$1</small>"))</small></del>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.RebatePrice ?? 0).ToString("F2"), "<small>.$1</small>"))</strong>
}
else
{
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.Price ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</strong>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.Price ?? 0).ToString("F2"), "<small>.$1</small>"))</strong>
}
</p>
}
Expand Down
6 changes: 3 additions & 3 deletions src/ZKEACMS.Product/Views/Widget.ProductList.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
<div class="pull-left price">
@if (item.RebatePrice.HasValue)
{
<del class="text-muted"><small><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.Price ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</small></del>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.RebatePrice ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</strong>
<del class="text-muted"><small><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.Price ?? 0).ToString("F2"), "<small>.$1</small>"))</small></del>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.RebatePrice ?? 0).ToString("F2"), "<small>.$1</small>"))</strong>
}
else
{
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(Regex.Replace((item.Price ?? 0).ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))</strong>
<strong><small>@Html.CurrencySymbol()</small>@Html.Raw(CustomRegex.DecimalPoint().Replace((item.Price ?? 0).ToString("F2"), "<small>.$1</small>"))</strong>
}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override IActionResult GetList(DataTableOption query)
}
public IActionResult RedirectTo(string path)
{
if (path.IsNotNullAndWhiteSpace() && path.IndexOf(".html", StringComparison.OrdinalIgnoreCase) < 0 && CustomRegex.PostIdRegex.IsMatch(path))
if (path.IsNotNullAndWhiteSpace() && path.IndexOf(".html", StringComparison.OrdinalIgnoreCase) < 0 && CustomRegex.PostId().IsMatch(path))
{
return RedirectPermanent($"~/{(path ?? "").Trim()}.html");
}
Expand Down
2 changes: 1 addition & 1 deletion src/ZKEACMS.Redirection/Models/UrlRedirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void ParsePattern()
{
if (!string.IsNullOrEmpty(InComingUrl))
{
_regex = new Regex(InComingUrl, RegexOptions.Compiled | RegexOptions.IgnoreCase);
_regex = new Regex(InComingUrl, RegexOptions.IgnoreCase);
}
}
public string GetDestinationURL(string inComingUrl)
Expand Down
2 changes: 1 addition & 1 deletion src/ZKEACMS.Redirection/RedirectRouteConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public bool Match(HttpContext httpContext, IRouter route, string routeKey, Route
{
path = path.TrimEnd('/');
}
if (path.IndexOf(".html", StringComparison.OrdinalIgnoreCase) < 0 && CustomRegex.PostIdRegex.IsMatch(path))
if (path.IndexOf(".html", StringComparison.OrdinalIgnoreCase) < 0 && CustomRegex.PostId().IsMatch(path))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
using Microsoft.AspNetCore.Mvc;
using Easy.Mvc.Authorize;
using Easy.Extend;
using System.Text.RegularExpressions;

namespace ZKEACMS.SectionWidget.Controllers
{
public class SectionContentVideoController : Controller
public partial class SectionContentVideoController : Controller
{
private readonly ISectionContentProviderService _sectionContentProviderService;

Expand Down Expand Up @@ -63,7 +64,28 @@ public JsonResult Delete(string Id)

public ActionResult Play(string Id)
{
return View(_sectionContentProviderService.GetContent(Id));
SectionContentVideo video = _sectionContentProviderService.GetContent(Id) as SectionContentVideo;
if (video == null) return NotFound();

if (video.Code.IsNotNullAndWhiteSpace())
{
video.Code = RegexVideoCodeWidth().Replace(video.Code, eva =>
{
return $"{eva.Groups[1].Value}{eva.Groups[2].Value}{eva.Groups[3].Value}{eva.Groups[4].Value}{eva.Groups[5].Value}{video.Width}{eva.Groups[7].Value}";
});
video.Code = RegexVideoCodeHeight().Replace(video.Code, eva =>
{
return $"{eva.Groups[1].Value}{eva.Groups[2].Value}{eva.Groups[3].Value}{eva.Groups[4].Value}{eva.Groups[5].Value}{video.Height}{eva.Groups[7].Value}";
});
}

return View(video);
}

[GeneratedRegex("(\\s)?(width)(\\s)?([=:])(\\s)?(\\d+)(\\s)?", RegexOptions.IgnoreCase)]
private static partial Regex RegexVideoCodeWidth();

[GeneratedRegex("(\\s)?(height)(\\s)?([=:])(\\s)?(\\d+)(\\s)?", RegexOptions.IgnoreCase)]
private static partial Regex RegexVideoCodeHeight();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
}
else if (video.Code.IsNotNullAndWhiteSpace())
{
video.Code = Regex.Replace(video.Code, @"("")?width("")?=("")?(\d+)("")?", "width=\"" + (video.Width ?? 798) + "\"");
video.Code = Regex.Replace(video.Code, @"("")?width("")?:("")?(\d+)("")?", "width:" + (video.Width ?? 798));

video.Code = Regex.Replace(video.Code, @"("")?height("")?=("")?(\d+)("")?", "height=" + (video.Height ?? 500));
video.Code = Regex.Replace(video.Code, @"("")?height("")?:("")?(\d+)("")?", "height:" + (video.Height ?? 500));

@Html.Raw(video.Code)
}
</div>
2 changes: 1 addition & 1 deletion src/ZKEACMS.Shop/Views/Basket/CheckOut.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<div class="clearfix">
<div class="text-right">
<span class="price">
@Html.CurrencySymbol() @Html.Raw(Regex.Replace(item.Price.ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))
@Html.CurrencySymbol() @Html.Raw(CustomRegex.DecimalPoint().Replace(item.Price.ToString("F2"), "<small>.$1</small>"))
x <span class="quantity">@item.Quantity</span>
</span>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/ZKEACMS.Shop/Views/Basket/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<div class="text-right">
<span class="price">
@Html.CurrencySymbol()
@Html.Raw(Regex.Replace(item.Price.ToString("F2"), @"\.(\d+)", "<small>.$1</small>"))
@Html.Raw(CustomRegex.DecimalPoint().Replace(item.Price.ToString("F2"), "<small>.$1</small>"))
x <span class="quantity">@item.Quantity</span>
</span>
<a href="javascript:" class="remove glyphicon glyphicon-trash"></a>
Expand Down
2 changes: 1 addition & 1 deletion src/ZKEACMS.SpiderLog/Models/SearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public bool IsMatch(string userAgent)
{
if (userAgent.IsNullOrEmpty()) return false;

if (_regex == null) _regex = new Regex(Regex, RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (_regex == null) _regex = new Regex(Regex, RegexOptions.IgnoreCase);

return _regex.IsMatch(userAgent);
}
Expand Down

0 comments on commit 9b07bd6

Please sign in to comment.