checkpatch: calculate UTF-8 characters correctly
PATCH 1/1 checkpatch: calculate UTF-8 characters correctly
We do check UTF-8 correctness in checkpatch.pl (search for "patch and commit message should be encoded in UTF-8"), but we count bytes, not symbols when limiting line-length. Let's be consistent.
--- scripts/checkpatch.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +6 -1
@@ -7,6 +7,7 @@ use strict; use warnings; +use Encode qw(decode); use Term::ANSIColor qw(:constants); my $P = $0;
@@ -596,7 +597,11 @@ sub line_stats { # Pick the indent from the front of the line. my ($white) = ($line =~ /^(\s*)/); - return (length($line), length($white)); + # Use character count (not byte count) so multi-byte UTF-8 characters + # are counted as single characters. + my $line_chars = length(decode('UTF-8', $line, Encode::FB_DEFAULT)); + my $white_chars = length(decode('UTF-8', $white, Encode::FB_DEFAULT)); + return ($line_chars, $white_chars); } my $sanitise_quote = '';
Signature · 1 line
2.52.0
Quoted 22 lines · Vladimir Sementsov-Ogievskiy
We do check UTF-8 correctness in checkpatch.pl (search for "patch and commit message should be encoded in UTF-8"), but we count bytes, not symbols when limiting line-length. Let's be consistent.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- scripts/checkpatch.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)scripts/checkpatch.pl +1
-0@@ -7,6 +7,7 @@ use strict; use warnings; +use Encode qw(decode); use Term::ANSIColor qw(:constants); my $P = $0;
scripts/checkpatch.pl sub line_stats { · line 598
Quoted 2 lines
scripts/checkpatch.pl +0
-0@@ -596,7 +597,11 @@ sub line_stats { # Pick the indent from the front of the line. my ($white) = ($line =~ /^(\s*)/);
line_stats() still expands tabs before decoding UTF-8, so tab stops are computed from UTF-8 bytes rather than decoded characters.
For example, é<TAB>X is counted as length 8 with the current order, but length 9 if decoded before tab expansion. This is mostly hidden for C files because tabs are rejected, but .s/.S files may contain tabs.
Since this patch aims to make line_stats() use character semantics, it should decode before expanding tabs, or make expand_tabs() operate on a decoded string.
You could implement it like this:
```
@@ -588,8 +589,9 @@ sub line_stats {
sub line_stats {
my ($line) = @_;
- # Drop the diff line leader and expand tabs
+ # Drop the diff line leader, decode UTF-8, and expand tabs.
$line =~ s/^.//;
+ $line = decode('UTF-8', $line, Encode::FB_DEFAULT);
$line = expand_tabs($line);
# Pick the indent from the front of the line.
```scripts/checkpatch.pl sub line_stats { · line 599
Quoted 2 lines
scripts/checkpatch.pl +0
-1- return (length($line), length($white));
With that change, the original return (length($line), length($white)); can be kept; the extra decode() calls and new return below are not needed.
Thanks,
Chao
Quoted 11 lines
scripts/checkpatch.pl +5
-0+ # Use character count (not byte count) so multi-byte UTF-8 characters + # are counted as single characters. + my $line_chars = length(decode('UTF-8', $line, Encode::FB_DEFAULT)); + my $white_chars = length(decode('UTF-8', $white, Encode::FB_DEFAULT)); + return ($line_chars, $white_chars); } my $sanitise_quote = '';Signature · 1 line
2.52.0
Quoted 52 lines · Chao Liu
Quoted 22 lines · Vladimir Sementsov-Ogievskiy
We do check UTF-8 correctness in checkpatch.pl (search for "patch and commit message should be encoded in UTF-8"), but we count bytes, not symbols when limiting line-length. Let's be consistent.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- scripts/checkpatch.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)scripts/checkpatch.pl +1
-0@@ -7,6 +7,7 @@ use strict; use warnings; +use Encode qw(decode); use Term::ANSIColor qw(:constants); my $P = $0;scripts/checkpatch.pl sub line_stats { · line 598
Quoted 2 lines
scripts/checkpatch.pl +0
-0@@ -596,7 +597,11 @@ sub line_stats { # Pick the indent from the front of the line. my ($white) = ($line =~ /^(\s*)/);
line_stats()still expands tabs before decoding UTF-8, so tab stops are computed from UTF-8 bytes rather than decoded characters.For example,
é<TAB>Xis counted as length 8 with the current order, but length 9 if decoded before tab expansion. This is mostly hidden for C files because tabs are rejected, but.s/.Sfiles may contain tabs.Since this patch aims to make
line_stats()use character semantics, it should decode before expanding tabs, or makeexpand_tabs()operate on a decoded string.You could implement it like this:
``` @@ -588,8 +589,9 @@ sub line_stats { sub line_stats { my ($line) = @_; - # Drop the diff line leader and expand tabs + # Drop the diff line leader, decode UTF-8, and expand tabs. $line =~ s/^.//; + $line = decode('UTF-8', $line, Encode::FB_DEFAULT); $line = expand_tabs($line); # Pick the indent from the front of the line. ```Quoted 1 line
Quoted 4 lines
scripts/checkpatch.pl sub line_stats { · line 599
Quoted 1 line
scripts/checkpatch.pl +0
-1- return (length($line), length($white));With that change, the original
return (length($line), length($white));can be kept; the extradecode()calls and new return below are not needed.
Thanks a lot, I'll resend.
Signature · 2 lines
Best regards, Vladimir