From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 026773858C2F; Mon, 20 Feb 2023 22:00:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 026773858C2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676930459; bh=DytJMmhx4GZUw32WxNajubp8zkssSXO1okCcyHx95cw=; h=From:To:Subject:Date:From; b=d8esclLibLzvuaTeN37vyOoncVNSsomJLerdZjk/l9+DmKqs6M3W+uWLeMrWiL8NK 0LAlTDvQ+kQZTZ907A7P5vgqWzkQxKqZptdiu47ihGKdGvlFSoEsNInaYhjvLnCDhc Z9tNuVpePyn0VZkls5u+D5RoIrhxJxumssQbX18U= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: add more UTF-32 helper functions X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: 1cbe4b3dccf1d2f6bb443bb84dc50197b0e58bc4 X-Git-Newrev: ffba9604d10fa0df27eea5e1930a215086608581 Message-Id: <20230220220059.026773858C2F@sourceware.org> Date: Mon, 20 Feb 2023 22:00:59 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dffba9604d10= fa0df27eea5e1930a215086608581 commit ffba9604d10fa0df27eea5e1930a215086608581 Author: Corinna Vinschen AuthorDate: Mon Feb 20 22:29:37 2023 +0100 Commit: Corinna Vinschen CommitDate: Mon Feb 20 22:29:37 2023 +0100 Cygwin: add more UTF-32 helper functions =20 wcintowcs: convert UTF-16 to UTF-32 string wcilen: return number of characters in a UTF-32 string wcincmp: compare two fixed-size UTF-32 strings =20 Used in followup patches introducing collating symbols =20 Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/local_includes/wchar.h | 46 ++++++++++++++++++++++++++++++++= ++++ winsup/cygwin/strfuncs.cc | 17 +++++++++++++ 2 files changed, 63 insertions(+) diff --git a/winsup/cygwin/local_includes/wchar.h b/winsup/cygwin/local_inc= ludes/wchar.h index 10b44791c505..6f2a4ad10f20 100644 --- a/winsup/cygwin/local_includes/wchar.h +++ b/winsup/cygwin/local_includes/wchar.h @@ -39,10 +39,56 @@ extern wctomb_f __utf8_wctomb; =20 #define __WCTOMB (__get_current_locale ()->wctomb) =20 +/* convert wint_t string to wchar_t string. Make sure dest + has room for at least twice as much characters to account + for surrogate pairs, plus a wchar_t NUL. */ +void wcintowcs (wchar_t *, wint_t *, size_t); + /* replacement function for mbrtowc, returning a wint_t representing a UTF-32 value. Defined in strfuncs.cc */ extern size_t mbrtowi (wint_t *, const char *, size_t, mbstate_t *); =20 +/* like wcslen, just for wint_t */ +static inline size_t +wcilen (const wint_t *wcs) +{ + size_t ret =3D 0; + + if (wcs) + while (*wcs++) + ++ret; + return ret; +} + +/* like wcscmp, just for wint_t */ +static inline int +wcicmp (const wint_t *s1, const wint_t *s2) +{ + while (*s1 =3D=3D *s2++) + if (*s1++ =3D=3D 0) + return (0); + return (*s1 - *--s2); +} + +/* like wcsncmp, just for wint_t */ +static inline int +wcincmp (const wint_t *s1, const wint_t *s2, size_t n) +{ + if (n =3D=3D 0) + return (0); + do + { + if (*s1 !=3D *s2++) + { + return (*s1 - *--s2); + } + if (*s1++ =3D=3D 0) + break; + } + while (--n !=3D 0); + return (0); +} + #ifdef __cplusplus } #endif diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc index d62179a1de06..e343a2fcc6e8 100644 --- a/winsup/cygwin/strfuncs.cc +++ b/winsup/cygwin/strfuncs.cc @@ -112,6 +112,23 @@ transform_chars_af_unix (PWCHAR out, const char *path,= __socklen_t len) return out; } =20 +/* convert wint_t string to wchar_t string. Make sure dest + has room for at least twice as much characters to account + for surrogate pairs, plus a wchar_t NUL. */ +extern "C" void +wcintowcs (wchar_t *dest, wint_t *src, size_t len) +{ + while (*src && len-- > 0) + if (*src > 0xffff) + { + *dest++ =3D ((*src - 0x10000) >> 10) + 0xd800; + *dest++ =3D ((*src++ - 0x10000) & 0x3ff) + 0xdc00; + } + else + *dest++ =3D *src++; + *dest =3D '\0'; +} + /* replacement function for mbrtowc, returning a wint_t representing a UTF-32 value. */ extern "C" size_t