Skip to content

Commit

Permalink
Merge pull request #260 from Hampo/dev
Browse files Browse the repository at this point in the history
Update EmoteExtractor to support multibyte
  • Loading branch information
Syzuna authored Dec 4, 2023
2 parents cf5f549 + 1066b8b commit cdf0e4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion TwitchLib.Client.Models/Extractors/EmoteExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static List<Emote> 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, '/'))
Expand All @@ -30,7 +31,11 @@ public static List<Emote> 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;
Expand Down
11 changes: 10 additions & 1 deletion TwitchLib.Client.Test/Parsing.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")]
Expand Down

0 comments on commit cdf0e4c

Please sign in to comment.