From 23249ef206ef23760e814cb9f8170e7ae93dabcc Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 27 Mar 2022 23:42:26 -0700 Subject: New header and macro. This provides a portable case statement implemented directly as switch on GNU Awk, and using other approaches on other Awks that do not have switch. --- testcases-case | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 testcases-case (limited to 'testcases-case') diff --git a/testcases-case b/testcases-case new file mode 100644 index 0000000..6e62412 --- /dev/null +++ b/testcases-case @@ -0,0 +1,54 @@ +1: +$cppawk ' +#include +BEGIN { +#if defined case_temps && defined case && defined of && \ + defined matching && defined cbreak && defined otherwise + print 1 +#endif +}' +: +1 +-- +2: +printf "%s\n" foo bar xyzzy x b | $cppawk ' +#include +{ + case ($0) { + of ("foo") + print "** foo case: fallthrough!" + cfall; + of ("bar", "xyzzy") { + print "** bar/xyzzy case" + cbreak; + } + matching (/x/) + print "** /x/ case" + cbreak; + otherwise + print "** default" + cbreak; + } +}' +: +** foo case: fallthrough! +** bar/xyzzy case +** bar/xyzzy case +** bar/xyzzy case +** /x/ case +** default +-- +3: +$cppawk ' +#include +function f(arg, case_temps) +{ + case (arg) { + of (1, 2, 3) cret(arg * 10) + otherwise cret(-arg) + } +} + +BEGIN { print f(1), f(2), f(3), f(4) }' +: +10 20 30 -4 -- cgit v1.2.3