From 37c54cb20004f1bf5b5cdb20c2c0712287d0ebcd Mon Sep 17 00:00:00 2001 From: Proddy Date: Mon, 4 Dec 2023 21:26:05 +0000 Subject: [PATCH 1/2] Update EmoteExtractor to support multibyte Fixes https://github.com/TwitchLib/TwitchLib.Client/issues/259 --- TwitchLib.Client.Models/Extractors/EmoteExtractor.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TwitchLib.Client.Models/Extractors/EmoteExtractor.cs b/TwitchLib.Client.Models/Extractors/EmoteExtractor.cs index 86d60610..7e89eddf 100644 --- a/TwitchLib.Client.Models/Extractors/EmoteExtractor.cs +++ b/TwitchLib.Client.Models/Extractors/EmoteExtractor.cs @@ -10,6 +10,7 @@ public static List Extract(string? rawEmoteSetString, string message) if (string.IsNullOrEmpty(rawEmoteSetString) || string.IsNullOrEmpty(message)) return emotes; + System.Globalization.StringInfo messageInfo = new(message); // 25:5-9,28-32/28087:15-21 => 25:5-9,28-32 28087:15-21 #pragma warning disable CS8604 // Possible null reference argument. false positiv in NS 2.0 foreach (var emoteData in new SpanSliceEnumerator(rawEmoteSetString, '/')) @@ -30,7 +31,11 @@ public static List Extract(string? rawEmoteSetString, string message) var start = int.Parse(startSlice); var end = int.Parse(endSlice); #endif - emotes.Add(new(emoteId, message.Substring(start, end - start + 1), start, end)); + int trueStart = messageInfo.SubstringByTextElements(0, start + 1).Length - 1; + string name = messageInfo.SubstringByTextElements(start, end - start + 1); + int trueEnd = trueStart + name.Length - 1; + + emotes.Add(new(emoteId, name, trueStart, trueEnd)); } } return emotes; From 12a2d88ae978184cb4810e9ab9eafbdf660c469c Mon Sep 17 00:00:00 2001 From: Proddy Date: Mon, 4 Dec 2023 21:34:33 +0000 Subject: [PATCH 2/2] Add test for EmoteExtractor --- TwitchLib.Client.Test/Parsing.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/TwitchLib.Client.Test/Parsing.cs b/TwitchLib.Client.Test/Parsing.cs index af1a8e0c..4988a2e7 100644 --- a/TwitchLib.Client.Test/Parsing.cs +++ b/TwitchLib.Client.Test/Parsing.cs @@ -1,4 +1,5 @@ -using TwitchLib.Client.Models.Internal; +using TwitchLib.Client.Models.Extractors; +using TwitchLib.Client.Models.Internal; using Xunit; namespace TwitchLib.Client.Test; @@ -21,6 +22,14 @@ public void Badges() TagHelper.ToBadges(badges); } + [Fact] + public void EmoteExtractorExtract() + { + var emotes = EmoteExtractor.Extract("25:10-14", "One 😂 Two Kappa Three"); + Assert.True(emotes.Count == 1); + Assert.True(emotes[0].Name == "Kappa"); + } + [Theory] [InlineData("@ban-duration=350;room-id=12345678;target-user-id=87654321;tmi-sent-ts=1642719320727 :tmi.twitch.tv CLEARCHAT #dallas :ronni")]