Skip to content

Commit

Permalink
[Trimming] Remove unnecessary regular expressions (#21497)
Browse files Browse the repository at this point in the history
* Remove unused usign in Routing

* Remove regex for checking x:Class in XamlLoader

* Remove simple regex from KeyboardAutoManagerScroll in iOS

* Remove simple regex from PathBuilder
  • Loading branch information
simonrozsival authored Apr 9, 2024
1 parent b591e59 commit 2a76d88
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/Controls/src/Core/Routing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;

namespace Microsoft.Maui.Controls
{
Expand Down
80 changes: 76 additions & 4 deletions src/Controls/src/Xaml/XamlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Xaml.Diagnostics;
Expand Down Expand Up @@ -355,12 +354,85 @@ static string ReadResourceAsXaml(Type type, Assembly assembly, string likelyTarg

var xaml = reader.ReadToEnd();

var pattern = $"x:Class *= *\"{type.FullName}\"";
var regex = new Regex(pattern, RegexOptions.ECMAScript);
if (regex.IsMatch(xaml) || xaml.IndexOf($"x:Class=\"{type.FullName}\"") != -1)
if (ContainsXClass(xaml, type.FullName))
{
return xaml;
}
}
return null;

// Equivalent to regex $"x:Class *= *\"{fullName}\""
static bool ContainsXClass(string xaml, string fullName)
{
int index = 0;
while (index >= 0 && index < xaml.Length)
{
if (FindNextXClass()
&& SkipWhitespaces()
&& NextCharacter('=')
&& SkipWhitespaces()
&& NextCharacter('"')
&& NextFullName()
&& NextCharacter('"'))
{
return true;
}
}

return false;

bool FindNextXClass()
{
const string xClass = "x:Class";

index = xaml.IndexOf(xClass, startIndex: index);
if (index < 0)
{
return false;
}

index += xClass.Length;
return true;
}

bool SkipWhitespaces()
{
while (index < xaml.Length && xaml[index] == ' ')
{
index++;
}

return true;
}

bool NextCharacter(char character)
{
if (index < xaml.Length && xaml[index] != character)
{
return false;
}

index++;
return true;
}

bool NextFullName()
{
if (index >= xaml.Length - fullName.Length)
{
return false;
}

var slice = xaml.AsSpan().Slice(index, fullName.Length);
if (!MemoryExtensions.Equals(slice, fullName.AsSpan(), StringComparison.Ordinal))
{
return false;
}

index += fullName.Length;
return true;
}
}
}
}
}
18 changes: 15 additions & 3 deletions src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CoreGraphics;
Expand Down Expand Up @@ -254,8 +254,7 @@ static void AnimateHidingKeyboard()
if (description is null)
return null;

// remove everything except for numbers and commas
var temp = Regex.Replace(description, @"[^0-9,]", "");
var temp = RemoveEverythingExceptForNumbersAndCommas(description);
var dimensions = temp.Split(',');

if (dimensions.Length == 4
Expand All @@ -268,6 +267,19 @@ static void AnimateHidingKeyboard()
}

return null;

static string RemoveEverythingExceptForNumbersAndCommas(string input)
{
var sb = new StringBuilder(input.Length);
foreach (var character in input)
{
if (char.IsDigit(character) || character == ',')
{
sb.Append(character);
}
}
return sb.ToString();
}
}

// Used to debounce calls from different oberservers so we can be sure
Expand Down
22 changes: 20 additions & 2 deletions src/Graphics/src/Graphics/PathBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.Maui.Graphics
{
Expand Down Expand Up @@ -117,7 +116,7 @@ public PathF BuildPath(string pathAsString)
#else
pathAsString = pathAsString.Replace("Infinity", "0", StringComparison.Ordinal);
#endif
pathAsString = Regex.Replace(pathAsString, "([a-zA-Z])", " $1 ");
pathAsString = SeparateLetterCharsWithSpaces(pathAsString);
#if NETSTANDARD2_0
pathAsString = pathAsString.Replace("-", " -");
pathAsString = pathAsString.Replace(" E -", "E-");
Expand Down Expand Up @@ -214,6 +213,25 @@ public PathF BuildPath(string pathAsString)
#endif
}

static string SeparateLetterCharsWithSpaces(string input)
{
var sb = new StringBuilder(input.Length, maxCapacity: 3 * input.Length);
foreach (var character in input)
{
if (char.IsLetter(character))
{
sb.Append(' ');
sb.Append(character);
sb.Append(' ');
}
else
{
sb.Append(character);
}
}
return sb.ToString();
}

return _path;
}

Expand Down

0 comments on commit 2a76d88

Please sign in to comment.