[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] add atoi() function (and testsuite)
From: |
Michael Drüing |
Subject: |
[PATCH] add atoi() function (and testsuite) |
Date: |
Fri, 8 Nov 2019 01:14:14 +0100 |
---
src/std.pk | 35 +++++++++++++++++++++++++++++++++++
testsuite/poke.std/atoi-1.pk | 7 +++++++
testsuite/poke.std/atoi-10.pk | 6 ++++++
testsuite/poke.std/atoi-11.pk | 6 ++++++
testsuite/poke.std/atoi-12.pk | 6 ++++++
testsuite/poke.std/atoi-13.pk | 6 ++++++
testsuite/poke.std/atoi-2.pk | 6 ++++++
testsuite/poke.std/atoi-3.pk | 6 ++++++
testsuite/poke.std/atoi-4.pk | 6 ++++++
testsuite/poke.std/atoi-5.pk | 6 ++++++
testsuite/poke.std/atoi-6.pk | 6 ++++++
testsuite/poke.std/atoi-7.pk | 6 ++++++
testsuite/poke.std/atoi-8.pk | 6 ++++++
testsuite/poke.std/atoi-9.pk | 6 ++++++
14 files changed, 114 insertions(+)
create mode 100644 testsuite/poke.std/atoi-1.pk
create mode 100644 testsuite/poke.std/atoi-10.pk
create mode 100644 testsuite/poke.std/atoi-11.pk
create mode 100644 testsuite/poke.std/atoi-12.pk
create mode 100644 testsuite/poke.std/atoi-13.pk
create mode 100644 testsuite/poke.std/atoi-2.pk
create mode 100644 testsuite/poke.std/atoi-3.pk
create mode 100644 testsuite/poke.std/atoi-4.pk
create mode 100644 testsuite/poke.std/atoi-5.pk
create mode 100644 testsuite/poke.std/atoi-6.pk
create mode 100644 testsuite/poke.std/atoi-7.pk
create mode 100644 testsuite/poke.std/atoi-8.pk
create mode 100644 testsuite/poke.std/atoi-9.pk
diff --git a/src/std.pk b/src/std.pk
index 282b77b..9a0da96 100644
--- a/src/std.pk
+++ b/src/std.pk
@@ -210,3 +210,38 @@ defun qsort = (any[] array, Comparator cmp_f,
qsort (array, cmp_f, pi + 1, right);
}
}
+
+/* Convert string to number */
+
+defun atoi = (string s, int b = 10) long:
+ {
+ defvar result = 0;
+
+ defun htoi = (char c) int:
+ {
+ if (c >= '0' && c <= '9') return c - '0';
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
+ }
+
+ defun valid = (char c, int b) int:
+ {
+ if (b <= 10) return (c >= '0') && (c <= '0' + b - 1);
+ if (b == 16) return ((c >= '0') && (c <= '9')) ||
+ ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
+ }
+
+ if (!(b in [2, 8, 10, 16]))
+ raise E_generic;
+
+ for (c in s)
+ {
+ if (!valid(c, b))
+ return result;
+
+ result = result * b + htoi(c);
+ }
+
+ return result;
+ }
+
diff --git a/testsuite/poke.std/atoi-1.pk b/testsuite/poke.std/atoi-1.pk
new file mode 100644
index 0000000..b94a68c
--- /dev/null
+++ b/testsuite/poke.std/atoi-1.pk
@@ -0,0 +1,7 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("0", 2) } } */
+/* { dg-output "0L" } */
+
+
diff --git a/testsuite/poke.std/atoi-10.pk b/testsuite/poke.std/atoi-10.pk
new file mode 100644
index 0000000..2d01f31
--- /dev/null
+++ b/testsuite/poke.std/atoi-10.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("1777") } } */
+/* { dg-output "1777L" } */
+
diff --git a/testsuite/poke.std/atoi-11.pk b/testsuite/poke.std/atoi-11.pk
new file mode 100644
index 0000000..2152001
--- /dev/null
+++ b/testsuite/poke.std/atoi-11.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("fce2", 16) } } */
+/* { dg-output "64738L" } */
+
diff --git a/testsuite/poke.std/atoi-12.pk b/testsuite/poke.std/atoi-12.pk
new file mode 100644
index 0000000..13ccb92
--- /dev/null
+++ b/testsuite/poke.std/atoi-12.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("bEeF", 16) } } */
+/* { dg-output "48879L" } */
+
diff --git a/testsuite/poke.std/atoi-13.pk b/testsuite/poke.std/atoi-13.pk
new file mode 100644
index 0000000..53527a3
--- /dev/null
+++ b/testsuite/poke.std/atoi-13.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("deadbeef", 16) } } */
+/* { dg-output "-559038737L" } */
+
diff --git a/testsuite/poke.std/atoi-2.pk b/testsuite/poke.std/atoi-2.pk
new file mode 100644
index 0000000..63c3dcb
--- /dev/null
+++ b/testsuite/poke.std/atoi-2.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("1", 2) } } */
+/* { dg-output "1L" } */
+
diff --git a/testsuite/poke.std/atoi-3.pk b/testsuite/poke.std/atoi-3.pk
new file mode 100644
index 0000000..9629928
--- /dev/null
+++ b/testsuite/poke.std/atoi-3.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("01010101", 2) } } */
+/* { dg-output "85L" } */
+
diff --git a/testsuite/poke.std/atoi-4.pk b/testsuite/poke.std/atoi-4.pk
new file mode 100644
index 0000000..1f6820c
--- /dev/null
+++ b/testsuite/poke.std/atoi-4.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("111111111", 2) } } */
+/* { dg-output "511L" } */
+
diff --git a/testsuite/poke.std/atoi-5.pk b/testsuite/poke.std/atoi-5.pk
new file mode 100644
index 0000000..fbd8033
--- /dev/null
+++ b/testsuite/poke.std/atoi-5.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("x", 2) } } */
+/* { dg-output "0L" } */
+
diff --git a/testsuite/poke.std/atoi-6.pk b/testsuite/poke.std/atoi-6.pk
new file mode 100644
index 0000000..e88f994
--- /dev/null
+++ b/testsuite/poke.std/atoi-6.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("1111x234", 2) } } */
+/* { dg-output "15L" } */
+
diff --git a/testsuite/poke.std/atoi-7.pk b/testsuite/poke.std/atoi-7.pk
new file mode 100644
index 0000000..ccbd91a
--- /dev/null
+++ b/testsuite/poke.std/atoi-7.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { try atoi("1234", 3); catch if E_generic { print("pass"); } }
} */
+/* { dg-output "pass" } */
+
diff --git a/testsuite/poke.std/atoi-8.pk b/testsuite/poke.std/atoi-8.pk
new file mode 100644
index 0000000..d682ff6
--- /dev/null
+++ b/testsuite/poke.std/atoi-8.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("1777", 8) } } */
+/* { dg-output "1023L" } */
+
diff --git a/testsuite/poke.std/atoi-9.pk b/testsuite/poke.std/atoi-9.pk
new file mode 100644
index 0000000..cbb527a
--- /dev/null
+++ b/testsuite/poke.std/atoi-9.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+/* { dg-command { .set obase 10 } } */
+/* { dg-command { atoi("1777", 10) } } */
+/* { dg-output "1777L" } */
+
--
2.23.0
- [PATCH] add atoi() function (and testsuite),
Michael Drüing <=