On 13 February 2014, Liang Li <[email protected]> wrote:
> On Thursday, February 13, 2014 4:06:49 PM UTC-5, Bram Moolenaar wrote:
> > lcd wrote:
> > 
> > >     There is an asymmetry in handling indices for lists
> > > and strings.  Lists accept things like lst[n], lst[n1:n2],
> > > lst[n1:-n2], and lst[-n].  Strings accept str[n], str[n1:n2],
> > > str[n1:-n2] (all with the same meanings as the corresponding
> > > operations for lists), but not str[-n].  Negative indices in
> > > strings always return '', which is frustrating. :)
> > >
> > >     The patch below makes negative indices behave the same way for
> > > string as they do for lists (but they don't rise exceptions when
> > > out of range):
> > > 
> > >         :let a = '12345'
> > >         :echo a[-1]
> > >         5
> > >         :echo string(a[-6])
> > >         ''
> > > 
> > >     Negative indices for strings never did anything useful (at
> > > least as far as I can tell), so this is unlikely to break existing
> > > code.
> >
> > I suppose that would be OK, and useful, to add.
> >
> > Can you also add a test?
>
> Not to derail this, but regarding asymetries -- is there any reason
> why strings aren't treated like arrays of characters, ie, why I can't
> say let str[7]='p' or let str[7:9]='pqr'?

    I believe the reason is that you can't do that in Python. :)
Although, the semantics is already slightly different, since str[7:9] in
Vim is actually str[7:10] in Python.

    I suppose an argument can be made for implementing full splices for
both lists and strings.  E.g. something like list[3:5] = [] would delete
the corresponding elements etc.

    /lcd

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Raspunde prin e-mail lui