mirror of
https://github.com/9fans/plan9port.git
synced 2025-01-12 11:10:07 +00:00
wc: 4-byte utf-8 capable (Erik Quanstrom)
R=rsc http://codereview.appspot.com/3437044
This commit is contained in:
parent
74dd032175
commit
02d8e8247f
1 changed files with 59 additions and 16 deletions
75
src/cmd/wc.c
75
src/cmd/wc.c
|
@ -97,12 +97,13 @@ report(uvlong nline, uvlong nword, uvlong nrune, uvlong nbadr, uvlong nchar, cha
|
|||
* following table. If we're not in statesp or statewd when done, the
|
||||
* file ends with a partial rune.
|
||||
* | character
|
||||
* state |09,20| 0a |00-7f|80-bf|c0-df|e0-ef|f0-ff
|
||||
* -------+-----+-----+-----+-----+-----+-----+-----
|
||||
* statesp|ASP |ASPN |AWDW |AWDWX|AC2W |AC3W |AWDWX
|
||||
* statewd|ASP |ASPN |AWD |AWDX |AC2 |AC3 |AWDX
|
||||
* statec2|ASPX |ASPNX|AWDX |AWDR |AC2X |AC3X |AWDX
|
||||
* statec3|ASPX |ASPNX|AWDX |AC2R |AC2X |AC3X |AWDX
|
||||
* state |09,20| 0a |00-7f|80-bf|c0-df|e0-ef|f0-f7|f8-ff
|
||||
* -------+-----+-----+-----+-----+-----+-----+-----+-----
|
||||
* statesp|ASP |ASPN |AWDW |AWDWX|AC2W |AC3W |AC4W |AWDWX
|
||||
* statewd|ASP |ASPN |AWD |AWDX |AC2 |AC3 |AC4 |AWDX
|
||||
* statec2|ASPX |ASPNX|AWDX |AWDR |AC2X |AC3X |AC4X |AWDX
|
||||
* statec3|ASPX |ASPNX|AWDX |AC2R |AC2X |AC3X |AC4X |AWDX
|
||||
* statec4|ASPX |ASPNX|AWDX |AC3R |AC2X |AC3X |AC4X |AWDX f4 8f bf bf
|
||||
*/
|
||||
enum{ /* actions */
|
||||
AC2, /* enter statec2 */
|
||||
|
@ -110,8 +111,12 @@ enum{ /* actions */
|
|||
AC2W, /* enter statec2, count a word */
|
||||
AC2X, /* enter statec2, count a bad rune */
|
||||
AC3, /* enter statec3 */
|
||||
AC3R, /* enter statec3, don't count a rune */
|
||||
AC3W, /* enter statec3, count a word */
|
||||
AC3X, /* enter statec3, count a bad rune */
|
||||
AC4, /* enter statec4 */
|
||||
AC4W, /* enter statec4, count a word */
|
||||
AC4X, /* enter statec4, count a bad rune */
|
||||
ASP, /* enter statesp */
|
||||
ASPN, /* enter statesp, count a newline */
|
||||
ASPNX, /* enter statesp, count a newline, count a bad rune */
|
||||
|
@ -120,7 +125,7 @@ enum{ /* actions */
|
|||
AWDR, /* enter statewd, don't count a rune */
|
||||
AWDW, /* enter statewd, count a word */
|
||||
AWDWX, /* enter statewd, count a word, count a bad rune */
|
||||
AWDX /* enter statewd, count a bad rune */
|
||||
AWDX, /* enter statewd, count a bad rune */
|
||||
};
|
||||
uchar statesp[256]={ /* looking for the start of a word */
|
||||
AWDW, AWDW, AWDW, AWDW, AWDW, AWDW, AWDW, AWDW, /* 00-07 */
|
||||
|
@ -153,8 +158,8 @@ AC2W, AC2W, AC2W, AC2W, AC2W, AC2W, AC2W, AC2W, /* d0-d7 */
|
|||
AC2W, AC2W, AC2W, AC2W, AC2W, AC2W, AC2W, AC2W, /* d8-df */
|
||||
AC3W, AC3W, AC3W, AC3W, AC3W, AC3W, AC3W, AC3W, /* e0-e7 */
|
||||
AC3W, AC3W, AC3W, AC3W, AC3W, AC3W, AC3W, AC3W, /* e8-ef */
|
||||
AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,/* f0-f7 */
|
||||
AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX/* f8-ff */
|
||||
AC4W, AC4W, AC4W, AC4W, AC4W, AC4W, AC4W, AC4W, /* f0-f7 */
|
||||
AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,AWDWX,/* f8-ff */
|
||||
};
|
||||
uchar statewd[256]={ /* looking for the next character in a word */
|
||||
AWD, AWD, AWD, AWD, AWD, AWD, AWD, AWD, /* 00-07 */
|
||||
|
@ -187,8 +192,8 @@ AC2, AC2, AC2, AC2, AC2, AC2, AC2, AC2, /* d0-d7 */
|
|||
AC2, AC2, AC2, AC2, AC2, AC2, AC2, AC2, /* d8-df */
|
||||
AC3, AC3, AC3, AC3, AC3, AC3, AC3, AC3, /* e0-e7 */
|
||||
AC3, AC3, AC3, AC3, AC3, AC3, AC3, AC3, /* e8-ef */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* f0-f7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX /* f8-ff */
|
||||
AC4, AC4, AC4, AC4, AC4, AC4, AC4, AC4, /* f0-f7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* f8-ff */
|
||||
};
|
||||
uchar statec2[256]={ /* looking for 10xxxxxx to complete a rune */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 00-07 */
|
||||
|
@ -221,8 +226,8 @@ AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, /* d0-d7 */
|
|||
AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, /* d8-df */
|
||||
AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, /* e0-e7 */
|
||||
AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, /* e8-ef */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* f0-f7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX /* f8-ff */
|
||||
AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, /* f0-f7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* f8-ff */
|
||||
};
|
||||
uchar statec3[256]={ /* looking for 10xxxxxx,10xxxxxx to complete a rune */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 00-07 */
|
||||
|
@ -255,8 +260,42 @@ AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, /* d0-d7 */
|
|||
AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, /* d8-df */
|
||||
AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, /* e0-e7 */
|
||||
AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, /* e8-ef */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* f0-f7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX /* f8-ff */
|
||||
AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, /* f0-f7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* f8-ff */
|
||||
};
|
||||
uchar statec4[256]={ /* looking for 10xxxxxx,10xxxxxx,10xxxxxx to complete a rune */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 00-07 */
|
||||
AWDX, ASPX, ASPNX,AWDX, AWDX, AWDX, AWDX, AWDX, /* 08-0f */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 10-17 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 18-1f */
|
||||
ASPX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 20-27 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 28-2f */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 30-37 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 38-3f */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 40-47 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 48-4f */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 50-57 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 58-5f */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 60-67 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 68-6f */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 70-77 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 78-7f */
|
||||
AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, /* 80-87 */
|
||||
AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, /* 88-8f */
|
||||
AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, AC3R, /* 90-97 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* 98-9f */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* a0-a7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* a8-af */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* b0-b7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* b8-bf */
|
||||
AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, /* c0-c7 */
|
||||
AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, /* c8-cf */
|
||||
AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, /* d0-d7 */
|
||||
AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, AC2X, /* d8-df */
|
||||
AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, /* e0-e7 */
|
||||
AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, AC3X, /* e8-ef */
|
||||
AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, AC4X, /* f0-f7 */
|
||||
AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, AWDX, /* f8-ff */
|
||||
};
|
||||
void
|
||||
count(int f, char *name)
|
||||
|
@ -287,8 +326,12 @@ count(int f, char *name)
|
|||
case AC2W: state=statec2; nword++; break;
|
||||
case AC2X: state=statec2; nbadr++; break;
|
||||
case AC3: state=statec3; break;
|
||||
case AC3R: state=statec3; --nrune; break;
|
||||
case AC3W: state=statec3; nword++; break;
|
||||
case AC3X: state=statec3; nbadr++; break;
|
||||
case AC4: state=statec4; break;
|
||||
case AC4W: state=statec4; nword++; break;
|
||||
case AC4X: state=statec4; nbadr++; break;
|
||||
case ASP: state=statesp; break;
|
||||
case ASPN: state=statesp; nline++; break;
|
||||
case ASPNX: state=statesp; nline++; nbadr++; break;
|
||||
|
@ -304,6 +347,6 @@ count(int f, char *name)
|
|||
if(state!=statesp && state!=statewd)
|
||||
nbadr++;
|
||||
if(n<0)
|
||||
perror(name);
|
||||
fprint(2, "%s: %r\n", name);
|
||||
report(nline, nword, nrune, nbadr, nchar, name);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue