poke-devel
[Top][All Lists]
Advanced

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

[PATCH v2 1/4] std.pk: Implement strrchr


From: Arsen Arsenović
Subject: [PATCH v2 1/4] std.pk: Implement strrchr
Date: Sun, 29 Jan 2023 21:04:35 +0100

* libpoke/std.pk (strrchr): New function.  Finds the last
occurrence of a character in a string and returns its index, or
-1.
* doc/poke.texi (strrchr): Document new standard library function.
(String Functions): Add strrchr to menu, disambiguate menu entries
of strchr and strrchr.
* testsuite/poke.std/std-test.pk: Add strrchr test.
---
 ChangeLog                      | 11 +++++++++++
 doc/poke.texi                  | 17 ++++++++++++++++-
 libpoke/std.pk                 | 18 ++++++++++++++++++
 testsuite/poke.std/std-test.pk |  9 +++++++++
 4 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index c7e332c6..751eb110 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2023-01-28  Arsen Arsenović  <arsen@aarsen.me>
+
+       std.pk: Implement strrchr
+       * libpoke/std.pk (strrchr): New function.  Finds the last
+       occurrence of a character in a string and returns its index, or
+       -1.
+       * doc/poke.texi (strrchr): Document new standard library function.
+       (String Functions): Add strrchr to menu, disambiguate menu entries
+       of strchr and strrchr.
+       * testsuite/poke.std/std-test.pk: Add strrchr test.
+
 2023-01-28  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
 
        * libpoke/std.pk (isdigit): New function.
diff --git a/doc/poke.texi b/doc/poke.texi
index 88465629..0606f136 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -15672,7 +15672,8 @@ work on strings:
 @menu
 * ltrim::              Remove leading characters.
 * rtrim::              Remove trailing characters.
-* strchr::             Locate a character in a string.
+* strchr::             Locate a character in a string, from the beginning.
+* strrchr::            Locate a character in a string, from the end.
 @end menu
 
 @node ltrim
@@ -15715,6 +15716,20 @@ 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 strrchr
+@subsection @code{strrchr}
+@cindex @code{strrchr}
+@cindex character, locating in a string
+The standard function @code{strrchr} provides the following interface:
+
+@example
+fun strrchr = (string s, uint<8> c) int<32>: @{ @dots{} @}
+@end example
+
+It returns the index of the last occurrence of the character @var{c}
+in the string @var{s}.  If the character is not found in the string,
+this function returns -1.
+
 @node Character Functions
 @section Character Functions
 The Poke standard library provides the following functions to deal
diff --git a/libpoke/std.pk b/libpoke/std.pk
index 94b7dac9..9d9972d3 100644
--- a/libpoke/std.pk
+++ b/libpoke/std.pk
@@ -186,6 +186,24 @@ fun strchr = (string s, uint<8> c) int<32>:
     return i;
   }
 
+/* Return the index in the string S of the last occurrence of the
+   character C.  If C is not in S then return -1.  */
+
+fun strrchr = (string s, uint<8> c) int<32>:
+  {
+    if (s'length == 0)
+      return -1;
+
+    var i = (s'length - 1) as int<32>;
+    while (i >= 0)
+      {
+        if (s[i] == c)
+          break;
+        i--;
+      }
+    return i;
+  }
+
 /* Return S with leading characters belonging to the given set
    omitted.  By default the omitted characters are whitespaces.  */
 
diff --git a/testsuite/poke.std/std-test.pk b/testsuite/poke.std/std-test.pk
index bf370f9c..58d042e2 100644
--- a/testsuite/poke.std/std-test.pk
+++ b/testsuite/poke.std/std-test.pk
@@ -355,6 +355,15 @@ var tests = [
         assert (!isxdigit (0));
       },
   },
+  PkTest {
+    name = "strrchr",
+    func = lambda (string name) void:
+      {
+        assert (strrchr ("foo", 'o') == 2);
+        assert (strrchr ("foo", 'x') == -1);
+        assert (strrchr ("", 'x') == -1);
+      },
+  },
 ];
 
 exit (pktest_run (tests) ? 0 : 1);
-- 
2.39.1




reply via email to

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