]> Sergey Matveev's repositories - public-inbox.git/commitdiff
viewvcs: cleanup utf8 handling
authorEric Wong <e@80x24.org>
Fri, 1 Feb 2019 22:12:52 +0000 (22:12 +0000)
committerEric Wong <e@80x24.org>
Tue, 5 Feb 2019 04:35:29 +0000 (04:35 +0000)
Favor in-place utf8::decode since it's a bit faster without
method dispatch overhead; and don't care about validity just
yet.

HlMod->do_hl itself should return "utf8" strings, since other
parts of our code can use it, so it's not the job of ViewVCS to
post-process HlMod output.

lib/PublicInbox/HlMod.pm
lib/PublicInbox/ViewVCS.pm
t/hl_mod.t

index 237ffaca0a23723381a28e1cc2d1f7c4aa59e464..decfd714d7ba5e0551b125de94cf16d056642ca6 100644 (file)
@@ -107,7 +107,12 @@ sub do_hl {
                $g->setEncoding('utf-8');
                $g;
        };
-       \($gen->generateString($$str))
+
+       # we assume $$str is valid UTF-8, but the SWIG binding doesn't
+       # know that, so ensure it's marked as UTF-8 even if it isnt...
+       my $out = $gen->generateString($$str);
+       utf8::decode($out);
+       \$out;
 }
 
 # SWIG instances aren't reference-counted, but $self is;
index d67b5eb41dc20e455a74d4a6b7472d37b285e2bf..acdd822dc4274099e2b362ecd0512aa43759e69a 100644 (file)
@@ -16,7 +16,6 @@
 package PublicInbox::ViewVCS;
 use strict;
 use warnings;
-use Encode qw(find_encoding);
 use PublicInbox::SolverGit;
 use PublicInbox::WwwStream;
 use PublicInbox::Linkify;
@@ -33,7 +32,6 @@ END { $hl = undef };
 
 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
 my $max_size = 1024 * 1024; # TODO: configurable
-my $enc_utf8 = find_encoding('UTF-8');
 my $BIN_DETECT = 8000; # same as git
 
 sub html_page ($$$) {
@@ -122,14 +120,14 @@ sub solve_result {
                return html_page($ctx, 200, \$log);
        }
 
-       $$blob = $enc_utf8->decode($$blob);
+       # TODO: detect + convert to ensure validity
+       utf8::decode($$blob);
        my $nl = ($$blob =~ tr/\n/\n/);
        my $pad = length($nl);
 
        $l->linkify_1($$blob);
        my $ok = $hl->do_hl($blob, $path) if $hl;
        if ($ok) {
-               $$ok = $enc_utf8->decode($$ok);
                src_escape($$ok);
                $blob = $ok;
        } else {
index 80f88907e4ed577f74ed3e95d5a281cdd84c6d2f..c402f1f784f40b3963b53407d706e9bf4d7d93df 100644 (file)
@@ -19,6 +19,7 @@ my $orig = $str;
 {
        my $ref = $hls->do_hl(\$str, 'foo.perl');
        is(ref($ref), 'SCALAR', 'got a scalar reference back');
+       ok(utf8::valid($$ref), 'resulting string is utf8::valid');
        like($$ref, qr/I can see you!/, 'we can see ourselves in output');
        like($$ref, qr/&amp;&amp;/, 'escaped');