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
1 changed files with 21 additions and 13 deletions

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,