From e014d80a0f545be2d936cae2acd287a1a77a1966 Mon Sep 17 00:00:00 2001 From: AoshiW <48525283+AoshiW@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:47:32 +0200 Subject: [PATCH] nullable annotation for `MessageEmote.cs` (#251) Co-authored-by: AoshiW --- TwitchLib.Client.Models/MessageEmote.cs | 27 ++++++++++++++----------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/TwitchLib.Client.Models/MessageEmote.cs b/TwitchLib.Client.Models/MessageEmote.cs index 2a1c178e..1116ce5b 100644 --- a/TwitchLib.Client.Models/MessageEmote.cs +++ b/TwitchLib.Client.Models/MessageEmote.cs @@ -1,5 +1,4 @@ -#nullable disable -using System.Collections.ObjectModel; +using System.Collections.ObjectModel; using System.Text.RegularExpressions; // TODO: Missing builder @@ -215,7 +214,7 @@ public MessageEmote( string text, EmoteSource source = EmoteSource.Twitch, EmoteSize size = EmoteSize.Small, - ReplaceEmoteDelegate replacementDelegate = null) + ReplaceEmoteDelegate? replacementDelegate = null) { Id = id; Text = text; @@ -238,15 +237,15 @@ public class MessageEmoteCollection private const string BasePattern = @"(\b {0}\b)|(\b{0} \b)|(?<=\W){0}(?=$)|(?<=\s){0}(?=\s)|(^{0}$)"; /// Do not access directly! Backing field for - private string _currentPattern; - private Regex _regex; + private string? _currentPattern; + private Regex? _regex; private readonly EmoteFilterDelegate _preferredFilter; /// /// Property so that we can be confident /// always reflects changes to . /// - private string CurrentPattern + private string? CurrentPattern { get => _currentPattern; set @@ -259,7 +258,7 @@ private string CurrentPattern } } - private Regex CurrentRegex + private Regex? CurrentRegex { get { @@ -310,10 +309,15 @@ public MessageEmoteCollection(EmoteFilterDelegate preferredFilter) : this() /// The to add to the collection. public void Add(MessageEmote emote) { +#if NETSTANDARD2_0 if (_emotes.ContainsKey(emote.Text)) return; - _emotes.Add(emote.Text, emote); +#else + if(!_emotes.TryAdd(emote.Text, emote)) + return; +#endif + if (CurrentPattern == null) { //string i = String.Format(_basePattern, "(" + emote.EscapedText + "){0}"); @@ -360,7 +364,7 @@ public void Remove(MessageEmote emote) // Matches |(\bEMOTE\b) including the preceding | so that the following | and emote (if any) // merge seamlessly when this section is removed. Again, wrapped in a group. var otherEmotePattern = @"(\|\(\\b" + emote.EscapedText + @"\\b\))"; - var newPattern = Regex.Replace(CurrentPattern, firstEmotePattern + "|" + otherEmotePattern, ""); + var newPattern = Regex.Replace(CurrentPattern, firstEmotePattern + "|" + otherEmotePattern, ""); // todo: possible ArgumentNullException CurrentPattern = newPattern.Equals("") ? null : newPattern; } @@ -396,7 +400,7 @@ public void RemoveAll() /// A string where all of the original emote text has been replaced with /// its designated s /// - public string ReplaceEmotes(string originalMessage, EmoteFilterDelegate del = null, string prefix = "", string suffix = "") + public string ReplaceEmotes(string originalMessage, EmoteFilterDelegate? del = null, string prefix = "", string suffix = "") { if (CurrentRegex == null) { @@ -416,11 +420,10 @@ public string ReplaceEmotes(string originalMessage, EmoteFilterDelegate del = nu prefix += " "; if (match.Value[match.Value.Length - 1] == ' ') suffix = " " + suffix; - if (!_emotes.ContainsKey(emoteCode)) + if (!_emotes.TryGetValue(emoteCode, out var emote)) { return match.Value; } - var emote = _emotes[emoteCode]; return CurrentEmoteFilter(emote) ? prefix + emote.ReplacementString + suffix : match.Value; });