summaryrefslogtreecommitdiffhomepage
path: root/src/u16_mbstowcs.c
blob: 7f3428da8b7166ff4f84f587057cd7a600ecaf23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*******************************************************************/
/*  u16ports: u16 variants of wide character string functions.     */
/*  Copyright (C) 2017  SysDeer Technologies, LLC                  */
/*  Released under the Standard MIT License; see COPYING.U16PORTS. */
/*******************************************************************/

#include <wchar.h>
#include <uchar.h>
#include <u16ports/u16ports.h>

size_t u16_mbstowcs(uint16_t * dst, const char * src, size_t len)
{
	size_t		nbytes;
	uint16_t *	wch;
	mbstate_t	st = {0};

	for (wch=dst; len; src+=nbytes) {
		if ((nbytes = mbrtoc16(wch,src,4,&st)) == (size_t)-1) {
			return (size_t)(-1);

		} else if (nbytes == 0) {
			return wch - dst;

		} else if (nbytes == (size_t)-3) {
			nbytes = 0;
			len--;
			wch++;

		} else {
			len--;
			wch++;
		}
	}

	return wch - dst;
}