[vim 9 script] String indexing behavior different from legacy Vim

19 views
Skip to first unread message

Lifepillar

unread,
Jun 3, 2023, 6:20:10 AM6/3/23
to vim...@googlegroups.com
Legacy Vim script:

let text = 'àbc'
echo text[2]

Result: 'b'

Vim 9 script:

const text = 'àbc'
echo text[2]

Result: 'c'

Is the different behavior (counting chars vs bytes?) intentional?

Thanks,
Life.



Lifepillar

unread,
Jun 3, 2023, 6:26:16 AM6/3/23
to vim...@googlegroups.com
Mmh, even strpart(), which is supposed to count bytes, gives the same
result:

echo strpart(text, 2, 1)

is 'b' in legacy Vim, but 'c' in Vim 9 script.

Life.

Lifepillar

unread,
Jun 3, 2023, 6:38:36 AM6/3/23
to vim...@googlegroups.com
On 2023-06-03, Lifepillar <lifep...@lifepillar.me> wrote:
> On 2023-06-03, Lifepillar <lifep...@lifepillar.me> wrote:

> Mmh, even strpart(), which is supposed to count bytes, gives the same
> result:

Forget this: strpart() is fine. I have trouble just with indexing.

Life.


Bram Moolenaar

unread,
Jun 3, 2023, 7:12:53 AM6/3/23
to vim...@googlegroups.com, Lifepillar
Yes, in Vim9 script the index is in characters. In legacy script it is
in bytes.

The help for this doesn't have it's own tag, I'll add one.
You can find it above ":help vim9-gotchas".

--
hundred-and-one symptoms of being an internet addict:
101. U can read htis w/o ny porblm and cant figur eout Y its evn listd.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Lifepillar

unread,
Jun 3, 2023, 7:21:53 AM6/3/23
to vim...@googlegroups.com
On 2023-06-03, Bram Moolenaar <Br...@moolenaar.net> wrote:
>
>> Legacy Vim script:
>>
>> let text = 'àbc'
>> echo text[2]
>>
>> Result: 'b'
>>
>> Vim 9 script:
>>
>> const text = 'àbc'
>> echo text[2]
>>
>> Result: 'c'
>>
>> Is the different behavior (counting chars vs bytes?) intentional?
>
> Yes, in Vim9 script the index is in characters. In legacy script it is
> in bytes.
>
> The help for this doesn't have it's own tag, I'll add one.
> You can find it above ":help vim9-gotchas".

I was expecting to find it under `:help vim9-differences`, but either
I missed the relevant item, or it's not there, in which case it could be
added there.

Thanks,
Life.

Lifepillar

unread,
Jun 3, 2023, 7:30:28 AM6/3/23
to vim...@googlegroups.com
On 2023-06-03, Lifepillar <lifep...@lifepillar.me> wrote:
>> Yes, in Vim9 script the index is in characters. In legacy script it is
>> in bytes.
>>
>> The help for this doesn't have it's own tag, I'll add one.
>> You can find it above ":help vim9-gotchas".
>
> I was expecting to find it under `:help vim9-differences`, but either
> I missed the relevant item, or it's not there, in which case it could be
> added there.

Sorry, I read too fast. Maybe, a reference "see also vim9-gotchas" might
be added under vim9-differences, which seems to me the obvious place to
go (and probably easier to search than "gotchas").

Btw, despite the change having caused me some headache debugging one of
my scripts, I think that it makes a lot of sense for a text editor
scripting language to index strings by characters, so I welcome the
change!

Life.

Message has been deleted

Bram Moolenaar

unread,
Jun 3, 2023, 10:55:52 AM6/3/23
to vim...@googlegroups.com, Lifepillar
Well, that's an overview, not a complete list. There is no objective
way to decide what to put there, what is important enough. I can add
this one, but for expressions there are more things that can matter.
Adding a link to vim9-gotchas also would be useful.

--
Execuses for making a mistake:
In the morning: "I didn't have enough coffee yet!"
In the afternoon: "Look at all the things that I did right!"
In the evening: "I'm too tired!"

Bram Moolenaar

unread,
Jun 3, 2023, 12:03:57 PM6/3/23
to vim...@googlegroups.com, N i c o l a s

> Sorry but,
> Out of well indexing,
> which is faster ?
>
> 1) indexing by bytes -> legacy
> 2) indexing by characters -> vim9script

Indexing by bytes can be faster. But the difference is probably small,
the computation of the index probably matters more. You'll have to try
it out to know what happens in your situation.

--
hundred-and-one symptoms of being an internet addict:
102. When filling out your driver's license application, you give
your IP address.
Reply all
Reply to author
Forward
0 new messages