[Pharo-dev] Default line endings while writing to a file stream

Tim Mackinnon tim at testit.works
Fri May 31 17:10:12 EDT 2019


It’s a great question with a few follow on’s...

In github I think we can set it to ignore line endings for certain files (I’m assuming this is Exercism), although  I’ve noticed it doesn’t seem to always work very well (could be the wrong settings thought. We should check we have .st set to text in that file.

Similarly however, why is there a critic for line endings in methods? Anyone know what the big deal is? I world have thought we don’t care?

Tim

Sent from my iPhone

> On 31 May 2019, at 06:06, Ben Coman <btc at openinworld.com> wrote:
> 
> 
> I'm on Windows wanting to write a text file with LF endings.
> The code developed on Linux is thus...
>     (destinationDirectory / 'README.md') ensureCreateFile
>             writeStreamDo: [ :stream | stream nextPutAll: class comment ]
> 
> and I am stuck with CR line endings.  
> The specific symptom is that it screws `git diff` 
> Here is a simple experiment...
>     testfile := 'README.md' asFileReference.
>     testfile ensureCreateFile writeStreamDo: [ :stream |
>     stream nextPutAll: 'aa
> bb' ].
>     testfile binaryReadStream contents at: 3      "==>  Character cr " 
> 
> I think its safe to assume on Linux that will result in "==>  Character lf "
> but can someone please confirm?
> 
> So my issue is that I've got
>     #ensureCreateFile - returns a FileReference
> and
>     :stream - is a ZnCharacterWriteStream wrapping a ZnBufferedWriteStream wrapping BinaryFileStream.
> 
> and neither seem to have an easily accessible defaultLineEnding setting.
> Indeed, line endings are not a property of FileReference 
> and Binary & Characters have nothing to do with line endings,
> and questionable if Buffering is related.
> 
> Its more a property of a File, but IIUC that is being deprecated (??)
> 
> MultiByteFileStream has #lineEndConvention:
> but IIUC that also is being deprecated.
> 
> So what is the proper way to force default line endings?
> 
> 
> ------
> Now while composing this email I discovered String>>withUnixLineEndings.
> So I have a solution...
>     testfile := 'README.md' asFileReference.
>     testfile ensureCreateFile writeStreamDo: [ :stream |
>     stream nextPutAll: 'aa
> bb'  withUnixLineEndings ].
>     (testfile binaryReadStream contents at: 3) asCharacter   "==> Character lf "
> 
> but that seems to imply that on Windows...
> 'aa
> bb' at: 3  "==> Character cr "
> 
> and on Linux (someone please confirm)...
> 'aa
> bb' at: 3   "==> Character lf "
> 
> and that is *very* curious.  Strange that I never noticed it before and obviously that hasn't hurt me, 
> but considering the Principal Of Least Surprise it leaves me feeling somewhat violated :)




More information about the Pharo-dev mailing list