@@ -23,13 +23,14 @@ namespace Discord | |||||
/// <param name="unicode">The pure UTF-8 encoding of an emoji</param> | /// <param name="unicode">The pure UTF-8 encoding of an emoji</param> | ||||
public Emoji(string unicode) | public Emoji(string unicode) | ||||
{ | { | ||||
byte[] utf32 = Encoding.Unicode.GetBytes(unicode); | |||||
bool safe = false, any = false; | |||||
int codepoint = 0; | |||||
// NETStandard1.1 doesn't support UTF32 | |||||
#if !NETSTANDARD1_1 | |||||
byte[] utf32 = Encoding.UTF32.GetBytes(unicode); | |||||
for (var i = 0; i < utf32.Length; i += 4) | for (var i = 0; i < utf32.Length; i += 4) | ||||
{ | { | ||||
codepoint = BitConverter.ToInt32(utf32, i); | |||||
any = false; | |||||
int codepoint = BitConverter.ToInt32(utf32, i); | |||||
bool any = false; | |||||
for (var j = 0; j < Codepoints.Length; j++) | for (var j = 0; j < Codepoints.Length; j++) | ||||
{ | { | ||||
if (Codepoints[j] == codepoint) | if (Codepoints[j] == codepoint) | ||||
@@ -38,16 +39,13 @@ namespace Discord | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
if (any) | |||||
safe = true; | |||||
if (any) continue; | |||||
else | else | ||||
{ | |||||
safe = false; | |||||
break; | |||||
} | |||||
throw new ArgumentException("One or more characters was not a valid Emoji", nameof(unicode)); | |||||
} | } | ||||
if (!safe) | |||||
throw new ArgumentException("One or more characters was not a valid Emoji", nameof(unicode)); | |||||
#endif | |||||
Name = unicode; | Name = unicode; | ||||
} | } | ||||
@@ -5,11 +5,32 @@ namespace Discord | |||||
{ | { | ||||
public class EmoteTests | public class EmoteTests | ||||
{ | { | ||||
[Fact] | |||||
public void Emoji() | |||||
const string Smiley = "\U0001F603"; | |||||
const string Man = "\U0001F468"; | |||||
const string Woman = "\U0001F469"; | |||||
const string Girl = "\U0001F467"; | |||||
const string Boy = "\U0001F466"; | |||||
const string Join = "\u200D"; | |||||
[Fact] | |||||
public void Single_Emoji() | |||||
{ | |||||
Assert.Equal(Smiley, new Emoji(Smiley).Name); | |||||
Assert.Equal(Man, new Emoji(Man).Name); | |||||
Assert.Equal(Woman, new Emoji(Woman).Name); | |||||
Assert.Equal(Girl, new Emoji(Girl).Name); | |||||
Assert.Equal(Boy, new Emoji(Boy).Name); | |||||
} | |||||
[Fact] | |||||
public void Multipart_Emoji() | |||||
{ | |||||
string family = string.Concat(Man, Join, Woman, Join, Girl, Join, Boy); | |||||
Assert.Equal(family, new Emoji(family).Name); | |||||
} | |||||
[Fact] | |||||
public void Emoji_Fail() | |||||
{ | { | ||||
// Future: Validate emoji parsing | |||||
Assert.Equal("🦅", new Emoji("🦅").Name); | |||||
Assert.Throws<ArgumentException>(() => new Emoji("foxDab")); | |||||
} | } | ||||
[Fact] | [Fact] | ||||