texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

branch master updated: * tp/Texinfo/Convert/Plaintext.pm (add_newline_if


From: Gavin D. Smith
Subject: branch master updated: * tp/Texinfo/Convert/Plaintext.pm (add_newline_if_needed): Implement checking end of a string for newline using "use bytes".
Date: Wed, 24 Jan 2024 15:54:04 -0500

This is an automated email from the git hooks/post-receive script.

gavin pushed a commit to branch master
in repository texinfo.

The following commit(s) were added to refs/heads/master by this push:
     new 09f8c253ac * tp/Texinfo/Convert/Plaintext.pm (add_newline_if_needed): 
Implement checking end of a string for newline using "use bytes".
09f8c253ac is described below

commit 09f8c253acc5fce7202b130db3b75118fc4edffc
Author: Gavin Smith <gavinsmith0123@gmail.com>
AuthorDate: Wed Jan 24 20:53:48 2024 +0000

    * tp/Texinfo/Convert/Plaintext.pm (add_newline_if_needed):
    Implement checking end of a string for newline using "use bytes".
---
 ChangeLog                       |  5 +++++
 tp/Texinfo/Convert/Plaintext.pm | 30 ++++++++++++++++++------------
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 90b3ad6049..5eb692f275 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-01-24  Gavin Smith <gavinsmith0123@gmail.com>
+
+       * tp/Texinfo/Convert/Plaintext.pm (add_newline_if_needed):
+       Implement checking end of a string for newline using "use bytes".
+
 2024-01-24  Gavin Smith <gavinsmith0123@gmail.com>
 
        * tp/Texinfo/Convert/Plaintext.pm (_convert): Rename a variable.
diff --git a/tp/Texinfo/Convert/Plaintext.pm b/tp/Texinfo/Convert/Plaintext.pm
index 70fc248764..b8c9025c32 100644
--- a/tp/Texinfo/Convert/Plaintext.pm
+++ b/tp/Texinfo/Convert/Plaintext.pm
@@ -1045,16 +1045,21 @@ sub _update_locations_counts($$)
 sub _add_newline_if_needed($) {
   my $self = shift;
 
+  # The "bytes" pragma makes length and substr quicker for Perl strings that
+  # may possibly contain UTF-8 sequences.  Since we are only checking for
+  # ASCII newline at the end of the string, this does not change the result.
+  use bytes;
+
   if (defined($self->{'count_context'}->[-1]->{'pending_text'})
-        and $self->{'count_context'}->[-1]->{'pending_text'} =~ /(..)\z/s) {
-    # NB \z matches end of string, whereas $ can match *before* a newline
-    # at the end of a string.
-    if ($1 ne "\n\n") {
-      _stream_output($self, "\n");
-      _add_lines_count($self, 1);
-    }
+    and length($self->{'count_context'}->[-1]->{'pending_text'}) >= 2
+    and substr($self->{'count_context'}->[-1]->{'pending_text'}, -2)
+          ne "\n\n") {
+    _stream_output($self, "\n");
+    _add_lines_count($self, 1);
   } else {
     my $result = _stream_result($self);
+    # NB \z matches end of string, whereas $ can match *before* a newline
+    # at the end of a string.
     if ($result ne '' and $result ne "\n" and $result !~ /\n\n\z/) {
       _stream_output($self, "\n");
       _add_lines_count($self, 1);
@@ -1955,12 +1960,13 @@ sub _convert($$)
       # inlined below for efficiency
       #$self->_add_newline_if_needed();
 
+      use bytes;
       if (defined($self->{'count_context'}->[-1]->{'pending_text'})
-        and $self->{'count_context'}->[-1]->{'pending_text'} =~ /(..\z)/s) {
-        if ($1 ne "\n\n") {
-          _stream_output($self, "\n");
-          _add_lines_count($self, 1);
-        }
+        and length($self->{'count_context'}->[-1]->{'pending_text'}) >= 2
+        and substr($self->{'count_context'}->[-1]->{'pending_text'}, -2)
+              ne "\n\n") {
+        _stream_output($self, "\n");
+        _add_lines_count($self, 1);
       } else {
         my $result = _stream_result($self);
         if ($result ne '' and $result ne "\n" and $result !~ /\n\n\z/) {



reply via email to

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