From 10477fc2d42bc46d4e99099c67e029896c2101fa Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Wed, 9 Jul 2014 00:39:25 -0500 Subject: [PATCH] Make `while()` handle `break` --- builtin.c | 2 +- tests/all.test | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/builtin.c b/builtin.c index 219133a4..6d53a582 100644 --- a/builtin.c +++ b/builtin.c @@ -984,7 +984,7 @@ static const char* const jq_builtins[] = { "def while(cond; update): " " def _while: " " if cond then ., (update | _while) else empty end; " - " _while;", + " try _while catch if .==\"break\" then empty else . end;", "def limit(n; exp): if n < 0 then exp else foreach exp as $item ([n, null]; if .[0] < 1 then break else [.[0] -1, $item] end; .[1]) end;", }; #undef LIBM_DD diff --git a/tests/all.test b/tests/all.test index 1175f1a6..55cb3891 100644 --- a/tests/all.test +++ b/tests/all.test @@ -226,6 +226,10 @@ null 1 [1,2,4,8,16,32,64] +[while(.<100; .*2|if . > 10 then break else . end)] +1 +[1,2,4,8] + [foreach .[] as $item ([3, null]; if .[0] < 1 then break else [.[0] -1, $item] end; .[1])] [11,22,33,44,55,66,77,88,99] [11,22,33]