From f857f38f718fff586e373815a1bcad2701b4d983 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 7 Apr 2025 01:17:02 -0400 Subject: Add `is_between()` for various types --- src/stdlib/bytes.h | 3 +++ src/stdlib/integers.h | 6 ++++++ src/stdlib/nums.h | 6 ++++++ 3 files changed, 15 insertions(+) (limited to 'src/stdlib') diff --git a/src/stdlib/bytes.h b/src/stdlib/bytes.h index f875e7cb..2794d1d9 100644 --- a/src/stdlib/bytes.h +++ b/src/stdlib/bytes.h @@ -22,6 +22,9 @@ Byte_t Byte$from_int16(int16_t i, bool truncate); Closure_t Byte$to(Byte_t first, Byte_t last, OptionalInt8_t step); MACROLIKE Byte_t Byte$from_int8(int8_t i) { return (Byte_t)i; } MACROLIKE Byte_t Byte$from_bool(bool b) { return (Byte_t)b; } +MACROLIKE CONSTFUNC bool Byte$is_between(const Byte_t x, const Byte_t low, const Byte_t high) { + return low <= x && x <= high; +} extern const Byte_t Byte$min; extern const Byte_t Byte$max; diff --git a/src/stdlib/integers.h b/src/stdlib/integers.h index 8988e7c8..12fec62b 100644 --- a/src/stdlib/integers.h +++ b/src/stdlib/integers.h @@ -33,6 +33,9 @@ Closure_t type_name ## $to(c_type first, c_type last, Optional ## type_name ## _t step); \ Closure_t type_name ## $onward(c_type first, c_type step); \ PUREFUNC Optional ## type_name ## _t type_name ## $parse(Text_t text); \ + MACROLIKE CONSTFUNC bool type_name ## $is_between(const c_type x, const c_type low, const c_type high) { \ + return low <= x && x <= high; \ + } \ MACROLIKE PUREFUNC c_type type_name ## $clamped(c_type x, c_type min, c_type max) { \ return x < min ? min : (x > max ? max : x); \ } \ @@ -93,6 +96,9 @@ Text_t Int$value_as_text(Int_t i); PUREFUNC uint64_t Int$hash(const void *x, const TypeInfo_t *type); PUREFUNC int32_t Int$compare(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC int32_t Int$compare_value(const Int_t x, const Int_t y); +MACROLIKE CONSTFUNC bool Int$is_between(const Int_t x, const Int_t low, const Int_t high) { + return Int$compare_value(low, x) <= 0 && Int$compare_value(x, high) <= 0; +} PUREFUNC bool Int$equal(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC bool Int$equal_value(const Int_t x, const Int_t y); Text_t Int$format(Int_t i, Int_t digits); diff --git a/src/stdlib/nums.h b/src/stdlib/nums.h index f3de9dc5..84d87dd5 100644 --- a/src/stdlib/nums.h +++ b/src/stdlib/nums.h @@ -32,6 +32,9 @@ CONSTFUNC bool Num$isnan(double n); double Num$nan(Text_t tag); CONSTFUNC double Num$mix(double amount, double x, double y); OptionalNum_t Num$parse(Text_t text); +MACROLIKE CONSTFUNC bool Num$is_between(const double x, const double low, const double high) { + return low <= x && x <= high; +} MACROLIKE CONSTFUNC double Num$clamped(double x, double low, double high) { return (x <= low) ? low : (x >= high ? high : x); } @@ -88,6 +91,9 @@ CONSTFUNC bool Num32$isnan(float n); CONSTFUNC float Num32$mix(float amount, float x, float y); OptionalNum32_t Num32$parse(Text_t text); float Num32$nan(Text_t tag); +MACROLIKE CONSTFUNC bool Num32$is_between(const float x, const float low, const float high) { + return low <= x && x <= high; +} MACROLIKE CONSTFUNC float Num32$clamped(float x, float low, float high) { return (x <= low) ? low : (x >= high ? high : x); } -- cgit v1.2.3