public inbox for cygwin-cvs@sourceware.org help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org> To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: add more UTF-32 helper functions Date: Mon, 20 Feb 2023 22:00:59 +0000 (GMT) [thread overview] Message-ID: <20230220220059.026773858C2F@sourceware.org> (raw) https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=ffba9604d10fa0df27eea5e1930a215086608581 commit ffba9604d10fa0df27eea5e1930a215086608581 Author: Corinna Vinschen <corinna@vinschen.de> AuthorDate: Mon Feb 20 22:29:37 2023 +0100 Commit: Corinna Vinschen <corinna@vinschen.de> CommitDate: Mon Feb 20 22:29:37 2023 +0100 Cygwin: add more UTF-32 helper functions 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 Used in followup patches introducing collating symbols Signed-off-by: Corinna Vinschen <corinna@vinschen.de> 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_includes/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; #define __WCTOMB (__get_current_locale ()->wctomb) +/* 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 *); +/* like wcslen, just for wint_t */ +static inline size_t +wcilen (const wint_t *wcs) +{ + size_t ret = 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 == *s2++) + if (*s1++ == 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 == 0) + return (0); + do + { + if (*s1 != *s2++) + { + return (*s1 - *--s2); + } + if (*s1++ == 0) + break; + } + while (--n != 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; } +/* 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++ = ((*src - 0x10000) >> 10) + 0xd800; + *dest++ = ((*src++ - 0x10000) & 0x3ff) + 0xdc00; + } + else + *dest++ = *src++; + *dest = '\0'; +} + /* replacement function for mbrtowc, returning a wint_t representing a UTF-32 value. */ extern "C" size_t
reply other threads:[~2023-02-20 22:00 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20230220220059.026773858C2F@sourceware.org \ --to=corinna@sourceware.org \ --cc=cygwin-cvs@sourceware.org \ /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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).