Rust: implement ntoa::utoa16, ntoa::itoa16

This commit is contained in:
Alex Kotov 2022-02-01 08:15:00 +05:00
parent 54cd2e6dea
commit 9157ebf953
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
2 changed files with 34 additions and 2 deletions

View File

@ -10,5 +10,7 @@ mod tests {
fn test_ntoa() {
assert_eq!(utoa10(123), "123");
assert_eq!(itoa10(123), "123");
assert_eq!(utoa16(0x123), "123");
assert_eq!(itoa16(0x123), "123");
}
}

View File

@ -1,7 +1,13 @@
use std::ffi::CStr;
use kernaux_sys::{itoa10 as kernaux_itoa10, utoa10 as kernaux_utoa10};
use kernaux_sys::{ITOA10_BUFFER_SIZE, UTOA10_BUFFER_SIZE};
use kernaux_sys::{
itoa10 as kernaux_itoa10, itoa16 as kernaux_itoa16,
utoa10 as kernaux_utoa10, utoa16 as kernaux_utoa16,
};
use kernaux_sys::{
ITOA10_BUFFER_SIZE, ITOA16_BUFFER_SIZE, UTOA10_BUFFER_SIZE,
UTOA16_BUFFER_SIZE,
};
pub fn utoa10(value: u64) -> String {
let mut buffer: [i8; UTOA10_BUFFER_SIZE] = [0; UTOA10_BUFFER_SIZE];
@ -17,6 +23,20 @@ pub fn itoa10(value: i64) -> String {
String::from(result)
}
pub fn utoa16(value: u64) -> String {
let mut buffer: [i8; UTOA16_BUFFER_SIZE] = [0; UTOA16_BUFFER_SIZE];
unsafe { kernaux_utoa16(value, buffer.as_mut_ptr()) };
let result = unsafe { CStr::from_ptr(buffer.as_ptr()) }.to_str().unwrap();
String::from(result)
}
pub fn itoa16(value: i64) -> String {
let mut buffer: [i8; ITOA16_BUFFER_SIZE] = [0; ITOA16_BUFFER_SIZE];
unsafe { kernaux_itoa16(value, buffer.as_mut_ptr()) };
let result = unsafe { CStr::from_ptr(buffer.as_ptr()) }.to_str().unwrap();
String::from(result)
}
#[cfg(test)]
mod tests {
use super::*;
@ -30,4 +50,14 @@ mod tests {
fn test_itoa10() {
assert_eq!(itoa10(123), "123");
}
#[test]
fn test_utoa16() {
assert_eq!(utoa16(0x123), "123");
}
#[test]
fn test_itoa16() {
assert_eq!(itoa16(0x123), "123");
}
}