Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EmoteExtractor to support multibyte #260

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading