Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Martijn Dekker Newsgroups: gnu.bash.bug Subject: [PATCH] efficient [[ -v 1 ]] [was: Should [[ -v 1 ]] be supported?] Date: Sat, 18 Jan 2020 20:48:26 +0100 Lines: 44 Approved: bug-bash@gnu.org Message-ID: References: <5dac2cf2-2fac-0fd1-058f-6a84a3271738@case.edu> <858f35a5-50d1-0393-833b-7625f50979ac@inlv.org> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: usenet.stanford.edu 1579376918 17983 209.51.188.17 (18 Jan 2020 19:48:38 GMT) X-Complaints-To: action@cs.stanford.edu To: chet.ramey@case.edu, bug-bash@gnu.org Envelope-to: bug-bash@gnu.org User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 In-Reply-To: Content-Language: en-GB X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 37.59.109.123 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <858f35a5-50d1-0393-833b-7625f50979ac@inlv.org> X-Mailman-Original-References: <5dac2cf2-2fac-0fd1-058f-6a84a3271738@case.edu> Xref: csiph.com gnu.bash.bug:15815 Op 29-12-18 om 01:19 schreef Chet Ramey: > On 12/27/18 3:11 PM, Martijn Dekker wrote: > >> Consistency might be a better argument. If [[ -v foo ]] is equivalent to [[ >> -n ${foo+s} ]] for variables (with the advantage that you don't need 'eval' >> to handle arbitrary values of 'foo'), then perhaps it's not unreasonable to >> expect [[ -v 1 ]] to be equivalent to [[ -n ${1+s} ]]. > > The completeness argument is more rigorous, and there's a case to add this > in a future version of bash. He didn't make that argument, though. I noticed some deactivated code tagged bash-5.1 with my name on it. Cool. :) Isn't the way below much more efficient, though? There's no need to retrieve and discard the value of a positional parameter -- we can just compare the number to $#, i.e. number_of_args(). Any number between 0 and $#, inclusive, represents a set positional parameter by definition. - M. diff --git a/test.c b/test.c index 9cbc0436..27efd9f7 100644 --- a/test.c +++ b/test.c @@ -640,12 +640,7 @@ unary_test (op, arg) #if 0 /* TAG:bash-5.1 from Martijn Dekker */ else if (legal_number (arg, &r)) /* -v n == is $n set? */ { - char *t; - int ret; - t = get_dollar_var_value (r); - ret = t ? TRUE : FALSE; - free (t); - return ret; + return (r >= 0 && r <= number_of_args()); } #endif v = find_variable (arg); -- modernish -- harness the shell https://github.com/modernish/modernish