summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibC/wchar.cpp
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2021-10-10 21:34:00 +0200
committerBrian Gianforcaro <b.gianfo@gmail.com>2021-10-15 21:50:19 -0700
commit420bdccf0b528152b29aa3a1cdfc1abf889655ac (patch)
tree5caca9b611ac720c670a3a3941472403843299b2 /Userland/Libraries/LibC/wchar.cpp
parentb0babd062eda9e18fcc2450317f83912e3b84c43 (diff)
downloadserenity-420bdccf0b528152b29aa3a1cdfc1abf889655ac.zip
LibC: Implement mbsrtowcs
Diffstat (limited to 'Userland/Libraries/LibC/wchar.cpp')
-rw-r--r--Userland/Libraries/LibC/wchar.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/Userland/Libraries/LibC/wchar.cpp b/Userland/Libraries/LibC/wchar.cpp
index 7bed4b5518..dd7ca983cf 100644
--- a/Userland/Libraries/LibC/wchar.cpp
+++ b/Userland/Libraries/LibC/wchar.cpp
@@ -505,4 +505,38 @@ size_t wcsrtombs(char* dest, const wchar_t** src, size_t len, mbstate_t* ps)
written += ret;
}
}
+
+size_t mbsrtowcs(wchar_t* dst, const char** src, size_t len, mbstate_t* ps)
+{
+ static mbstate_t _anonymous_state = {};
+
+ if (ps == nullptr)
+ ps = &_anonymous_state;
+
+ size_t written = 0;
+ while (written < len || !dst) {
+ // Convert next multibyte to wchar.
+ size_t ret = mbrtowc(dst, *src, MB_LEN_MAX, ps);
+
+ // Multibyte sequence is invalid.
+ if (ret == -1ul) {
+ errno = EILSEQ;
+ return (size_t)-1;
+ }
+
+ // Null byte has been reached.
+ if (**src == '\0') {
+ *src = nullptr;
+ return written;
+ }
+
+ *src += ret;
+ written += 1;
+ if (dst)
+ dst += 1;
+ }
+
+ // If we are here, we have written `len` wchars, but not reached the null byte.
+ return written;
+}
}