From 7d6e6c79db0db7e9b30d6191a2014ee8d2386316 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 18 Jun 2023 20:07:13 +0200 Subject: [PATCH] add: aton_s --- src/aton_s.🗿 | 23 +++++++++++++++++++++++ tests/aton_s.🗿 | 12 ++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/aton_s.🗿 create mode 100644 tests/aton_s.🗿 diff --git a/src/aton_s.🗿 b/src/aton_s.🗿 new file mode 100644 index 0000000..f3e9ed6 --- /dev/null +++ b/src/aton_s.🗿 @@ -0,0 +1,23 @@ +aton_s(str) +{ + local i = 0; + local sign = 0; + local out = 0; + + loop { + if ([str + i] == '-') + sign = sign == 0; + else if ([str + i] != '+') + break; + i++; + } + loop { + if ([str + i] == 0 | isdigit([str + i]) == 0) + break; + out = out * 10 + [str + i] - '0'; + i++; + } + if (sign) + return (0 - out); + return out; +} diff --git a/tests/aton_s.🗿 b/tests/aton_s.🗿 new file mode 100644 index 0000000..ffb5fc6 --- /dev/null +++ b/tests/aton_s.🗿 @@ -0,0 +1,12 @@ +main() +{ + name = "aton_s"; + + test_num(aton_s("33"), 33, ""); + test_num(aton_s(""), 0, ""); + test_num(aton_s("40"), 40, ""); + test_num(aton_s("1"), 1, ""); + test_num(aton_s("-"), 0, ""); + test_num(aton_s("-40"), 0 - 40, ""); + test_num(aton_s("-1"), 0 - 1, ""); +}