Skip to content

Commit

Permalink
fix: Cleans up FixHtml (#1757)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed May 4, 2024
1 parent af67c48 commit becd7aa
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 63 deletions.
10 changes: 5 additions & 5 deletions Projects/Server/Mobiles/Mobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3273,9 +3273,9 @@ public virtual void AddNameProperties(IPropertyList list)

string suffix = hasTitle switch
{
true when hasGuild => $" {Title} [{Utility.FixHtmlFormattable(guild.Abbreviation)}]",
true when hasGuild => $" {Title} [{guild.Abbreviation.FixHtmlFormattable()}]",
true => $" {Title}",
false when hasGuild => $" [{Utility.FixHtmlFormattable(guild.Abbreviation)}]",
false when hasGuild => $" [{guild.Abbreviation.FixHtmlFormattable()}]",
_ => " "
};

Expand All @@ -3291,16 +3291,16 @@ public virtual void AddNameProperties(IPropertyList list)
{
if (NewGuildDisplay)
{
list.Add($"{Utility.FixHtmlFormattable(guildTitle)}, {Utility.FixHtmlFormattable(guild.Name)}");
list.Add($"{guildTitle.FixHtmlFormattable()}, {guild.Name.FixHtmlFormattable()}");
}
else
{
list.Add($"{Utility.FixHtmlFormattable(guildTitle)}, {Utility.FixHtmlFormattable(guild.Name)} Guild{type}");
list.Add($"{guildTitle.FixHtmlFormattable()}, {guild.Name.FixHtmlFormattable()} Guild{type}");
}
}
else
{
list.Add(Utility.FixHtml(guild.Name));
list.Add(guild.Name.FixHtml());
}
}
}
Expand Down
58 changes: 32 additions & 26 deletions Projects/Server/Utilities/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class Utility
private static Dictionary<IPAddress, IPAddress> _ipAddressTable;

private static SkillName[] _allSkills =
{
[
SkillName.Alchemy,
SkillName.Anatomy,
SkillName.AnimalLore,
Expand Down Expand Up @@ -82,19 +82,19 @@ public static class Utility
// SkillName.Mysticism,
// SkillName.Imbuing,
SkillName.Throwing
};
];

private static readonly SkillName[] m_CombatSkills =
{
[
SkillName.Archery,
SkillName.Swords,
SkillName.Macing,
SkillName.Fencing,
SkillName.Wrestling
};
];

private static readonly SkillName[] m_CraftSkills =
{
[
SkillName.Alchemy,
SkillName.Blacksmith,
SkillName.Fletching,
Expand All @@ -104,7 +104,7 @@ public static class Utility
SkillName.Inscribe,
SkillName.Tailoring,
SkillName.Tinkering
};
];

private static readonly Stack<ConsoleColor> m_ConsoleColors = new();

Expand Down Expand Up @@ -280,51 +280,58 @@ public static bool MatchCidr(this IPAddress cidrAddress, int prefixLength, IPAdd
return ip >= min && ip <= max;
}

public static string FixHtml(string str)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string FixHtml(this string str) => ((ReadOnlySpan<char>)str).FixHtml();

public static string FixHtml(this ReadOnlySpan<char> str)
{
if (string.IsNullOrEmpty(str))
if (str.IsNullOrWhiteSpace())
{
return str;
return str.ToString();
}

var chars = str.ToPooledArray();
var chars = STArrayPool<char>.Shared.Rent(str.Length);
var span = chars.AsSpan(0, str.Length);
str.CopyTo(span);

FixHtml(span);

return span.ToString();
var fixedStr = span.ToString();
STArrayPool<char>.Shared.Return(chars);
return fixedStr;
}

public static void FixHtml(Span<char> chars)
public static void FixHtml(this Span<char> chars)
{
if (chars.Length == 0)
{
return;
}

ReadOnlySpan<char> invalid = stackalloc []{ '<', '>', '#' };
ReadOnlySpan<char> replacement = stackalloc []{ '(', ')', '-' };
ReadOnlySpan<char> invalid = ['<', '>', '#'];
ReadOnlySpan<char> replacement = ['(', ')', '-'];

chars.ReplaceAny(invalid, replacement);
}

public static PooledArraySpanFormattable FixHtmlFormattable(string str)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PooledArraySpanFormattable FixHtmlFormattable(this string str) =>
((ReadOnlySpan<char>)str).FixHtmlFormattable();

public static PooledArraySpanFormattable FixHtmlFormattable(this ReadOnlySpan<char> str)
{
var chars = str.ToPooledArray();
var chars = STArrayPool<char>.Shared.Rent(str.Length);
var span = chars.AsSpan(0, str.Length);
var formattable = new PooledArraySpanFormattable(chars, str.Length);

if (!string.IsNullOrEmpty(str))
if (!str.IsNullOrWhiteSpace())
{
FixHtml(span);
}

return formattable;
}

