1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00

Rust: split "ntoa::[u|i]toa" funcs

This commit is contained in:
Alex Kotov 2022-06-02 13:31:54 +03:00
parent ea3cee8612
commit 841d2d20e8
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -30,9 +30,6 @@ pub enum Error {
}
pub fn utoa(value: u64, config: Config, prefix: Option<&str>) -> Result {
let mut buffer: [i8; UTOA_MIN_BUFFER_SIZE + MAX_PREFIX_LEN] =
[0; UTOA_MIN_BUFFER_SIZE + MAX_PREFIX_LEN];
let prefix = if let Some(prefix) = prefix {
if prefix.len() > MAX_PREFIX_LEN {
return Err(Error::PrefixTooLong(prefix.len()));
@ -42,6 +39,26 @@ pub fn utoa(value: u64, config: Config, prefix: Option<&str>) -> Result {
None
};
utoac(value, config, prefix)
}
pub fn itoa(value: i64, config: Config, prefix: Option<&str>) -> Result {
let prefix = if let Some(prefix) = prefix {
if prefix.len() > MAX_PREFIX_LEN {
return Err(Error::PrefixTooLong(prefix.len()));
}
Some(CString::new(prefix)?)
} else {
None
};
itoac(value, config, prefix)
}
fn utoac(value: u64, config: Config, prefix: Option<CString>) -> Result {
let mut buffer: [i8; UTOA_MIN_BUFFER_SIZE + MAX_PREFIX_LEN] =
[0; UTOA_MIN_BUFFER_SIZE + MAX_PREFIX_LEN];
unsafe {
kernaux_utoa(
value,
@ -58,19 +75,10 @@ pub fn utoa(value: u64, config: Config, prefix: Option<&str>) -> Result {
Ok(String::from(result))
}
pub fn itoa(value: i64, config: Config, prefix: Option<&str>) -> Result {
fn itoac(value: i64, config: Config, prefix: Option<CString>) -> Result {
let mut buffer: [i8; ITOA_MIN_BUFFER_SIZE + MAX_PREFIX_LEN] =
[0; ITOA_MIN_BUFFER_SIZE + MAX_PREFIX_LEN];
let prefix = if let Some(prefix) = prefix {
if prefix.len() > MAX_PREFIX_LEN {
return Err(Error::PrefixTooLong(prefix.len()));
}
Some(CString::new(prefix)?)
} else {
None
};
unsafe {
kernaux_itoa(
value,