summaryrefslogtreecommitdiff
path: root/dist/Data-Dumper
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2021-05-25 11:52:14 -0600
committerNicholas Clark <nick@ccl4.org>2021-06-30 08:32:26 +0000
commitfcbd4a2938c13706c5e26cd84141255c0ac9e202 (patch)
tree0c537d14c2ac5a44087c407f7089cc61712e0b2c /dist/Data-Dumper
parent11eb60b7e13a0e889bff409fd9bd08afdb4e85e9 (diff)
downloadperl-fcbd4a2938c13706c5e26cd84141255c0ac9e202.tar.gz
Dumper.xs: Revise calculation of needed size
This changes the calculation of how large a buffer is needed to use a loop instead of a chain of conditionals. I think it is easier to read, certainly it is tidier, and has the added (small) benefit that it doesn't give up and choose the worst case scenario for large code points.
Diffstat (limited to 'dist/Data-Dumper')
-rw-r--r--dist/Data-Dumper/Dumper.xs12
1 files changed, 5 insertions, 7 deletions
diff --git a/dist/Data-Dumper/Dumper.xs b/dist/Data-Dumper/Dumper.xs
index 7c49c9e9cb..b5d7e39fc6 100644
--- a/dist/Data-Dumper/Dumper.xs
+++ b/dist/Data-Dumper/Dumper.xs
@@ -272,13 +272,11 @@ esc_q_utf8(pTHX_ SV* sv, const char *src, STRLEN slen, I32 do_utf8, I32 useqq)
* first byte */
increment = (k == 0 && *s != '\0') ? 1 : UTF8SKIP(s);
- grow += 4 + (k <= 0xFF ? 2 : k <= 0xFFF ? 3 : k <= 0xFFFF ? 4 :
-#if UVSIZE == 4
- 8 /* We may allocate a bit more than the minimum here. */
-#else
- k <= 0xFFFFFFFF ? 8 : UVSIZE * 4
-#endif
- );
+ grow += 6; /* Smallest we do is "\x{FF}" */
+ k >>= 4;
+ while ((k >>= 4) != 0) { /* Add space for each nibble */
+ grow++;
+ }
}
else if (useqq) { /* Not utf8, must be <= 0xFF, hence 2 hex
* digits. */