public static int InsensitiveCompare(string first, string second) => first.InsensitiveCompare(second);

public static bool InsensitiveStartsWith(string first, string second) => first.InsensitiveStartsWith(second);

public static Direction GetDirection(Point3D from, Point3D to) => GetDirection(from.X, from.Y, to.X, to.Y);

public static Direction GetDirection(Point2D from, Point2D to) => GetDirection(from.X, from.Y, to.X, to.Y);
Expand Down Expand Up @@ -793,7 +800,7 @@ public static List<T> RandomSample<T>(this List<T> source, int count)
{
if (count <= 0)
{
return new List<T>();
return [];
}

var length = source.Count;
Expand Down Expand Up @@ -1225,14 +1232,14 @@ public static int Abs(this int value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add<T>(ref List<T> list, T value)
{
list ??= new List<T>();
list ??= [];
list.Add(value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add<T>(ref HashSet<T> set, T value)
{
set ??= new HashSet<T>();
set ??= [];
set.Add(value);
}

Expand Down Expand Up @@ -1504,8 +1511,7 @@ typeCode switch
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrWhiteSpace(this ReadOnlySpan<char> span) =>
span == default || span.IsEmpty || span.IsWhiteSpace();
public static bool IsNullOrWhiteSpace(this ReadOnlySpan<char> span) => span.IsEmpty || span.IsWhiteSpace();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool InTypeList<T>(this T obj, Type[] types) => obj.GetType().InTypeList(types);
Expand Down
2 changes: 1 addition & 1 deletion Projects/UOContent/Commands/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public bool Run(Mobile from)
return false;
}

if (Condition.Length > 0 && !Utility.InsensitiveStartsWith(Condition, "where"))
if (Condition.Length > 0 && !Condition.InsensitiveStartsWith("where"))
{
from.SendMessage("The condition field must start with \"where\".");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public override void OnResponse(Mobile from, string text)

if (from.CheckAlive() && m_Book.IsChildOf(from.Backpack))
{
m_Book.BookName = Utility.FixHtml(text.Trim());
m_Book.BookName = text.Trim().FixHtml();

from.SendLocalizedMessage(1062480); // The bulk order book's name has been changed.
}
Expand Down
2 changes: 1 addition & 1 deletion Projects/UOContent/Engines/Help/PageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public PageEntry(Mobile sender, string message, PageType type)
{
Sender = sender;
Sent = Core.Now;
Message = Utility.FixHtml(message);
Message = message.FixHtml();
Type = type;
PageLocation = sender.Location;
PageMap = sender.Map;
Expand Down
6 changes: 3 additions & 3 deletions Projects/UOContent/Engines/Help/SpeechLogGump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public SpeechLogGump(Mobile player, List<SpeechLogEntry> log, int page)
10,
280,
20,
$"<basefont color=#A0A0FF><center>SPEECH LOG - {playerName} (<i>{Utility.FixHtmlFormattable(playerAccount)}</i>)</center></basefont>"
$"<basefont color=#A0A0FF><center>SPEECH LOG - {playerName} (<i>{playerAccount.FixHtmlFormattable()}</i>)</center></basefont>"
);

var lastPage = (log.Count - 1) / MaxEntriesPerPage;
Expand Down Expand Up @@ -82,8 +82,8 @@ public SpeechLogGump(Mobile player, List<SpeechLogEntry> log, int page)
builder.AppendFormat(
"<u>{0}</u> (<i>{1}</i>): {2}",
name,
Utility.FixHtml(account),
Utility.FixHtml(speech)
account.FixHtml(),
speech.FixHtml()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public override void OnResponse(NetState sender, in RelayInfo info)
{
case 1:
{
var guildName = Utility.FixHtml(info.GetTextEntry(5) ?? "");
var guildAbbrev = Utility.FixHtml(info.GetTextEntry(6) ?? "");
var guildName = (info.GetTextEntry(5) ?? "").FixHtml();
var guildAbbrev = (info.GetTextEntry(6) ?? "").FixHtml();

if (guildName.Length <= 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void SetCharter_Callback(Mobile from, string text)
return;
}

var charter = Utility.FixHtml(text.Trim());
var charter = text.Trim().FixHtml();

if (charter.Length > 50)
{
Expand All @@ -193,7 +193,7 @@ public void SetWebsite_Callback(Mobile from, string text)
return;
}

var site = Utility.FixHtml(text.Trim());
var site = text.Trim().FixHtml();

if (site.Length > 50)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void SetTitle_Callback(Mobile from, string text)
return;
}

var title = Utility.FixHtml(text.Trim());
var title = text.Trim().FixHtml();

if (title.Length > 20)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ public void CreateAlliance_Callback(Mobile from, string text)
}
else
{
var name = Utility.FixHtml(text.Trim());
var name = text.Trim().FixHtml();

if (!CheckProfanity(name))
{
Expand Down
2 changes: 1 addition & 1 deletion Projects/UOContent/Gumps/RunebookGump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public override void OnResponse(Mobile from, string text)

if (m_Book.CheckAccess(from))
{
m_Book.Description = Utility.FixHtml(text.Trim());
m_Book.Description = text.Trim().FixHtml();

from.CloseGump<RunebookGump>();
from.SendGump(new RunebookGump(from, m_Book));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ public override void OnResponse(NetState sender, in RelayInfo info)
m_Bear.EditLimit = Core.Now + TimeSpan.FromMinutes(10);
}

m_Bear.Line1 = Utility.FixHtml(line1);
m_Bear.Line2 = Utility.FixHtml(line2);
m_Bear.Line3 = Utility.FixHtml(line3);
m_Bear.Line1 = line1.FixHtml();
m_Bear.Line2 = line2.FixHtml();
m_Bear.Line3 = line3.FixHtml();

from.SendMessage("You add the personalized greeting to your St. Valentine Bear.");
}
Expand Down
8 changes: 4 additions & 4 deletions Projects/UOContent/Items/Books/BookPackets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public static void OldHeaderChange(NetState state, SpanReader reader)
var title = reader.ReadAsciiSafe(60);
var author = reader.ReadAsciiSafe(30);

book.Title = Utility.FixHtml(title);
book.Author = Utility.FixHtml(author);
book.Title = title.FixHtml();
book.Author = author.FixHtml();
}

public static void HeaderChange(NetState state, SpanReader reader)
Expand Down Expand Up @@ -80,8 +80,8 @@ public static void HeaderChange(NetState state, SpanReader reader)

var author = reader.ReadUTF8Safe(authorLength);

book.Title = Utility.FixHtml(title);
book.Author = Utility.FixHtml(author);
book.Title = title.FixHtml();
book.Author = author.FixHtml();
}

public static void ContentChange(NetState state, SpanReader reader)
Expand Down
8 changes: 4 additions & 4 deletions Projects/UOContent/Items/Guilds/Guildstone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public override void GetProperties(IPropertyList list)
}

// list.Add( 1060802, Utility.FixHtml( name ) ); // Guild name: ~1_val~
list.Add(1060802, $"{Utility.FixHtmlFormattable(name)} [{Utility.FixHtmlFormattable(abbr)}]");
list.Add(1060802, $"{name.FixHtmlFormattable()} [{abbr.FixHtmlFormattable()}]");
}
else if (_guildName != null && _guildAbbrev != null)
{
list.Add(1060802, $"{Utility.FixHtmlFormattable(_guildName)} [{Utility.FixHtmlFormattable(_guildAbbrev)}]");
list.Add(1060802, $"{_guildName.FixHtmlFormattable()} [{_guildAbbrev.FixHtmlFormattable()}]");
}
}

Expand Down Expand Up @@ -254,11 +254,11 @@ public override void GetProperties(IPropertyList list)
}

