From b739b6e05cce987b92eeefc317ff57c7be7956c5 Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Mon, 30 Sep 2024 09:50:47 -0600 Subject: [PATCH] sv_pos_2ub_forwards: Replace rolled-own with utf8_hop --- sv.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/sv.c b/sv.c index 2de62af3b819..f0b0b006d4b8 100644 --- a/sv.c +++ b/sv.c @@ -7491,23 +7491,16 @@ S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send, PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS; - while (s < send && uoffset) { - --uoffset; - s += UTF8SKIP(s); - } - if (s == send) { - *at_end = TRUE; - } - else if (s > send) { + SSize_t overshoot; + s = utf8_hop_forward_overshoot(s, uoffset, send, &overshoot); + if (s >= send) { *at_end = TRUE; - /* This is the existing behaviour. Possibly it should be a croak, as - it's actually a bounds error */ - s = send; } + /* If the unicode position is beyond the end, we return the end but shouldn't cache that position */ - *canonical_position = (uoffset == 0); - *uoffset_p -= uoffset; + *canonical_position = ! overshoot; + *uoffset_p -= overshoot; return s - start; }