From 0a152b9beffbaf731bb391a0bdf8a5ca1885b344 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 18 Jun 2023 20:13:52 +0200 Subject: [PATCH] add: ntoa_s --- src/ntoa_s.🗿 | 40 ++++++++++++++++++++++++++++++++++++++++ tests/ntoa_s.🗿 | 26 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/ntoa_s.🗿 create mode 100644 tests/ntoa_s.🗿 diff --git a/src/ntoa_s.🗿 b/src/ntoa_s.🗿 new file mode 100644 index 0000000..f5e206c --- /dev/null +++ b/src/ntoa_s.🗿 @@ -0,0 +1,40 @@ +ntoa_get_size(number) +{ + local size = 0; + + if (number == 0) + size++; + loop + { + if (number == 0) + return (size); + number = number / 10; + size++; + } +} + +ntoa_s(number) +{ + local str; + local size; + + size = ntoa_get_size(number); + if (number >= 0x8000) + size++; + str = galloc(size + 1); + if (str == 0) + return (0); + [str + size] = 0; + if (number >= 0x8000) + [str] = '-'; + else if (number == 0) + [str] = '0'; + loop + { + if (number == 0) + return (str); + [str + size - 1] = number % 10 + '0'; + number = number / 10; + size--; + } +} diff --git a/tests/ntoa_s.🗿 b/tests/ntoa_s.🗿 new file mode 100644 index 0000000..dbc71c6 --- /dev/null +++ b/tests/ntoa_s.🗿 @@ -0,0 +1,26 @@ +main() +{ + local ptr; + + name = "ntoa_s"; + + ptr = ntoa_s(9000); + test_str(ptr, "9000", ""); + free(ptr); + + ptr = ntoa_s(55); + test_str(ptr, "55", ""); + free(ptr); + + ptr = ntoa_s(0); + test_str(ptr, "0", ""); + free(ptr); + + ptr = ntoa_s(0 - 55); + test_str(ptr, "-55", ""); + free(ptr); + + ptr = ntoa_s(0 - 1); + test_str(ptr, "-1", ""); + free(ptr); +}