[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] New functions to strip whitespace from strings
From: |
John Darrington |
Subject: |
[PATCH] New functions to strip whitespace from strings |
Date: |
Tue, 12 Nov 2019 12:33:29 +0100 |
* src/std.pk (ltrim): New function.
* src/std.pk (rtrim): New function.
* doc/poke.texi (String Functions): New chapter.
* testsuite/poke.std/ltrim.pk: New file.
* testsuite/poke.std/rtrim.pk: New file.
---
ChangeLog | 8 ++++++++
doc/poke.texi | 37 +++++++++++++++++++++++++++++++++++++
src/std.pk | 27 +++++++++++++++++++++++++++
testsuite/poke.std/ltrim.pk | 4 ++++
testsuite/poke.std/rtrim.pk | 4 ++++
5 files changed, 80 insertions(+)
create mode 100644 testsuite/poke.std/ltrim.pk
create mode 100644 testsuite/poke.std/rtrim.pk
diff --git a/ChangeLog b/ChangeLog
index f2a5d66..5acd43c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
2019-11-12 John Darrington <address@hidden>
+ * src/std.pk (ltrim): New function.
+ * src/std.pk (rtrim): New function.
+ * doc/poke.texi (String Functions): New chapter.
+ * testsuite/poke.std/ltrim.pk: New file.
+ * testsuite/poke.std/rtrim.pk: New file.
+
+2019-11-12 John Darrington <address@hidden>
+
* src/pk-file.c (pk_cmd_file): Use strerror for reason.
(pk_cmd_load_file): ditto.
diff --git a/doc/poke.texi b/doc/poke.texi
index 0b0a015..c1620be 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -103,6 +103,7 @@ The Standard Library
* Standard Integral Types:: int, long and the like.
* Standard Offset Types:: off64 and the like.
* Conversion Functions:: catos, atoi, etc.
+* String Functions:: Functions which deal with strings.
* Sorting Functions:: qsort.
Hacking poke
@@ -1440,6 +1441,9 @@ Poke supports a notion of @dfn{strings} which is very
similar to the C
programming language: a string value is a sequence of characters that
is terminated by the so-called @dfn{null character}.
+The standard library provides functions which process strings.
+@xref{String Functions}.
+
@menu
* String Literals:: Writing string values.
* String Types:: string.
@@ -3511,6 +3515,39 @@ after the parsed integer. Thus, this works:
10L
@end example
+@node String Functions
+@chapter String Functions
+
+The Poke standard library provides the following functions to do
+work on strings:
+
+@menu
+* ltrim:: Remove leading whitespace.
+* rtrim:: Remove trailing whitespace.
+@end menu
+
+@node ltrim
+@section ltrim
+
+The standard function @code{ltrim} provides the following interface:
+
+@example
+defun ltrim = (string s) string: @{ ... @}
+@end example
+
+It returns a copy of the input string @code{s} with any leading whitespace
removed.
+
+@node rtrim
+@section rtrim
+
+The standard function @code{rtrim} provides the following interface:
+
+@example
+defun rtrim = (string s) string: @{ ... @}
+@end example
+
+It returns a copy of the input string @code{s} with any trailing whitespace
removed.
+
@node Sorting Functions
@chapter Sorting Functions
diff --git a/src/std.pk b/src/std.pk
index 414a065..075bfb1 100644
--- a/src/std.pk
+++ b/src/std.pk
@@ -60,6 +60,33 @@ defun catos = (char[] chars) string:
return s;
}
+ /* Return S with leading whitespace omitted. */
+defun ltrim = (string s) string:
+ {
+ defvar result = "";
+ for (c in s)
+ {
+ if ((c != ' ' && c != '\t') || result != "")
+ result = result + c as string;
+ }
+ return result;
+ }
+
+ /* Return S with trailing whitespace omitted. */
+defun rtrim = (string s) string:
+ {
+ defvar result = "";
+ defvar i = s'length;
+ while (i > 0)
+ {
+ defvar c = s[i - 1];
+ if ((c != ' ' && c != '\t') || result != "")
+ result = c as string + result;
+ i = i - 1;
+ }
+ return result;
+ }
+
defun atoi = (string s, int b = 10) long:
{
defvar result = 0;
diff --git a/testsuite/poke.std/ltrim.pk b/testsuite/poke.std/ltrim.pk
new file mode 100644
index 0000000..1a08c0a
--- /dev/null
+++ b/testsuite/poke.std/ltrim.pk
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+
+/* { dg-command { ltrim (" a string to trim ") } } */
+/* { dg-output "a string to trim " } */
diff --git a/testsuite/poke.std/rtrim.pk b/testsuite/poke.std/rtrim.pk
new file mode 100644
index 0000000..b62bf61
--- /dev/null
+++ b/testsuite/poke.std/rtrim.pk
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+
+/* { dg-command { rtrim (" a string to trim ") } } */
+/* { dg-output " a string to trim" } */
--
2.11.0