News for hackers. Stuff that matters.
Re:Regex character "$" doesn't mean "end-of-string" by masklinn on Wednesday March 20, 2024 @07:49AM (#39765177)
> It is start and end of line.

You seem to have redefined “line” as “not a line”.

> The confusion

I’m sure redefining “line” as “nothing like what anyone reasonable would interpret as a line” will help a lot and right clear up the confusion.

Re:Regex character "$" doesn't mean "end-of-string" by danbruc on Wednesday March 20, 2024 @07:56AM (#39765247)
The POSIX definition of a line is a sequence of non-newline characters - possibly zero - followed by a newline. Everything that does not end with a newline is not a [complete] line. So strictly speaking it would even be correct that cat$ does not match cat because there is no terminating newline, it should only match cat\n. But as lines missing a terminating newline is a thing, it seems reasonable to be less strict.
Re:Regex character "$" doesn't mean "end-of-string" by masklinn on Wednesday March 20, 2024 @01:21PM (#39769523)
> a line is a sequence of non-newline characters

Works for me.

How do you square that with your assertion that in your invention of "single-line mode" you implicitly define "line" as matching \n\n?

Re:Regex character "$" doesn't mean "end-of-string" by danbruc on Wednesday March 20, 2024 @01:45PM (#39769841)
If you are not in multi-line mode, then a single line is expected and consequently there is at most one newline at the end of the string. You can of course pick an input that violates this, run it against a multi-line string with several newlines in it. cat\n\n will not match cat$ because there is something between cat and the end of the line, it just happens to be a newline but without any special meaning because it is not the last character and you did not say that the input is multi-line.
Re:Regex character "$" doesn't mean "end-of-string" by sltkr on Wednesday March 20, 2024 @03:50PM (#39771227)
Python violates that definition however, by allowing internal newlines in strings. For example /^c[^a]t$/ matches "c\nt\n", but according to POSIX that's not a line.

I suspect the real reason for Python's behavior starts with the early decision to include the terminating newline in the string returned by IOBase.readline().

Python's peculiar choice has some minor advantages: you can distinguish between files that do and don't end with a terminating newline (the latter are invalid according to POSIX, but common in practice, especially on Windows), and you can reconstruct the original file by simply concatenating the line strings, which is occasionally useful.

