On Tue, Feb 21, 2017 at 9:30 AM, T L <tapir....@gmail.com> wrote:

>
>
> On Tuesday, February 21, 2017 at 1:30:24 AM UTC+8, Ian Lance Taylor wrote:
>>
>> On Mon, Feb 20, 2017 at 9:12 AM, T L <tapi...@gmail.com> wrote:
>> >
>> > I know gc assures this, but I don't know if it is compiler specified or
>> a
>> > spec rule.
>>
>> I can't think of any way that a Go program that does not unsafe could
>> detect whether a string slice uses the same memory as the string being
>> sliced.  Also, I can easily imagine that in some cases it would be
>> desirable to make a copy, as when writing s[i:i+1] when len(s) > 4096
>> and there are no other references to s.  So I don't think this should
>> be written down in the spec.  I think Go programs should trust that
>> the implementation will make the right choice, which will of course be
>> to normally not copy the string.
>>
>> Ian
>>
>
> My intention is to make sure s[a:b] will always share the underlying bytes
> with s,
>

Quoting the spec:

If the sliced operand of a valid slice expression is a nil slice, the
result is a nil slice. Otherwise, the result shares its underlying array
with the operand.


> otherwise strings.Index(s[fromIndex:], sep) would be inefficient if
> s[fromIndex:] doesn't share the underlying bytes with s.
>

Ian's point was: You should assume that it's efficient whether or not it
actually shares memory. If that assumption turns out to be false, you can
always file a bug against that particular implementation.


> I know gc can assure this. My concern is whether or not other compilers
> can assure this too if the spec doesn't cover this.
> And I think if the spec doesn't cover this, a new strings.IndexFrom
> function should be added to the standard lib.
>

If you are really, really convinced, that you need this (but you don't. You
are worrying about theoretical concerns that are incredibly unlikely to be
relevant in practice), just write it yourself. Copy the code from the
stdlib and adjust it to your needs.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to