git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/3] gitweb: Fix fallback mode of to_utf8 subroutine

From
Jakub Narebski <jnareb@gmail.com>
Date
Dec 19, 2011, 00:54 UTC
Message-ID
<201112190154.19107.jnareb@gmail.com>
In-Reply-To
<1324113743-21498-1-git-send-email-jnareb@gmail.com>

e5d3de5 (gitweb: use Perl built-in utf8 function for UTF-8 decoding., 2007-12-04) was meant to make gitweb faster by using Perl's internals (see subsection "Messing with Perl's Internals" in Encode(3pm) manpage)

Simple benchmark confirms that (old = 00f429a, new = this version); note that it is synthetic benchmark of standalone subroutines, not of gitweb itself

        old  new
  old    -- -65%
  new  189%   --

Unfortunately it made fallback mode of to_utf8 do not work... except for default value 'latin1' of $fallback_encoding ('latin1' is Perl native encoding), which is why it was not noticed for such long time.

utf8::valid(STRING) is an internal function that tests whether STRING is in a _consistent state_ regarding UTF-8. It returns true is well-formed UTF-8 and has the UTF-8 flag on _*or*_ if string is held as bytes (both these states are 'consistent'). For gitweb the second option was true, as output from git commands is opened without ':utf8' layer.

What made it work at all for STRING in 'latin1' encoding is the fact that utf8:decode(STRING) turns on UTF-8 flag only if source string is valid UTF-8 and contains multi-byte UTF-8 characters... and that if string doesn't have UTF-8 flag set it is treated as in native Perl encoding, i.e. 'latin1' / 'iso-8859-1' (unless native encoding it is EBCDIC ;-)). It was ':utf8' layer that actually converted 'latin1' (no UTF-8 flag == native == 'latin1) to 'utf8'.

Let's make use of the fact that utf8:decode(STRING) returns false if STRING is invalid as UTF-8 to check whether to enable fallback mode.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
Excuse me for overly long commit message...
Resent as part of to_utf8 fixes for better visibility
 gitweb/gitweb.perl |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index d24763b..75b0970 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -1443,8 +1443,7 @@ sub validate_refname {
 sub to_utf8 {
 	my $str = shift;
 	return undef unless defined $str;
-	if (utf8::valid($str)) {
-		utf8::decode($str);
+	if (utf8::valid($str) && utf8::decode($str)) {
 		return $str;
 	} else {
 		return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
-- 
1.7.6
Previous: Junio C HamanoNext: Jakub Narebski
Message 6 of 8 in “gitweb: Various to_utf8 / esc_html fixes”
  1. 0/3 gitweb: Various to_utf8 / esc_html fixesJakub Narebski, Dec 17, 2011
  2. 1/3 gitweb: Call to_utf8() on input string in chop_and_escape_str()Jakub Narebski, Dec 17, 2011
  3. 2/3 gitweb: esc_html() site name for title in OPMLJakub Narebski, Dec 17, 2011
  4. 3/3 gitweb: Output valid utf8 in git_blame_common('data')Jakub Narebski, Dec 17, 2011
  5. Junio C HamanoDec 17, 2011
  6. 4/3 gitweb: Fix fallback mode of to_utf8 subroutineJakub Narebski, Dec 19, 2011
  7. Jakub NarebskiDec 19, 2011
  8. 4/3 gitweb: Fix fallback mode of to_utf8 subroutineJakub Narebski, Dec 19, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.