Skip to content
Snippets Groups Projects
Commit 54e2e84b authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Merge branch 'wcsto_' into 'master'

Adding some wcst* functions

See merge request redox-os/relibc!400
parents 127f34a2 9642d2ab
No related branches found
No related tags found
1 merge request!400Adding some wcst* functions
Pipeline #12240 failed
use core::{convert::TryInto, ptr};
use crate::{ use crate::{
header::{ctype, errno::*, stdlib::*}, header::{ctype, errno::*, stdlib::*, wctype::iswspace},
platform::{self, types::*}, platform::{self, types::*},
}; };
...@@ -56,23 +58,3 @@ pub unsafe extern "C" fn strtoumax( ...@@ -56,23 +58,3 @@ pub unsafe extern "C" fn strtoumax(
base 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!();
}
...@@ -660,14 +660,28 @@ macro_rules! strtou_impl { ...@@ -660,14 +660,28 @@ macro_rules! strtou_impl {
strtou_impl!($type, $ptr, $base, false) strtou_impl!($type, $ptr, $base, false)
}; };
($type:ident, $ptr:expr, $base:expr, $negative:expr) => {{ ($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); $ptr = $ptr.add(2);
base = 16;
} }
if base == 0 {
base = if *$ptr == '0' as wchar_t {
8
} else {
10
};
};
let mut result: $type = 0; 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 { if $negative {
result.checked_sub(digit as $type) result.checked_sub(digit as $type)
} else { } else {
...@@ -711,6 +725,34 @@ pub unsafe extern "C" fn wcstol( ...@@ -711,6 +725,34 @@ pub unsafe extern "C" fn wcstol(
result 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] #[no_mangle]
pub unsafe extern "C" fn wcstoul( pub unsafe extern "C" fn wcstoul(
mut ptr: *const wchar_t, mut ptr: *const wchar_t,
...@@ -725,6 +767,34 @@ pub unsafe extern "C" fn wcstoul( ...@@ -725,6 +767,34 @@ pub unsafe extern "C" fn wcstoul(
result 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] // #[no_mangle]
pub extern "C" fn wcswcs(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t { pub extern "C" fn wcswcs(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t {
unimplemented!(); unimplemented!();
......
...@@ -117,6 +117,8 @@ EXPECT_NAMES=\ ...@@ -117,6 +117,8 @@ EXPECT_NAMES=\
wchar/wcstod \ wchar/wcstod \
wchar/wcstok \ wchar/wcstok \
wchar/wcstol \ wchar/wcstol \
wchar/wcstoimax \
wchar/wcstoumax \
wchar/wcscasecmp \ wchar/wcscasecmp \
wchar/wcsncasecmp \ wchar/wcsncasecmp \
wchar/wcswidth \ wchar/wcswidth \
......
-123
255
44027
8
10
16
The decimal equivalents are: 2001, 6340800, -3624224 and 0. The decimal equivalents are: 2001, 6340800, -3624224 and 7340031.
nptr = `10110134932`
wcstoumax = 10110134932
Stopped scan at ``
#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 */
}
\ No newline at end of file
#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);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment