libc: Add wcslen (with a test) and wcscat

This commit is contained in:
apio 2022-11-12 14:16:00 +01:00
parent 35829a6998
commit e54f033578
5 changed files with 68 additions and 4 deletions

View File

@ -1,11 +1,24 @@
#ifndef _WCHAR_H
#define _WCHAR_H
#include <bits/macros.h>
#include <stddef.h>
#ifndef __WINT_TYPE__
#define __WINT_TYPE__ unsigned int
typedef unsigned int wint_t;
#ifdef __cplusplus
extern "C"
{
#endif
/* Return the length of a wide-character string. */
size_t wcslen(const wchar_t* str);
/* Concatenates the wide-character string src into dest. */
__lc_is_deprecated wchar_t* wcscat(wchar_t* dest, const wchar_t* src);
#ifdef __cplusplus
}
#endif
typedef __WINT_TYPE__ wint_t;
#endif

24
libs/libc/src/wchar.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <wchar.h>
extern "C"
{
size_t wcslen(const wchar_t* str)
{
const wchar_t* i = str;
for (; *i; ++i)
;
return (i - str);
}
wchar_t* wcscat(wchar_t* dest, const wchar_t* src)
{
size_t dest_len = wcslen(dest);
size_t i;
for (i = 0; *(src + i); i++) *(dest + dest_len + i) = *(src + i);
*(dest + dest_len + i) = L'\0';
return dest;
}
}

View File

@ -4,7 +4,7 @@ DESTDIR := $(LUNA_ROOT)/initrd/bin
build:
@mkdir -p $(TESTDIR)/bin
$(LUNA_ROOT)/tools/sync-libc.sh
$(CC) $(TESTDIR)/string.c $(TESTDIR)/stdlib.c $(TESTDIR)/Test.c -I$(LUNA_ROOT)/tests -o $(TESTDIR)/bin/test-libc -Wall -Wextra -Wno-deprecated-declarations -Wno-stringop-overread -Werror
$(CC) $(TESTDIR)/string.c $(TESTDIR)/stdlib.c $(TESTDIR)/wchar.c $(TESTDIR)/Test.c -I$(LUNA_ROOT)/tests -o $(TESTDIR)/bin/test-libc -Wall -Wextra -Wno-deprecated-declarations -Wno-stringop-overread -Werror
install:
$(LUNA_ROOT)/tools/clean.sh

View File

@ -30,6 +30,9 @@ DEFINE_TEST(mktemp);
DEFINE_TEST(qsort);
DEFINE_TEST(bsearch);
// wchar.h
DEFINE_TEST(wcslen);
int main()
{
START_TEST_CASE(string.h);
@ -61,4 +64,7 @@ int main()
RUN_TEST(mktemp);
RUN_TEST(qsort);
RUN_TEST(bsearch);
START_TEST_CASE(wchar.h);
RUN_TEST(wcslen);
}

21
tests/libc/wchar.c Normal file
View File

@ -0,0 +1,21 @@
#include "Test.h"
#include <wchar.h>
DEFINE_TEST(wcslen)
{
START_TEST(wcslen);
const wchar_t* str = L"Hello, World!";
size_t len = wcslen(str);
EXPECT_EQ(len, 13);
wchar_t null[] = {L'\0'};
len = wcslen(null);
EXPECT_EQ(len, 0);
TEST_SUCCESS();
}