Skip to main content

relibc/header/inttypes/
mod.rs

1//! `inttypes.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/inttypes.h.html>.
4
5use crate::{
6    header::{
7        ctype::{self, isspace},
8        errno::{EINVAL, ERANGE},
9        stdlib::{convert_hex, convert_integer, convert_octal, detect_base, is_positive},
10    },
11    platform::{
12        self,
13        types::{c_char, c_int, c_long, intmax_t, uintmax_t, wchar_t},
14    },
15};
16
17/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/imaxabs.html>.
18///
19/// Computes the absolute value of the integer `j`.
20#[unsafe(no_mangle)]
21pub extern "C" fn imaxabs(j: intmax_t) -> intmax_t {
22    j.abs()
23}
24
25/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/inttypes.h.html>.
26///
27/// Structure type that is the type of the value returned by the `imaxdiv()`
28/// function.
29#[repr(C)]
30pub struct imaxdiv_t {
31    /// The quotient.
32    quot: intmax_t,
33    /// The remainder.
34    rem: intmax_t,
35}
36
37/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/imaxdiv.html>.
38///
39/// Computes `numer` / `denom` and `numer` % `denom` in a single operation.
40///
41/// Returns the struct `imaxdiv_t`, comprising both the quotient and remainder.
42#[unsafe(no_mangle)]
43pub extern "C" fn imaxdiv(numer: intmax_t, denom: intmax_t) -> imaxdiv_t {
44    imaxdiv_t {
45        quot: numer / denom,
46        rem: numer % denom,
47    }
48}
49
50/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strtoimax.html>.
51///
52/// Equivalent to `strtol()` and `strtoll()`, except that the initial portion
53/// of the string shall be converted to `intmax_t`.
54///
55/// Upon success, returns the converted value. If no conversion could be
56/// performed or the value of `base` is not supported, returns `0`.
57#[unsafe(no_mangle)]
58pub unsafe extern "C" fn strtoimax(
59    nptr: *const c_char,
60    endptr: *mut *mut c_char,
61    base: c_int,
62) -> intmax_t {
63    strto_impl!(
64        intmax_t,
65        false,
66        intmax_t::MAX,
67        intmax_t::MIN,
68        nptr,
69        endptr,
70        base
71    )
72}
73
74/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strtoimax.html>.
75///
76/// Equivalent to `strtoul()` and `strtoull()`, except that the initial portion
77/// of the string shall be converted to `uintmax_t`.
78///
79/// Upon success, returns the converted value. If no conversion could be
80/// performed or the value of `base` is not supported, returns `0`.
81#[unsafe(no_mangle)]
82pub unsafe extern "C" fn strtoumax(
83    nptr: *const c_char,
84    endptr: *mut *mut c_char,
85    base: c_int,
86) -> uintmax_t {
87    strto_impl!(
88        uintmax_t,
89        false,
90        uintmax_t::MAX,
91        uintmax_t::MIN,
92        nptr,
93        endptr,
94        base
95    )
96}
97
98/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/wcstoimax.html>.
99///
100/// Equivalent to `wctol()` and `wctoll()`, except that the initial portion
101/// of the wide string shall be converted to `intmax_t`.
102///
103/// Upon success, returns the converted value. If no conversion could be
104/// performed, returns `0`.
105#[expect(clippy::cast_lossless)] // not all users of `wcsto_impl!` are lossless
106#[unsafe(no_mangle)]
107pub unsafe extern "C" fn wcstoimax(
108    mut nptr: *const wchar_t,
109    endptr: *mut *mut wchar_t,
110    base: c_int,
111) -> intmax_t {
112    skipws!(nptr);
113    let result = wcsto_impl!(intmax_t, nptr, base);
114    if !endptr.is_null() {
115        unsafe { *endptr = nptr.cast_mut() };
116    }
117    result
118}
119
120/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/wcstoimax.html>.
121///
122/// Equivalent to `wctoul()` and `wctoull()`, except that the initial portion
123/// of the wide string shall be converted to `uintmax_t`.
124///
125/// Upon success, returns the converted value. If no conversion could be
126/// performed, returns `0`.
127#[unsafe(no_mangle)]
128pub unsafe extern "C" fn wcstoumax(
129    mut nptr: *const wchar_t,
130    endptr: *mut *mut wchar_t,
131    base: c_int,
132) -> uintmax_t {
133    skipws!(nptr);
134    let result = wcsto_impl!(uintmax_t, nptr, base);
135    if !endptr.is_null() {
136        unsafe { *endptr = nptr.cast_mut() };
137    }
138    result
139}