// list.Add( 1060802, Utility.FixHtml( name ) ); // Guild name: ~1_val~
list.Add(1060802, $"{Utility.FixHtmlFormattable(name)} [{Utility.FixHtmlFormattable(abbr)}]");
list.Add(1060802, $"{name.FixHtmlFormattable()} [{abbr.FixHtmlFormattable()}]");
}
else if (_guildName != null && _guildAbbrev != null)
{
list.Add(1060802, $"{Utility.FixHtmlFormattable(_guildName)} [{Utility.FixHtmlFormattable(_guildAbbrev)}]");
list.Add(1060802, $"{_guildName.FixHtmlFormattable()} [{_guildAbbrev.FixHtmlFormattable()}]");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Projects/UOContent/Items/Misc/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public override void OnResponse(Mobile from, string text)
return;
}

_key.Description = Utility.FixHtml(text);
_key.Description = text.FixHtml();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public override void OnResponse(NetState state, in RelayInfo info)
else
{
_target.EngravedText =
Utility.FixHtml(relay.Length > 64 ? relay[..64] : relay);
(relay.Length > 64 ? relay[..64] : relay).FixHtml();
state.Mobile.SendLocalizedMessage(1072361); // You engraved the object.
_target.InvalidateProperties();
_tool.UsesRemaining -= 1;
Expand Down
Loading

0 comments on commit becd7aa

Please sign in to comment.