From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16798 invoked by uid 1002); 12 Aug 2003 07:58:43 -0000 Mailing-List: contact gentoo-dev-help@gentoo.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@gentoo.org Received: (qmail 11327 invoked from network); 12 Aug 2003 07:58:42 -0000 Date: Tue, 12 Aug 2003 00:58:41 -0700 From: "Robin H. Johnson" To: luke-jr@gentoo.org Cc: gentoo-dev@gentoo.org Message-ID: <20030812075841.GA23861@curie-int.orbis-terrarum.net> Mail-Followup-To: luke-jr@gentoo.org, gentoo-dev@gentoo.org Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="CUfgB8w4ZwR/yMy5" Content-Disposition: inline User-Agent: Mutt/1.5.4i Subject: [gentoo-dev] pure bash implementations of 'index' and 'rindex' X-Archives-Salt: ad281bcf-c17f-4462-b322-442559b48f44 X-Archives-Hash: 736e3f12cb297f29e1d1c240e78361b1 --CUfgB8w4ZwR/yMy5 Content-Type: multipart/mixed; boundary="tThc/1wpZn/ma/RB" Content-Disposition: inline --tThc/1wpZn/ma/RB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Luke-Jr asked in #gentoo-dev for these, but I thought others might find them useful as well. --=20 Robin Hugh Johnson E-Mail : robbat2@orbis-terrarum.net Home Page : http://www.orbis-terrarum.net/?l=3Dpeople.robbat2 ICQ# : 30269588 or 41961639 GnuPG FP : 11AC BA4F 4778 E3F6 E4ED F38E B27B 944E 3488 4E85 --tThc/1wpZn/ma/RB Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="index.bash" Content-Transfer-Encoding: quoted-printable #!/bin/bash # Minimalist implementations of the 'index' and 'rindex' functions in pure = Bash # returns a 0 based offset into your haystack string # This is provided solely as-is, and all I ask is a one line thank you note # with my email address. # Written by Robin H. Johnson function index() { local haystack=3D"$1" local needle=3D"$2" local fromstart=3D"${3-0}" haystack=3D"${haystack:${fromstart}}" local tmp=3D"${haystack/${needle}*}" [ "${#tmp}" -ge "${#haystack}" ] && echo -1 || echo $((${#tmp}+${fromstart}= -1)) } function rindex() { local haystack=3D"$1" local needle=3D"$2" local fromend=3D"${3-0}" haystack=3D"${haystack:0:$((${#haystack}-${fromend}+1))}" local tmp=3D"${haystack%${needle}*}" [ "${#tmp}" -ge "${#haystack}" ] && echo -1 || echo $((${#tmp}-1)) } # Testing code HAYSTACK=3D"Mary had a little lamb but the lamb had had measles" NEEDLE=3D"had" echo "'${HAYSTACK}' (${#HAYSTACK})" echo -n "Looking for needle '${NEEDLE}' [expect 4]: " index "${HAYSTACK}" "${NEEDLE}"=20 echo -n "Looking for needle '${NEEDLE}' after 7 [expect 35]: " index "${HAYSTACK}" "${NEEDLE}" 7 echo -n "Looking for needle 'foo' [expect -1]: " index "${HAYSTACK}" "foo" echo -n "Looking for needle 'foo' after 7 [expect -1]: " index "${HAYSTACK}" "foo" 7 echo -n "Looking for needle '${NEEDLE}' in reverse [expect 39]: " rindex "${HAYSTACK}" "${NEEDLE}" echo -n "Looking for needle '${NEEDLE}' in reverse skipping 10 [expect 35]:= " rindex "${HAYSTACK}" "${NEEDLE}" 10 echo -n "Looking for needle '${NEEDLE}' in reverse skipping 20 [expect 4]: " rindex "${HAYSTACK}" "${NEEDLE}" 20 echo -n "Looking for needle 'foo' in reverse [expect -1]: " rindex "${HAYSTACK}" "foo" echo -n "Looking for needle 'foo' in reverse skipping 10 [expect -1]: " rindex "${HAYSTACK}" "foo" 10 --tThc/1wpZn/ma/RB-- --CUfgB8w4ZwR/yMy5 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) Comment: Robbat2 @ Orbis-Terrarum Networks iD8DBQE/OJ4xsnuUTjSIToURAjNYAKCcSwFoWZ2RMqj/zzcQx3jE2mPXOgCfeKw0 ukW4pA5EsPHUNveVVYOe7Nk= =e8Il -----END PGP SIGNATURE----- --CUfgB8w4ZwR/yMy5--