Advertisement
FrayxRulez

EnumerateByComposedCharacterSequence

Oct 21st, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace TestEmojiEnumeration
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var text = "ciao ๐Ÿ˜Ž๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ ciao ๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ง๐Ÿ‘ณ๐Ÿฟ ciao";
  15.             foreach (var last in EnumerateByComposedCharacterSequence(text))
  16.             {
  17.                 Debug.WriteLine(last);
  18.             }
  19.  
  20.             Console.ReadLine();
  21.         }
  22.  
  23.         public static IEnumerable<string> EnumerateByComposedCharacterSequence(string text)
  24.         {
  25.             var last = string.Empty;
  26.             var joiner = true;
  27.  
  28.             for (int i = 0; i < text.Length; i++)
  29.             {
  30.                 if (char.IsSurrogatePair(text, i))
  31.                 {
  32.                     // skin modifier for emoji diversity acts as a joiner
  33.                     if (!joiner && !IsSkinModifierCharacter(text, i))
  34.                     {
  35.                         yield return last;
  36.                         last = string.Empty;
  37.                         joiner = true;
  38.                     }
  39.  
  40.                     last += text[i + 0];
  41.                     last += text[i + 1];
  42.                     joiner = false;
  43.                     i++;
  44.                 }
  45.                 else if (text[i] == 0x200D) // zero width joiner
  46.                 {
  47.                     last += text[i];
  48.                     joiner = true;
  49.                 }
  50.                 else
  51.                 {
  52.                     if (last.Length > 0)
  53.                     {
  54.                         yield return last;
  55.                     }
  56.  
  57.                     yield return text[i].ToString();
  58.                     last = string.Empty;
  59.                     joiner = true;
  60.                 }
  61.             }
  62.  
  63.             if (last.Length > 0)
  64.             {
  65.                 yield return last;
  66.             }
  67.         }
  68.  
  69.         public static bool IsSkinModifierCharacter(string s, int index)
  70.         {
  71.             if (index + 2 <= s.Length)
  72.             {
  73.                 char c1 = s[index + 0];
  74.                 char c2 = s[index + 1];
  75.                 return c1 == '\ud83c' && c2 >= '\udffb' && c2 <= '\udfff';
  76.             }
  77.  
  78.             return false;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement