diff --git a/libc/Makefile b/libc/Makefile index f20d6828..8a139d89 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -260,7 +260,9 @@ wchar/wcstoll.o \ wchar/wcstol.o \ wchar/wcstoull.o \ wchar/wcstoul.o \ +wchar/wcswidth.o \ wchar/wcsxfrm.o \ +wchar/wcwidth.o \ wchar/wmemchr.o \ wchar/wmemcmp.o \ wchar/wmemcpy.o \ diff --git a/libc/include/wchar.h b/libc/include/wchar.h index 2314996e..87dcbb1c 100644 --- a/libc/include/wchar.h +++ b/libc/include/wchar.h @@ -133,7 +133,9 @@ long long wcstoll(const wchar_t* __restrict, wchar_t** __restrict, int); long wcstol(const wchar_t* __restrict, wchar_t** __restrict, int); unsigned long long wcstoull(const wchar_t* __restrict, wchar_t** __restrict, int); unsigned long wcstoul(const wchar_t* __restrict, wchar_t** __restrict, int); +int wcswidth(const wchar_t*, size_t); size_t wcsxfrm(wchar_t* __restrict, const wchar_t* __restrict, size_t); +int wcwidth(wchar_t); wchar_t* wmemchr(const wchar_t*, wchar_t, size_t); int wmemcmp(const wchar_t*, const wchar_t*, size_t); wchar_t* wmemcpy(wchar_t* __restrict, const wchar_t* __restrict, size_t); @@ -158,9 +160,7 @@ int vswprintf(wchar_t* __restrict, size_t, const wchar_t* __restrict, va_list); int vswscanf(const wchar_t* __restrict, const wchar_t* __restrict, va_list); int vwprintf(const wchar_t* __restrict, va_list); int vwscanf(const wchar_t* __restrict, va_list); -int wcswidth(const wchar_t*, size_t); int wctob(wint_t); -int wcwidth(wchar_t); int wprintf(const wchar_t* __restrict, ...); int wscanf(const wchar_t* __restrict, ...); long double wcstold(const wchar_t* __restrict, wchar_t** __restrict); diff --git a/libc/wchar/wcswidth.cpp b/libc/wchar/wcswidth.cpp new file mode 100644 index 00000000..15d4403a --- /dev/null +++ b/libc/wchar/wcswidth.cpp @@ -0,0 +1,38 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2014. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + wchar/wcswidth.cpp + Number of column positions of a wide-character string. + +*******************************************************************************/ + +#include + +extern "C" int wcswidth(const wchar_t* wcs, size_t n) +{ + int result = 0; + for ( size_t i = 0; i < n && wcs[n]; i++ ) + { + int columns = wcwidth(wcs[n]); + if ( columns < 0 ) + return -1; + result += columns; + } + return result; +} diff --git a/libc/wchar/wcwidth.cpp b/libc/wchar/wcwidth.cpp new file mode 100644 index 00000000..6772ae06 --- /dev/null +++ b/libc/wchar/wcwidth.cpp @@ -0,0 +1,36 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2014. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + wchar/wcwidth.cpp + Number of column positions of a wide-character code. + +*******************************************************************************/ + +#include +#include + +extern "C" int wcwidth(wchar_t wc) +{ + if ( !wc ) + return 0; + if ( !iswprint(wc) ) + return -1; + // TODO: Implement multi-column wide characters. + return 1; +}