poke-devel
[Top][All Lists]
Advanced

[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 08:26:33 +0100

        * src/std.pk (ltrim): New function.
        * src/std.pk (rtrim): New function.
---
 src/std.pk | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/std.pk b/src/std.pk
index 414a065..5335567 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;
-- 
2.11.0




reply via email to

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