git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: CRLF, LF ... CR ?

From
JBJens Bauer <jens-lists@gpio.dk>
Date
Sep 27, 2012, 13:42 UTC
Message-ID
<20120927154230865453.d374a3bc@gpio.dk>
In-Reply-To
<7vvcf0rnw9.fsf@alter.siamese.dyndns.org>
Hi Junio and David.

Rule is in fact quite simple. If it's a text-file and it contains a LF, a CRLF or a CR, then that's a line-break. :) -So everywhere a LF is checked for, a CR should most likely be checked for. Usually, when checking for CRLF, one is looking for the LF. If a CR precedes the LF, the CR is discarded. It's done this way, in case we have a small buffer (say 100 bytes), and we read the file only 100 bytes each time. If we searched for CR instead, we can't check the next character without a lot of clumsy or slow coding. -But even today, I don't believe that a line would exceed 2000 characters; although, one could have a 16K buffer, then *if* the last character in that buffer is a CR, read ahead (just a single byte read would be acceptable in that case).

Unfortunately, it seems that it's not only old Mac OS users that have the problem: <http://stackoverflow.com/questions/10491564/git-and-cr-vs-lf-but-not-crlf>

...That's a Windows user, who seem to be quite lazy - but if he already has a huge repository filled with CR, I do understand why he don't want to change things. Here, adding scanning for CR later, can still solve his problem, because the files are not modified.

But on Mac OS X, there are also problems. If an application was written long ago (for Mac OS 9 or Carbon), it might still use CR instead of LF, if it's ported.

Anyway, Linus is right about not just popping code in, because someone, somewhere needs it for a single use - even if that is me. ;) It's not leading to any kind of crashes if there's no scan for CR. But on the other hand, it saves the community from FAQ about how to get it working, and it's always an advantage not modifying files more than necessary, when dealing with a VCS/SCM.

The safest way is to hunt for the LF, remembering the previous character. The following is just some untested code I wrote as an example on how it could approximately be done - it does not have to use this many lines. This is a buffer-based example:

#define LF 0x0a #define CR 0x0d

	const char	*buffer;		/* buffer containing entire text file */
	const char	*b;				/* beginning of line */
	const char	*s;				/* source */
	const char	*e;				/* end pointer */
	char		c;
	char		last;
	const char	*l;				/* pointer to last character */
	s = buffer;
	e = &s[length];
	b = s;
	c = 0;
	while(s < e)
	{
		last = c;
		c = *s++;
		if(LF == c)				/* deal with Linux, UNIX and DOS */
		{
			b = l;
			// we have a linefeed -> new line.
			l = s;
			if(CR == last)
			{
				l = s - 1;
			}
			/* line contents are from b to l */
		}
		else if(CR == last)		/* deal with old Mac OS and other weirdos. ;) */
		{
			b = l;
			l = s - 1;
			/* line contents are from b to l */
		}
	}
-As written above, it can be optimized. There's a small bug though; it doesn't scan if the very last character in the file is a CR.

Love Jens

On Wed, 26 Sep 2012 23:16:38 -0700, Junio C Hamano wrote:
Show 12 quoted lines
> David Aguilar <davvid@gmail.com> writes:
> 
>> That said, perhaps the "autocrlf" code is simple enough that it
>> could be easily tweaked to also handle this special case,...
> 
> I wouldn't be surprised if it is quite simple.
> 
> We (actually Linus, IIRC) simply declared from the get-go that it is
> not worth spending any line of code only to worry about pre OSX
> Macintosh when we did the end-of-line stuff, and nobody so far
> showed any need.
> 
Previous: Junio C Hamano
Message 18 of 18 in “CRLF, LF ... CR ?”
  1. Jens BauerSep 13, 2012
  2. Drew NorthupSep 13, 2012
  3. Jeff KingSep 13, 2012
  4. Jens BauerSep 13, 2012
  5. Jens BauerSep 13, 2012
  6. Jeff KingSep 13, 2012
  7. Jens BauerSep 13, 2012
  8. Johannes SixtSep 13, 2012
  9. Jens BauerSep 13, 2012
  10. Erik Faye-LundSep 13, 2012
  11. David AguilarSep 14, 2012
  12. Jens BauerSep 14, 2012
  13. Junio C HamanoSep 14, 2012
  14. David AguilarSep 26, 2012
  15. Jens BauerSep 26, 2012
  16. Jens BauerSep 26, 2012
  17. Junio C HamanoSep 27, 2012
  18. Jens BauerSep 27, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.