<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">I would like to discuss the String.Index problem within Swift. I know the current situation of String.Index is based on the nature of the underlaying data structure of the string.</div><div class=""><br class=""></div><div class="">But could we just make String.Index contain offset information? Or make offset index subscript available for accessing character in String?</div><div class=""><br class=""></div><div class="">for example:</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(186, 45, 162);" class="">let</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class=""> a = </span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">"01234"</span><br class=""><span style="color: rgb(62, 30, 129); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">0</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 0<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">0</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">...</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">4</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 01234<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[...]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 01234</span><br class=""><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[..&lt;</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 01<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[...</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 012<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">...]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 234<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">...</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">3</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 23<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">...</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 2</span><br class=""><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(186, 45, 162);" class="">if</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class=""> </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(186, 45, 162);" class="">let</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class=""> number = </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">.</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(49, 89, 93);" class="">index</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(of: </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(209, 47, 27);" class="">"1"</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">) {<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(number) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 1<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(62, 30, 129);" class="">print</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">a</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">[number...]) </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 132, 0);" class="">// 1234<br class=""></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">}</span></blockquote><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><br class=""></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">0 equals to Collection.Index of collection.index(startIndex, offsetBy: 0)</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">1 equals to</span><span style="background-color: rgb(255, 255, 255);" class="">&nbsp;Collection.Index of collection.index(startIndex, offsetBy: 1)</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">...</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">we keep the String.Index, but allow another kind of index, which is called "offsetIndex" to access the String.Index and the character in the string.</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">Any Collection could use the offset index to access their element, regarding the real index of it.</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">I have make the Collection OffsetIndexable protocol available here, and make it&nbsp;more&nbsp;accessible&nbsp;for StringProtocol considering all API related to the index.</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><a href="https://github.com/frogcjn/OffsetIndexableCollection-String-Int-Indexable-" class="">https://github.com/frogcjn/OffsetIndexableCollection-String-Int-Indexable-</a></span></div></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">If someone want to make the offset index/range available for any collection, you just need to extend the collection:</span></div><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; word-wrap: normal; padding: 16px; overflow: auto; line-height: 1.45; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">extension</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);"><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">String</span></span> : <span class="pl-e" style="box-sizing: border-box; color: rgb(111, 66, 193);">OffsetIndexableCollection </span>{
}

<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">extension</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">Substring</span> : <span class="pl-e" style="box-sizing: border-box; color: rgb(111, 66, 193);">OffsetIndexableCollection </span>{
}</pre><div class=""><br class=""></div></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">I hope the Swift core team could consider bring the offset index to string, or make it&nbsp;available to other collection, thus let&nbsp;developer to decide whether&nbsp;their collection could use offset indices as an assistant for the real index of the collection.</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><br class=""></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">Thanks!</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">Jiannan</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(186, 45, 162);" class=""></span></div></blockquote></div></body></html>