Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TestEmojiEnumeration
- {
- class Program
- {
- static void Main(string[] args)
- {
- var text = "ciao ๐๐จโ๐จโ๐ฆโ๐ฆ๐จโ๐จโ๐งโ๐ง๐ฉโ๐ฉโ๐งโ๐ฆ ciao ๐ฉโ๐ฉโ๐ง๐ณ๐ฟ ciao";
- foreach (var last in EnumerateByComposedCharacterSequence(text))
- {
- Debug.WriteLine(last);
- }
- Console.ReadLine();
- }
- public static IEnumerable<string> EnumerateByComposedCharacterSequence(string text)
- {
- var last = string.Empty;
- var joiner = true;
- for (int i = 0; i < text.Length; i++)
- {
- if (char.IsSurrogatePair(text, i))
- {
- // skin modifier for emoji diversity acts as a joiner
- if (!joiner && !IsSkinModifierCharacter(text, i))
- {
- yield return last;
- last = string.Empty;
- joiner = true;
- }
- last += text[i + 0];
- last += text[i + 1];
- joiner = false;
- i++;
- }
- else if (text[i] == 0x200D) // zero width joiner
- {
- last += text[i];
- joiner = true;
- }
- else
- {
- if (last.Length > 0)
- {
- yield return last;
- }
- yield return text[i].ToString();
- last = string.Empty;
- joiner = true;
- }
- }
- if (last.Length > 0)
- {
- yield return last;
- }
- }
- public static bool IsSkinModifierCharacter(string s, int index)
- {
- if (index + 2 <= s.Length)
- {
- char c1 = s[index + 0];
- char c2 = s[index + 1];
- return c1 == '\ud83c' && c2 >= '\udffb' && c2 <= '\udfff';
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement