zsh-users
 help / color / mirror / code / Atom feed
* A challenge – non-extendedglob whitespace trimming
@ 2018-10-24 15:12 Sebastian Gniazdowski
  0 siblings, 0 replies; only message in thread
From: Sebastian Gniazdowski @ 2018-10-24 15:12 UTC (permalink / raw)
  To: Zsh Users

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-10-24 15:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-24 15:12 A challenge – non-extendedglob whitespace trimming Sebastian Gniazdowski

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).