<div dir="ltr">Ah, thanks!</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 12, 2017 at 11:45 AM, Martin R <span dir="ltr"><<a href="mailto:martinr448@gmail.com" target="_blank">martinr448@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>The enumerateSubstrings method of (NS)String has a .byComposedCharacterSequences option which causes Emoji sequences like "👨👩👧👦" to be treated as a single unit:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> f(</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures"> s: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures">) -> [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures">] {</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">var</span><span style="font-variant-ligatures:no-common-ligatures"> a: [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures">] = []</span></div><div style="margin:0px;line-height:normal;color:rgb(112,61,170)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> s.</span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">enumerateSubstrings</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(in: s.</span><span style="font-variant-ligatures:no-common-ligatures">startIndex</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">..<s.</span><span style="font-variant-ligatures:no-common-ligatures">endIndex</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, options: .</span><span style="font-variant-ligatures:no-common-ligatures">byComposedCharacterSequences</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">) {</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> (c, </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures">) </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">in</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> c = c { a.</span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">append</span><span style="font-variant-ligatures:no-common-ligatures">(c) }</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> a</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><div style="margin:0px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;line-height:normal;font-family:'Apple Color Emoji';color:rgb(0,132,0)"><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(62,30,129)">print</span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">f</span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">"</span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b">👨👩👧👦👷🏾♀️</span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">"</span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)) </span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures">// ["</span><span style="font-variant-ligatures:no-common-ligatures">👨👩👧👦</span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures">", "</span><span style="font-variant-ligatures:no-common-ligatures">👷🏾♀️</span><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures">"]</span></div><div><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures"><br></span></div></div></div><div><span style="line-height:normal;font-family:Menlo;font-variant-ligatures:no-common-ligatures"><br></span></div><div>As I understand it from <a href="https://oleb.net/blog/2016/12/emoji-4-0/" target="_blank">https://oleb.net/blog/<wbr>2016/12/emoji-4-0/</a>, Emoji sequences are considered as a single grapheme cluster in Unicode 9, which means that you can simply do something like</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="font-variant-ligatures:no-common-ligatures;color:#703daa"> Array</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures">"</span><span style="line-height:normal;font-family:'Apple Color Emoji';font-variant-ligatures:no-common-ligatures">👨👩👧👦👷🏾♀️</span><span style="font-variant-ligatures:no-common-ligatures">"</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">characters</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div></div><div><br></div><div>once Unicode 9 is adopted in Swift.</div><div><br></div><div>Regards, Martin</div><div><br></div><div><br></div><div><blockquote type="cite"><div><div class="h5"><div>On 12. May 2017, at 10:43, Jens Persson via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_-180644258713097290Apple-interchange-newline"></div></div><div><div><div class="h5"><div dir="ltr"><div>I want a function f such that:</div><div><br></div><div>f("abc") == ["a", "b", "c"]</div><div><br></div><div>f("café") == ["c", "a", "f", "é"]</div><div><br></div><div>f("👨👩👧👦👷🏾♀️") == ["👨👩👧👦", "👷🏾♀️"]</div><div><br></div><div>I'm not sure if the last example renders correctly by mail for everyone but the input String contains these _two_ "natural/visual characters":</div><div>(1) A family emoji</div><div>(2) a construction worker (woman, with skin tone modifier) emoji.</div><div>and the result is an Array of two strings (one for each emoji).</div><div><br></div><div>The first two examples are easy, the third example is the tricky one.</div><div><br></div><div><div>Is there a (practical) way to do this (in Swift 3)?</div></div><div><br></div><div>/Jens</div><div><br></div><div><br></div><div><br></div><div>PS</div><div><br></div><div>It's OK if the function has to depend on eg a graphics context etc.</div><div>(I tried writing a function so that it extracts the glyphs, using NSTextStorage, NSLayoutManager and the AppleColorEmoji font, but it says that "👨👩👧👦👷🏾♀️" contains 18(!) glyphs, whereas eg "café" contains 4 as expected.)</div><div><br></div><div><div>If the emojis of the third example doesn't look like they should in this mail, here is another way to write the exact same example using only simple text:</div><div><br></div><div>let inputOfThirdExample = "\u{1F468}\u{200D}\u{1F469}\u{<wbr>200D}\u{1F467}\u{200D}\u{<wbr>1F466}\u{1F477}\u{1F3FE}\u{<wbr>200D}\u{2640}\u{FE0F}"<br></div><div><br></div><div>let result = f(inputOfThirdExample)</div><div><div><br></div><div>let expectedResult = ["\u{1F468}\u{200D}\u{1F469}\<wbr>u{200D}\u{1F467}\u{200D}\u{<wbr>1F466}", "\u{1F477}\u{1F3FE}\u{200D}\u{<wbr>2640}\u{FE0F}"]</div><div><br></div><div>print(result.elementsEqual(<wbr>result)) // Should print true<br></div></div></div><div><br></div><div><br></div></div></div></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></blockquote></div><br></div>