mirror of
https://github.com/tailix/libkernaux.git
synced 2025-02-24 15:55:41 -05:00
Main: include/kernaux/ntoa.h: functions "kernaux_[u|i]toa16" put default prefix
This commit is contained in:
parent
11df5e4693
commit
f1e773f6fd
10 changed files with 61 additions and 49 deletions
|
@ -7,6 +7,7 @@
|
|||
"KERNAUX_ITOA_MIN_BUFFER_SIZE"
|
||||
* include/kernaux/ntoa.h: Functions "kernaux_[u|i]toa[10|16]" return the end
|
||||
of a buffer
|
||||
* include/kernaux/ntoa.h: functions "kernaux_[u|i]toa16" put default prefix
|
||||
|
||||
2022-05-28 Alex Kotov <kotovalexarian@gmail.com>
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ extern "C" {
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#define KERNAUX_NTOA_DEFAULT_PREFIX_16 "0x"
|
||||
|
||||
// "1111111111111111111111111111111111111111111111111111111111111111"
|
||||
#define KERNAUX_UTOA_MIN_BUFFER_SIZE (64 + 1)
|
||||
|
||||
|
@ -21,12 +23,12 @@ extern "C" {
|
|||
// "-9223372036854775808"
|
||||
#define KERNAUX_ITOA10_BUFFER_SIZE (20 + 1)
|
||||
|
||||
// "ffffffffffffffff"
|
||||
#define KERNAUX_UTOA16_BUFFER_SIZE (16 + 1)
|
||||
// "0xffffffffffffffff"
|
||||
#define KERNAUX_UTOA16_BUFFER_SIZE (16 + 2 + 1)
|
||||
|
||||
// "7fffffffffffffff"
|
||||
// "-8000000000000000"
|
||||
#define KERNAUX_ITOA16_BUFFER_SIZE (17 + 1)
|
||||
// "0x7fffffffffffffff"
|
||||
// "-0x8000000000000000"
|
||||
#define KERNAUX_ITOA16_BUFFER_SIZE (17 + 2 + 1)
|
||||
|
||||
char *kernaux_utoa(uint64_t value, char *buffer, int base, const char *prefix);
|
||||
char *kernaux_itoa(int64_t value, char *buffer, int base, const char *prefix);
|
||||
|
|
|
@ -217,10 +217,10 @@ assert 'KernAux.itoa10' do
|
|||
end
|
||||
|
||||
assert 'KernAux.utoa16' do
|
||||
test_utoa16 0, '0'
|
||||
test_utoa16 1, '1'
|
||||
test_utoa16 0x123, '123'
|
||||
test_utoa16 2**32 - 1, (2**32 - 1).to_s(16)
|
||||
test_utoa16 0, '0x0'
|
||||
test_utoa16 1, '0x1'
|
||||
test_utoa16 0x123, '0x123'
|
||||
test_utoa16 2**32 - 1, "0x#{(2**32 - 1).to_s(16)}"
|
||||
|
||||
assert_raise RangeError, 'can\'t convert negative number to uint64_t' do
|
||||
KernAux.utoa16(-1)
|
||||
|
@ -228,11 +228,11 @@ assert 'KernAux.utoa16' do
|
|||
end
|
||||
|
||||
assert 'KernAux.itoa16' do
|
||||
test_itoa16 0, '0'
|
||||
test_itoa16 1, '1'
|
||||
test_itoa16(-1, '-1')
|
||||
test_itoa16 0x123, '123'
|
||||
test_itoa16(-0x123, '-123')
|
||||
test_itoa16 2**31 - 1, (2**31 - 1).to_s(16)
|
||||
test_itoa16(-2**31, (-2**31).to_s(16))
|
||||
test_itoa16 0, '0x0'
|
||||
test_itoa16 1, '0x1'
|
||||
test_itoa16(-1, '-0x1')
|
||||
test_itoa16 0x123, '0x123'
|
||||
test_itoa16(-0x123, '-0x123')
|
||||
test_itoa16 2**31 - 1, "0x#{(2**31 - 1).to_s(16)}"
|
||||
test_itoa16(-2**31, "-0x#{(2**31).to_s(16)}")
|
||||
end
|
||||
|
|
|
@ -8,38 +8,40 @@ RSpec.describe KernAux, '.itoa16' do
|
|||
|
||||
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
||||
|
||||
def sign = number < 0 ? '-' : ''
|
||||
|
||||
it { is_expected.to be_instance_of String }
|
||||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq number.to_s 16 }
|
||||
it { is_expected.to eq "#{sign}0x#{number.abs.to_s(16)}" }
|
||||
|
||||
context 'when number is 0' do
|
||||
let(:number) { 0 }
|
||||
|
||||
it { is_expected.to eq '0' }
|
||||
it { is_expected.to eq '0x0' }
|
||||
end
|
||||
|
||||
context 'when number is 1' do
|
||||
let(:number) { 1 }
|
||||
|
||||
it { is_expected.to eq '1' }
|
||||
it { is_expected.to eq '0x1' }
|
||||
end
|
||||
|
||||
context 'when number is -1' do
|
||||
let(:number) { -1 }
|
||||
|
||||
it { is_expected.to eq '-1' }
|
||||
it { is_expected.to eq '-0x1' }
|
||||
end
|
||||
|
||||
context 'when number is min int64_t' do
|
||||
let(:number) { -2**63 }
|
||||
|
||||
it { is_expected.to eq number.to_s 16 }
|
||||
it { is_expected.to eq "-0x#{number.abs.to_s(16)}" }
|
||||
end
|
||||
|
||||
context 'when number is max int64_t' do
|
||||
let(:number) { 2**63 - 1 }
|
||||
|
||||
it { is_expected.to eq number.to_s 16 }
|
||||
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
||||
end
|
||||
|
||||
context 'when number is lesser than min uint64_t' do
|
||||
|
|
|
@ -10,18 +10,18 @@ RSpec.describe KernAux, '.utoa16' do
|
|||
|
||||
it { is_expected.to be_instance_of String }
|
||||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq number.to_s 16 }
|
||||
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
||||
|
||||
context 'when number is 0' do
|
||||
let(:number) { 0 }
|
||||
|
||||
it { is_expected.to eq '0' }
|
||||
it { is_expected.to eq '0x0' }
|
||||
end
|
||||
|
||||
context 'when number is max uint64_t' do
|
||||
let(:number) { 2**64 - 1 }
|
||||
|
||||
it { is_expected.to eq number.to_s 16 }
|
||||
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
||||
end
|
||||
|
||||
context 'when number is -1' do
|
||||
|
|
|
@ -5,8 +5,8 @@ pub const ITOA_MIN_BUFFER_SIZE: usize = 65 + 1;
|
|||
|
||||
pub const UTOA10_BUFFER_SIZE: usize = 20 + 1;
|
||||
pub const ITOA10_BUFFER_SIZE: usize = 20 + 1;
|
||||
pub const UTOA16_BUFFER_SIZE: usize = 16 + 1;
|
||||
pub const ITOA16_BUFFER_SIZE: usize = 17 + 1;
|
||||
pub const UTOA16_BUFFER_SIZE: usize = 16 + 2 + 1;
|
||||
pub const ITOA16_BUFFER_SIZE: usize = 17 + 2 + 1;
|
||||
|
||||
#[link(name = "kernaux")]
|
||||
extern "C" {
|
||||
|
@ -172,7 +172,7 @@ mod tests {
|
|||
unsafe { utoa16(0x123, buffer.as_mut_ptr()) };
|
||||
let result =
|
||||
unsafe { CStr::from_ptr(buffer.as_ptr()) }.to_str().unwrap();
|
||||
assert_eq!(result, "123");
|
||||
assert_eq!(result, "0x123");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -181,12 +181,12 @@ mod tests {
|
|||
unsafe { itoa16(0x123, buffer.as_mut_ptr()) };
|
||||
let result =
|
||||
unsafe { CStr::from_ptr(buffer.as_ptr()) }.to_str().unwrap();
|
||||
assert_eq!(result, "123");
|
||||
assert_eq!(result, "0x123");
|
||||
|
||||
let mut buffer: [i8; ITOA16_BUFFER_SIZE] = [0; ITOA16_BUFFER_SIZE];
|
||||
unsafe { itoa16(-0x123, buffer.as_mut_ptr()) };
|
||||
let result =
|
||||
unsafe { CStr::from_ptr(buffer.as_ptr()) }.to_str().unwrap();
|
||||
assert_eq!(result, "-123");
|
||||
assert_eq!(result, "-0x123");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +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");
|
||||
assert_eq!(utoa16(0x123), "0x123");
|
||||
assert_eq!(itoa16(0x123), "0x123");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,20 +62,20 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_utoa16() {
|
||||
assert_eq!(utoa16(0), "0");
|
||||
assert_eq!(utoa16(1), "1");
|
||||
assert_eq!(utoa16(0x123), "123");
|
||||
assert_eq!(utoa16(u64::MAX), "ffffffffffffffff");
|
||||
assert_eq!(utoa16(0), "0x0");
|
||||
assert_eq!(utoa16(1), "0x1");
|
||||
assert_eq!(utoa16(0x123), "0x123");
|
||||
assert_eq!(utoa16(u64::MAX), "0xffffffffffffffff");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_itoa16() {
|
||||
assert_eq!(itoa16(0), "0");
|
||||
assert_eq!(itoa16(1), "1");
|
||||
assert_eq!(itoa16(-1), "-1");
|
||||
assert_eq!(itoa16(0x123), "123");
|
||||
assert_eq!(itoa16(-0x123), "-123");
|
||||
assert_eq!(itoa16(i64::MAX), "7fffffffffffffff");
|
||||
assert_eq!(itoa16(i64::MIN), "-8000000000000000");
|
||||
assert_eq!(itoa16(0), "0x0");
|
||||
assert_eq!(itoa16(1), "0x1");
|
||||
assert_eq!(itoa16(-1), "-0x1");
|
||||
assert_eq!(itoa16(0x123), "0x123");
|
||||
assert_eq!(itoa16(-0x123), "-0x123");
|
||||
assert_eq!(itoa16(i64::MAX), "0x7fffffffffffffff");
|
||||
assert_eq!(itoa16(i64::MIN), "-0x8000000000000000");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,10 +74,10 @@ char *kernaux_itoa10(int64_t value, char *buffer)
|
|||
|
||||
char *kernaux_utoa16(uint64_t value, char *buffer)
|
||||
{
|
||||
return kernaux_utoa(value, buffer, 'x', NULL);
|
||||
return kernaux_utoa(value, buffer, 'x', KERNAUX_NTOA_DEFAULT_PREFIX_16);
|
||||
}
|
||||
|
||||
char *kernaux_itoa16(int64_t value, char *buffer)
|
||||
{
|
||||
return kernaux_itoa(value, buffer, 'x', NULL);
|
||||
return kernaux_itoa(value, buffer, 'x', KERNAUX_NTOA_DEFAULT_PREFIX_16);
|
||||
}
|
||||
|
|
|
@ -552,7 +552,8 @@ int main()
|
|||
) {
|
||||
const char *const end1 =
|
||||
kernaux_utoa16(utoa16_cases[index].value, buffer);
|
||||
assert(strcmp(buffer, utoa16_cases[index].result) == 0);
|
||||
assert(strncmp(buffer, "0x", 2) == 0);
|
||||
assert(strcmp(&buffer[2], utoa16_cases[index].result) == 0);
|
||||
assert(end1 == str_end(buffer));
|
||||
}
|
||||
}
|
||||
|
@ -568,14 +569,20 @@ int main()
|
|||
const int64_t value = itoa16_cases[index].value;
|
||||
|
||||
const char *const end1 = kernaux_itoa16(value, buffer);
|
||||
assert(strcmp(buffer, itoa16_cases[index].result) == 0);
|
||||
if (value >= 0) {
|
||||
assert(strncmp(buffer, "0x", 2) == 0);
|
||||
assert(strcmp(&buffer[2], itoa16_cases[index].result) == 0);
|
||||
} else {
|
||||
assert(strncmp(buffer, "-0x", 3) == 0);
|
||||
assert(strcmp(&buffer[3], &itoa16_cases[index].result[1]) == 0);
|
||||
}
|
||||
assert(end1 == str_end(buffer));
|
||||
|
||||
if (value <= 0) continue;
|
||||
|
||||
const char *const end2 = kernaux_itoa16(-value, buffer);
|
||||
assert(buffer[0] == '-');
|
||||
assert(strcmp(&buffer[1], itoa16_cases[index].result) == 0);
|
||||
assert(strncmp(buffer, "-0x", 3) == 0);
|
||||
assert(strcmp(&buffer[3], itoa16_cases[index].result) == 0);
|
||||
assert(end2 == str_end(buffer));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue