ANSI C says that about text files:
Data read in from a text stream will necessarily compare equal to
the data that were earlier written out to that stream only if: the
data consist only of printing characters and the control characters
horizontal tab and new-line; no new-line character is immediately
preceded by space characters; and the last character is a new-line
character.
So, there is no garanties that a text file with embedded zeros will be
read correctly, no matter how we implement it.
Just for the record: In my machine, the following program,
local count = 0
for l in io.lines() do
count = count + #l
end
print(count)
reading the Bible, takes ~0.07s with the current implementation and
~0.14s with this proposal.