Page MenuHomeFreeBSD

D57591.id179749.diff
No OneTemporary

D57591.id179749.diff

Index: printf/printf.1
===================================================================
--- printf/printf.1
+++ printf/printf.1
@@ -106,6 +106,21 @@
Multibyte characters can be constructed using multiple
.Cm \e Ns Ar num
sequences.
+.It Cm \eu Ns Ar HHHH
+Write the character whose Unicode code point value is the
+1- to 4-digit hexadecimal number
+.Ar HHHH ,
+encoded as a multibyte sequence in the current locale.
+.It Cm \eU Ns Ar HHHHHHHH
+Write the character whose Unicode code point value is the
+1- to 8-digit hexadecimal number
+.Ar HHHHHHHH ,
+encoded as a multibyte sequence in the current locale.
+Code points greater than
+.Li 0x10FFFF
+and surrogate values
+.Pq Li 0xD800 No \(en Li 0xDFFF
+are rejected.
.El
.Pp
Each format specification is introduced by the percent character
Index: printf/printf.c
===================================================================
--- printf/printf.c
+++ printf/printf.c
@@ -522,6 +522,53 @@
} else
*store = (char)value;
break;
+ /* universal character name */
+ case 'u': case 'U': {
+ char mb[MB_LEN_MAX];
+ int i, maxdigits, n;
+ wchar_t wc;
+
+ maxdigits = (*fmt == 'u') ? 4 : 8;
+ value = 0;
+ for (i = 0, ++fmt; i < maxdigits &&
+ isxdigit((unsigned char)*fmt); i++, ++fmt) {
+ value <<= 4;
+ if (*fmt <= '9')
+ value += *fmt - '0';
+ else if (*fmt <= 'F')
+ value += *fmt - 'A' + 10;
+ else
+ value += *fmt - 'a' + 10;
+ }
+ --fmt;
+ if (i == 0) {
+ /* No hex digits; emit \u or \U literally. */
+ *store = *fmt;
+ break;
+ }
+ if (value > 0x10FFFF ||
+ (value >= 0xD800 && value <= 0xDFFF)) {
+ warnx("invalid universal character "
+ "name \\%c%0*X", maxdigits == 4 ? 'u' : 'U',
+ maxdigits, value);
+ *store = '?';
+ break;
+ }
+ wc = (wchar_t)value;
+ n = wctomb(mb, wc);
+ if (n <= 0) {
+ warnx("character \\%c%0*X not representable "
+ "in current locale",
+ maxdigits == 4 ? 'u' : 'U',
+ maxdigits, value);
+ *store = '?';
+ break;
+ }
+ for (i = 0; i < n - 1; i++)
+ *store++ = mb[i];
+ *store = mb[n - 1];
+ break;
+ }
default:
*store = *fmt;
break;
Index: printf/tests/Makefile
===================================================================
--- printf/tests/Makefile
+++ printf/tests/Makefile
@@ -16,6 +16,7 @@
${PACKAGE}FILES+= regress.missingpos1.out
${PACKAGE}FILES+= regress.s.out
${PACKAGE}FILES+= regress.sh
+${PACKAGE}FILES+= regress.utf8.out
${PACKAGE}FILES+= regress.zero.out
.include <bsd.test.mk>
Index: printf/tests/regress.sh
===================================================================
--- printf/tests/regress.sh
+++ printf/tests/regress.sh
@@ -1,7 +1,7 @@
REGRESSION_START($1)
-echo '1..24'
+echo '1..25'
REGRESSION_TEST(`b', `printf "abc%b%b" "def\n" "\cghi"')
REGRESSION_TEST(`d', `printf "%d,%5d,%.5d,%0*d,%.*d\n" 123 123 123 5 123 5 123')
@@ -27,5 +27,6 @@
REGRESSION_TEST(`missingpos1', `printf "%*1\$.*2\$s" 1 1 1 2>&1')
REGRESSION_TEST(`missingpos1', `printf "%1\$*.*2\$s" 1 1 1 2>&1')
REGRESSION_TEST(`bwidth', `printf "%8.2b" "a\nb\n"')
+REGRESSION_TEST(`utf8', `printf "\u2A7D\n"')
REGRESSION_END()
Index: printf/tests/regress.utf8.out
===================================================================
--- /dev/null
+++ printf/tests/regress.utf8.out
@@ -0,0 +1 @@
+⩽

File Metadata

Mime Type
text/plain
Expires
Thu, Jul 2, 4:05 AM (4 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
34585841
Default Alt Text
D57591.id179749.diff (3 KB)

Event Timeline