You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "casaroli (via GitHub)" <gi...@apache.org> on 2026/10/09 07:34:22 UTC
[PR] libs/libc/stream: Support %lc and %ls in printf() with CONFIG_LIBC_LOCALE. [nuttx]
casaroli opened a new pull request, #20544:
URL: https://github.com/apache/nuttx/pull/20544
## Summary
C99 specifies `%lc` for a wide character (`wint_t`) and `%ls` for a wide string (`wchar_t *`), written as their multibyte sequences. `printf()` ignored the `l` and handled them as `%c` and `%s`: `%lc` wrote only the low byte of the character, and `%ls` read the wide string as bytes and stopped at the first zero byte, which for ASCII text is after the first character.
For example, bash's `help` builds each line of its command list as a wide string and prints it with `%ls`, so the list came out as lines of spaces.
With `CONFIG_LIBC_LOCALE`, this PR converts them with `wcrtomb()`. For `%ls` the precision limits the number of bytes, and a character that does not fit is not written in part. A character that cannot be converted makes `printf()` fail with `EILSEQ`, as C99 specifies.
## Impact
Only with `CONFIG_LIBC_LOCALE` (existing option, default n). `printf()` is in almost every image, so the conversion is built only with the locale support. Without it, `%lc` and `%ls` behave as before.
## Testing
`qemu-armv8a:knsh`, QEMU 11, a test program:
```c
printf("[%ls]\n", L"abc");
printf("[%5ls][%-5ls]\n", L"ab", L"ab");
printf("[%.2ls]\n", L"abcd");
printf("[%lc][%lc]\n", (wint_t)L'x', (wint_t)0xe9);
n = snprintf(b, sizeof(b), "%ls", L"été");
printf("[%.1ls]\n", L"é");
```
Before (and without `CONFIG_LIBC_LOCALE`):
```
[a]
[ a][a ]
[a]
[x][?]
snprintf returned 1: [?]
[?] (a 2-byte character does not fit in 1 byte)
```
After, with `CONFIG_LIBC_LOCALE`:
```
[abc]
[ ab][ab ]
[ab]
[x][é]
snprintf returned 5: [été]
[] (a 2-byte character does not fit in 1 byte)
```
Builds with `-Wno-cpp -Werror`: `lm3s6965-ek:qemu-flat` and `mps2-an500:nsh` as they are; `qemu-armv8a:nsh`, `qemu-armv8a:knsh`, `mps2-an500:nsh`, `raspberrypi-pico:nsh`, `rv-virt:nsh` and `qemu-armv8a:nsh` with `CONFIG_LIBC_NUMBERED_ARGS`, all with `CONFIG_LIBC_LOCALE`.
`tools/checkpatch.sh` reports only the "Bad alignment" errors that `lib_libvsprintf.c` already has on master (its `case` labels are level with the braces).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] libs/libc/stream: Support %lc and %ls in printf() with CONFIG_LIBC_LOCALE. [nuttx]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #20544:
URL: https://github.com/apache/nuttx/pull/20544#issuecomment-6076799450
<!-- membrowse-pr-comment -->
## <img src="https://membrowse.com/membrowse-logo.svg" height="24" align="top"> MemBrowse Memory Report
*No memory changes detected for:*
- [arduino-mega2560](https://membrowse.com/public/apache/nuttx/target/arduino-mega2560/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
- [esp32-devkitc](https://membrowse.com/public/apache/nuttx/target/esp32-devkitc/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
- [hifive1-revb](https://membrowse.com/public/apache/nuttx/target/hifive1-revb/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
- [mirtoo](https://membrowse.com/public/apache/nuttx/target/mirtoo/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
- [qemu-armv8a](https://membrowse.com/public/apache/nuttx/target/qemu-armv8a/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
- [qemu-intel64](https://membrowse.com/public/apache/nuttx/target/qemu-intel64/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
- [rx65n-rsk2mb](https://membrowse.com/public/apache/nuttx/target/rx65n-rsk2mb/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
- [s698pm-dkit](https://membrowse.com/public/apache/nuttx/target/s698pm-dkit/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
- [stm32-nucleo-f103rb](https://membrowse.com/public/apache/nuttx/target/stm32-nucleo-f103rb/compare?head=f0e8c7108c466ea12a540fff146e94a8eb32747b&base=5b383ae5baa0a9e5ccdcd00f146e29d9f3ccc22c)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] libs/libc/stream: Support %lc and %ls in printf() with CONFIG_LIBC_LOCALE. [nuttx]
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 merged PR #20544:
URL: https://github.com/apache/nuttx/pull/20544
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org