emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?


From: Tassilo Horn
Subject: Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?
Date: Mon, 16 Jul 2012 21:30:16 +0200
User-agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.1.50 (gnu/linux)

Lars Magne Ingebrigtsen <address@hidden> writes:

>> I'm not even able to come up with some concrete use-case where I'd
>> like to have the suggested behavior.
>
> It's a very common use case for me.  I know that a string can't be
> longer than X for some particular use, so I have to say
>
> (insert (if (> (length string) 4)
>             (substring string 0 4)
>           string))
>
> or something.          

Well, in those cases I'd just use (format "%.4" string), possibly with
"%4.4s" / "%-4.4s" if I also want padding to generate tabular-like
aligned output.

In any case, IMHO the little drawback in conciseness is worth the added
safety.  At least for me, whenever I have an index wrong it's because I
had false assumptions on my data (or because of my general arithmetic
disorder).  And then I'm happy if the error occurs as soon as possible
rather than having wrong results or an error later on where it might be
much harder to find the real cause.

Of course, that's no argument against another optional parameter to
substring to make it explicit that the TO index might be larger than the
length of the string.

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]