poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] std.pk: add new functions `isdigit_p' and `isxdigit_p'


From: Mohammad-Reza Nabipoor
Subject: [PATCH 1/2] std.pk: add new functions `isdigit_p' and `isxdigit_p'
Date: Thu, 26 Jan 2023 01:01:20 +0100

2023-01-26  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * libpoke/std.pk (isdigit_p): New function.
        (isxdigit_p): Likewise.
        * testsuite/poke.std/std-test.pk (tests): Add tests for `isdigit_p'
        and `isxdigit_p'.
        * doc/poke.texi (The Standard Library): Add doc.
---
 ChangeLog                      |  8 +++++
 doc/poke.texi                  | 39 ++++++++++++++++++++++
 libpoke/std.pk                 | 14 ++++++++
 testsuite/poke.std/std-test.pk | 60 ++++++++++++++++++++++++++++++++++
 4 files changed, 121 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 47636ce0..fc67576c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-01-26  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * libpoke/std.pk (isdigit_p): New function.
+       (isxdigit_p): Likewise.
+       * testsuite/poke.std/std-test.pk (tests): Add tests for `isdigit_p'
+       and `isxdigit_p'.
+       * doc/poke.texi (The Standard Library): Add doc.
+
 2023-01-25  Jose E. Marchesi  <jemarch@gnu.org>
 
        * configure.ac: Bump version number to 3.0.
diff --git a/doc/poke.texi b/doc/poke.texi
index 437eef5d..ddbd5566 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -253,6 +253,7 @@ The Standard Library
 * Conversion Functions::       catos, atoi, @i{etc}.
 * Array Functions::             Functions which deal with arrays.
 * String Functions::           Functions which deal with strings.
+* Character Functions::         Functions which deal with characters.
 * Values Functions::            Functions which deal with values in general.
 * Sorting Functions::          qsort.
 * CRC Functions::               Cyclic Redundancy Checksums.
@@ -15318,6 +15319,7 @@ facilities provided by the library.
 * Conversion Functions::       catos, atoi, @i{etc}.
 * Array Functions::             Functions which deal with arrays.
 * String Functions::           Functions which deal with strings.
+* Character Functions::         Functions which deal with characters.
 * Values Functions::            Functions which deal with values in general.
 * IO Space Functions::          Functions that operate on IO spaces.
 * Sorting Functions::          qsort.
@@ -15623,6 +15625,43 @@ It returns the index of the first occurrence of the 
character @var{c}
 in the string @var{s}.  If the character is not found in the string,
 this function returns the length of the string.
 
+@node Character Functions
+@section Character Functions
+The Poke standard library provides the following functions to deal
+with characters:
+
+@menu
+* isdigit_p::           Is given character a decimal digit?
+* isxdigit_p::          Is given character a hexadecimal digit?
+@end menu
+
+@node isdigit_p
+
+The standard function @code{isdigit_p} provides the following
+interface:
+
+@example
+fun isdigit_p = (uint<8> @var{c}) int<32>
+@end example
+
+@noindent
+it returns 0 when the given ASCII character @var{c} is not a decimal
+digit, otherwise it returns a non-zero value.
+
+@node isxdigit_p
+
+The standard function @code{isxdigit_p} provides the following
+interface:
+
+@example
+fun isxdigit_p = (uint<8> @var{c}) int<32>
+@end example
+
+@noindent
+it returns 0 when the given ASCII character @var{c} is not a hexadecimal
+digit, otherwise it returns a non-zero value.
+
+
 @node Values Functions
 @section Values Functions
 The Poke standard library provides the following functions to handle
diff --git a/libpoke/std.pk b/libpoke/std.pk
index 22699251..3084b557 100644
--- a/libpoke/std.pk
+++ b/libpoke/std.pk
@@ -494,3 +494,17 @@ fun with_cur_ios = (int<32> ios,
     raise exc;
   }
 }
+
+/* Determine that the given ASCII character is a decimal digit or not.  */
+
+fun isdigit_p = (uint<8> c) int<32>:
+{
+  return c - '0' < 10;
+}
+
+/* Determine that the given ASCII character is a hexadecimal digit or not.  */
+
+fun isxdigit_p = (uint<8> c) int<32>:
+{
+  return (c - '0' < 10UB) || ((c | 0x20UB) - 'a' < 6UB);
+}
diff --git a/testsuite/poke.std/std-test.pk b/testsuite/poke.std/std-test.pk
index b3c6efe9..3e09ebef 100644
--- a/testsuite/poke.std/std-test.pk
+++ b/testsuite/poke.std/std-test.pk
@@ -295,6 +295,66 @@ var tests = [
         qsort ([4,3,2,1], cmpints);
       },
   },
+  PkTest {
+    name = "isdigit_p",
+    func = lambda (string name) void:
+      {
+        assert (isdigit_p ('0'));
+        assert (isdigit_p ('1'));
+        assert (isdigit_p ('2'));
+        assert (isdigit_p ('3'));
+        assert (isdigit_p ('4'));
+        assert (isdigit_p ('5'));
+        assert (isdigit_p ('6'));
+        assert (isdigit_p ('7'));
+        assert (isdigit_p ('8'));
+        assert (isdigit_p ('9'));
+        assert (!isdigit_p ('a'));
+        assert (!isdigit_p ('B'));
+        assert (!isdigit_p ('}'));
+        assert (!isdigit_p (' '));
+        assert (!isdigit_p (0));
+        assert (!isdigit_p (0xa));
+        assert (!isdigit_p ('!'));
+      },
+  },
+  PkTest {
+    name = "isxdigit_p",
+    func = lambda (string name) void:
+      {
+        assert (isxdigit_p ('0'));
+        assert (isxdigit_p ('1'));
+        assert (isxdigit_p ('2'));
+        assert (isxdigit_p ('3'));
+        assert (isxdigit_p ('4'));
+        assert (isxdigit_p ('5'));
+        assert (isxdigit_p ('6'));
+        assert (isxdigit_p ('7'));
+        assert (isxdigit_p ('8'));
+        assert (isxdigit_p ('9'));
+        assert (isxdigit_p ('a'));
+        assert (isxdigit_p ('b'));
+        assert (isxdigit_p ('c'));
+        assert (isxdigit_p ('d'));
+        assert (isxdigit_p ('e'));
+        assert (isxdigit_p ('f'));
+        assert (isxdigit_p ('A'));
+        assert (isxdigit_p ('B'));
+        assert (isxdigit_p ('C'));
+        assert (isxdigit_p ('D'));
+        assert (isxdigit_p ('E'));
+        assert (isxdigit_p ('F'));
+        assert (!isxdigit_p ('x'));
+        assert (!isxdigit_p ('X'));
+        assert (!isxdigit_p (' '));
+        assert (!isxdigit_p ('}'));
+        assert (!isxdigit_p ('('));
+        assert (!isxdigit_p ('!'));
+        assert (!isxdigit_p ('\n'));
+        assert (!isxdigit_p (0xa));
+        assert (!isxdigit_p (0));
+      },
+  },
 ];
 
 exit (pktest_run (tests) ? 0 : 1);
-- 
2.39.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]