Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: extended braces syntax, {1..32..-03}



On 5 December 2010 18:25, Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Sun, 5 Dec 2010 16:41:20 +0100
> Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>> This patch adds the syntax {start..end..step} to brace expansion. It
>> also allows negative numbers to be used (this was allowed when
>> braceccl was set before, which worked inconsistently). It also fixes
>> up zero-padding with negative numbers. You can specify zeropadding in
>> the step, as well as a negative step.
>>...
>> Missing still is documentation, but I've had this lying around since
>> august so I figured I'd post it and see if anyone wants to change
>> anything first.
>
> As long as we [have] tests that pass, it's OK by me.
>
> Embarrassingly, there isn't a set of tests for brace expansion at the
> moment, though a few aspects are covered in other tests. ÂHere is a
> short 'before' set of tests (I tried it out with the patch applied even
> though it doesn't test the new features).

Here's a patch that adds documentation and any tests I could think of
(it includes your patch (you had a typo "pading" on one line)).

I hesitated a bit on what the ordinal form of the variable "n3" is but
went with n3th instead of n3rd despite the latter being funnier.

http://mika.l3ib.org/patches/zsh-braces-test-docs.patch

From a59420f1cd5f9c440cedde08cec87c68939e16c8 Mon Sep 17 00:00:00 2001
From: Mikael Magnusson <mikachu@xxxxxxxxx>
Date: Sun, 5 Dec 2010 19:57:51 +0100
Subject: [PATCH] Add documentation and tests for braces

---
 Doc/Zsh/expn.yo    |   13 ++++++-
 Test/.distfiles    |    1 +
 Test/D09brace.ztst |   99 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+), 1 deletions(-)
 create mode 100644 Test/D09brace.ztst

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 585e8a9..b904621 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -1354,9 +1354,20 @@ where var(n1) and var(n2) are integers,
 is expanded to every number between
 var(n1) and var(n2) inclusive.  If either number begins with a
 zero, all the resulting numbers will be padded with leading zeroes to
-that minimum width.  If the numbers are in decreasing order the
+that minimum width, but for negative numbers the tt(-) character is also
+included in the width.  If the numbers are in decreasing order the
 resulting sequence will also be in decreasing order.

