From d77668d77a155f2c6f40252151ee97374b5b62cf Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Fri, 3 May 2024 22:43:11 -0700 Subject: [PATCH] fix: Cleans up some string allocations from trim (#1759) --- Projects/UOContent/Accounting/AccountHandler.cs | 2 +- .../UOContent/Engines/Bulk Orders/Books/BulkOrderBook.cs | 3 ++- Projects/UOContent/Gumps/Guilds/GuildListGump.cs | 8 +------- Projects/UOContent/Gumps/Guilds/GuildMobileListGump.cs | 8 +------- .../Gumps/Guilds/New Guild System/GuildInfoGump.cs | 5 +++-- .../Gumps/Guilds/New Guild System/GuildMemberInfoGump.cs | 3 ++- .../Gumps/Guilds/New Guild System/OtherGuildInfo.cs | 2 +- Projects/UOContent/Gumps/RunebookGump.cs | 3 ++- Projects/UOContent/Multis/Boats/BaseBoat.cs | 3 ++- 9 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Projects/UOContent/Accounting/AccountHandler.cs b/Projects/UOContent/Accounting/AccountHandler.cs index f115fcac0..a35c2741a 100644 --- a/Projects/UOContent/Accounting/AccountHandler.cs +++ b/Projects/UOContent/Accounting/AccountHandler.cs @@ -309,7 +309,7 @@ public static class AccountHandler if (Accounts.GetAccount(un) is not Account acct) { // To prevent someone from making an account of just '' or a bunch of meaningless spaces - if (AutoAccountCreation && un.Trim().Length > 0) + if (AutoAccountCreation && !string.IsNullOrWhiteSpace(un)) { e.State.Account = acct = CreateAccount(e.State, un, pw); e.Accepted = acct?.CheckAccess(e.State) ?? false; diff --git a/Projects/UOContent/Engines/Bulk Orders/Books/BulkOrderBook.cs b/Projects/UOContent/Engines/Bulk Orders/Books/BulkOrderBook.cs index 26b860ea9..2ffa7e602 100644 --- a/Projects/UOContent/Engines/Bulk Orders/Books/BulkOrderBook.cs +++ b/Projects/UOContent/Engines/Bulk Orders/Books/BulkOrderBook.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using ModernUO.Serialization; using Server.ContextMenus; @@ -287,7 +288,7 @@ public partial class BulkOrderBook : Item, ISecurable if (from.CheckAlive() && m_Book.IsChildOf(from.Backpack)) { - m_Book.BookName = text.Trim().FixHtml(); + m_Book.BookName = text.AsSpan().Trim().FixHtml(); from.SendLocalizedMessage(1062480); // The bulk order book's name has been changed. } diff --git a/Projects/UOContent/Gumps/Guilds/GuildListGump.cs b/Projects/UOContent/Gumps/Guilds/GuildListGump.cs index a91f740f4..c69761f95 100644 --- a/Projects/UOContent/Gumps/Guilds/GuildListGump.cs +++ b/Projects/UOContent/Gumps/Guilds/GuildListGump.cs @@ -50,13 +50,7 @@ namespace Server.Gumps var g = m_List[i]; - string name; - - if ((name = g.Name) != null && (name = name.Trim()).Length <= 0) - { - name = "(empty)"; - } - + string name = g.Name?.Trim().DefaultIfNullOrEmpty("(empty)"); AddLabel(radio ? 55 : 20, 35 + i % 11 * 30, 0, name); } } diff --git a/Projects/UOContent/Gumps/Guilds/GuildMobileListGump.cs b/Projects/UOContent/Gumps/Guilds/GuildMobileListGump.cs index b4666db8f..9041044e6 100644 --- a/Projects/UOContent/Gumps/Guilds/GuildMobileListGump.cs +++ b/Projects/UOContent/Gumps/Guilds/GuildMobileListGump.cs @@ -51,13 +51,7 @@ namespace Server.Gumps var m = m_List[i]; - string name; - - if ((name = m.Name) != null && (name = name.Trim()).Length <= 0) - { - name = "(empty)"; - } - + string name = m.Name?.Trim().DefaultIfNullOrEmpty("(empty)"); AddLabel(radio ? 55 : 20, 35 + i % 11 * 30, 0, name); } } diff --git a/Projects/UOContent/Gumps/Guilds/New Guild System/GuildInfoGump.cs b/Projects/UOContent/Gumps/Guilds/New Guild System/GuildInfoGump.cs index 55c1f86c8..cb8ab9f56 100644 --- a/Projects/UOContent/Gumps/Guilds/New Guild System/GuildInfoGump.cs +++ b/Projects/UOContent/Gumps/Guilds/New Guild System/GuildInfoGump.cs @@ -1,3 +1,4 @@ +using System; using Server.Factions; using Server.Gumps; using Server.Mobiles; @@ -173,7 +174,7 @@ namespace Server.Guilds return; } - var charter = text.Trim().FixHtml(); + var charter = text.AsSpan().Trim().FixHtml(); if (charter.Length > 50) { @@ -193,7 +194,7 @@ namespace Server.Guilds return; } - var site = text.Trim().FixHtml(); + var site = text.AsSpan().Trim().FixHtml(); if (site.Length > 50) { diff --git a/Projects/UOContent/Gumps/Guilds/New Guild System/GuildMemberInfoGump.cs b/Projects/UOContent/Gumps/Guilds/New Guild System/GuildMemberInfoGump.cs index 5fac2a927..960988946 100644 --- a/Projects/UOContent/Gumps/Guilds/New Guild System/GuildMemberInfoGump.cs +++ b/Projects/UOContent/Gumps/Guilds/New Guild System/GuildMemberInfoGump.cs @@ -1,3 +1,4 @@ +using System; using Server.Gumps; using Server.Mobiles; using Server.Network; @@ -265,7 +266,7 @@ namespace Server.Guilds return; } - var title = text.Trim().FixHtml(); + var title = text.AsSpan().Trim().FixHtml(); if (title.Length > 20) { diff --git a/Projects/UOContent/Gumps/Guilds/New Guild System/OtherGuildInfo.cs b/Projects/UOContent/Gumps/Guilds/New Guild System/OtherGuildInfo.cs index a923a47b3..7f922bfcc 100644 --- a/Projects/UOContent/Gumps/Guilds/New Guild System/OtherGuildInfo.cs +++ b/Projects/UOContent/Gumps/Guilds/New Guild System/OtherGuildInfo.cs @@ -677,7 +677,7 @@ namespace Server.Guilds } else { - var name = text.Trim().FixHtml(); + var name = text.AsSpan().Trim().FixHtml(); if (!CheckProfanity(name)) { diff --git a/Projects/UOContent/Gumps/RunebookGump.cs b/Projects/UOContent/Gumps/RunebookGump.cs index e9422f289..90f688ecd 100644 --- a/Projects/UOContent/Gumps/RunebookGump.cs +++ b/Projects/UOContent/Gumps/RunebookGump.cs @@ -1,3 +1,4 @@ +using System; using Server.Items; using Server.Multis; using Server.Network; @@ -480,7 +481,7 @@ namespace Server.Gumps if (m_Book.CheckAccess(from)) { - m_Book.Description = text.Trim().FixHtml(); + m_Book.Description = text.AsSpan().Trim().FixHtml(); from.CloseGump(); from.SendGump(new RunebookGump(from, m_Book)); diff --git a/Projects/UOContent/Multis/Boats/BaseBoat.cs b/Projects/UOContent/Multis/Boats/BaseBoat.cs index 2c881045b..e1fbcbe72 100644 --- a/Projects/UOContent/Multis/Boats/BaseBoat.cs +++ b/Projects/UOContent/Multis/Boats/BaseBoat.cs @@ -784,7 +784,8 @@ namespace Server.Multis if (e.Speech.Length > 8) { - Rename(e.Speech[8..].Trim().DefaultIfNullOrEmpty(null)); + var newName = e.Speech.AsSpan(8).Trim(); + Rename(newName.Length == 0 ? null : newName.ToString()); } }