uchar.h: char16_t must be uint_least16_t

We attempted to make it a 32bit char type, but that is actually not allowed
unless we also make uint_least16_t a 32bit type. And even then, we
wouldn't be allowed to store or handle values wider than 16bit.

Comply more closely to the standard. As a result, mbtoc16r is not
implemented. c16rtomb is implemented by casting the char to 32bit, which
isn't really correct either (I think you're supposed to be able to feed
the two halves of a > 16bit codepoint in two separate calls and get a
meaningful result out?)

Related to #15990 but we may want an actual implementation?

Change-Id: If8198675c27dd2aa412bc44d12d3df4e31d3e8c7
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2623
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
Adrien Destugues 2020-05-09 13:11:45 +02:00 committed by waddlesplash
parent 566914fad4
commit 22337d8f4b

View File

@ -1,44 +1,59 @@
/*
* Copyright 2018 Haiku, Inc. All Right Reserved
* Copyright 2018-2020 Haiku, Inc. All Right Reserved
* Distributed under the terms of MIT license.
*/
#ifndef _UCHAR_H
#define _UCHAR_H
#include <wchar.h>
#ifdef __cplusplus
extern "C" {
#else
typedef wchar_t char32_t; /* our wchar_t is utf32 */
typedef wchar_t char16_t;
#endif
#define __STD_UTF_32__ 1
/* We don't define __STD_UTF_16__, so the format of char16_t is unspecified */
typedef uint_least32_t char32_t;
typedef uint_least16_t char16_t;
#define __STD_UTF_32__ 1
#define __STD_UTF_16__ 1
// TODO implement mbrtoc16
static __inline size_t
c16rtomb(char *dest, char16_t wc, mbstate_t *mbState)
c16rtomb(char *dest, char32_t wc, mbstate_t *mbState)
{
return wcrtomb(dest, wc, mbState);
wchar_t tmp = (wchar_t)wc;
return wcrtomb(dest, tmp, mbState);
}
static __inline size_t
mbrtoc16(char16_t *dest, const char *src, size_t srcLength, mbstate_t *mbState)
mbrtoc32(char32_t *dest, const char *src, size_t srcLength, mbstate_t *mbState)
{
return mbrtowc(dest, src, srcLength, mbState);
return mbrtowc((wchar_t*)dest, src, srcLength, mbState);
}
static __inline size_t
c32rtomb(char *dest, char32_t wc, mbstate_t *mbState)
{
return wcrtomb(dest, wc, mbState);
return wcrtomb(dest, (wchar_t)wc, mbState);
}
static __inline size_t
mbrtoc32(char32_t *dest, const char *src, size_t srcLength, mbstate_t *mbState)
{
return mbrtowc(dest, src, srcLength, mbState);
return mbrtowc((wchar_t*)dest, src, srcLength, mbState);
}
#ifdef __cplusplus
}
#endif