This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new c1c4e7d4afa scanf: handle hexadecimal floating-point notation.
c1c4e7d4afa is described below
commit c1c4e7d4afa846e2b5ed68c41c4fbc8cf7542a12
Author: anpeiyun <[email protected]>
AuthorDate: Wed Nov 25 21:16:34 2026 +0800
scanf: handle hexadecimal floating-point notation.
When scanf() is used with the %a, %A, %e, %E, %f, %F, %g or %G conversion
specifiers, it should also accept hexadecimal floating-point notation. The
general form is:
[sign] 0x [hexdigits] . [hexdigits] p [sign] [decimal exponent]
Signed-off-by: anpeiyun <[email protected]>
---
libs/libc/stream/lib_libvscanf.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/libs/libc/stream/lib_libvscanf.c b/libs/libc/stream/lib_libvscanf.c
index 12cd425e354..dfc58529cf7 100644
--- a/libs/libc/stream/lib_libvscanf.c
+++ b/libs/libc/stream/lib_libvscanf.c
@@ -1032,6 +1032,7 @@ static int vscanf_internal(FAR struct lib_instream_s
*stream, FAR int *lastc,
/* Copy the real string into a temporary working buffer. */
fwidth = 0;
+ base = 10;
expnt = false;
sign = false;
dot = false;
@@ -1050,6 +1051,17 @@ static int vscanf_internal(FAR struct lib_instream_s
*stream, FAR int *lastc,
stopconv = true;
}
}
+ else if (c == 'x' || c == 'X')
+ {
+ if (fwidth && tmp[fwidth - 1] == '0')
+ {
+ base = 16;
+ }
+ else
+ {
+ stopconv = true;
+ }
+ }
else if (c == '.')
{
if (!dot)
@@ -1062,7 +1074,8 @@ static int vscanf_internal(FAR struct lib_instream_s
*stream, FAR int *lastc,
stopconv = true;
}
}
- else if (c == 'e' || c == 'E')
+ else if (((c == 'e' || c == 'E') && base == 10) ||
+ ((c == 'p' || c == 'P') && base == 16))
{
if (!expnt)
{
@@ -1074,13 +1087,15 @@ static int vscanf_internal(FAR struct lib_instream_s
*stream, FAR int *lastc,
stopconv = true;
}
}
- else if (!(c >= '0' && c <= '9'))
+ else if ((c >= '0' && c <= '9') ||
+ (c >= 'a' && c <= 'f') ||
+ (c >= 'A' && c <= 'F'))
{
- stopconv = true;
+ sign = true;
}
else
{
- sign = true;
+ stopconv = true;
}
if (!stopconv)