Avoid StringWriter cast and string format

This commit is contained in:
Wildan M
2026-05-20 09:10:25 +07:00
parent b07babc03c
commit 4a7f2d80ff
7 changed files with 32 additions and 25 deletions
+7 -4
View File
@@ -15,6 +15,7 @@ use crate::{
netinet_in::{INADDR_NONE, in_addr, in_addr_t},
sys_socket::{constants::AF_INET, socklen_t},
},
io::Write,
platform::{
self,
types::{c_char, c_int, c_void},
@@ -193,10 +194,12 @@ pub unsafe extern "C" fn inet_ntop(
4,
)
};
let addr = format!("{}.{}.{}.{}\0", s_addr[0], s_addr[1], s_addr[2], s_addr[3]);
unsafe {
ptr::copy(addr.as_ptr().cast::<c_char>(), dst, addr.len());
}
let mut w = platform::StringWriter(dst, size as usize);
let _ = write!(
w,
"{}.{}.{}.{}\0",
s_addr[0], s_addr[1], s_addr[2], s_addr[3]
);
dst
}
}
+2 -2
View File
@@ -2,7 +2,7 @@ use crate::platform::types::{c_char, size_t, ssize_t};
use super::{DEFAULT_MONETARY, FormatFlags, LocaleMonetaryInfo, apply_grouping};
use alloc::string::{String, ToString};
use core::{ffi::CStr, slice};
use core::{ffi::CStr, fmt::Write, slice};
use libm::{fabs, pow, round, trunc};
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strfmon.html>.
@@ -284,7 +284,7 @@ fn format_monetary(
if frac_digits > 0 {
result.push_str(monetary.mon_decimal_point);
// Zero-pad fractional part to the specified width
result.push_str(&format!("{:0>width$}", frac_part, width = frac_digits));
let _ = write!(result, "{:0>width$}", frac_part, width = frac_digits);
}
// 12) if the currency symbol follows the amount, add it now
+2 -2
View File
@@ -1479,7 +1479,7 @@ pub unsafe extern "C" fn vsnprintf(
) -> c_int {
unsafe {
printf::printf(
&mut platform::StringWriter(s.cast::<u8>(), n),
&mut platform::StringWriter(s, n),
CStr::from_ptr(format),
ap,
)
@@ -1496,7 +1496,7 @@ pub unsafe extern "C" fn snprintf(
) -> c_int {
unsafe {
printf::printf(
&mut platform::StringWriter(s.cast::<u8>(), n),
&mut platform::StringWriter(s, n),
CStr::from_ptr(format),
__valist.as_va_list(),
)
+4 -5
View File
@@ -359,17 +359,16 @@ pub unsafe extern "C" fn strdup(s1: *const c_char) -> *mut c_char {
pub unsafe extern "C" fn strerror_l(errnum: c_int, _loc: locale_t) -> *mut c_char {
use core::fmt::Write;
static strerror_buf: RawCell<[u8; STRERROR_MAX]> = RawCell::new([0; STRERROR_MAX]);
let mut w = platform::StringWriter(unsafe { strerror_buf.unsafe_mut().as_mut_ptr() }, unsafe {
strerror_buf.unsafe_ref().len()
});
static STRERROR_BUF: RawCell<[c_char; STRERROR_MAX]> = RawCell::new([0; STRERROR_MAX]);
let strerror_ptr = unsafe { STRERROR_BUF.unsafe_mut().as_mut_ptr() };
let mut w = platform::StringWriter(strerror_ptr, STRERROR_MAX);
let _ = match STR_ERROR.get(errnum as usize) {
Some(e) => w.write_str(e),
None => w.write_fmt(format_args!("Unknown error {}", errnum)),
};
(unsafe { strerror_buf.unsafe_mut().as_mut_ptr() }).cast::<c_char>()
strerror_ptr
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strerror.html>.
+2 -7
View File
@@ -499,13 +499,8 @@ pub unsafe extern "C" fn strftime(
format: *const c_char,
timeptr: *const tm,
) -> size_t {
let ret = unsafe {
strftime::strftime(
&mut platform::StringWriter(s.cast::<u8>(), maxsize),
format,
timeptr,
)
};
let mut w = platform::StringWriter(s, maxsize);
let ret = unsafe { strftime::strftime(&mut w, format, timeptr) };
if ret < maxsize { ret } else { 0 }
}