Skip to content

Commit

Permalink
Bug 707984: Extend fmtquote to cope with high range unicode.
Browse files Browse the repository at this point in the history
Use surrogate pairs in output as required.

Also (thanks to Tor!) fix fmtquote_xml output.

Thanks to Xybei for the report, and the first version of the patch.
  • Loading branch information
robinwatts committed Nov 4, 2024
1 parent 58d6c93 commit 6b1cd71
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion source/fitz/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ static void fmtquote(struct fmtbuf *out, const char *s, int sq, int eq, int verb
for (i = 0; i < n; ++i)
fmtputc(out, s[i]);
}
else
else if (c <= 0xffff)
{
fmtputc(out, '\\');
fmtputc(out, 'u');
Expand All @@ -262,6 +262,24 @@ static void fmtquote(struct fmtbuf *out, const char *s, int sq, int eq, int verb
fmtputc(out, "0123456789ABCDEF"[(c>>4)&15]);
fmtputc(out, "0123456789ABCDEF"[(c)&15]);
}
else
{
/* Use a surrogate pair */
int hi = 0xd800 + ((c - 0x10000) >> 10);
int lo = 0xdc00 + ((c - 0x10000) & 0x3ff);
fmtputc(out, '\\');
fmtputc(out, 'u');
fmtputc(out, "0123456789ABCDEF"[(hi>>12)&15]);
fmtputc(out, "0123456789ABCDEF"[(hi>>8)&15]);
fmtputc(out, "0123456789ABCDEF"[(hi>>4)&15]);
fmtputc(out, "0123456789ABCDEF"[(hi)&15]);
fmtputc(out, '\\');
fmtputc(out, 'u');
fmtputc(out, "0123456789ABCDEF"[(lo>>12)&15]);
fmtputc(out, "0123456789ABCDEF"[(lo>>8)&15]);
fmtputc(out, "0123456789ABCDEF"[(lo>>4)&15]);
fmtputc(out, "0123456789ABCDEF"[(lo)&15]);
}
} else {
if (c == sq || c == eq)
fmtputc(out, '\\');
Expand Down Expand Up @@ -356,6 +374,12 @@ static void fmtquote_xml(struct fmtbuf *out, const char *s)
if (c < 32 || c >= 127) {
fmtputc(out, '&');
fmtputc(out, '#');
fmtputc(out, 'x');
if (c > 65535)
{
fmtputc(out, "0123456789ABCDEF"[(c>>20)&15]);
fmtputc(out, "0123456789ABCDEF"[(c>>16)&15]);
}
if (c > 255)
{
fmtputc(out, "0123456789ABCDEF"[(c>>12)&15]);
Expand Down

0 comments on commit 6b1cd71

Please sign in to comment.