develooper Front page | perl.perl5.porters | Postings from February 2021

Re: Looking for someone to adopt adding trim() to core

Thread Previous | Thread Next
From:
demerphq
Date:
February 15, 2021 12:30
Subject:
Re: Looking for someone to adopt adding trim() to core
Message ID:
CANgJU+UD6871q3Z=OU7dtXBepRxXv5Nvaakf1ConG24GQJhk2w@mail.gmail.com
On Mon, 15 Feb 2021 at 13:24, Christian Walde <walde.christian@gmail.com> wrote:
>
> On Mon, 15 Feb 2021 13:16:19 +0100, demerphq <demerphq@gmail.com> wrote:
>
> > On Mon, 15 Feb 2021 at 12:56, Christian Walde <walde.christian@gmail.com> wrote:
> >>
> >> On Mon, 15 Feb 2021 12:52:29 +0100, Christian Walde <walde.christian@gmail.com> wrote:
> >>
> >> > Also, as a small early meta reply:
> >> >
> >> > Your perspective on this matter appears to be entirely about implementation categorization and what metaphysical issues there might be with that.
> >> >
> >> > I don't see you demonstrate in any way how a problem is created for the average user of Perl, meanwhile all of your solutions create problems for the average user of perl that mean they'll rather just copypaste another trim.
> >> >
> >> > Remember: "Easy things should be easy and hard things should be possible."
> >> >
> >> >
> >> > Think about this in terms of user stories.
> >>
> >> Oh yeah, and keep in mind that Perl is primarily a string manipulation language.
> >
> > In all my years hacking on the regex engine I never once noticed. Thanks.
>
>
> It is entirely unacceptable for you to insult me as you did here.

I am sorry you find sarcasm insulting. Given my past contributions to
perl I found the fact you added that comment was intended to insult
me. I decided to respond with sarcasm instead of insult as I decided
maybe you didnt realize who I was or what insight I have on how perl
works.

Ironically i was compiling the below patch as a well intentioned way
to move this forward. You still dont seem to realize I am trying to
give you what you want, but you dont seem to recognize it.

I really *would* like to see a trim in core, just not as a keyword.
And I have repeatedly given good reason for why I think so, and below
I offer an implementation, albeit incomplete.

Lets stop fighting here please. I didnt mean to insult you, I really
do apologize if my tone came across that way.

cheers,
Yves
$ ./perl -le'print scalar::trim(" foo ")'
foo
norole:yorton@psykopsis:blead:~/git_tree/perl$ git diff
diff --git a/universal.c b/universal.c
index c459064a6c..04938a3158 100644
--- a/universal.c
+++ b/universal.c
@@ -1244,6 +1244,126 @@ XS(XS_NamedCapture_flags)
     }
 }

+
+/* returns the smallest ptr where buf <= ptr < end which is not a
space, otherwise
+ * return end */
+const unsigned char *
+_first_not_space_ascii(pTHX_ const unsigned char *buf, const unsigned
char *end)
+{
+    while (buf < end && isSPACE(*buf)) {
+        ++buf;
+    }
+    /* paranoia - and more self documenting - consider what might happen
+     * if buf started out larger than end? */
+    if (buf > end) buf = end;
+    return buf;
+}
+
+/* returns the smallest ptr where buf <= ptr < end which is not a
space, otherwise
+ * return end */
+const unsigned char *
+_first_not_space_utf8(pTHX_ const unsigned char *buf, const unsigned char *end)
+{
+    while (buf < end && isSPACE_utf8_safe(buf, end)) {
+        buf += UTF8SKIP(buf);
+    }
+    /* paranoia - and more self documenting - consider what might happen if buf
+     * started out larger than end? */
+    if ( buf > end) buf = end;
+    return buf;
+}
+
+/* returns the largest ptr where buf <= ptr < end which is not a
space, otherwise
+ * returns buf */
+const unsigned char *
+_last_not_space_ascii(pTHX_ const unsigned char *buf, const unsigned char *end)
+{
+    const unsigned char *last= end - 1;
+    while (buf <= last && isSPACE(*last))
+        last--;
+    /* paranoia - and more self documenting - consider what might happen if buf
+     * started out larger than end? */
+    if ( buf <= last) return last+1;
+    return buf;
+}
+
+/* returns the largest ptr where buf <= ptr < end which is not a
space, otherwise
+ * returns buf */
+const unsigned char *
+_last_not_space_utf8(pTHX_ const unsigned char *buf, const unsigned char *end)
+{
+    const unsigned char *last= end;
+
+    while (buf < last) {
+        const unsigned char * csr = last - 1;
+        while (UTF8_IS_CONTINUATION(*csr)) {
+            --csr;
+            if (csr < buf)
+                return buf;
+        }
+        if (!isSPACE_utf8_safe(csr,last))
+            return last;
+        last= csr;
+    }
+    /* paranoia - and more self documenting - consider what might happen if buf
+     * started out larger than end? */
+    return buf;
+}
+
+/* returns the smallest ptr where buf <= ptr < end which is not a
space, otherwise
+ * return end */
+const unsigned char *
+_first_not_space(pTHX_ const unsigned char *buf, const unsigned char
*end, int utf8)
+{
+    if (utf8)
+        return _first_not_space_utf8(aTHX_ buf, end);
+    else
+        return _first_not_space_ascii(aTHX_ buf, end);
+}
+
+/* returns the largest ptr where buf <= ptr < end which is not a
space, otherwise
+ * returns buf */
+const unsigned char *
+_last_not_space(pTHX_ const unsigned char *buf, const unsigned char
*end, int utf8)
+{
+    if (utf8)
+        return _last_not_space_utf8(aTHX_ buf, end);
+    else
+        return _last_not_space_ascii(aTHX_ buf, end);
+}
+
+
+XS(XS_scalar_trim); /* prototype to pass -Wmissing-prototypes */
+XS(XS_scalar_trim)
+{
+    dXSARGS;
+    SV * input;
+    SV * ret = NULL;
+
+    if (items != 1)
+        croak_xs_usage(cv, "trim");
+
+    SP -= items;
+    PUTBACK;
+
+    input = ST(0);
+    if (SvOK(input)) {
+        STRLEN               len;
+        const unsigned char *src = (const unsigned char *)SvPV(input, len);
+        const unsigned char *end = src + len;
+        const unsigned char *start = _first_not_space(aTHX_ src, end,
SvUTF8(input));
+        const unsigned char *stop  = _last_not_space(aTHX_ start,
end, SvUTF8(input));
+
+        ret = newSVpvn((const char *) start, stop-start);
+        if (SvUTF8(input))
+            SvUTF8_on(ret);
+    }
+
+    PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
+    XSRETURN(1);
+}
+
+
 #include "vutil.h"
 #include "vxs.inc"

@@ -1293,6 +1413,7 @@ static const struct xsub_details these_details[] = {
     {"Tie::Hash::NamedCapture::FIRSTKEY", XS_NamedCapture_FIRSTKEY, NULL, 0 },
     {"Tie::Hash::NamedCapture::NEXTKEY", XS_NamedCapture_FIRSTKEY, NULL, 1 },
     {"Tie::Hash::NamedCapture::flags", XS_NamedCapture_flags, NULL, 0 },
+    {"scalar::trim", XS_scalar_trim, "$", 0 },
 };

 STATIC OP*


-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About