From d76bc1f8128d0beec0a760eac7ad8559e5342f7a Mon Sep 17 00:00:00 2001 From: Sinai Date: Fri, 30 Apr 2021 23:43:27 +1000 Subject: [PATCH] Add ContainsIgnoreCase helper to reduce string alloc, cleanup --- src/Core/Search/SearchProvider.cs | 12 ++++++------ src/Core/Utility/MiscUtility.cs | 18 ++++++++++++++++++ src/Core/{Unity => Utility}/UnityHelpers.cs | 0 src/UI/Widgets/AutoComplete/TypeCompleter.cs | 6 +++--- src/UI/Widgets/ButtonList/ButtonListSource.cs | 2 +- .../Widgets/TransformTree/CachedTransform.cs | 6 +----- src/UI/Widgets/TransformTree/TransformTree.cs | 8 +++----- src/UnityExplorer.csproj | 3 ++- 8 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 src/Core/Utility/MiscUtility.cs rename src/Core/{Unity => Utility}/UnityHelpers.cs (100%) diff --git a/src/Core/Search/SearchProvider.cs b/src/Core/Search/SearchProvider.cs index 122cf32..004303e 100644 --- a/src/Core/Search/SearchProvider.cs +++ b/src/Core/Search/SearchProvider.cs @@ -17,13 +17,13 @@ namespace UnityExplorer.Core.Search var nameFilter = ""; if (!string.IsNullOrEmpty(input)) - nameFilter = input.ToLower(); + nameFilter = input; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var type in asm.TryGetTypes().Where(it => it.IsSealed && it.IsAbstract)) { - if (!string.IsNullOrEmpty(nameFilter) && !type.FullName.ToLower().Contains(nameFilter)) + if (!string.IsNullOrEmpty(nameFilter) && !type.FullName.ContainsIgnoreCase(nameFilter)) continue; list.Add(type); @@ -53,7 +53,7 @@ namespace UnityExplorer.Core.Search var nameFilter = ""; if (!string.IsNullOrEmpty(input)) - nameFilter = input.ToLower(); + nameFilter = input; var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; @@ -64,7 +64,7 @@ namespace UnityExplorer.Core.Search { try { - if (!string.IsNullOrEmpty(nameFilter) && !type.FullName.ToLower().Contains(nameFilter)) + if (!string.IsNullOrEmpty(nameFilter) && !type.FullName.ContainsIgnoreCase(nameFilter)) continue; ReflectionProvider.Instance.FindSingleton(s_instanceNames, type, flags, instances); @@ -138,7 +138,7 @@ namespace UnityExplorer.Core.Search string nameFilter = null; if (!string.IsNullOrEmpty(input)) - nameFilter = input.ToLower(); + nameFilter = input; bool canGetGameObject = (sceneFilter != SceneFilter.Any || childFilter != ChildFilter.Any) && (context == SearchContext.GameObject || typeof(Component).IsAssignableFrom(searchType)); @@ -152,7 +152,7 @@ namespace UnityExplorer.Core.Search foreach (var obj in allObjects) { // name check - if (!string.IsNullOrEmpty(nameFilter) && !obj.name.ToLower().Contains(nameFilter)) + if (!string.IsNullOrEmpty(nameFilter) && !obj.name.ContainsIgnoreCase(nameFilter)) continue; if (canGetGameObject) diff --git a/src/Core/Utility/MiscUtility.cs b/src/Core/Utility/MiscUtility.cs new file mode 100644 index 0000000..46a0d26 --- /dev/null +++ b/src/Core/Utility/MiscUtility.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; + +namespace UnityExplorer +{ + public static class MiscUtility + { + private static CultureInfo _enCulture = new CultureInfo("en-US"); + + public static bool ContainsIgnoreCase(this string _this, string s) + { + return _enCulture.CompareInfo.IndexOf(_this, s, CompareOptions.IgnoreCase) >= 0; + } + } +} diff --git a/src/Core/Unity/UnityHelpers.cs b/src/Core/Utility/UnityHelpers.cs similarity index 100% rename from src/Core/Unity/UnityHelpers.cs rename to src/Core/Utility/UnityHelpers.cs diff --git a/src/UI/Widgets/AutoComplete/TypeCompleter.cs b/src/UI/Widgets/AutoComplete/TypeCompleter.cs index 82720ab..0b3e5f9 100644 --- a/src/UI/Widgets/AutoComplete/TypeCompleter.cs +++ b/src/UI/Widgets/AutoComplete/TypeCompleter.cs @@ -59,7 +59,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete string displayName = Utility.SignatureHighlighter.ParseFullSyntax(type, true); string fullName = RuntimeProvider.Instance.Reflection.GetDeobfuscatedType(type).FullName; - string filteredName = fullName.ToLower(); + string filteredName = fullName; list.Add(new CachedType { @@ -89,7 +89,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete timeOfLastCheck = Time.time; - value = value?.ToLower() ?? ""; + value = value ?? ""; if (string.IsNullOrEmpty(value)) { @@ -118,7 +118,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete if (added.Contains(entry.FullNameValue)) continue; - if (entry.FullNameForFilter.Contains(value)) + if (entry.FullNameForFilter.ContainsIgnoreCase(value)) AddToDict(entry); added.Add(entry.FullNameValue); diff --git a/src/UI/Widgets/ButtonList/ButtonListSource.cs b/src/UI/Widgets/ButtonList/ButtonListSource.cs index 2b07ea8..71bbe08 100644 --- a/src/UI/Widgets/ButtonList/ButtonListSource.cs +++ b/src/UI/Widgets/ButtonList/ButtonListSource.cs @@ -25,7 +25,7 @@ namespace UnityExplorer.UI.Widgets public string CurrentFilter { get => currentFilter; - set => currentFilter = value?.ToLower() ?? ""; + set => currentFilter = value ?? ""; } private string currentFilter; diff --git a/src/UI/Widgets/TransformTree/CachedTransform.cs b/src/UI/Widgets/TransformTree/CachedTransform.cs index 89d04ad..21d1c91 100644 --- a/src/UI/Widgets/TransformTree/CachedTransform.cs +++ b/src/UI/Widgets/TransformTree/CachedTransform.cs @@ -13,8 +13,6 @@ namespace UnityExplorer.UI.Widgets public int InstanceID { get; private set; } public CachedTransform Parent { get; internal set; } - //public string Name { get; internal set; } - //public int ChildCount { get; internal set; } public int Depth { get; internal set; } public bool Expanded => Tree.IsCellExpanded(InstanceID); @@ -24,15 +22,13 @@ namespace UnityExplorer.UI.Widgets Tree = tree; Value = transform; Parent = parent; + InstanceID = transform.GetInstanceID(); Update(transform, depth); } public void Update(Transform transform, int depth) { Value = transform; - InstanceID = transform.GetInstanceID(); - //Name = Value.name; - //ChildCount = Value.childCount; Depth = depth; } } diff --git a/src/UI/Widgets/TransformTree/TransformTree.cs b/src/UI/Widgets/TransformTree/TransformTree.cs index 9162d14..5d11f1e 100644 --- a/src/UI/Widgets/TransformTree/TransformTree.cs +++ b/src/UI/Widgets/TransformTree/TransformTree.cs @@ -22,7 +22,7 @@ namespace UnityExplorer.UI.Widgets get => currentFilter; set { - currentFilter = value?.ToLower() ?? ""; + currentFilter = value ?? ""; if (!wasFiltering && Filtering) wasFiltering = true; else if (wasFiltering && !Filtering) @@ -133,7 +133,7 @@ namespace UnityExplorer.UI.Widgets private bool FilterHierarchy(Transform obj) { - if (obj.name.ToLower().Contains(currentFilter)) + if (obj.name.ContainsIgnoreCase(currentFilter)) return true; if (obj.childCount <= 0) @@ -146,10 +146,8 @@ namespace UnityExplorer.UI.Widgets return false; } - public void SetCell(TransformCell iCell, int index) + public void SetCell(TransformCell cell, int index) { - var cell = iCell as TransformCell; - if (index < displayedObjects.Count) cell.ConfigureCell(displayedObjects[index], index); else diff --git a/src/UnityExplorer.csproj b/src/UnityExplorer.csproj index 573c880..ab7c893 100644 --- a/src/UnityExplorer.csproj +++ b/src/UnityExplorer.csproj @@ -219,6 +219,7 @@ + @@ -280,7 +281,7 @@ - +