zsh-users
 help / color / mirror / code / Atom feed
From: "Lawrence Velázquez" <larryv@zsh.org>
To: zsh-users@zsh.org
Subject: Re: optimal expansions?
Date: Sat, 20 Apr 2024 18:54:15 -0400	[thread overview]
Message-ID: <8947389c-2243-4b83-b59e-3535c6201228@app.fastmail.com> (raw)
In-Reply-To: <3d15311d-b5fa-4a55-a17e-e61fcdded7d0@eastlink.ca>

On Sat, Apr 20, 2024, at 10:23 AM, Ray Andrews wrote:
> ... this line is Basque to me.  It works but I have no idea how.  
> Anyway that's for me to research.  It baffles me that there's color 
> codes in there.
>>           print -P "%B%2F${pkg//\%/%%}%f%b"
>>         fi

The "-P" option tells "print" to interpret prompt escapes like "%B"
and "%F" (see zshmisc(1)), allowing you to avoid hardcoding ECMA-48
escape sequences.  The "${pkg//\%/%%}" expansion interpolates the
package name with all "%" characters escaped.


> Seems we want '-r' most of the time.

Yes, unless you actually do want "print" to interpret substrings
like "\n".  Sometimes you do, sometimes you don't.  If the operands
are dynamic and not under your control, you generally don't.


> Now that I know not to use newlines for splitting.  And of 
> course that obviates most of my '(@f)'s too.

That's because Roman's solution uses "read" to stream the output
from "apt-file" instead of saving it all and postprocessing it.
This doesn't have anything to do with "print" though.


> But I'm betting your code will be faster.

If speed is a concern...

% cat af_awk
#!/bin/sh -

apt-file search -- "$1" | awk '
	BEGIN {
		fg_bld_grn = "\33[1;32m"
		reset = "\33[0m"
	}

	$1 != last_pkg {
		last_pkg = $1
		printf "\n%s%s%s\n", fg_bld_grn, $1, reset
	}

	{
		# Preserve runs of blanks in paths.
		print substr($0, 1 + index($0, " "))
	}

	END {
		# Lack of input implies that apt-file failed.
		exit !NR
	}
'
% apt-file search .txt | wc -l
138962
% for x in ray roman awk; do eval time ./af_$x .txt; done >/dev/null
./af_ray .txt  39.43s user 1.74s system 102% cpu 40.099 total
./af_roman .txt  12.99s user 24.97s system 130% cpu 29.069 total
./af_awk .txt  3.08s user 1.02s system 131% cpu 3.131 total


-- 
vq


  reply	other threads:[~2024-04-20 22:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-19 19:22 Ray Andrews
2024-04-19 20:40 ` Lawrence Velázquez
2024-04-19 23:25   ` Ray Andrews
2024-04-20  7:42 ` Roman Perepelitsa
2024-04-20 14:23   ` Ray Andrews
2024-04-20 22:54     ` Lawrence Velázquez [this message]
2024-04-20 23:59       ` Ray Andrews
2024-04-21 12:23     ` Roman Perepelitsa
2024-04-21 14:09       ` Ray Andrews
2024-04-21 14:19         ` Roman Perepelitsa
2024-04-21 16:14           ` Stephane Chazelas
2024-04-21 17:39             ` Roman Perepelitsa
2024-04-21 20:13               ` Stephane Chazelas
2024-04-21 20:46                 ` Lawrence Velázquez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8947389c-2243-4b83-b59e-3535c6201228@app.fastmail.com \
    --to=larryv@zsh.org \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).