LibC: Start work on locales

This patch adds 2 locales, POSIX locale and UTF8 locale.

functions `mbstowcs()` and `strcoll()` use locales to do convertions and
comparison respectively.
This commit is contained in:
2024-08-07 19:00:05 +03:00
parent 7afdfb150f
commit 3b23458ecc
5 changed files with 183 additions and 13 deletions

View File

@@ -1,8 +1,10 @@
#include <BAN/Assert.h>
#include <BAN/Limits.h>
#include <BAN/Math.h>
#include <BAN/UTF8.h>
#include <ctype.h>
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -512,6 +514,41 @@ int putenv(char* string)
return 0;
}
size_t mbstowcs(wchar_t* __restrict pwcs, const char* __restrict s, size_t n)
{
auto* us = reinterpret_cast<const unsigned char*>(s);
size_t len = 0;
switch (__getlocale(LC_CTYPE))
{
case LOCALE_INVALID:
ASSERT_NOT_REACHED();
case LOCALE_POSIX:
while (*us && len < n)
pwcs[len++] = *us++;
break;
case LOCALE_UTF8:
while (*us && len < n)
{
auto wch = BAN::UTF8::to_codepoint(us);
if (wch == BAN::UTF8::invalid)
{
errno = EILSEQ;
return -1;
}
pwcs[len++] = wch;
us += BAN::UTF8::byte_length(*us);
}
break;
}
if (len < n)
pwcs[len] = 0;
return len;
}
void* bsearch(const void* key, const void* base, size_t nel, size_t width, int (*compar)(const void*, const void*))
{
if (nel == 0)