The downside of this choice is that as a caller you have to deal with strings that may-or-may-not contain a terminating newline character, which is annoying (I often end up calling rstrip() or strip() on every line returned by readline(), just to get rid of the newlines; read().splitlines() is an option too if you don't mind reading the entire file into memory upfront).

My guess is that Python's behavior is just a hack to make re.match() easier to use with readline(), rather than based on any principled belief about what lines are.

Re:Regex character "$" doesn't mean "end-of-string" by danbruc on Wednesday March 20, 2024 @04:33PM (#39771687)
Python's behavior is not a hack, it is the common behavior. $ matches at the end of the string or before the last character if that is a newline, which is logically the same as the end of a single line. But as you said, you can have additional newlines inside of the string which is also the common behavior and not specific to python. Personally I think of this as you just assume that the string is a single line and match $ accordingly, either at the end of the string or before a terminating newline, if there are additional newlines, you treat them mostly as normal characters, with the exception of dot not matching newlines unless you set the single-line/dot-all flag.
Re:Regex character "$" doesn't mean "end-of-string" by sltkr on Wednesday March 20, 2024 @06:55PM (#39773013)
> Python's behavior [..] is the common behavior.

The very post we're commenting on shows that that's not true: PHP, Python, Java and .NET (C#) share one behavior (accept "\n" as "$"), and ECMAScript (Javascript), Golang, and Rust share another behavior (do not accept "\n" as $).

Let's not argue about which is “the most common”; all of these languages are sufficiently common to say that there is no single common behavior.

> $ matches at the end of the string or before the last character if that is a newline, which is logically the same as the end of a single line.

Yes, that is Python's behavior (and PHP's, Java's, etc.). You're just describing it; not motivating why it has to work that way or why it's more correct than the obvious alternative of only matching the end of the string.

Subjectively, I find it odd that /^cat$/ matches not just the obvious string "cat" but also the string "cat\n". And I think historically, it didn't. I tried several common tools that predate Python:

  - awk 'BEGIN { print ("cat\n" ~ /^cat$/) }' prints 0
  - in GNU ed, /^M/ does not match any lines
  - in vim, /^M/ does not match any lines
  - sed -n '/\n/p' does not print any lines
  - grep -P '\n' does not match any lines
  - (I wanted to try `grep -E` too but I don't know how to escape a newline)
  - perl -e 'print ("cat\n" =~ /^cat$/)' prints 1
So the consensus seems to be that the classic UNIX line-based tools match the regex against the line excluding the newline terminator (which makes sense since it isn't part of the content of that line) and therefore $ only needs to match the end of the string.

The odd one out is Perl: it seems to have introduced the idea that $ can match a newline at the end of the string, probably for similar reasons as Python. All of this suggests to me that allowing $ to match both "\n" and "" at the end of the string was a hack designed to make it easier to deal with strings without control characters and string that end with a single newline.

Re:Regex character "$" doesn't mean "end-of-string" by Izkata on Wednesday March 20, 2024 @07:36PM (#39773317)
> - in vim, /^M/ does not match any lines

But /\n/ does

Re:Regex character "$" doesn't mean "end-of-string" by sltkr on Wednesday March 20, 2024 @11:01PM (#39774533)
Thanks for the correction! That's interesting.
Re:Regex character "$" doesn't mean "end-of-string" by danbruc on Thursday March 21, 2024 @10:15AM (#39778771)
So the consensus seems to be that the classic UNIX line-based tools match the regex against the line excluding the newline terminator (which makes sense since it isn't part of the content of that line) and therefore $ only needs to match the end of the string.

If you read a line, you usually remove the newline at the end but you could also keep it as Python does. If you remove the newline, then a line can never contain a newline, the case cat\n can never occur. If you keep the newline, there will be exactly one newline as the last character and you arguably want cat$ to match cat\n because that newline is the end of the line but not part of the content. It makes perfect sense that $ matches at the end of the string or before a newline as the last character as it will do the right thing whether or not you strip the newline.

If you want cat$ to not match cat\n, then you are obviously not dealing with lines, you have a string with a newline at the end but you consider this newline part of the content instead of terminating the line. But ^ and $ are made for lines, so they do not work as expected. I also get what people are complaining about, if you are not in multi-line and have a proper line with at most one newline at the end, then it will behave exactly as if you are in multi-line which raises the question why you would have those two modes to begin with. Not multi-line only behaves differently if you have additional newlines or one newline not at the end, that is if you do not have a proper line, so why should $ still behave as if you were dealing with a line?

Re:Regex character "$" doesn't mean "end-of-string" by al_borland on Thursday March 21, 2024 @02:43AM (#39775488)
I suppose this is why certain config files are strict about ending with a newline, without it, the last line wouldn’t technically be a line?
Re:Regex character "$" doesn't mean "end-of-string" by zeehio on Thursday March 21, 2024 @03:03AM (#39775561)
I'm now amused by the idea of a malicious compliance linter telling me "you have an issue at your code, on line NaN"
Re:Regex character "$" doesn't mean "end-of-string" by Bjartr on Wednesday March 20, 2024 @08:39AM (#39765595)
The line delimiter is a newline.

If you have a file containing `A\nB\nC` in a file, the file is three lines long.

I guess it could be argued that a file containing `A\nB\nC\n` has four lines, with the fourth having zero length.

That a regex is applying to an in memory string vs a file doesn't feel to me like it should have different semantics.

Digging into the history a little, it looks like regexes were popularized in text editors and other file oriented tooling. In those contexts I imagine it would be far more common to want to discard or ignore the trailing zero length line than to process it like every other line in a file.

Re:Regex character "$" doesn't mean "end-of-string" by akdev1l on Wednesday March 20, 2024 @08:46AM (#39765671)
Technically the “newline” character is actually a line _terminator_. Hence “A\n” is one line, not two. The “\n” is always at the end of a line by definition.
Re:Regex character "$" doesn't mean "end-of-string" by rerdavies on Wednesday March 20, 2024 @11:40AM (#39768136)
Technically, that is one of two possible interpretations, and you seem to have invented a "by definition" out of thin air.

Very very technically a "newline" character indicates the start of a new line, which is why it is not called the "end-of-line" character.

Re:Regex character "$" doesn't mean "end-of-string" by cortesoft on Wednesday March 20, 2024 @12:01PM (#39768405)
I mean, the person you are responding to didn't invent the definition out of thin air... the POSIX standard did:

3.206 Line A sequence of zero or more non- <newline> characters plus a terminating <newline> character.

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition...

Re:Regex character "$" doesn't mean "end-of-string" by nomel on Wednesday March 20, 2024 @12:44PM (#39769052)
Posix getline() includes EOF as a line terminator:

    getline() reads an entire line from stream, storing the address
       of the buffer containing the text into *lineptr.  The buffer is
       null-terminated and includes the newline character, if one was
       found.
    ...
    ... a delimiter character is not added if one was
       not present in the input before end of file was reached.
EOF seems same as end-of-string.
Re:Regex character "$" doesn't mean "end-of-string" by lsaferite on Friday March 22, 2024 @11:55AM (#39791923)
Your quoted documentation says otherwise. It says that a 'line' include the delimiter, '\n', in the line buffer. It also says that is no delimiter is found before the EOF is reached that the line buffer will not include the delimiter. That means the line buffer can clearly indicate an incomplete line by the absence of the delimiter. To be clear, EOF isn't a 'line terminator', it's the end of the data stream.
Re:Regex character "$" doesn't mean "end-of-string" by nomel on Friday March 22, 2024 @06:24PM (#39795489)
Yes, "EOF seems same as end-of-string."
Re:Regex character "$" doesn't mean "end-of-string" by pepa65 on Friday March 29, 2024 @02:58AM (#39861359)
No, getline() will stop reading at the newline, even if more (non-NUL) characters follow. EOF is end-of-file.
Re:Regex character "$" doesn't mean "end-of-string" by mabster on Wednesday March 20, 2024 @06:02PM (#39772531)
I don't know why no-one here sees this as a bad design...

If a line is missing a newline then we just disregard it?!

A way better way to deal with newline is it's a separator like comma. And like in modern languages we allow a final separator, but ignore it so that is easier for tools to generate files.

Now all combinations of characters, including newline characters, has an interpretation without dropping anything.

Re:Regex character "$" doesn't mean "end-of-string" by danbruc on Thursday March 21, 2024 @01:09PM (#39781390)
I also always preferred the interpretation of a newline as a separator instead of as a terminator for files because I never liked the final newline causing a new empty line in the editor and as you thought that it was bad design that you can have a somewhat invalid file.

But if you look beyond files, the interpretation as a terminator also makes perfect sense, when you receive text over a serial connection it signals that the line is complete which does not necessarily imply that another line will follow. The same in a file, if the terminating newline is missing, you can deduce that an incomplete write occurred and some data might be missing. If you decide to have a newline as a separator after the last line but to ignore it, then you can not represent an empty last line.

I guess you would need two different characters, one terminator and one separator. You could start a line with \n and end it with \r. The \n separates the line from the one before, then \r terminates the line and marks it as complete. You would get \r\n between lines as on Windows and the last line would only have \r if complete or would otherwise count as incomplete. Then again you could almost get the same thing with \n only, you would just have to change the interpretation, instead of \n giving you a line and no \n giving you not a line, you would have to say that \n gives you a complete line and no \n gives you an incomplete line. With that you could however not have an incomplete empty line.

Re:Regex character "$" doesn't mean "end-of-string" by pepa65 on Friday March 29, 2024 @02:56AM (#39861346)
This effort of building in redundancy is pointless. We just need a newline to know where to start the output on a new line. If you want to safeguard the proper content of a file, a whole lot more is needed.
Re:Regex character "$" doesn't mean "end-of-string" by pepa65 on Friday March 29, 2024 @02:45AM (#39861311)
How about a null-byte then? That's not a newline character, but all POSIX tools will treat it as EOF.
Re:Regex character "$" doesn't mean "end-of-string" by pepa65 on Friday March 29, 2024 @02:52AM (#39861337)
So this is what "3.403 Text File" says:

A file that contains characters organized into zero or more lines [so characters with no newlines are OK]

No NUL, and lines (delimited by and including newline) not exceeding LINE_MAX bytes.

Re:Regex character "$" doesn't mean "end-of-string" by LK5ZJwMwgBbHuVI on Wednesday March 20, 2024 @01:39PM (#39769761)
It doesn't indicate the start of a new line, or files would start with it. Files end with it, which is why it is a line terminator. And it is by definition: by the standard, by the way cat and/or your shell and/or your terminal work together, and by the way standard utilities like `wc` treat the file.
Re:Regex character "$" doesn't mean "end-of-string" by wtetzner on Wednesday March 20, 2024 @09:03AM (#39765853)
So if you have "A" in a file with no newline, there are no lines in that file?
Re:Regex character "$" doesn't mean "end-of-string" by jepler on Wednesday March 20, 2024 @09:16AM (#39765999)
Yes, that is a file with zero lines that ends with an "incomplete line". Processing of such files by standard line-oriented utilities is undefined in the opengroup spec. So, for instance, the effect of "grep"ping such a file is not defined. Heck, even "cat"ting such a file gives non-ideal results, such as colliding with the regular shell prompt. For this reason, a lot of software projects I work on check and correct this condition whenever creating a commit.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1... ("text file")

Re:Regex character "$" doesn't mean "end-of-string" by rerdavies on Wednesday March 20, 2024 @11:41AM (#39768159)
The opengroup spec says no such thing.
Re:Regex character "$" doesn't mean "end-of-string" by simonh on Wednesday March 20, 2024 @12:35PM (#39768922)
3.206 Line

A sequence of zero or more non- <newline> characters plus a terminating <newline> character.

See also ‘3.403 Text File’ for the definition of a text file. No new line characters, no lines. No lines, not a text file.

Re:Regex character "$" doesn't mean "end-of-string" by wtetzner on Friday March 22, 2024 @09:56AM (#39790819)
> No lines, not a text file.

That seems like a broken (maybe just bad?) definition/specification to me. A blob of JSON in a file isn't "text" if there's no newline character trailing it?

Re:Regex character "$" doesn't mean "end-of-string" by simonh on Saturday March 23, 2024 @05:42PM (#39803294)
There are other definitions of a text file than the opengroup spec, particularly for specific OS platforms. I’m not sure what convention JSON follows.

As a spec it’s fine. It defines a text file in such a way that you can easily write code to process such a file deterministicaly.

Re:Regex character "$" doesn't mean "end-of-string" by rovr138 on Wednesday March 20, 2024 @09:31AM (#39766213)
> Yes, that is a file with zero lines that ends with an "incomplete line".

It's a file with zero complete lines. But it has 1 line, that's incomplete, right?

The file starts empty. Anything in it starts "a line". So it's 1 incomplete line.

I hate weird states.

Re:Regex character "$" doesn't mean "end-of-string" by xyzzy_plugh on Wednesday March 20, 2024 @09:56AM (#39766586)
No, it is valid for a file to have content but no lines.

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.

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.
Re:Regex character "$" doesn't mean "end-of-string" by nativeit on Wednesday March 20, 2024 @02:47PM (#39770498)
I get this is largely a semantic debate, but find it a little ironic so many programmers seem put off with the idea of a line count that starts at “0”.
Re:Regex character "$" doesn't mean "end-of-string" by DougBTX on Wednesday March 20, 2024 @11:54AM (#39768311)
Another way to look at it is that concatenating files should sum the line count. Concatenating two empty files produces an empty file, so 0 + 0 = 0. If “incomplete lines” are not counted as lines, then the maths still works out. If they counted as lines, it would end up as 1 + 1 = 1.
Re:Regex character "$" doesn't mean "end-of-string" by akdev1l on Wednesday March 20, 2024 @09:58AM (#39766614)
No, a line is defined as a sequence of characters (bytes?) with a line terminator at the end.

Technically as per posix a file as you describe is actually a binary file without any lines. Basically just random binary data that happens to kind of look like a line.

Re:Regex character "$" doesn't mean "end-of-string" by coryrc on Wednesday March 20, 2024 @12:19PM (#39768671)
Pedantically, if it doesn't end with a newline, it's considered a binary file and not a text file. Binary files don't have lines.

In practice, most utilities expecting text files will still operate on it.

Re:Regex character "$" doesn't mean "end-of-string" by PaulDavisThe1st on Wednesday March 20, 2024 @01:41PM (#39769787)
No file has lines.

"Lines" are a convention established by (or not) software reading a data stream.

Re:Regex character "$" doesn't mean "end-of-string" by coryrc on Wednesday March 20, 2024 @03:16PM (#39770824)
Ackshully
Re:Regex character "$" doesn't mean "end-of-string" by wtetzner on Friday March 22, 2024 @09:54AM (#39790806)
That's a weird way to look at it. Binary files might not have "lines", but there's no reason they couldn't include a byte with value 10 (the ASCII value for \n). Software reading that file wouldn't know the difference, right?

Also, why couldn't you have a text file without any lines?

Re:Regex character "$" doesn't mean "end-of-string" by coryrc on Friday March 22, 2024 @06:35PM (#39795568)
All I'm addressing is GP's comment:

    It's a file with zero complete lines. But it has 1 line, that's incomplete, right?
Because the Unix definition of text file requires the file to end with a newline. "Lines" only exist in the context of text files. If there's no terminating newline, it's (pedantically) not a text file and so has no lines. Now, in practice, if you open() that file in text mode, it doesn't TMK return an error if the terminating newline isn't present, but it's undefined behaviour.

And if you do have a terminating newline, then you have at least one line :).

Re:Regex character "$" doesn't mean "end-of-string" by mort96 on Wednesday March 20, 2024 @11:36AM (#39768082)
It's a file with 0 lines and some trailing garbage.
Re:Regex character "$" doesn't mean "end-of-string" by pxc on Saturday March 23, 2024 @03:38PM (#39802562)
Here's another way to think about this:

This isn't a weird state. It's a language problem. An 'incomplete line' isn't a type of line, it's an unfortunate name for a thing that is not a line. Just like how the 'wor' is an incomplete word (the word 'word'), but 'wor' is, of course, not a word.

Same thing for formalisms like equations in algebra or formulas in propositional logic— we have the phrase 'well-formed formula', and we might describe some sequences of terms as 'incomplete formulas' or perhaps 'ill-formed formulas', but those phrases don't describe anything that meets the formal system's definition of 'formula' at all— they are not formulas. 'Ill-formed formula' is not a compositional phrase where 'ill-formed' describes a feature of a 'formula'. It's a bit of convenient language for what we can intuitively or metaphorically recognize as a formula-ish thing.

Re:Regex character "$" doesn't mean "end-of-string" by mbrubeck on Wednesday March 20, 2024 @10:27AM (#39767072)

    $ echo -n "A" | wc --lines
    0
Re:Regex character "$" doesn't mean "end-of-string" by keybored on Wednesday March 20, 2024 @12:09PM (#39768526)
Yep. since wc(1) apparently strictly adheres to what a newline-terminated text file is. This is why plaintext files should end with a newline. :)

See: https://stackoverflow.com/a/25322168/1725151

Re:Regex character "$" doesn't mean "end-of-string" by LK5ZJwMwgBbHuVI on Wednesday March 20, 2024 @01:33PM (#39769691)
Why don't you go ask?

    $ echo -n foo | wc -l
    0
Re:Regex character "$" doesn't mean "end-of-string" by wtetzner on Friday March 22, 2024 @09:57AM (#39790831)
wc just counts newline characters. I'm not sure why it would be the ultimate authority on anything.
Re:Regex character "$" doesn't mean "end-of-string" by Gormo on Wednesday March 20, 2024 @10:11AM (#39766830)
Suddenly the DOS/Windows solution of using \r\n instead of just \n seems to offer some advantages.
Re:Regex character "$" doesn't mean "end-of-string" by deaddodo on Wednesday March 20, 2024 @12:14PM (#39768590)
The "Windows way" is the "right way" for a few reasons.

This is definitely not one of them.

Re:Regex character "$" doesn't mean "end-of-string" by int_19h on Wednesday March 20, 2024 @06:15PM (#39772647)
Which are the valid reasons, legacy meanings of those characters aside?
Re:Regex character "$" doesn't mean "end-of-string" by deaddodo on Saturday March 23, 2024 @08:37PM (#39804126)
I mean, it was what everyone had agreed upon previously. Microsoft was the only party to follow through. For all the guff they get for not following standards, it was the one standard they did.

You don't have to love a company to acknowledge they did something right.

Re:Regex character "$" doesn't mean "end-of-string" by samatman on Wednesday March 20, 2024 @10:29AM (#39767104)
This does precisely nothing to solve the ambiguity issue when a final line lacks a newline. The representation of that newline isn't relevant to the problem.
Re:Regex character "$" doesn't mean "end-of-string" by Izkata on Wednesday March 20, 2024 @01:09PM (#39769371)
It's actually slightly worse: Windows defines newline as a delimiter, not a terminator. So this:

  foo\nbar\n
Would be 2 lines in *nix and 3 lines in windows.
Re:Regex character "$" doesn't mean "end-of-string" by Gormo on Monday March 25, 2024 @08:45AM (#39815623)
The point is that having a sequence of two delimiters to signal the end of the logical line allows you to have single instances of either delimiter included within the text. This allows visual line breaks to be included within the same line as understood by the regex parser.
Re:Regex character "$" doesn't mean "end-of-string" by danbruc on Thursday March 21, 2024 @10:56AM (#39779270)
Despite the downvotes your comment received, I think you have a good point. There are two uses for a newline, first to signal the end of a line, for example when sending text over a serial connection, and second to separate two lines, for example in a text file.

To indicate that a serially received line is complete, the interpretation as a terminator makes perfect sense - abcd\n is a complete line, abc is a still incomplete line. In a text file the interpretation as a separator might be preferable because that gets rid of the issue of the last line not having a newline - a\nb\nc are three lines separated by two newlines, a\nb\nc\n are four lines separated by three newlines and the last line is empty.

But then it might also be useful to have a terminator in a text file to be able to detect an incompletely written line. So using two characters, one for each purpose, could solve the problem. \r means the line is complete, \n means it follows a next line. abc is an incomplete line, abcd\r is a complete line and no line follows, abcd\r\n is a complete line and a second incomplete line follows which is currently empty. abcd\r\n\r are two complete lines, the second one empty. abcd\r\nefg is a complete line followed by an incomplete line. abcd\r\nefg\r are two complete lines. You could even have two incomplete lines abc\nefg.

But I think Windows always uses \r\n because this is how you get to a newline on a typewriter or really old printer, you return the carriage and feed the paper one line. I do not think that they had the idea of differentiating between terminator and separator, otherwise you could have only \r and maybe even only \n sometimes. But in principle this could work quite nicely, I guess. You could start a line with \n and end it with \r, this would give you \r\n between lines and \r after the final line. Or nothing if the final line is incomplete or \r\n if the final line is incomplete and currently empty. The odd thing would be a newline as the very first character, maybe one could suppress that. This would also be compatible with Windows and nix, it would just consider all nix lines incomplete. Only abc\rdef\r would not really make sense, two complete lines but the second one is not a new line.

If I ever get to write a new operating system, I will inflict this on humanity.

Re:Regex character "$" doesn't mean "end-of-string" by joshjje on Wednesday March 20, 2024 @12:41PM (#39769017)
“A\n” is two lines.
Re:Regex character "$" doesn't mean "end-of-string" by LK5ZJwMwgBbHuVI on Wednesday March 20, 2024 @01:40PM (#39769767)
Factually incorrect.