From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Bastien Newsgroups: gmane.emacs.devel Subject: Re: Why (substring "abc" 0 4) does not return "abc" instead of an error? Date: Mon, 16 Jul 2012 21:25:16 +0200 Organization: GNU Message-ID: <87obnfse3n.fsf@gnu.org> References: <87fw8smxag.fsf@gnu.org> <877gu4mv85.fsf@gnu.org> <871ukcmb47.fsf@gnu.org> <87hat7bqmh.fsf@gnu.org> <874np7h9ms.fsf@thinkpad.tsdh.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1342466696 17337 80.91.229.3 (16 Jul 2012 19:24:56 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 16 Jul 2012 19:24:56 +0000 (UTC) Cc: Juanma Barranquero , Stefan Monnier , emacs-devel@gnu.org To: Tassilo Horn Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jul 16 21:24:55 2012 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SqquY-0004cq-6C for ged-emacs-devel@m.gmane.org; Mon, 16 Jul 2012 21:24:50 +0200 Original-Received: from localhost ([::1]:51779 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SqquX-0000jD-Fk for ged-emacs-devel@m.gmane.org; Mon, 16 Jul 2012 15:24:49 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:37927) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SqquU-0000j7-PB for emacs-devel@gnu.org; Mon, 16 Jul 2012 15:24:47 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SqquT-0001oO-8Z for emacs-devel@gnu.org; Mon, 16 Jul 2012 15:24:46 -0400 Original-Received: from mail-wg0-f49.google.com ([74.125.82.49]:56213) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SqquT-0001o1-0T for emacs-devel@gnu.org; Mon, 16 Jul 2012 15:24:45 -0400 Original-Received: by wgbez12 with SMTP id ez12so3831381wgb.30 for ; Mon, 16 Jul 2012 12:24:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=sender:from:to:cc:subject:in-reply-to:organization:references :user-agent:date:message-id:mime-version:content-type :content-transfer-encoding; bh=o1d8Dv06P2fE0EBkFss40oHv+jzWHSwr/V/+2R5m/54=; b=TOwlkwgQhbWoBJKc6enGxnc8M/kkTiCnRhkQ3mVoQmI4XTlC7H2dPBi396lyMR+xTC KyzeRapihtjJriHyGKTs1f50jKpieiJf3VippVm+ze4kvFimQVrQBEbWQ+0fnJ1kxuQk Hswu/z3ib+a+53w7oDC7wNnHJ4SRcPQ1N9LI8OMBDPRmuhhWvx8iwdTpstBq8J6aWxO/ EgvgVLHP6I2j8CRNZQGCe3V8G+kEqCEiu6AjiUehe9t7eLfG6uDjwU1x3o4cxOKbzW7y zD4CLiuERrE3jsxPCkQUZGwBWJMbrJsRR4eJosbgAi4zZ9f3VLD82UZNQHEMmHQTkpKW dW9w== Original-Received: by 10.216.136.158 with SMTP id w30mr6384108wei.136.1342466684084; Mon, 16 Jul 2012 12:24:44 -0700 (PDT) Original-Received: from myhost.localdomain (mar75-2-81-56-68-112.fbx.proxad.net. [81.56.68.112]) by mx.google.com with ESMTPS id b7sm33861410wiz.9.2012.07.16.12.24.42 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 16 Jul 2012 12:24:43 -0700 (PDT) Original-Received: by myhost.localdomain (Postfix, from userid 1000) id 0A8748207; Mon, 16 Jul 2012 21:25:17 +0200 (CEST) In-Reply-To: <874np7h9ms.fsf@thinkpad.tsdh.de> (Tassilo Horn's message of "Mon, 16 Jul 2012 19:57:15 +0200") User-Agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.1.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 74.125.82.49 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:151685 Archived-At: Hi Tassilo, Tassilo Horn writes: > Bastien writes: > >>> We have general functionality when you want to ignore some errors, >>> such as condition-case. >> >> Also, I'm fine with >> >> (substring "abc" -1 1) >> => #ERROR > > I don't see why that justifies an error and (substring "abc" 0 4) does > not. -1 is a valid FROM index meaning the length of the string minus > one. Its just that the TO index is smaller than FROM here, but IMO > that's the same class of errors as a too large TO index. I would distinguish problems caused by only one indice from those caused by the relationship between two indices. Just nit-picking. >> so using ̀€condition-case' would not help me distinguish >> between the case above and (substring "abc" 0 4), which >> is what I want. > > A condition-case handler has access to the args given to the erroring > form, so the cases could be distinguished although both signal > args-of-range. Well, not that it would help you much here. Fair enough. >> I see the benefit of having >> >> (substring "abc" 0 4) >> => "abc" >> >> in terms of simplifying Elisp writing -- and I still fail >> to see the harm (but maybe Pascal will tell me where he has >> been bitten by this.) > > In my experiences, out-of-range indices into strings or arrays are > almost always programming errors. I'm not even able to come up with > some concrete use-case where I'd like to have the suggested behavior. > Either I know exactly what I'm operating on and use indices, or I have > only some general assumptions and then use more fuzzy things like > splitting by regular expressions. I get your point about programming errors. And I think it's just part of my brain that cannot help thinking of the third argument as a _length_ instead of an _index_. I now see how allowing to fail quietly would make this misconception more common. How about these two defun-future-defsubst candidates? (defun string-head (string n) "Return N characters starting from the beginning of STRING. If N is larger than the length of STRING, return it." (substring string 0 (min n (length string)))) (defun string-tail (string n) "Return N characters starting from the end of STRING. If N is larger than the length of STRING, return it." (let* ((l (length string)) (s (- l n)) (d (if (> s 0) s 0))) (substring string d l))) -- Bastien