Path: blob/master/waterbox/libc/functions/uchar/mbrtoc16.c
2 views
/* size_t mbrtoc16( char16_t *, const char *, size_t, mbstate_t * )12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#ifndef REGTEST7#include <uchar.h>8#include <errno.h>9#include <stdint.h>10#include <assert.h>11#include "_PDCLIB_encoding.h"12#include "_PDCLIB_locale.h"1314size_t mbrtoc16_l(15char16_t *restrict pc16,16const char *restrict s,17size_t n,18mbstate_t *restrict ps,19locale_t restrict l20)21{22size_t dstlen = 1;23size_t nr = n;2425if(!l->_Codec->__mbstoc16s) {26// No UTF-16 support in codec. Must synthesize on top of UCS-4 support.2728if(ps->_Surrogate) {29// If a pending surrogate is stored in the state30*pc16 = ps->_Surrogate;31ps->_Surrogate = 0;32return (size_t) -3;33}3435char32_t c32;36size_t res = mbrtoc32_l(&c32, s, n, ps, l);37if(res != (size_t) -1) {38// Conversion was successful. Check for surrogates39if(c32 <= 0xFFFF) {40// BMP char41*pc16 = c32;42} else {43// Supplementary char44*pc16 = 0xD800 | (c32 >> 10);45ps->_Surrogate = 0xDC00 | (c32 & 0x3FF);46}47}48return res;49} else if(l->_Codec->__mbstoc16s(&pc16, &dstlen, &s, &nr, ps)) {50// Successful conversion51if(dstlen == 0) {52// A character was output53if(nr == n) {54// The output character resulted entirely from stored state55return (size_t) -3;56} else if(pc16[-1] == 0) {57// Was null character58return 0;59} else {60// Count of processed characters61return n - nr;62}63} else {64assert(nr == 0 && "Must have processed whole input");65return (size_t) -2;66}67} else {68// Failed conversion69errno = EILSEQ;70return (size_t) -1;71}72}7374size_t mbrtoc16(75char16_t *restrict pc16,76const char *restrict s,77size_t n,78mbstate_t *restrict ps79)80{81return mbrtoc16_l(pc16, s, n, ps, _PDCLIB_threadlocale());82}8384#endif8586#ifdef TEST87#include "_PDCLIB_test.h"8889int main( void )90{91TESTCASE( NO_TESTDRIVER );92return TEST_RESULTS;93}94#endif959697