From f348bfa27ccd04c239b6f20f1e43756c20da87ff Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Tue, 26 Aug 2025 20:41:16 +0200 Subject: [PATCH] Fix pluralization if the value exceeds .Machine$integer.max. --- NEWS.md | 3 +++ R/pluralize.R | 5 ++++- tests/testthat/_snaps/pluralization.md | 15 +++++++++++++++ tests/testthat/test-pluralization.R | 9 +++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index d874a010..de6baf9f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ * New `{.num}` and `{.bytes}` inline styles to format numbers and bytes (@m-muecke, #644, #588, #643). +* Make `pluralize()` support values exceeding `.Machine$integer.max` (@mcol, + #773). + # cli 3.6.5 * `code_highlight()` supports long strings and symbols diff --git a/R/pluralize.R b/R/pluralize.R index c7663fc6..07c77c11 100644 --- a/R/pluralize.R +++ b/R/pluralize.R @@ -9,7 +9,10 @@ make_quantity <- function(object) { val <- if (is.numeric(object)) { stopifnot(length(object) == 1) - if (is.finite(object)) as.integer(object) else object + if (is.finite(object) && abs(object) <= .Machine$integer.max) + as.integer(object) + else + object } else { length(object) } diff --git a/tests/testthat/_snaps/pluralization.md b/tests/testthat/_snaps/pluralization.md index 3ffb2eb0..e47cf4ec 100644 --- a/tests/testthat/_snaps/pluralization.md +++ b/tests/testthat/_snaps/pluralization.md @@ -292,3 +292,18 @@ Output Will remove the -Inf packages. +# issue 773 + + Code + print(pluralize("{.Machine$integer.max} value{?s}")) + Output + 2147483647 values + Code + print(pluralize("{.Machine$integer.max + 1} value{?s}")) + Output + 2147483648 values + Code + print(pluralize("{-1L * .Machine$integer.max - 1} value{?s}")) + Output + -2147483648 values + diff --git a/tests/testthat/test-pluralization.R b/tests/testthat/test-pluralization.R index d1b50cda..3cb2b974 100644 --- a/tests/testthat/test-pluralization.R +++ b/tests/testthat/test-pluralization.R @@ -134,3 +134,12 @@ test_that("Edge cases for pluralize() (#701)", { print(pluralize("Will remove {?no/the/the} {-Inf} package{?s}.")) }) }) + +test_that("issue 773", { + expect_snapshot({ + # Should be pluralized + print(pluralize("{.Machine$integer.max} value{?s}")) + print(pluralize("{.Machine$integer.max + 1} value{?s}")) + print(pluralize("{-1L * .Machine$integer.max - 1} value{?s}")) + }) +})