about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/multibyte/wcsrtombs.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/multibyte/wcsrtombs.c b/src/multibyte/wcsrtombs.c
index 5cf8f3eb..30be415d 100644
--- a/src/multibyte/wcsrtombs.c
+++ b/src/multibyte/wcsrtombs.c
@@ -21,8 +21,13 @@ size_t wcsrtombs(char *restrict s, const wchar_t **restrict ws, size_t n, mbstat
 		}
 		return n;
 	}
-	while (n>=4 && **ws) {
-		if (**ws >= 0x80u) {
+	while (n>=4) {
+		if (**ws-1u >= 0x7fu) {
+			if (!**ws) {
+				*s = 0;
+				*ws = 0;
+				return N-n;
+			}
 			l = wcrtomb(s, **ws, 0);
 			if (!(l+1)) return -1;
 			s += l;
@@ -33,8 +38,13 @@ size_t wcsrtombs(char *restrict s, const wchar_t **restrict ws, size_t n, mbstat
 		}
 		(*ws)++;
 	}
-	while (n && **ws) {
-		if (**ws >= 0x80u) {
+	while (n) {
+		if (**ws-1u >= 0x7fu) {
+			if (!**ws) {
+				*s = 0;
+				*ws = 0;
+				return N-n;
+			}
 			l = wcrtomb(buf, **ws, 0);
 			if (!(l+1)) return -1;
 			if (l>n) return N-n;
@@ -47,7 +57,5 @@ size_t wcsrtombs(char *restrict s, const wchar_t **restrict ws, size_t n, mbstat
 		}
 		(*ws)++;
 	}
-	if (n) *s = 0;
-	*ws = 0;
-	return N-n;
+	return N;
 }