<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">`Slice` family of types (there are many) are well documented to share the indices and inherit the semantics. All collections that have a SubSequence of a Slice type, share indices. Unfortunately, standard library is not well documented in general and collection API have undergone big changes in Swift 3.0, so this may not be very clear now. <div class=""><br class=""></div><div class="">The problem with `String.CharacterView` is that its SubSequence is not a `Slice` type.</div><div class=""><br class=""></div><div class="">Try this out:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> subSeq1 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"abc"</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">prefix</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> subSeq2 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"abc"</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">prefix</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(type(of:</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">subSeq1</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// prints CharacterView</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(type(of:</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">subSeq2</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// prints ArraySlice<Character></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Converting `</span></span><span style="font-family: Helvetica; font-size: 12px;" class="">String.CharacterView</span><span style="font-family: Helvetica; font-size: 12px;" class="">` to Array<Character> is an expensive operation and will kill the performance of string manipulation, but will work as you expect, since Array is properly sliced.</span></div></div></div><div class=""><div class=""><div class=""><br class=""><div class=""><div><blockquote type="cite" class=""><div class="">On Oct 13, 2016, at 5:42 PM, Tim Vermeulen <<a href="mailto:tvermeulen@me.com" class="">tvermeulen@me.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Alright. Does this mean that we can otherwise assume that collections share indices with their subsequences? It might be worth documenting, one way or the other.<div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 14 Oct 2016, at 02:40, Hooman Mehr <<a href="mailto:hooman@mac.com" class="">hooman@mac.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">This is a bug reported multiple times in different forms. My version of it is: <a href="https://bugs.swift.org/browse/SR-1487" class="">SR-1487</a>. <div class=""><br class=""></div><div class="">It remains open because it is not easy to fix with the existing design of String. Apparently core standard library team are working on an overhaul of String to address this and other usability and performance issues.</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 13, 2016, at 5:12 PM, Tim Vermeulen via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Is it a requirement that collections share indices with its subsequence? Array and ArraySlice do share indices, which is why ArraySlice isn’t zero-based, and I think this is convenient. But String.CharacterView doesn’t seem to share indices with its subsequence (which is String.CharacterView as well). Consider this example:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> foo = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"foobar"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">characters</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> index = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">foo</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">index</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">foo</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">startIndex</span><span style="font-variant-ligatures: no-common-ligatures" class="">, offsetBy: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> bar = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">foo</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">suffix</span><span style="font-variant-ligatures: no-common-ligatures" class="">(from: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">index</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// "bar"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">foo</span><span style="font-variant-ligatures: no-common-ligatures" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">index</span><span style="font-variant-ligatures: no-common-ligatures" class="">] </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// "b" :)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">foo</span><span style="font-variant-ligatures: no-common-ligatures" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">bar</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">startIndex</span><span style="font-variant-ligatures: no-common-ligatures" class="">] </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// "f" :(</span></div></div><div class=""><br class=""></div><div class="">So does this mean that we can’t assume that collections and their subsequences share their indices (which could be very handy), or is this just a bug related to String.CharacterView?</div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div></div></div></body></html>