Skip to content

Commit

Permalink
Fix SonarQube CodeQA issue
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Jan 27, 2025
1 parent 412c4ac commit fe27db8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace CommunityToolkit.Labs.WinUI.Labs.MarkdownTextBlock;

public static class Extensions
public static partial class Extensions
{
public const string Blue = "#FF0000FF";
public const string White = "#FFFFFFFF";
Expand Down Expand Up @@ -387,6 +387,9 @@ public static bool IsAtInsertionPosition(this TextPointer position, LogicalDirec
return !currentRect.Equals(nextRect);
}

[GeneratedRegex(@"([^)\s]+)\s*=\s*\d+x\d+\s*", RegexOptions.NonBacktracking, 3000)]
internal static partial Regex GetUrlWidthAndHeight();

public static string RemoveImageSize(string? url)
{
if (string.IsNullOrEmpty(url))
Expand All @@ -395,10 +398,8 @@ public static string RemoveImageSize(string? url)
}

// Create a regex pattern to match the URL with width and height
string pattern = @"([^)\s]+)\s*=\s*\d+x\d+\s*";

// Replace the matched URL with the URL only
string result = Regex.Replace(url, pattern, "$1", RegexOptions.Compiled);
string result = GetUrlWidthAndHeight().Replace(url, "$1");

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@
using System;
using System.Text;
using System.Text.RegularExpressions;
#pragma warning disable SYSLIB1045

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

internal class HtmlBlockRenderer : MarkdownObjectRenderer<WinUIRenderer, HtmlBlock>
internal partial class HtmlBlockRenderer : MarkdownObjectRenderer<WinUIRenderer, HtmlBlock>
{
[GeneratedRegex(@"\t|\n|\r", RegexOptions.NonBacktracking, 3000)]
internal static partial Regex GetTabAndNewLineMatch();

[GeneratedRegex("&nbsp;", RegexOptions.NonBacktracking, 3000)]
internal static partial Regex GetNonBreakingSpaceMatch();

protected override void Write(WinUIRenderer renderer, HtmlBlock obj)
{
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
if (obj == null) throw new ArgumentNullException(nameof(obj));
ArgumentNullException.ThrowIfNull(renderer);
ArgumentNullException.ThrowIfNull(obj);

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

string html = Regex.Replace(stringBuilder.ToString(), @"\t|\n|\r", "", RegexOptions.Compiled);
html = Regex.Replace(html, "&nbsp;", " ", RegexOptions.Compiled);
string html = GetTabAndNewLineMatch().Replace(stringBuilder.ToString(), "");
html = GetNonBreakingSpaceMatch().Replace(html, " ");
HtmlDocument doc = new();
doc.LoadHtml(html);
HtmlWriter.WriteHtml(renderer, doc.DocumentNode.ChildNodes);
Expand Down
5 changes: 3 additions & 2 deletions Hi3Helper.TaskScheduler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
// ReSharper disable once IdentifierTypo
// ReSharper disable StringLiteralTypo

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

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

// Try to get the first task
TaskSched? task = tasks?
Expand Down

0 comments on commit fe27db8

Please sign in to comment.