if (SvUTF8(TARG)) {
/* Calculate exact length, let's not estimate. */
STRLEN targlen = 0;
- U8 *result;
- U8 *send;
STRLEN l;
UV nchar = 0;
UV nwide = 0;
+ U8 * const send = tmps + len;
- send = tmps + len;
while (tmps < send) {
const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
tmps += UTF8SKIP(tmps);
tmps -= len;
if (nwide) {
+ U8 *result;
+ U8 *p;
+
Newxz(result, targlen + 1, U8);
+ p = result;
while (tmps < send) {
const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
tmps += UTF8SKIP(tmps);
- result = uvchr_to_utf8_flags(result, ~c, UNICODE_ALLOW_ANY);
+ p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
}
- *result = '\0';
- result -= targlen;
+ *p = '\0';
sv_setpvn(TARG, (char*)result, targlen);
SvUTF8_on(TARG);
+ Safefree(result);
}
else {
+ U8 *result;
+ U8 *p;
+
Newxz(result, nchar + 1, U8);
+ p = result;
while (tmps < send) {
const U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY);
tmps += UTF8SKIP(tmps);
- *result++ = ~c;
+ *p++ = ~c;
}
- *result = '\0';
- result -= nchar;
+ *p = '\0';
sv_setpvn(TARG, (char*)result, nchar);
SvUTF8_off(TARG);
+ Safefree(result);
}
- Safefree(result);
SETs(TARG);
RETURN;
}