all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Why (substring "abc" 0 4) does not return "abc" instead of an error?
@ 2012-07-15 23:15 Bastien
  2012-07-15 23:29 ` Juanma Barranquero
  0 siblings, 1 reply; 42+ messages in thread
From: Bastien @ 2012-07-15 23:15 UTC (permalink / raw)
  To: emacs-devel

As the subject says: I wonder why 

  (substring "abc" 0 4)

does not return "abc".

Is it a design choice or due to some implementation 
constraints?

Thanks,

-- 
 Bastien




^ permalink raw reply	[flat|nested] 42+ messages in thread
* Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?
@ 2012-07-16  3:45 Dmitry Gutov
  2012-07-16  7:32 ` Bastien
  2012-07-16 13:10 ` Pascal J. Bourguignon
  0 siblings, 2 replies; 42+ messages in thread
From: Dmitry Gutov @ 2012-07-16  3:45 UTC (permalink / raw)
  To: lekktu; +Cc: bzg, emacs-devel

Juanma Barranquero <lekktu@gmail.com> writes:

 > On Mon, Jul 16, 2012 at 1:59 AM, Bastien <bzg@gnu.org> wrote:
 >
 >> I read (substring "abc" 0 4) as "return the biggest substring
 >> between 0 and 4" -- even if the string does not have 4 characters.
 >
 > "Even if the string does not have 4 characters" is not even suggested
 > in substring's doc.

FWIW, it's common behavior in many other programming languages.

--Dmitry



^ permalink raw reply	[flat|nested] 42+ messages in thread
* Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?
@ 2012-07-16 19:00 Dmitry Gutov
  2012-07-16 19:51 ` Tassilo Horn
  0 siblings, 1 reply; 42+ messages in thread
From: Dmitry Gutov @ 2012-07-16 19:00 UTC (permalink / raw)
  To: tassilo; +Cc: bzg, lekktu, monnier, emacs-devel

Tassilo Horn <tassilo@member.fsf.org> writes:
 > BTW: C++ string's substr method doesn't quite have the suggested
 > behavior.  It's arguments are not two indices, but one index and one
 > length n (the number of characters that should be returned).  If the
 > index is out of range, you'll get an out_of_range exception.  n however
 > may span longer than the rest of the string in which case the returned
 > string is shorter than the given length n.  But that's a different
 > story: indexes have to be in range.
 >
 > Ditto for Ruby: String::slice also gets an index and a length, not two
 > indices.

True, but it also accepts range as parameter. Neither form
raises error:

irb(main):007:0> "abc".slice(1, 10)
=> "bc"
irb(main):008:0> "abc".slice(0..1)
=> "ab"

Same thing with JavaScript:

--
[22:51:01.141] "abc".substring(0,1)
[22:51:01.147] "a"
[22:51:03.341] "abc".substring(1,2)
[22:51:03.345] "b"
[22:51:13.902] "abc".substring(1,20)
[22:51:13.905] "bc"
[22:52:00.768] "abc".substr(2,1)
[22:52:00.772] "c"

So far I don't see another language, aside from Emacs Lisp, that
interprets negative value of second index as "count from the end", yet
raises an "out of range" error if that value is too big.

--Dmitry



^ permalink raw reply	[flat|nested] 42+ messages in thread

end of thread, other threads:[~2012-07-16 22:53 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-15 23:15 Why (substring "abc" 0 4) does not return "abc" instead of an error? Bastien
2012-07-15 23:29 ` Juanma Barranquero
2012-07-15 23:59   ` Bastien
2012-07-16  0:10     ` Juanma Barranquero
2012-07-16  7:14       ` Bastien
2012-07-16 16:15         ` Stefan Monnier
2012-07-16 16:22           ` Bastien
2012-07-16 16:46           ` Bastien
2012-07-16 17:57             ` Tassilo Horn
2012-07-16 18:51               ` Lars Magne Ingebrigtsen
2012-07-16 19:30                 ` Bastien
2012-07-16 19:30                 ` Tassilo Horn
2012-07-16 20:20                 ` Pascal J. Bourguignon
2012-07-16 19:25               ` Bastien
2012-07-16 19:43                 ` Bastien
2012-07-16 20:19             ` Pascal J. Bourguignon
2012-07-16 20:30             ` Stefan Monnier
2012-07-16 22:28               ` Lennart Borgman
2012-07-16 22:48                 ` Bastien
2012-07-16 22:53                   ` Lennart Borgman
2012-07-16  7:38       ` Andreas Schwab
2012-07-16  9:40         ` Juanma Barranquero
  -- strict thread matches above, loose matches on Subject: below --
2012-07-16  3:45 Dmitry Gutov
2012-07-16  7:32 ` Bastien
2012-07-16  7:52   ` Thierry Volpiatto
2012-07-16  8:38     ` Bastien
2012-07-16 13:03   ` Dmitry Gutov
2012-07-16 14:32     ` Bastien
2012-07-16 13:10 ` Pascal J. Bourguignon
2012-07-16 14:40   ` Bastien
2012-07-16 15:00     ` Pascal J. Bourguignon
2012-07-16 15:07       ` Lennart Borgman
2012-07-16 15:19         ` Pascal J. Bourguignon
2012-07-16 15:22         ` Bastien
2012-07-16 15:46       ` Bastien
2012-07-16 15:49         ` Bastien
2012-07-16 19:49           ` Thien-Thi Nguyen
2012-07-16 22:32             ` Bastien
2012-07-16 15:56         ` Pascal J. Bourguignon
2012-07-16 16:13           ` Bastien
2012-07-16 19:00 Dmitry Gutov
2012-07-16 19:51 ` Tassilo Horn

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.