From 841d2d20e83585272828286883071f3112fb53ee Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 2 Jun 2022 13:31:54 +0300 Subject: [PATCH] Rust: split "ntoa::[u|i]toa" funcs --- pkgs/rust/kernaux/src/ntoa.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/pkgs/rust/kernaux/src/ntoa.rs b/pkgs/rust/kernaux/src/ntoa.rs index 8bbce27..232d3c5 100644 --- a/pkgs/rust/kernaux/src/ntoa.rs +++ b/pkgs/rust/kernaux/src/ntoa.rs @@ -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) -> 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) -> 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,