From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: A challenge – non-extendedglob whitespace trimming
Date: Wed, 24 Oct 2018 17:12:38 +0200 [thread overview]
Message-ID: <CAKc7PVBQtU69UgRadStikW5_-sLN7A3JB_tMPWXNyTdjVN6J8w@mail.gmail.com> (raw)
Hello!
I think this is a valid challenge:
1. One can do the following to strip whitespace from string $str:
str=${${str##[[:space:]]##}%%[[:space:]]##}
This uses ## operator and thus requires setopt extendedglob.
2. Extendedglob-free code that does the same:
str="${str#"${str%%[! $'\t']*}"}" # leading whitespace
str="${str%"${str##*[! $'\t']}"}" # trailing whitespace
It's tricky, but the main thing one has to notice to grasp this: The
inner ${str%%...} substitution will match from end to beginning of
string till last non-whitespace. So it will left only leading
whitespace. Which is then removed from start of the string with
#-subst.
3. I have an array `pairs', holding elements like this: pairs=( "a ->
b" " c ->d " ), etc. I want to obtain array with additional split
on "->" (so it is nicely fitting into a hash, key-a val-b, etc.).
Following is an extendedglob-dependant code that performs the split
and also trims leading and trailing whitespace from all elements:
pairs=( "${(@)${(@)${(@s:->:)pairs}##[[:space:]]##}%%[[:space:]]##}" )
4. Is there a extendedglob-free (not depending on it) version of the
above point 3.?
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
reply other threads:[~2018-10-24 15:13 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=CAKc7PVBQtU69UgRadStikW5_-sLN7A3JB_tMPWXNyTdjVN6J8w@mail.gmail.com \
--to=sgniazdowski@gmail.com \
--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).