Re: [swift-dev] Make offset index available for String

2018-01-02 Thread Karl Wagner via swift-dev
Swift used to do this, but we switched it around so indexes couldn’t self-increment. One of the problems was that strings are value-types. So you would get an index, then append stuff to the string, but when you tried to advance the index again it would blow up. The index retained the backing,

Re: [swift-dev] Make offset index available for String

2017-12-18 Thread Cao, Jiannan via swift-dev
I implemented the second approach: SuperIndexhttps://github.com/frogcjn/SuperStringIndex/SuperString is a special version of String. Its SuperIndex keeps a reference to the string, let the index calculate the offset.struct SuperIndex : Comparable, Strideable, CustomStringConvertible {        var ow

Re: [swift-dev] Make offset index available for String

2017-12-18 Thread Cao, Jiannan via swift-dev
I implemented the second approach: SuperIndex https://github.com/frogcjn/SuperStringIndex/ SuperString is a special version of String. Its SuperIndex keeps a reference to the string, let the index calculate the offset. struct SuperIndex : Compara

Re: [swift-dev] Make offset index available for String

2017-12-18 Thread Michael Ilseman via swift-dev
An index that keeps a reference to the collection is an iterator, e.g. the IndexingIterator[1], which String already provides. [1] https://developer.apple.com/documentation/swift/indexingiterator > On Dec 18, 2017, at 12:53 AM, Cao, Jiannan wrote: > > Or we can copy the design of std::vector:

Re: [swift-dev] Make offset index available for String

2017-12-18 Thread Cao, Jiannan via swift-dev
Or we can copy the design of std::vector::iterator in C++.The index could keep a reference to the collection. When the index being offset by + operator, since it keeps a reference to the collection owner, it could call the owner to offset the index. let startIndex = s.startIndex s[startIndex+1]

Re: [swift-dev] Make offset index available for String

2017-12-18 Thread Cao, Jiannan via swift-dev
Or we can copy the design of std::vector::iterator in C++.The index could keep a reference to the collection. When the index being offset by + operator, it could call the owner to offset the index, since it keeps a reference to the collection owner. let startIndex = s.startIndex s[startIndex+1]

Re: [swift-dev] Make offset index available for String

2017-12-18 Thread Cao, Jiannan via swift-dev
Or we can copy the design of C++. the index keep a reference to the sequence. The index can be offset by + operator, since it keeps a reference to the sequence owner, it should call the owner to offset the index. let startIndex = s.startIndex s[startIndex+1] public struct MyIndex : Comparable wh

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Cao, Jiannan via swift-dev
for example: caféz the offset index of z is always 4. Which means the 4-th character of the string. You can always use s[s.index(s.startIndex, offsetBy:4)] to access the z. but the encodedOffset index of z maybe 16 or 20. This is not the offset concept of the collection, but the encoded offset c

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Michael Ilseman via swift-dev
Yes, I was trying to highlight that they are different and should be treated different. This was because it seemed you were conflating the two in your argument. You claim that people expect it, and I’m pointing out that what people actually expect (assuming they’re coming from C or languages wit

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Cao, Jiannan via swift-dev
This offset is unicode offset, is not the offset of element. For example: index(startIndex, offsetBy:1) is encodedOffset 4 or 8, not 1. Offset indexable is based on the offset of count of each element/index. it is the same result of s.index(s.startIndex, offsetBy:i) The encodedOffset is the unde

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Michael Ilseman via swift-dev
> On Dec 14, 2017, at 4:49 PM, Cao, Jiannan via swift-dev > wrote: > > People used to the offset index system instead of the String.Index. Using > offset indices to name the elements, count the elements is normal and nature. > The offset system that you’re referring to is totally available

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Cao, Jiannan via swift-dev
People used to the offset index system instead of the String.Index. Using offset indices to name the elements, count the elements is normal and nature. This offset index system has a long history and a real meaning to the collection. The subscript s[i] has a fix meaning of "getting the i-th elem

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Kelvin Ma via swift-dev
perhaps i’m biased because most of the work i do is on strings that are meant to be machine read and written but if you ask me, user-facing unicode strings are the special case and it seems the entire API is optimized for them, and not more common ASCII strings. On Thu, Dec 14, 2017 at 3:40 PM, Wa

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Wallacy via swift-dev
95% ASCII? Even if you forgot the rest of the World and consider only US this doesn’t seems right. But of course people (in the general) don’t care about Unicode Clusters. Because that, we should focus on keep Unicode string manipulation easy, instead just forget the Unicode rules. Em qui, 14 de

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Kelvin Ma via swift-dev
I think there’s a middle ground here as the current API is overly complex and general. 95% of the time people are dealing with ASCII strings and don’t care about things like unicode cluster boundaries and whatnot On Thu, Dec 14, 2017 at 12:13 PM, Jordan Rose via swift-dev < swift-dev@swift.org> wr

Re: [swift-dev] Make offset index available for String

2017-12-14 Thread Jordan Rose via swift-dev
We really don't want to make subscripting a non-O(1) operation. That just provides false convenience and encourages people to do the wrong thing with Strings anyway. I'm always interested in why people want this kind of ability. Yes, it's nice for teaching programming to be able to split string