Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve string comparison performance #1299

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Wpf.Ui/Appearance/ApplicationAccentColorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using Wpf.Ui.Extensions;
using Wpf.Ui.Interop;

namespace Wpf.Ui.Appearance;
Expand Down
10 changes: 5 additions & 5 deletions src/Wpf.Ui/Appearance/ApplicationThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static void Apply(FrameworkElement frameworkElement)

ResourceDictionary[] resourcesRemove = frameworkElement
.Resources.MergedDictionaries.Where(e => e.Source is not null)
.Where(e => e.Source.ToString().ToLower().Contains(LibraryNamespace))
.Where(e => e.Source.ToString().Contains(LibraryNamespace, StringComparison.OrdinalIgnoreCase))
.ToArray();

foreach (ResourceDictionary? resource in UiApplication.Current.Resources.MergedDictionaries)
Expand Down Expand Up @@ -288,19 +288,19 @@ private static void FetchApplicationTheme()
return;
}

string themeUri = themeDictionary.Source.ToString().Trim().ToLower();
string themeUri = themeDictionary.Source.ToString();

if (themeUri.Contains("light"))
if (themeUri.Contains("light", StringComparison.OrdinalIgnoreCase))
{
_cachedApplicationTheme = ApplicationTheme.Light;
}

if (themeUri.Contains("dark"))
if (themeUri.Contains("dark", StringComparison.OrdinalIgnoreCase))
{
_cachedApplicationTheme = ApplicationTheme.Dark;
}

if (themeUri.Contains("highcontrast"))
if (themeUri.Contains("highcontrast", StringComparison.OrdinalIgnoreCase))
{
_cachedApplicationTheme = ApplicationTheme.HighContrast;
}
Expand Down
27 changes: 11 additions & 16 deletions src/Wpf.Ui/Appearance/ResourceDictionaryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,17 @@ public bool HasDictionary(string resourceLookup)
return null;
}

resourceLookup = resourceLookup.ToLower().Trim();

foreach (ResourceDictionary t in applicationDictionaries)
{
string resourceDictionaryUri;

if (t?.Source != null)
{
resourceDictionaryUri = t.Source.ToString().ToLower().Trim();
resourceDictionaryUri = t.Source.ToString();

if (
resourceDictionaryUri.Contains(SearchNamespace)
&& resourceDictionaryUri.Contains(resourceLookup)
resourceDictionaryUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase)
&& resourceDictionaryUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase)
)
{
return t;
Expand All @@ -72,11 +70,11 @@ public bool HasDictionary(string resourceLookup)
continue;
}

resourceDictionaryUri = t1.Source.ToString().ToLower().Trim();
resourceDictionaryUri = t1.Source.ToString();

if (
!resourceDictionaryUri.Contains(SearchNamespace)
|| !resourceDictionaryUri.Contains(resourceLookup)
!resourceDictionaryUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase)
|| !resourceDictionaryUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase)
)
{
continue;
Expand Down Expand Up @@ -107,17 +105,15 @@ public bool UpdateDictionary(string resourceLookup, Uri? newResourceUri)
return false;
}

resourceLookup = resourceLookup.ToLower().Trim();

for (var i = 0; i < applicationDictionaries.Count; i++)
{
string sourceUri;

if (applicationDictionaries[i]?.Source != null)
{
sourceUri = applicationDictionaries[i].Source.ToString().ToLower().Trim();
sourceUri = applicationDictionaries[i].Source.ToString();

if (sourceUri.Contains(SearchNamespace) && sourceUri.Contains(resourceLookup))
if (sourceUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase) && sourceUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase))
{
applicationDictionaries[i] = new() { Source = newResourceUri };

Expand All @@ -134,11 +130,10 @@ public bool UpdateDictionary(string resourceLookup, Uri? newResourceUri)

sourceUri = applicationDictionaries[i]
.MergedDictionaries[j]
.Source.ToString()
.ToLower()
.Trim();
.Source
.ToString();

if (!sourceUri.Contains(SearchNamespace) || !sourceUri.Contains(resourceLookup))
if (!sourceUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase) || !sourceUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase))
{
continue;
}
Expand Down
24 changes: 11 additions & 13 deletions src/Wpf.Ui/Appearance/SystemThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,60 +69,58 @@ private static SystemTheme GetCurrentSystemTheme()

