summaryrefslogtreecommitdiff
path: root/handy.h
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2019-03-20 15:31:03 -0600
committerKarl Williamson <khw@cpan.org>2019-03-21 11:17:45 -0600
commit305fe86ebe53ebaec4b94533f37a83774647e346 (patch)
tree9bd0bf050f8999826ebaae0bebe76a90ff46adb3 /handy.h
parentef65a74af186beb93566cf827c5f543f4aa14645 (diff)
downloadperl-305fe86ebe53ebaec4b94533f37a83774647e346.tar.gz
Generalize macro and move to handy.h
The macro verified that its input was in the range '1' to '9' by using a subtraction and a single conditional. This commit generalizes this non-obvious method of avoiding a conditional, and moves it to handy.h so it can be used in other places.
Diffstat (limited to 'handy.h')
-rw-r--r--handy.h6
1 files changed, 6 insertions, 0 deletions
diff --git a/handy.h b/handy.h
index 57ad62dc61..150fb08c56 100644
--- a/handy.h
+++ b/handy.h
@@ -1094,6 +1094,12 @@ patched there. The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
#define FITS_IN_8_BITS(c) (1)
#endif
+/* Returns true if c is in the range l..u
+ * Written with the cast so it only needs one conditional test
+ */
+#define inRANGE(c, l, u) (__ASSERT_((u) >= (l)) \
+ ((WIDEST_UTYPE) (((c) - (l)) | 0) <= ((WIDEST_UTYPE) ((u) - (l)))))
+
#ifdef EBCDIC
# ifndef _ALL_SOURCE
/* The native libc isascii() et.al. functions return the wrong results