Blame src/u16_mbstowcs.c

24bfc0
/*******************************************************************/
24bfc0
/*  u16ports: u16 variants of wide character string functions.     */
24bfc0
/*  Copyright (C) 2017  Z. Gilboa                                  */
24bfc0
/*  Released under the Standard MIT License; see COPYING.U16PORTS. */
24bfc0
/*******************************************************************/
24bfc0
24bfc0
#include <wchar.h>
24bfc0
#include <uchar.h>
24bfc0
#include <u16ports/u16ports.h>
24bfc0
24bfc0
size_t u16_mbstowcs(uint16_t * dst, const char * src, size_t len)
24bfc0
{
24bfc0
	size_t		nbytes;
24bfc0
	uint16_t *	wch;
24bfc0
	mbstate_t	st = {0};
24bfc0
24bfc0
	for (wch=dst; len; src+=nbytes) {
24bfc0
		if ((nbytes = mbrtoc16(wch,src,4,&st)) == (size_t)-1) {
24bfc0
			return (size_t)(-1);
24bfc0
24bfc0
		} else if (nbytes == 0) {
24bfc0
			return wch - dst;
24bfc0
24bfc0
		} else if (nbytes == (size_t)-3) {
24bfc0
			nbytes = 0;
24bfc0
			len--;
24bfc0
			wch++;
24bfc0
24bfc0
		} else {
24bfc0
			len--;
24bfc0
			wch++;
24bfc0
		}
24bfc0
	}
24bfc0
24bfc0
	return wch - dst;
24bfc0
}