<div><div class="gmail_msg">On Sat, Apr 1, 2017 at 2:52 AM, Pitiphong Phongpattranont via swift-evolution <span class="gmail_msg">&lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br class="gmail_msg"></div><div class="gmail_msg"><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Hello Swift Community,<br class="gmail_msg">
<br class="gmail_msg">
I’ve found a problem on Swift String API with Thai language. In Thai, we have 44 Consonants, 32 vowels and 5 tone marks. A special attribute of Thai vowels is that they can be put on anywhere around a consonant, some of them are placed after a consonant (ชา), some are before (แช), some are above (ชี) and some are below (ชุ). Since all vowels must be placed along with a consonants but they’re place in difference places around a consonant, Unicode standard says that some of the Thai vowels are Grapheme Base and some are Grapheme Extend.<br class="gmail_msg">
<br class="gmail_msg">
And because Swift String is fully Unicode compliance and by having some vowels be a Grapheme Extend makes some Thai vowels have a invalid attributes in some aspects. For example a word “ชี” (a nun) and “ชา” (tea) both have one consonant (in this case is ช) and one vowel (ี and า). When we ask how many characters are there in those words or does this word contain a ช character, we should get the same results from those 2 words (2 characters and it contains ช). However I found that in Swift String API, I will get a difference answers from those questions.<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
// You can try this code snippet in a Swift Playground<br class="gmail_msg">
let chi = &quot;ชี&quot;<br class="gmail_msg"></blockquote><div class="gmail_msg"><br class="gmail_msg"></div></div></div></div></div><div><div class="gmail_msg"><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg"><div class="gmail_msg">A Swift &quot;character&quot; models a Unicode extended grapheme cluster, which may or may not be a character with respect to a human language.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I don&#39;t speak Thai, but do you have a reference saying that the Unicode standard regards these as two separate extended grapheme clusters? Unless I&#39;m mistaken, according to the reference (<a href="http://unicode.org/cldr/utility/character.jsp?a=0E35" class="gmail_msg" target="_blank">http://unicode.org/cldr/utility/character.jsp?a=0E35</a>), the combining vowel U+0E35 extends the grapheme cluster. Therefore, Unicode rules require this to be reported as one extended grapheme cluster, which would make this one single Swift &quot;character&quot;. The remaining behaviors appear to be consistent with the Unicode standard as well.</div></div></div></div></div><div><div class="gmail_msg"><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg"><div class="gmail_msg"> </div><blockquote class="gmail_quote gmail_msg" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
let cha = “ชา&quot;<br class="gmail_msg">
<br class="gmail_msg">
// Value of these 2 lines below should be 2<br class="gmail_msg">
chi.characters.count<br class="gmail_msg">
cha.characters.count<br class="gmail_msg">
<br class="gmail_msg">
// Value of these 3 lines below should be true<br class="gmail_msg">
chi.contains(&quot;ช&quot;)<br class="gmail_msg">
cha.contains(&quot;ช&quot;)<br class="gmail_msg">
chi.characters.contains(&quot;ช”)<br class="gmail_msg">
<br class="gmail_msg">
// end of code snippet<br class="gmail_msg"></blockquote><div class="gmail_msg"><br class="gmail_msg"></div></div></div></div></div><div><div class="gmail_msg"><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg"><div class="gmail_msg">It sounds like you want to query by Unicode scalar, which you can do:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">```</div><p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:menlo;color:rgb(112,61,170)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="gmail_msg">chi</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">unicodeScalars</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(62,30,129)" class="gmail_msg">contains</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="gmail_msg">&quot;</span><span style="font-stretch:normal;line-height:normal;font-family:ayuthaya;font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="gmail_msg">ช</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="gmail_msg">&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">) // true</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:menlo;color:rgb(112,61,170)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="gmail_msg">cha</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">unicodeScalars</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(62,30,129)" class="gmail_msg">contains</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="gmail_msg">&quot;</span><span style="font-stretch:normal;line-height:normal;font-family:ayuthaya;font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="gmail_msg">ช</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="gmail_msg">&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">) // true</span></p><div class="gmail_msg">```</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">From some brief reading, it appears that there&#39;s some disagreement about how Unicode treats Thai (<a href="http://www.thai-language.com/forums/t/linguistics/writing/t14254" class="gmail_msg" target="_blank">http://www.thai-language.com/forums/t/linguistics/writing/t14254</a>). However, Swift strictly implements Unicode, which would be the forum to consider any issues.</div></div></div></div></div><div><div class="gmail_msg"><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><blockquote class="gmail_quote gmail_msg" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
I’m not sure that if Swift team is aware of this problem and do they have any opinion on it. I know that Unicode is very very hard and do aware of that there would be a revamp on String API in Swift 4 so I want to make this into a discussion before Swift 4 is released.<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
Thank you,<br class="gmail_msg">
Bank (Pitiphong)<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div></div></div></div>