mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 17:33:38 +00:00
Add tools to find too-long lines.
tools/line_len_stats:
A new script that computes and prints a histogram of the line lengths
in the input given to it.
tools/find_long_lines:
A new script that, when given a line length and a list of filenames,
prints out the location of the lines in those files that are longer
than the given line length.
This commit is contained in:
41
tools/find_long_lines
Executable file
41
tools/find_long_lines
Executable file
@@ -0,0 +1,41 @@
|
||||
#!/bin/sh
|
||||
# vim: ft=sh ts=4 sw=4 et
|
||||
#
|
||||
# This script, when invoked with a number (representing a line length)
|
||||
# and a list of filenames, will look for lines longer than the given length
|
||||
# in each of the listed files. When it finds such a line, it prints
|
||||
#
|
||||
# - the name of the file,
|
||||
# - line number within that file, and
|
||||
# - the length of that line.
|
||||
#
|
||||
|
||||
case "$#" in
|
||||
0)
|
||||
echo "usage: find_long_lines limit_length filename1 ..."
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
LIMIT_LEN="$1"
|
||||
export LIMIT_LEN
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
|
||||
awk "
|
||||
BEGIN {
|
||||
limit_len = ${LIMIT_LEN};
|
||||
CUR_FILENAME = \"\";
|
||||
line_number = 0;
|
||||
}
|
||||
{
|
||||
if (FILENAME != CUR_FILENAME) {
|
||||
CUR_FILENAME = FILENAME;
|
||||
line_number = 0;
|
||||
}
|
||||
++line_number;
|
||||
len = length(\$0);
|
||||
if (len > limit_len + 0) {
|
||||
printf \"%s:%d: %d\n\", FILENAME, line_number, len;
|
||||
}
|
||||
}" "$@"
|
||||
27
tools/line_len_stats
Executable file
27
tools/line_len_stats
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/bin/sh
|
||||
# vim: ft=sh ts=4 sw=4 et
|
||||
#
|
||||
# Generate a histogram of the line lengths in the input, whether that input
|
||||
# comes from stdin or from a list of filenames on the command line.
|
||||
#
|
||||
# Usage: line_len_stats [filename ...]
|
||||
#
|
||||
|
||||
awk '
|
||||
START {
|
||||
maxlen = 0;
|
||||
}
|
||||
{
|
||||
len = length($0);
|
||||
count[len] += 1;
|
||||
if (len > maxlen) {
|
||||
maxlen = len;
|
||||
}
|
||||
}
|
||||
END {
|
||||
for (len = 1; len <= maxlen; len++) {
|
||||
if (count[len] != 0) {
|
||||
printf "len %4d: %d\n", len, count[len];
|
||||
}
|
||||
}
|
||||
}' "$@"
|
||||
Reference in New Issue
Block a user