neosmart / unicode.net

A Unicode library for .NET, supporting UTF8, UTF16, and UTF32. With an extra helping of emoji for good measure 🔥🌶️😁
MIT License
87 stars 23 forks source link

IsEmoji returns false erroneously for leg 🦵 emoji #19

Closed zzullick closed 2 years ago

zzullick commented 2 years ago

Hi,

Thanks for this great little library. I've been using it and ran into a weird issue. I'm using it to validate input data via an attribute. I had a case where the ":leg:" emoji, or "🦵", codepoint U+1F9B5 returns false with maxSymbolCount = 1 when I expected it to return true.

Actual: NeoSmart.Unicode.Emoji.IsEmoji("🦵", 1) returns false Expected: NeoSmart.Unicode.Emoji.IsEmoji("🦵", 1) returns true

Here is the code I'm using:

    public class EmojiAttribute : ValidationAttribute
    {
        public int NumSymbols { get; set; } = 1;

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
                return new ValidationResult("Emoji must be specified.");

            var emoji = value.ToString();
            if (!NeoSmart.Unicode.Emoji.IsEmoji(emoji, NumSymbols))
                return new ValidationResult($"Emoji must be a valid unicode string with precisely {NumSymbols} symbols.");

            return ValidationResult.Success;
        }
    }

Thanks!

mqudsi commented 2 years ago

You're right, I can reproduce this locally.

mqudsi commented 2 years ago

This is because the leg emoji was from a newer Unicode TR than that supported by the published version. I've just merged Unicode 13 support into the git master branch that updates the emoji unicode range definition and this should no longer be an issue.

Purely out of curiosity and so I can understand our users, may I ask how you found this project and what you are using it for?

mqudsi commented 2 years ago

A new version of the package has been uploaded to nuget.org with the fix.

zzullick commented 2 years ago

Hey, thanks for looking into this and fixing it! I really appreciate it.

I'm pretty sure I found the project through some Google searches looking for combinations of C# and unicode emoji. I'm using it to validate input server-side against an "emoji" field which the user can select on the client from a UI for @ https://praise.fun. The reason I especially found this library useful was the built-in logic for checking the "number of symbols" which didn't appear to be a trivial or straightforward exercise.

mqudsi commented 2 years ago

No problem, thanks for sharing! I initially wrote the library for use with an iMessage client and needed to make messages consisting of up to n emoji display in a larger size myself :)

Cool website idea, btw.