From: Jens Gustedt <Jens.Gustedt@inria.fr>
To: musl@lists.openwall.com
Subject: [musl] [C23 scanf 3/3] C23: implement wN and wfN specifiers for scanf functions
Date: Wed, 31 May 2023 16:05:43 +0200 [thread overview]
Message-ID: <ae4f840bb48367d0a9aabeb5f12102caef2c71e9.1685536319.git.Jens.Gustedt@inria.fr> (raw)
In-Reply-To: <cover.1685536319.git.Jens.Gustedt@inria.fr>
---
src/stdio/vfscanf.c | 30 ++++++++++++++++++++++++++++++
src/stdio/vfwscanf.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+)
diff --git a/src/stdio/vfscanf.c b/src/stdio/vfscanf.c
index f72ac9c9..f8ace92f 100644
--- a/src/stdio/vfscanf.c
+++ b/src/stdio/vfscanf.c
@@ -41,6 +41,15 @@ static void store_int(void *dest, int size, unsigned long long i)
}
}
+static int getint(unsigned char const**s) {
+ int i=0;
+ if (**s != '0') for (; isdigit(**s); (*s)++) {
+ if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
+ else i = 10*i + (**s-'0');
+ }
+ return i;
+}
+
static void *arg_n(va_list ap, unsigned int n)
{
void *p;
@@ -57,6 +66,7 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
{
int width;
int size;
+ int fast16;
int alloc = 0;
int base;
const unsigned char *p;
@@ -150,6 +160,26 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
case 'L':
size = SIZE_L;
break;
+ case 'w':
+ // See if "fast" is requested. Difference is only
+ // relevant for a fast type of minimum width 16.
+ fast16 = SIZE_h;
+ if (*p == 'f') {
+ fast16 = SIZE_def;
+ ++p;
+ }
+ switch (getint(&p)) {
+ default: goto fmt_fail;
+ case 8: size = SIZE_hh; break;
+ case 16: size = fast16; break;
+ case 32: size = SIZE_def; break;
+#if UINTPTR_MAX >= UINT64_MAX
+ case 64: size = SIZE_l; break;
+#else
+ case 64: size = SIZE_ll; break;
+#endif
+ }
+ break;
case 'b': case 'd': case 'i': case 'o': case 'u': case 'x':
case 'a': case 'e': case 'f': case 'g':
case 'A': case 'E': case 'F': case 'G': case 'X':
diff --git a/src/stdio/vfwscanf.c b/src/stdio/vfwscanf.c
index 17f5a2f9..6fe2749c 100644
--- a/src/stdio/vfwscanf.c
+++ b/src/stdio/vfwscanf.c
@@ -41,6 +41,15 @@ static void store_int(void *dest, int size, unsigned long long i)
}
}
+static int getint(wchar_t const**s) {
+ int i=0;
+ if (**s != L'0') for (i=0; iswdigit(**s); (*s)++) {
+ if (i > INT_MAX/10U || **s-L'0' > INT_MAX-10*i) i = -1;
+ else i = 10*i + (**s-L'0');
+ }
+ return i;
+}
+
static void *arg_n(va_list ap, unsigned int n)
{
void *p;
@@ -87,6 +96,7 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
{
int width;
int size;
+ int fast16;
int alloc;
const wchar_t *p;
int c, t;
@@ -173,6 +183,26 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
case 'L':
size = SIZE_L;
break;
+ case 'w':
+ // See if "fast" is requested. Difference is only
+ // relevant for a fast type of minimum width 16.
+ fast16 = SIZE_h;
+ if (*p == 'f') {
+ fast16 = SIZE_def;
+ ++p;
+ }
+ switch (getint(&p)) {
+ default: goto fmt_fail;
+ case 8: size = SIZE_hh; break;
+ case 16: size = fast16; break;
+ case 32: size = SIZE_def; break;
+#if UINTPTR_MAX >= UINT64_MAX
+ case 64: size = SIZE_l; break;
+#else
+ case 64: size = SIZE_ll; break;
+#endif
+ }
+ break;
case 'b': case 'd': case 'i': case 'o': case 'u': case 'x':
case 'a': case 'e': case 'f': case 'g':
case 'A': case 'E': case 'F': case 'G': case 'X':
--
2.34.1
prev parent reply other threads:[~2023-05-31 14:06 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1685536319.git.Jens.Gustedt@inria.fr>
2023-05-31 14:05 ` [musl] [C23 scanf 1/3] C23: Add the b specifier for scanf and allow 0b and 0B prefixes in integers Jens Gustedt
2023-05-31 14:05 ` [musl] [C23 scanf 2/3] C23: add the SCNb macros Jens Gustedt
2023-05-31 14:05 ` Jens Gustedt [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ae4f840bb48367d0a9aabeb5f12102caef2c71e9.1685536319.git.Jens.Gustedt@inria.fr \
--to=jens.gustedt@inria.fr \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).