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

Re: [bug] git diff --word-diff gives wrong result for utf-8 chinese

From
Jeff King <peff@peff.net>
Date
Dec 1, 2022, 20:06 UTC
Message-ID
<Y4kJWJObB5Er2CXZ@coredump.intra.peff.net>
In-Reply-To
<4dac768f-1104-a565-db4c-8a1b7eb2870d@dunelm.org.uk>
On Thu, Dec 01, 2022 at 02:51:29PM +0000, Phillip Wood wrote:
Show 15 quoted lines
> On 01/12/2022 07:33, Ping Yin wrote:
> > > > If the rule is "break on ascii whitespace",
> > 
> > Is there a way to achieve this: break english by word, and break
> > chinese by utf-8 character
> 
> You could extend your current regex so that it matches whole utf-8
> codepoints which is what git does for the builtin userdiff regexes. I've not
> tested it but I think
> 
> git config --global diff.wordregex "[[:alnum:]_]+|[^[:space:]]|$(printf
> '[\xc0-\xff][\x80-\xbf]+')"
> 
> should work. The downside is that you end up with a .gitconfig that is not
> valid utf-8. Perhaps someone else has a clever idea to get around that.

I think in more advanced regular expression engines you can do stuff like matching "[\x{4e00}-\x{9fcc}]", or even "\p{Han}". But I don't know that the stock libc regex is capable of anything like this, even with EREs. That's the only option Git provides for matching word regexes, but in theory we could support libpcre. We already can optionally build against it; we would just need config/plumbing to get it into diff.c:find_word_boundaries().

-Peff
Previous: Ping Yin
Message 12 of 12 in “[bug] git diff --word-diff gives wrong result for utf-8 chinese”
  1. Ping YinNov 29, 2022
  2. Ping YinNov 29, 2022
  3. Bagas SanjayaNov 29, 2022
  4. Ævar Arnfjörð BjarmasonNov 29, 2022
  5. Junio C HamanoNov 29, 2022
  6. Jeff KingNov 29, 2022
  7. Jeff KingNov 29, 2022
  8. Ping YinDec 1, 2022
  9. Ping YinDec 1, 2022
  10. Phillip WoodDec 1, 2022
  11. Ping YinDec 1, 2022
  12. Jeff KingDec 1, 2022

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.