Re:Regex character "$" doesn't mean "end-of-string"
by joshjje on Wednesday March 20, 2024 @12:49PM
(#39769103)
Thats beyond ridiculous. Most languages when you are reading a line from a file, and it doesn't have a \n terminator, its going to give you that line, not say, oops, this isn't a line sorry.
Re:Regex character "$" doesn't mean "end-of-string"
by LK5ZJwMwgBbHuVI on Wednesday March 20, 2024 @01:36PM
(#39769725)
That's a relatively recent invention compared to tools like `wc` (or your favorite `sh` for that matter). See also: https://perldoc.perl.org/functions/chop wherein the norm was "just cut off the last character of the line, it will always be a newline"
Re:Regex character "$" doesn't mean "end-of-string"
by squeaky-clean on Wednesday March 20, 2024 @02:35PM
(#39770372)
Most languages but not all. I've even been bit by this recently in cron.
Assuming that EOF is identical to \\nEOF will end up causing trouble for you one day, because it's not actually identical.
Re:Regex character "$" doesn't mean "end-of-string"
by int_19h on Wednesday March 20, 2024 @06:14PM
(#39772630)
I don't think you can meaningfully generalize to "most languages" here. To give an example, two extremely popular languages are C and Python. Both have a standard library function to read a line from a text stream - fgets() for C, readline() for Python. In both cases, the behavior is to read up to and including the newline character, but also to stop if EOF is encountered before then. Which means that the return value is different for terminated vs unterminated final lines in both languages - in particular, if there's no \n before EOF, the value returned is not a line (as it does not end with a newline), and you have to explicitly write your code to accommodate that.
Semantically many libraries treat that as a line because while \n<EOF> means "the end of the last line" having just <EOF> adds additional complexity the user has to handle to read the remaining input. But by the book it's not "a line".
If I said "ten buckets of water" does that mean ten full buckets? Or does a bucket with a drop in it count as "a bucket of water?" If I asked for ten buckets of water and you brought me nine and one half-full, is that acceptable? What about ten half-full buckets?
A line ends in a newline. A file with no newlines in it has no lines.