[Po4a-commits] "po4a/lib/Locale/Po4a Man.pm,1.195,1.196"

Nicolas FRANCOIS nekral-guest at alioth.debian.org
Fri Feb 16 11:43:00 CET 2007


Update of /cvsroot/po4a/po4a/lib/Locale/Po4a
In directory alioth:/tmp/cvs-serv19744/lib/Locale/Po4a

Modified Files:
	Man.pm 
Log Message:
Speed up:
Cache the result when we convert a non-breaking space to its
representation in the input and output charset.


Index: Man.pm
===================================================================
RCS file: /cvsroot/po4a/po4a/lib/Locale/Po4a/Man.pm,v
retrieving revision 1.195
retrieving revision 1.196
diff -u -d -r1.195 -r1.196
--- Man.pm	14 Feb 2007 22:06:32 -0000	1.195
+++ Man.pm	16 Feb 2007 10:42:58 -0000	1.196
@@ -967,12 +967,8 @@
     $str =~ s/\n([.'])/ $1/mg;
 
     # Change ascii non-breaking space to groff one
-    my $nbs_out = "\xA0";
-    my $enc_length;
-    eval("\$enc_length = Encode::from_to(\$nbs_out, \"latin1\",
-                                         \$self->get_out_charset,
-                                         1)");
-    $str =~ s/\Q$nbs_out/\\ /sg if defined $enc_length;
+    my $nbs_out = get_out_nbs($self->get_out_charset);
+    $str =~ s/\Q$nbs_out/\\ /sg if defined $nbs_out;
     # No nbsp (said "\ " in groff on the last pos of the line, or groff adds
     # an extra space
     $str =~ s/\\ \n/\\ /sg;
@@ -1154,19 +1150,9 @@
                             #          until the next fi macro.
 
 
-    # change the non-breaking space according to the input document charset
-    $nbs = "\xA0";
-    my $enc_length;
-    if (defined $self->{TT}{'file_in_charset'} and
-        length $self->{TT}{'file_in_charset'})
-    {
-        eval ("\$enc_length = Encode::from_to(\$nbs, \"latin-1\",
-                                              \$self->{TT}{'file_in_charset'},
-                                              1)");
-    }
-    # fall back solution
-    $nbs = "PO4A:VERY_IMPROBABLE_STRING_USEDFOR_NON-BREAKING-SPACES"
-        unless defined $enc_length;
+    # We want to change the non-breaking space according to the input
+    # document charset
+    $nbs = get_in_nbs($self->{TT}{'file_in_charset'});
 
   LINE:
     undef $self->{type};
@@ -1320,6 +1306,74 @@
     $mdoc_mode = 0;
 } # end of main
 
+# Cache the results of get_in_nbs and get_out_nbs
+{
+    my $last_in_charset;
+    my $last_in_nbs;
+
+# get_in_nbs(charset)
+# Return the representation of a non breaking space in the input charset
+# (given in argument).
+# or PO4A:VERY_IMPROBABLE_STRING_USEDFOR_NON-BREAKING-SPACES if this
+# character doesn't exist in this charset.
+    sub get_in_nbs() {
+        my $charset = shift;
+
+        return $last_in_nbs
+            if (    defined $charset
+                and defined $last_in_charset
+                and $charset eq $last_in_charset);
+
+        my $nbs = "\xA0";
+        my $length;
+        if (defined $charset and length $charset)
+        {
+            eval ("\$length = Encode::from_to(\$nbs, \"latin-1\",
+                                              \$charset,
+                                              1)");
+        }
+        # fall back solution
+        $nbs = "PO4A:VERY_IMPROBABLE_STRING_USEDFOR_NON-BREAKING-SPACES"
+            unless defined $length;
+        $last_in_charset = $charset;
+        $last_in_nbs = $nbs;
+
+        return $last_in_nbs;
+    }
+
+    my $last_out_charset;
+    my $last_out_nbs;
+# get_out_nbs(charset)
+# Return the representation of a non breaking space in the output charset
+# (given in argument).
+# or undef if this character doesn't exist in this charset.
+    sub get_out_nbs() {
+        my $charset = shift;
+
+        return $last_out_nbs
+            if (    defined $charset
+                and defined $last_out_charset
+                and $charset eq $last_out_charset);
+
+        my $nbs = "\xA0";
+        my $length;
+        if (defined $charset and length $charset)
+        {
+            eval ("\$length = Encode::from_to(\$nbs, \"latin-1\",
+                                              \$charset,
+                                              1)");
+        }
+        # fall back solution
+        undef $nbs
+            unless defined $length;
+        $last_out_charset = $charset;
+        $last_out_nbs = $nbs;
+
+        return $last_out_nbs;
+    }
+
+}
+
 # We can't push the header in the first line of the document, as in the
 # other module, because the first line may contain indications on how the
 # man page must be processed.




More information about the Po4a-commits mailing list