[] beyond end of string

11 views
Skip to first unread message

Invisible

unread,
Apr 16, 2021, 4:04:27 AM4/16/21
to LDMud Talk
Hello,

in 3.3.720:

string s = "test";

s[3] => 't'    // so far so good

s[4] => 0    // ** unexpected **

s[5] => runtime_error    // expected


Is this intentional? I couldn't find it in the documentation (in fact
doc/LPC/operators doesn't even mention strings in the section about
"expr1[expr2]").


I have a usecase where this is actually quite nice (saves me a check for
>=strlen), but I'm hesitant to use undocumented behavior. But then
again i can always use 'if (test[4..4] == "")' instead.


cya

  Invis

Stephan Weinberger

unread,
Apr 16, 2021, 4:08:53 AM4/16/21
to ldmud...@googlegroups.com
Addendum:

[x..x] is a bit slower than [x] though... so actually I would keep this
behavior but add proper documentation.

Gnomi

unread,
Apr 16, 2021, 4:57:32 AM4/16/21
to ldmud...@googlegroups.com
Hi Invisible,

Invisible wrote:
> string s = "test";
>
> s[3] => 't'    // so far so good
>
> s[4] => 0    // ** unexpected **
>
> s[5] => runtime_error    // expected
>
>
> Is this intentional? I couldn't find it in the documentation (in fact
> doc/LPC/operators doesn't even mention strings in the section about
> "expr1[expr2]").

The corresponding documentation is in /doc/efun/[]:

In the first form, the <index> must be within the bounds of
the string/array, or a runtime error occurs.

The behavior at the string end (s[4] returning 0 instead of a runtime
error) is deprecated. With pragma warn_deprecated you'll get "Warning:
Indexing past string end is deprecated". So this may change and also
result in a runtime error in future versions.

Regards,
Gnomi.

Invisible

unread,
Apr 16, 2021, 5:05:36 AM4/16/21
to ldmud...@googlegroups.com

On 16.04.21 10:39, Gnomi wrote:
>
> The corresponding documentation is in /doc/efun/[]:
>
> In the first form, the <index> must be within the bounds of
> the string/array, or a runtime error occurs.
>
> The behavior at the string end (s[4] returning 0 instead of a runtime
> error) is deprecated. With pragma warn_deprecated you'll get "Warning:
> Indexing past string end is deprecated". So this may change and also
> result in a runtime error in future versions.


I see, didn't expect it in efun.

Slices will still work beyond the end of the string (returning an empty
string)?

cya
  Invis

Gnomi

unread,
Apr 16, 2021, 5:31:43 AM4/16/21
to ldmud...@googlegroups.com
Hi,

Invisible wrote:
> Slices will still work beyond the end of the string (returning an empty
> string)?

Good question. Since LDMud 3.3.531 array slices give a warning when the
index is out of bounds (with pragma range_check). For strings there is
no such warning. You could argue that the behavior in both cases should
be the same.

However this warning currently does not mean that this is deprecated
behavior, it is just an optional hint that your bounds are incorrect.
So currently there are no plans to turn that into an error.

Regards,
Gnomi.
Reply all
Reply to author
Forward
0 new messages