Fix line count function.

Simplify line count function by using string.gsub instead of LPeg to count newlines.
Escape control characters in error messages.