lineSplitter ignores the the trailing newline?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Nov 10 01:26:10 UTC 2019


On Saturday, November 9, 2019 4:07:29 PM MST Jonathan Marler via 
Digitalmars-d wrote:
> On Saturday, 9 November 2019 at 23:04:11 UTC, Jonathan Marler
>
> wrote:
> > On Saturday, 9 November 2019 at 19:28:52 UTC, Paul Backus wrote:
> >> On Saturday, 9 November 2019 at 19:00:31 UTC, Jonathan Marler
> >>
> >> wrote:
> >>> In people's opinion, should lineSplitter handle files with
> >>> trailing newlines differently or the same?  Currently,
> >>> lineSplitter will ignore the trailing newline.  If it didn't
> >>> ignore it, then I'd expect that a file with a trailing
> >>> newline would return an empty string as its last element, but
> >>> it doesn't do this.
> >>
> >> lineSplitter follows the Unix convention of treating newlines
> >> as line *terminators* rather than line *separators* (as we can
> >> see from the name of its template argument "KeepTerminator").
> >> Under this convention, a trailing newline terminates the final
> >> line in a file, but does not start a new one, so there is no
> >> need for an additional empty line.
> >
> > Interesting, do you have any links and or references that
> > establish that unix uses newlines as terminators rather than
> > separators?
>
> Also, if unix is using it as a "terminator" instead of a
> "separator", if you don't have a trailing newline at the end of
> the file then the last line wouldn't be "terminated" properly, so
> should you ignore the last line?  Should that be an error?
>
> If so, then it sounds like the correct solution for the "tolf"
> tool is to always make sure the last line is terminated with a
> newline character.  Does that sound correct?

Per the POSIX standard, lines are always terminated by a newline.

https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline

Text editors and the like will generally ensure that it's there. Now, I
don't see much reason to treat it as an error if you manage to have a text
file or other block of text that doesn't end with a newline, since reaching
the end of the file or text makes it pretty clear that the line ended. Also,
while POSIX and its utilities may be designed to assume that lines always
end with newlines (including at the end of a file), Windows doesn't make
that same assumption.

- Jonathan M Davis





More information about the Digitalmars-d mailing list