Adding some wcst* functions

This commit is contained in:
Darley Barreto
2023-05-22 16:01:04 +00:00
committed by Jeremy Soller
parent 127f34a244
commit 9642d2ab02
10 changed files with 116 additions and 25 deletions
+3 -21
View File
@@ -1,5 +1,7 @@
use core::{convert::TryInto, ptr};
use crate::{
header::{ctype, errno::*, stdlib::*},
header::{ctype, errno::*, stdlib::*, wctype::iswspace},
platform::{self, types::*},
};
@@ -56,23 +58,3 @@ pub unsafe extern "C" fn strtoumax(
base
)
}
#[allow(unused)]
// #[no_mangle]
pub extern "C" fn wcstoimax(
nptr: *const wchar_t,
endptr: *mut *mut wchar_t,
base: c_int,
) -> intmax_t {
unimplemented!();
}
#[allow(unused)]
// #[no_mangle]
pub extern "C" fn wcstoumax(
nptr: *const wchar_t,
endptr: *mut *mut wchar_t,
base: c_int,
) -> uintmax_t {
unimplemented!();
}
+73 -3
View File
@@ -660,14 +660,28 @@ macro_rules! strtou_impl {
strtou_impl!($type, $ptr, $base, false)
};
($type:ident, $ptr:expr, $base:expr, $negative:expr) => {{
if $base == 16 && *$ptr == '0' as wchar_t && *$ptr.add(1) | 0x20 == 'x' as wchar_t {
let mut base = $base;
if (base == 16 || base == 0)
&& *$ptr == '0' as wchar_t
&& (*$ptr.add(1) == 'x' as wchar_t || *$ptr.add(1) == 'X' as wchar_t)
{
$ptr = $ptr.add(2);
base = 16;
}
if base == 0 {
base = if *$ptr == '0' as wchar_t {
8
} else {
10
};
};
let mut result: $type = 0;
while let Some(digit) = char::from_u32(*$ptr as u32).and_then(|c| c.to_digit($base as u32))
while let Some(digit) = char::from_u32(*$ptr as u32).and_then(|c| c.to_digit(base as u32))
{
let new = result.checked_mul($base as $type).and_then(|result| {
let new = result.checked_mul(base as $type).and_then(|result| {
if $negative {
result.checked_sub(digit as $type)
} else {
@@ -711,6 +725,34 @@ pub unsafe extern "C" fn wcstol(
result
}
#[no_mangle]
pub unsafe extern "C" fn wcstoll(
mut ptr: *const wchar_t,
end: *mut *mut wchar_t,
base: c_int,
) -> c_longlong {
skipws!(ptr);
let result = strto_impl!(c_longlong, ptr, base);
if !end.is_null() {
*end = ptr as *mut _;
}
result
}
#[no_mangle]
pub unsafe extern "C" fn wcstoimax(
mut ptr: *const wchar_t,
end: *mut *mut wchar_t,
base: c_int,
) -> intmax_t {
skipws!(ptr);
let result = strto_impl!(intmax_t, ptr, base);
if !end.is_null() {
*end = ptr as *mut _;
}
result
}
#[no_mangle]
pub unsafe extern "C" fn wcstoul(
mut ptr: *const wchar_t,
@@ -725,6 +767,34 @@ pub unsafe extern "C" fn wcstoul(
result
}
#[no_mangle]
pub unsafe extern "C" fn wcstoull(
mut ptr: *const wchar_t,
end: *mut *mut wchar_t,
base: c_int,
) -> c_ulonglong {
skipws!(ptr);
let result = strtou_impl!(c_ulonglong, ptr, base);
if !end.is_null() {
*end = ptr as *mut _;
}
result
}
#[no_mangle]
pub unsafe extern "C" fn wcstoumax(
mut ptr: *const wchar_t,
end: *mut *mut wchar_t,
base: c_int,
) -> uintmax_t {
skipws!(ptr);
let result = strtou_impl!(uintmax_t, ptr, base);
if !end.is_null() {
*end = ptr as *mut _;
}
result
}
// #[no_mangle]
pub extern "C" fn wcswcs(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t {
unimplemented!();
+2
View File
@@ -117,6 +117,8 @@ EXPECT_NAMES=\
wchar/wcstod \
wchar/wcstok \
wchar/wcstol \
wchar/wcstoimax \
wchar/wcstoumax \
wchar/wcscasecmp \
wchar/wcsncasecmp \
wchar/wcswidth \
@@ -0,0 +1,6 @@
-123
255
44027
8
10
16
@@ -1 +1 @@
The decimal equivalents are: 2001, 6340800, -3624224 and 0.
The decimal equivalents are: 2001, 6340800, -3624224 and 7340031.
@@ -0,0 +1,3 @@
nptr = `10110134932`
wcstoumax = 10110134932
Stopped scan at ``
+13
View File
@@ -0,0 +1,13 @@
#include <inttypes.h>
#include <stdio.h>
#include <wchar.h>
int main(void) {
wchar_t* endptr;
wprintf(L"%ld\n", wcstoimax(L" -123junk", &endptr, 10)); /* base 10 */
wprintf(L"%ld\n", wcstoimax(L"11111111", &endptr, 2)); /* base 2 */
wprintf(L"%ld\n", wcstoimax(L"XyZ", &endptr, 36)); /* base 36 */
wprintf(L"%ld\n", wcstoimax(L"010", &endptr, 0)); /* octal auto-detection */
wprintf(L"%ld\n", wcstoimax(L"10", &endptr, 0)); /* decimal auto-detection */
wprintf(L"%ld\n", wcstoimax(L"0x10", &endptr, 0)); /* hexadecimal auto-detection */
}
+15
View File
@@ -0,0 +1,15 @@
#include <inttypes.h>
#include <stdio.h>
#include <wchar.h>
int main(void) {
wchar_t *nptr;
wchar_t *endptr;
uintmax_t j;
int base = 10;
nptr = L"10110134932";
printf("nptr = `%ls`\n", nptr);
j = wcstoumax(nptr, &endptr, base);
printf("wcstoumax = %ju\n", j);
printf("Stopped scan at `%ls`\n", endptr);
}