summaryrefslogtreecommitdiff
path: root/internal/util/math.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-08-11 09:23:36 +0000
committerLibravatar GitHub <noreply@github.com>2024-08-11 11:23:36 +0200
commit865b3aeaac8f165462796a7a5f8cf04ae7724d0f (patch)
treee17d58bf351e99fb512900ed5c3b6a60fe7c6196 /internal/util/math.go
parent[bugfix] ensure testrig package only compiled-in when debug enabled (#3185) (diff)
downloadgotosocial-865b3aeaac8f165462796a7a5f8cf04ae7724d0f.tar.xz
[bugfix] updated pinned counts on status delete (#3188)
* include pinned status when incrementing / decrementing status counts * remove the pinned increment on status creation * code comments * microoptimize decr
Diffstat (limited to 'internal/util/math.go')
-rw-r--r--internal/util/math.go9
1 files changed, 9 insertions, 0 deletions
diff --git a/internal/util/math.go b/internal/util/math.go
index e1850f772..f6e78fa1c 100644
--- a/internal/util/math.go
+++ b/internal/util/math.go
@@ -23,6 +23,15 @@ type Number interface {
~uintptr | ~float32 | ~float64
}
+// Decr performs a safe decrement of
+// n, clamping minimum value at zero.
+func Decr[N Number](n N) N {
+ if n <= 0 {
+ return 0
+ }
+ return n - 1
+}
+
// Div performs a safe division of
// n1 and n2, checking for zero n2. In the
// case of zero n2, zero is returned.