Skip to content

Commit fe27db8

Browse files
committed
Fix SonarQube CodeQA issue
1 parent 412c4ac commit fe27db8

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

CollapseLauncher/XAMLs/Theme/CustomControls/CommunityToolkit.Labs/MarkdownTextBlock/Extensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace CommunityToolkit.Labs.WinUI.Labs.MarkdownTextBlock;
2424

25-
public static class Extensions
25+
public static partial class Extensions
2626
{
2727
public const string Blue = "#FF0000FF";
2828
public const string White = "#FFFFFFFF";
@@ -387,6 +387,9 @@ public static bool IsAtInsertionPosition(this TextPointer position, LogicalDirec
387387
return !currentRect.Equals(nextRect);
388388
}
389389

390+
[GeneratedRegex(@"([^)\s]+)\s*=\s*\d+x\d+\s*", RegexOptions.NonBacktracking, 3000)]
391+
internal static partial Regex GetUrlWidthAndHeight();
392+
390393
public static string RemoveImageSize(string? url)
391394
{
392395
if (string.IsNullOrEmpty(url))
@@ -395,10 +398,8 @@ public static string RemoveImageSize(string? url)
395398
}
396399

397400
// Create a regex pattern to match the URL with width and height
398-
string pattern = @"([^)\s]+)\s*=\s*\d+x\d+\s*";
399-
400401
// Replace the matched URL with the URL only
401-
string result = Regex.Replace(url, pattern, "$1", RegexOptions.Compiled);
402+
string result = GetUrlWidthAndHeight().Replace(url, "$1");
402403

403404
return result;
404405
}

CollapseLauncher/XAMLs/Theme/CustomControls/CommunityToolkit.Labs/MarkdownTextBlock/Renderers/ObjectRenderers/HtmlBlockRenderer.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@
99
using System;
1010
using System.Text;
1111
using System.Text.RegularExpressions;
12-
#pragma warning disable SYSLIB1045
1312

1413
namespace CommunityToolkit.Labs.WinUI.Labs.MarkdownTextBlock.Renderers.ObjectRenderers;
1514

16-
internal class HtmlBlockRenderer : MarkdownObjectRenderer<WinUIRenderer, HtmlBlock>
15+
internal partial class HtmlBlockRenderer : MarkdownObjectRenderer<WinUIRenderer, HtmlBlock>
1716
{
17+
[GeneratedRegex(@"\t|\n|\r", RegexOptions.NonBacktracking, 3000)]
18+
internal static partial Regex GetTabAndNewLineMatch();
19+
20+
[GeneratedRegex("&nbsp;", RegexOptions.NonBacktracking, 3000)]
21+
internal static partial Regex GetNonBreakingSpaceMatch();
22+
1823
protected override void Write(WinUIRenderer renderer, HtmlBlock obj)
1924
{
20-
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
21-
if (obj == null) throw new ArgumentNullException(nameof(obj));
25+
ArgumentNullException.ThrowIfNull(renderer);
26+
ArgumentNullException.ThrowIfNull(obj);
2227

2328
StringBuilder stringBuilder = new();
2429
foreach (StringLine line in obj.Lines.Lines)
@@ -31,8 +36,8 @@ protected override void Write(WinUIRenderer renderer, HtmlBlock obj)
3136
stringBuilder.AppendLine(lineText);
3237
}
3338

34-
string html = Regex.Replace(stringBuilder.ToString(), @"\t|\n|\r", "", RegexOptions.Compiled);
35-
html = Regex.Replace(html, "&nbsp;", " ", RegexOptions.Compiled);
39+
string html = GetTabAndNewLineMatch().Replace(stringBuilder.ToString(), "");
40+
html = GetNonBreakingSpaceMatch().Replace(html, " ");
3641
HtmlDocument doc = new();
3742
doc.LoadHtml(html);
3843
HtmlWriter.WriteHtml(renderer, doc.DocumentNode.ChildNodes);

Hi3Helper.TaskScheduler/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using System.Text.RegularExpressions;
67
// ReSharper disable once IdentifierTypo
78
// ReSharper disable StringLiteralTypo
89

@@ -101,7 +102,7 @@ private static TaskSched Create(TaskService taskService, string scheduleName, st
101102
private static void TryDelete(TaskService taskService, string scheduleName)
102103
{
103104
// Try to get the tasks
104-
TaskSched[] tasks = taskService.FindAllTasks(new System.Text.RegularExpressions.Regex(scheduleName), false);
105+
TaskSched[] tasks = taskService.FindAllTasks(new Regex(scheduleName, RegexOptions.Compiled, TimeSpan.FromSeconds(5)), false);
105106

106107
// If null, then ignore
107108
if (tasks == null || tasks.Length == 0)
@@ -124,7 +125,7 @@ private static void TryDelete(TaskService taskService, string scheduleName)
124125
private static TaskSched? GetExistingTask(TaskService taskService, string scheduleName, string execPath)
125126
{
126127
// Try to get the tasks
127-
TaskSched[] tasks = taskService.FindAllTasks(new System.Text.RegularExpressions.Regex(scheduleName), false);
128+
TaskSched[] tasks = taskService.FindAllTasks(new Regex(scheduleName, RegexOptions.Compiled, TimeSpan.FromSeconds(5)), false);
128129

129130
// Try to get the first task
130131
TaskSched? task = tasks?

0 commit comments

Comments
 (0)