+An expression of the form `tt({)var(n1)tt(..)var(n2)tt(..)var(n3)tt(})',
+where var(n1), var(n2), and var(n3) are integers,
+is expanded as above, but only every var(n3)th number starting from var(n1)
+is output.  If var(n3) is negative the numbers are output in reverse order,
+this is slightly different from simply swapping var(n1) and var(n2) in the case
+that the step var(n3) doesn't evenly divide the range.  Zero padding can be
+specified in any of the three numbers, specifying it in the third can be useful
+to pad for example `tt({-99..100..01})' which is not possible to
specify by putting a
+0 on either of the first two numbers (i.e. pad to two characters).
+
 If a brace expression matches none of the above forms, it is left
 unchanged, unless the option tt(BRACE_CCL) (an abbreviation for `brace
 character class') is set.
diff --git a/Test/.distfiles b/Test/.distfiles
index 7376cec..0e69174 100644
--- a/Test/.distfiles
+++ b/Test/.distfiles
@@ -28,6 +28,7 @@ D05array.ztst
 D06subscript.ztst
 D07multibyte.ztst
 D08cmdsubst.ztst
+D09brace.ztst
 E01options.ztst
 E02xtrace.ztst
 Makefile.in
diff --git a/Test/D09brace.ztst b/Test/D09brace.ztst
new file mode 100644
index 0000000..d0ec93c
--- /dev/null
+++ b/Test/D09brace.ztst
@@ -0,0 +1,99 @@
+# Tests for brace expansion
+
+%prep
+
+  foo=(a b c)
+  arr=(foo bar baz)
+
+%test
+
+  print X{1,2,{3..6},7,8}Y
+0:Basic brace expansion
+>X1Y X2Y X3Y X4Y X5Y X6Y X7Y X8Y
+
+  print ${foo}{one,two,three}$arr
+0:Brace expansion with arrays, no RC_EXPAND_PARAM
+>a b conefoo ctwofoo cthreefoo bar baz
+
+  print ${^foo}{one,two,three}$arr
+0:Brace expansion with arrays, with RC_EXPAND_PARAM (1)
+>aonefoo atwofoo athreefoo bonefoo btwofoo bthreefoo conefoo ctwofoo
cthreefoo bar baz
+
+  print ${foo}{one,two,three}$^arr
+0:Brace expansion with arrays, with RC_EXPAND_PARAM (2)
+>a b conefoo ctwofoo cthreefoo conebar ctwobar cthreebar conebaz
ctwobaz cthreebaz
+
+  print ${^foo}{one,two,three}$^arr
+0:Brace expansion with arrays, with RC_EXPAND_PARAM (3)
+>aonefoo atwofoo athreefoo aonebar atwobar athreebar aonebaz atwobaz
athreebaz bonefoo btwofoo bthreefoo bonebar btwobar bthreebar bonebaz
btwobaz bthreebaz conefoo ctwofoo cthreefoo conebar ctwobar cthreebar
conebaz ctwobaz cthreebaz
+
+  print X{01..4}Y
+0:Numeric range expansion, padding (1)
+>X01Y X02Y X03Y X04Y
+
+  print X{1..04}Y
+0:Numeric range expansion, padding (2)
+>X01Y X02Y X03Y X04Y
+
+  print X{7..12}Y
+0:Numeric range expansion, padding (or not) (3)
+>X7Y X8Y X9Y X10Y X11Y X12Y
+
+  print X{07..12}Y
+0:Numeric range expansion, padding (4)
+>X07Y X08Y X09Y X10Y X11Y X12Y
+
+  print X{7..012}Y
+0:Numeric range expansion, padding (5)
+>X007Y X008Y X009Y X010Y X011Y X012Y
+
+  print X{4..1}Y
+0:Numeric range expansion, decreasing
+>X4Y X3Y X2Y X1Y
+
+  print X{1..4}{1..4}Y
+0:Numeric range expansion, combined braces
+>X11Y X12Y X13Y X14Y X21Y X22Y X23Y X24Y X31Y X32Y X33Y X34Y X41Y
X42Y X43Y X44Y
+
+  print X{-4..4}Y
+0:Numeric range expansion, negative numbers (1)
+>X-4Y X-3Y X-2Y X-1Y X0Y X1Y X2Y X3Y X4Y
+
+  print X{4..-4}Y
+0:Numeric range expansion, negative numbers (2)
+>X4Y X3Y X2Y X1Y X0Y X-1Y X-2Y X-3Y X-4Y
+
+  print X{004..-4..2}Y
+0:Numeric range expansion, stepping and padding (1)
+>X004Y X002Y X000Y X-02Y X-04Y
+
+  print X{4..-4..02}Y
+0:Numeric range expansion, stepping and padding (1)
+>X04Y X02Y X00Y X-2Y X-4Y
+
+  print X{1..32..3}Y
+0:Numeric range expansion, step alignment (1)
+>X1Y X4Y X7Y X10Y X13Y X16Y X19Y X22Y X25Y X28Y X31Y
+
+  print X{1..32..-3}Y
+0:Numeric range expansion, step alignment (2)
+>X31Y X28Y X25Y X22Y X19Y X16Y X13Y X10Y X7Y X4Y X1Y
+
+  print X{32..1..3}Y
+0:Numeric range expansion, step alignment (3)
+>X32Y X29Y X26Y X23Y X20Y X17Y X14Y X11Y X8Y X5Y X2Y
+
+  print X{32..1..-3}Y
+0:Numeric range expansion, step alignment (4)
+>X2Y X5Y X8Y X11Y X14Y X17Y X20Y X23Y X26Y X29Y X32Y
+
+  setopt brace_ccl
+  print X{za-q521}Y
+  unsetopt brace_ccl
+0:BRACE_CCL on
+>X1Y X2Y X5Y XaY XbY XcY XdY XeY XfY XgY XhY XiY XjY XkY XlY XmY XnY
XoY XpY XqY XzY
+
+  print X{za-q521}Y
+0:BRACE_CCL off
+>X{za-q521}Y
+
-- 
1.7.3



-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author