pharo-project / pharo

Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk.
http://pharo.org
Other
1.19k stars 350 forks source link

String>>#lines does not return correctly the lines of a String #9002

Open badetitou opened 3 years ago

badetitou commented 3 years ago

Bug description The method lines does not return the last empty line of a String. But, an empty line is a line

To Reproduce Steps to reproduce the behavior:

  1. In a playground
  2. Execute

    'hello
    
    world 
    ' lines "#('hello' '' 'world ')"

Expected behavior The last line should be present in the array as an empty line.

Example:

'hello

world 
' lines "#('hello' '' 'world ' '')"

Screenshots If applicable, add screenshots to help explain your problem.

Version information:

Expected development cost

low

svenvc commented 3 years ago

I believe there were discussion about this in the past. I also thinks this could be dangerous to change.

On macOS/Linux I can do:

$ cat > /tmp/lines.txt
one
two
three

$ wc -l /tmp/lines.txt 
       3 /tmp/lines.txt

$ hexdump -c lines.txt 
0000000   o   n   e  \n   t   w   o  \n   t   h   r   e   e  \n   f   o
0000010   u   r  \n   f   i   v   e  \n   s   i   x  \n                
000001c

In other words this is considered to be 3 lines, just like in Pharo

'one
two
three' lines  "#('one' 'two' 'three')"

I see no reason to change this.

I does of course depend of the definition of 'line', i.e. whether an EOL ends a line or separates 2 lines.

badetitou commented 3 years ago

Ok, I understand it can be the intended behavior.

If so, I would love to have a little comment in the lines method and a test that presents the choice made.

Something like:

testLineCount
    | sampleCRString sampleLFString sampleCRLFString sampleCRLFString2 |
    sampleCRString := 'Fred', String cr, 'the', String cr, 'Bear'.
    sampleLFString := 'Fred', String lf, 'the', String lf, 'Bear'.
    sampleCRLFString := 'Fred', String crlf, 'the', String crlf, 'Bear'.
    sampleCRLFString2 := 'Fred', String crlf, 'the', String crlf, 'Bear', String crlf.  

    self assert: sampleCRString lineCount equals: 3.
    self assert: sampleLFString lineCount equals: 3.
    self assert: sampleCRLFString lineCount equals: 3.
    self assert: sampleCRLFString2 lineCount equals: 3.
svenvc commented 3 years ago

OK