From ec6243bc5b256dedc7f23cf05f12d3c1497eb074 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen@gmail.com>
Date: Mon, 27 Mar 2023 19:13:47 +0100
Subject: [PATCH] wctrans/towctrans implementation proposal.

close #32
---
 src/header/wctype/mod.rs | 23 +++++++++++++++++++++++
 tests/wctype/towlower.c  |  4 ++--
 tests/wctype/towupper.c  |  4 ++--
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/src/header/wctype/mod.rs b/src/header/wctype/mod.rs
index 005bca044..d0fc6dd5b 100644
--- a/src/header/wctype/mod.rs
+++ b/src/header/wctype/mod.rs
@@ -8,6 +8,7 @@ mod casecmp;
 mod punct;
 
 pub type wctype_t = u32;
+pub type wctrans_t = *const i32;
 
 pub const WEOF: wint_t = 0xFFFF_FFFFu32;
 
@@ -24,6 +25,9 @@ pub const WCTYPE_SPACE: wctype_t = 10;
 pub const WCTYPE_UPPER: wctype_t = 11;
 pub const WCTYPE_XDIGIT: wctype_t = 12;
 
+const WCTRANSUP: wctrans_t = 1 as wctrans_t;
+const WCTRANSLW: wctrans_t = 2 as wctrans_t;
+
 #[no_mangle]
 pub extern "C" fn iswctype(wc: wint_t, desc: wctype_t) -> c_int {
     match desc {
@@ -173,3 +177,22 @@ pub extern "C" fn towlower(wc: wint_t) -> wint_t {
 pub extern "C" fn towupper(wc: wint_t) -> wint_t {
     casemap(wc, 1)
 }
+
+#[no_mangle]
+pub extern "C" fn wctrans(class: *const c_char) -> wctrans_t {
+    let class_cstr = unsafe { CStr::from_ptr(class) };
+    match class_cstr.to_bytes() {
+        b"toupper" => WCTRANSUP,
+        b"tolower" => WCTRANSLW,
+        _ => 0 as wctrans_t,
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn towctrans(wc: wint_t, trans: wctrans_t) -> wint_t {
+    match trans {
+        WCTRANSUP => towupper(wc),
+        WCTRANSLW => towlower(wc),
+        _ => wc,
+    }
+}
diff --git a/tests/wctype/towlower.c b/tests/wctype/towlower.c
index a99d4252b..fe92d71c9 100644
--- a/tests/wctype/towlower.c
+++ b/tests/wctype/towlower.c
@@ -6,7 +6,7 @@ int main() {
     wchar_t *str = L"HaLf WiDe ChAr StRiNg!\n";
     fputws(str, stdout);
     for (int i = 0; i < wcslen(str); i++) {
-        putwchar(towlower(str[i]));
+        putwchar(towctrans(str[i], wctrans("tolower")));
     }
     return 0;
-}
\ No newline at end of file
+}
diff --git a/tests/wctype/towupper.c b/tests/wctype/towupper.c
index 61fb09edf..72404edd7 100644
--- a/tests/wctype/towupper.c
+++ b/tests/wctype/towupper.c
@@ -6,7 +6,7 @@ int main() {
     wchar_t *str = L"HaLf WiDe ChAr StRiNg!\n";
     fputws(str, stdout);
     for (int i = 0; i < wcslen(str); i++) {
-        putwchar(towupper(str[i]));
+        putwchar(towctrans(str[i], wctrans("toupper")));
     }
     return 0;
-}
\ No newline at end of file
+}
-- 
GitLab