The branch main has been updated by des:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=50eb68efcb473c8c20c016d618a8cd674d3706dc

commit 50eb68efcb473c8c20c016d618a8cd674d3706dc
Author:     Dag-Erling Smørgrav <[email protected]>
AuthorDate: 2026-12-02 16:53:09 +0000
Commit:     Dag-Erling Smørgrav <[email protected]>
CommitDate: 2026-12-02 16:53:48 +0000

    stdio: Implement ranges for wchar scansets
    
    The C and POSIX standards allow but do not require hyphenated ranges to
    be supported in scanf() / wscanf() scansets.  We implemented them for
    scanf() but not for wscanf().  Do so using similar logic as for scanf().
    This is hard to do efficiently for wide characters, which can in theory
    span the full 32-bit range (and even the actual range of about 1 million
    Unicode codepoints is difficult to handle), so we don't try to be
    efficient, but just focus on producing the same results for wscanf() as
    for scanf().
    
    MFC after:      1 week
    Reviewed by:    kevans, emaste
    Differential Revision:  https://reviews.freebsd.org/D62200
---
 lib/libc/stdio/vfwscanf.c | 46 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 6 deletions(-)

diff --git a/lib/libc/stdio/vfwscanf.c b/lib/libc/stdio/vfwscanf.c
index fc8c61baa978..9e4c04d99f0d 100644
--- a/lib/libc/stdio/vfwscanf.c
+++ b/lib/libc/stdio/vfwscanf.c
@@ -43,6 +43,7 @@
 #include <ctype.h>
 #include <inttypes.h>
 #include <limits.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
@@ -52,6 +53,7 @@
 #include <wctype.h>
 #include "un-namespace.h"
 
+#include "collate.h"
 #include "libc_private.h"
 #include "local.h"
 #include "xlocale_private.h"
@@ -92,16 +94,48 @@ struct ccl {
        int compl;              /* ccl is complemented? */
 };
 
-static __inline int
+static __inline bool
 inccl(const struct ccl *ccl, wint_t wi)
 {
+       struct xlocale_collate *table =
+           (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
+       const wchar_t *c;
+       bool in = false;
 
-       if (ccl->compl) {
-               return (wmemchr(ccl->start, wi, ccl->end - ccl->start)
-                   == NULL);
-       } else {
-               return (wmemchr(ccl->start, wi, ccl->end - ccl->start) != NULL);
+       for (c = ccl->start; c < ccl->end; c++) {
+               /*
+                * Check if the current character matches.
+                */
+               if (wi == *c) {
+                       in = true;
+                       break;
+               }
+               /*
+                * Check if the current character is followed by a hyphen
+                * and another character greater than or equal to the
+                * current one; if so, treat that as a range.
+                */
+               if (table->__collate_load_error) {
+                       if (ccl->end - c > 2 && c[1] == L'-' && c[2] >= c[0]) {
+                               if (c[0] <= wi && wi <= c[2]) {
+                                       in = true;
+                                       break;
+                               }
+                               c += 2;
+                       }
+               } else {
+                       if (ccl->end - c > 2 && c[1] == L'-' &&
+                           __collate_range_cmp(c[2], c[0]) >= 0) {
+                               if (__collate_range_cmp(c[0], wi) <= 0 &&
+                                   __collate_range_cmp(wi, c[2]) <= 0) {
+                                       in = true;
+                                       break;
+                               }
+                               c += 2;
+                       }
+               }
        }
+       return (ccl->compl ? !in : in);
 }
 
 /*

Reply via email to