[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/4] std.pk: Implement strrchr
From: |
Arsen Arsenović |
Subject: |
[PATCH 1/4] std.pk: Implement strrchr |
Date: |
Sun, 29 Jan 2023 15:54:38 +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.
---
Afternoon,
This patchset implements the pk_vercmp function, intended for comparing
GNU poke version strings, as well as the requisite standard library
facilities used in such a string manipulation heavy function. The
implementation in pk_vercmp is dual purpose, in that it both verifies
and parses a version string.
An alternative implementation could be written that populates a struct
with broken out version information, rather than ad-hoc parsing two
strings at once as is demonstrated here, as a pk_verparse, with
pk_vercmp simply being a helper that returns the lexicographic
comparison of two parsed versions. I had initially opted to write a
non-verifying lax version of this function, but bolted strictness onto
it later, which is why this is the implementation I am presenting. If
you wish, I can upgrade to the approach described in this paragraph.
Other patches are unaffected by this decision, and so, can be reviewed
in either case.
Are [1,3] OK for master? What do you think about 4 (the discussion in
the paragraph above?
Thanks in advance, have a lovely day.
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..f0cd3a7e 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
- [PATCH 1/4] std.pk: Implement strrchr,
Arsen Arsenović <=