if (!string.IsNullOrEmpty(currentTheme))
{
currentTheme = currentTheme.ToLower().Trim();

// This may be changed in the next versions, check the Insider previews
if (currentTheme.Contains("basic.theme"))
if (currentTheme.Contains("basic.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.Light;
}

if (currentTheme.Contains("aero.theme"))
if (currentTheme.Contains("aero.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.Light;
}

if (currentTheme.Contains("dark.theme"))
if (currentTheme.Contains("dark.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.Dark;
}

if (currentTheme.Contains("hcblack.theme"))
if (currentTheme.Contains("hcblack.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.HCBlack;
}

if (currentTheme.Contains("hcwhite.theme"))
if (currentTheme.Contains("hcwhite.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.HCWhite;
}

if (currentTheme.Contains("hc1.theme"))
if (currentTheme.Contains("hc1.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.HC1;
}

if (currentTheme.Contains("hc2.theme"))
if (currentTheme.Contains("hc2.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.HC2;
}

if (currentTheme.Contains("themea.theme"))
if (currentTheme.Contains("themea.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.Glow;
}

if (currentTheme.Contains("themeb.theme"))
if (currentTheme.Contains("themeb.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.CapturedMotion;
}

if (currentTheme.Contains("themec.theme"))
if (currentTheme.Contains("themec.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.Sunrise;
}

if (currentTheme.Contains("themed.theme"))
if (currentTheme.Contains("themed.theme", StringComparison.OrdinalIgnoreCase))
{
return SystemTheme.Flow;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Wpf.Ui/Controls/AutoSuggestBox/AutoSuggestBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,13 @@ private void DefaultFiltering(string text)
return;
}

var splitText = text.ToLowerInvariant().Split(' ');
var splitText = text.Split(' ');
var suitableItems = OriginalItemsSource
.Cast<object>()
.Where(item =>
{
var itemText = GetStringFromObj(item)?.ToLowerInvariant();
return splitText.All(key => itemText?.Contains(key) ?? false);
var itemText = GetStringFromObj(item);
return splitText.All(key => itemText?.Contains(key, StringComparison.OrdinalIgnoreCase) ?? false);
})
.ToList();

Expand Down
2 changes: 1 addition & 1 deletion src/Wpf.Ui/Controls/BreadcrumbBar/BreadcrumbBarItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/* Based on Windows UI Library */

using Wpf.Ui.Converters;


// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;
Expand Down
2 changes: 0 additions & 2 deletions src/Wpf.Ui/Controls/EventIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using Wpf.Ui.Extensions;

namespace Wpf.Ui.Controls;

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions src/Wpf.Ui/Controls/GridView/GridViewRowPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Reflection;
using System.Windows.Controls;

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/Wpf.Ui/Controls/IconElement/IconElementConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using Wpf.Ui.Extensions;

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;

Expand Down
1 change: 0 additions & 1 deletion src/Wpf.Ui/Controls/IconElement/IconSourceElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// All Rights Reserved.

using System.Windows.Markup;
using Wpf.Ui.Converters;

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;
Expand Down
2 changes: 0 additions & 2 deletions src/Wpf.Ui/Controls/IconElement/SymbolIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using Wpf.Ui.Extensions;

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;

Expand Down
2 changes: 0 additions & 2 deletions src/Wpf.Ui/Controls/TextBlock/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using Wpf.Ui.Extensions;

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;

Expand Down
1 change: 0 additions & 1 deletion src/Wpf.Ui/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System.Diagnostics;
using System.Windows.Controls;
using Wpf.Ui.Converters;
using Wpf.Ui.Input;

// ReSharper disable once CheckNamespace
Expand Down
1 change: 0 additions & 1 deletion src/Wpf.Ui/Controls/TitleBar/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics;
using System.Windows.Input;
using Wpf.Ui.Designer;
using Wpf.Ui.Extensions;
using Wpf.Ui.Input;
using Wpf.Ui.Interop;

Expand Down
1 change: 0 additions & 1 deletion src/Wpf.Ui/Controls/TitleBar/TitleBarButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using Wpf.Ui.Extensions;
using Wpf.Ui.Interop;

// ReSharper disable once CheckNamespace
Expand Down
26 changes: 26 additions & 0 deletions src/Wpf.Ui/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

#if NETFRAMEWORK
using System.Diagnostics.Contracts;

namespace Wpf.Ui.Extensions;

internal static class StringExtensions
{
/// <summary>
/// Returns a value indicating whether a specified string occurs within this string, using the specified comparison rules.
/// </summary>
/// <param name="source">Source string.</param>
/// <param name="value">The string to seek.</param>
/// <param name="comparison">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns>true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.</returns>
[Pure]
public static bool Contains(this string source, string value, StringComparison comparison)
{
return source.IndexOf(value, comparison) >= 0;
}
}
#endif
1 change: 1 addition & 0 deletions src/Wpf.Ui/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
global using System.Windows;
global using System.Windows.Interop;
global using System.Windows.Media;
global using Wpf.Ui.Extensions;
2 changes: 1 addition & 1 deletion src/Wpf.Ui/UiApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static bool ApplicationHasResources(Application application)
return application
.Resources.MergedDictionaries.Where(e => e.Source is not null)
.Any(e =>
e.Source.ToString().ToLower().Contains(Appearance.ApplicationThemeManager.LibraryNamespace)
e.Source.ToString().Contains(Appearance.ApplicationThemeManager.LibraryNamespace, StringComparison.OrdinalIgnoreCase)
);
}
}
Loading