File: cclass.awk

package info (click to toggle)
mawk 1.3.4.20260129-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,244 kB
  • sloc: ansic: 19,998; sh: 4,627; yacc: 1,182; awk: 903; makefile: 301
file content (64 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# $MawkId: cclass.awk,v 1.3 2024/07/24 08:10:05 tom Exp $
# count character-classes

function count_one(s,r,	rc,regex) {
	rc = 0;
	regex = "[[:" r ":]]+";
	while (match(s,regex)) {
		# printf "\t%s: %s\n", regex, substr(s, RSTART, RLENGTH);
		rc += RLENGTH;
		s = substr(s,RSTART + RLENGTH);
	}
	return rc;
}

function count_all(s) {
	total += length(s);
	_alnum += count_one(s, "alnum");
	_alpha += count_one(s, "alpha");
	_blank += count_one(s, "blank");
	_cntrl += count_one(s, "cntrl");
	_digit += count_one(s, "digit");
	_graph += count_one(s, "graph");
	_lower += count_one(s, "lower");
	_print += count_one(s, "print");
	_punct += count_one(s, "punct");
	_space += count_one(s, "space");
	_upper += count_one(s, "upper");
	_xdigit += count_one(s, "xdigit");
}
function report(name,value) {
	printf("%-8s %*d %5.1f%%\n", name ":", width, value, 100 * value / total);
}
BEGIN{
	total = 0;
	_alnum = 0;
}
/./{ count_all($0); }
/$/{ count_all(RS); }
END{
	if (total == 0) {
		for (c = 0; c < 255; ++c) {
			count_all(sprintf("%c", c));
		}
	}
	printf "total: %d\n", total;
	scale = total;
	width = 1;
	while (scale > 1) {
		scale /= 10;
		width++;
	}
	report("alnum", _alnum);
	report("alpha", _alpha);
	report("blank", _blank);
	report("cntrl", _cntrl);
	report("digit", _digit);
	report("graph", _graph);
	report("lower", _lower);
	report("print", _print);
	report("punct", _punct);
	report("space", _space);
	report("upper", _upper);
	report("xdigit", _xdigit);
}