Character, Word and Line Count for Current File

, , , Thursday, March 19, 2009 3 comments

// command wc(): Display on the message line,
// the total numbers of characters, words and
// lines
in the current file.
//
// This ubiquitous procedure
has been done ad
// infinitum by many developers. It's almost
// as common
as Hello World! I don't presume
// to hold any claim to it.
//
// On a large file of several thousand lines
// or more, it can take a few seconds to
// display its results

_command wc() {
cc = 0; // character count
wc = 0; // word count
lc = 0; // line count
item = 0;
_save_pos2(p);
top_of_buffer();
for (;;) {
get_line line;
lc++;
for (i = 1; i < length(line); i++) {
cc++;
if (isalnum(substr(line,i,1))) {
item = 1;
} else if (item && !isalnum(substr(line,i,1))) {
wc++;
item = 0;
}
}
status=down();
if (status == BOTTOM_OF_FILE_RC) {
message("chars: " cc " / words: " wc " / lines: " lc);
break;
}
}
_restore_pos2(p);
}

GlossyBlue Blogger by Black Quanta. Theme & Icons by N.Design Studio
Entries RSS Comments RSS