[swift-users] function advancedBy() bugs

zh ao owenzx at gmail.com
Tue May 17 23:51:50 CDT 2016


Work around:

do {

    var str = "abcdefg"

    var str1 = str + "hijklmn"

*    let startIndex =
str1.rangeOfComposedCharacterSequenceAtIndex(str.startIndex).startIndex*

*    var range = startIndex..<str1.endIndex*

    range.startIndex = range.startIndex.advancedBy(10)

}


If you encounter this issue, you need to
use rangeOfComposedCharacterSequenceAtIndex to update to hidden endIndex.


do {

    var str = "abcdefg"

    var range = str.startIndex..<str.endIndex // 0..<7

    str += "hijklmn"

//    range.endIndex = str.endIndex // 0..<14

    *range = str.**startIndex..<str.endIndex*

    let index = range.startIndex.advancedBy(10)

}


Reassign the whole range instead of just set the endIndex.


Zhaoxin

On Wed, May 18, 2016 at 10:14 AM, zh ao <owenzx at gmail.com> wrote:

> I just make a clearer example.
>
> do {
>
>     var str = "abcdefg"
>
>     var str1 = str + "hijklmn"
>
>     var range = str.startIndex..<str1.endIndex
>
>     range.startIndex = range.startIndex.advancedBy(10)
>
>     //fatal error: cannot increment endIndex
>
> }
>
>
> It seams that range.startIndex internally contains a fault
> endIndex(str.endIndex, it should be its own endIndex, range.endIndex),
> which leads the issue.
>
>
> Zhaoxin
>
> On Wed, May 18, 2016 at 12:02 AM, Dmitri Gribenko <gribozavr at gmail.com>
> wrote:
>
>> On Tue, May 17, 2016 at 5:46 AM, zh ao via swift-users
>> <swift-users at swift.org> wrote:
>> > It seams that I encountered bugs in function advancedBy().
>> >
>> > do {
>> >     var str = "abcdefg"
>> >     var range = str.startIndex..<str.endIndex // 0..<7
>> >     str += "hijklmn"
>> >     range.endIndex = str.endIndex // 0..<14
>> >     let index = range.startIndex.advancedBy(10)
>> >     //fatal error: cannot increment endIndex
>> > }
>>
>> Hi,
>>
>> Thank you for the report.  To me, this issue looks the same as
>> https://bugs.swift.org/browse/SR-1487.
>>
>> Dmitri
>>
>> --
>> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
>> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160518/52108f1a/attachment.html>


More information about the swift-users mailing list