This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Replace a loop with strcspn()
authorKarl Williamson <khw@cpan.org>
Sat, 7 Dec 2019 23:08:15 +0000 (16:08 -0700)
committerKarl Williamson <khw@cpan.org>
Mon, 9 Dec 2019 22:32:49 +0000 (15:32 -0700)
A later commit will add a terminating character for the span, so we use
this function instead of memchr.

regcomp.c

index cb8e509..3202323 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -19849,22 +19849,21 @@ Perl__get_regclass_nonbitmap_data(pTHX_ const regexp *prog,
                      * here to the next \n */
 
                     remaining -= len;
-                    while (*(si_string + len) != '\n' && remaining > 0) {
-                        remaining--;
-                        len++;
-                    }
-                    if (*(si_string + len) == '\n') {
-                        len++;
-                        remaining--;
-                    }
+                    len = strcspn(si_string, "\n");
+                    remaining -= len;
                     if (matches_string) {
-                        sv_catpvn(matches_string, si_string, len - 1);
+                        sv_catpvn(matches_string, si_string, len);
                     }
                     else {
-                        matches_string = newSVpvn(si_string, len - 1);
+                        matches_string = newSVpvn(si_string, len);
                     }
-                    si_string += len;
                     sv_catpvs(matches_string, " ");
+
+                    si_string += len;
+                    if (remaining && UCHARAT(si_string) == '\n') {
+                        si_string++;
+                        remaining--;
+                    }
                 } /* end of loop through the text */
 
                 assert(matches_string);