Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Subject: Re: [gentoo-portage-dev] [PATCH] isolated-functions.sh: eliminate loop in has()
Date: Fri, 22 Apr 2016 15:15:13
Message-Id: 571A3FF9.4050909@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH] isolated-functions.sh: eliminate loop in has() by rindeal
1 On 04/22/2016 06:07 AM, rindeal wrote:
2 > From edc6df44de4e0f22322062c7c7e1b973bd89f4cd Mon Sep 17 00:00:00 2001
3 > From: Jan Chren <dev.rindeal@×××××.com <mailto:dev.rindeal@×××××.com>>
4 > Date: Fri, 22 Apr 2016 14:21:08 +0200
5 > Subject: [PATCH] isolated-functions.sh: eliminate loop in has()
6 >
7 > Looping is slow and clutters debug log.
8 > Still this wouldn't matter that much if has() wasn't one of the most
9 > used functions.
10 >
11 > Thus this patch should bring a general improvement.
12 > ---
13 > bin/isolated-functions.sh | 10 ++++------
14 > 1 file changed, 4 insertions(+), 6 deletions(-)
15 >
16 > diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh
17 > index e320f71..6900f99 100644
18 > --- a/bin/isolated-functions.sh
19 > +++ b/bin/isolated-functions.sh
20 > @@ -463,14 +463,12 @@ hasv() {
21 > }
22 >
23 > has() {
24 > - local needle=$1
25 > + local needle=$'\a'"$1"$'\a'
26 > shift
27 > + local IFS=$'\a'
28 > + local haystack=$'\a'"$@"$'\a'
29 >
30 > - local x
31 > - for x in "$@"; do
32 > - [ "${x}" = "${needle}" ] && return 0
33 > - done
34 > - return 1
35 > + [[ "${haystack}" == *"${needle}"* ]]
36 > }
37 >
38 > __repo_attr() {
39 > --
40 > 2.7.3
41
42 We used to have a similar implementation, but it was changed to a loop
43 in order to be 100% compatible with PMS. As far as I know, the only way
44 to implement it in a way that truely respects whitespace in elements is
45 with a loop.
46
47 BTW, your version would have to use this in order to respect word
48 broundaries:
49
50 [[ " ${haystack} " == *" ${needle} "* ]]
51
52 However, that still doesn't completely respect whitespace. Consider:
53
54 has " b c " a b c d e
55
56 I'm not aware of any cases in which we actually need to respect
57 whitespace in the has function, but according to PMS is should respect
58 whitespace IIRC.
59 --
60 Thanks,
61 Zac