gkjpettet / ObjoScript

A dynamic stack-based VM language written in Xojo.
MIT License
6 stars 2 forks source link

ReadLines Function Fails on Files with multiple EndofLines #7

Open simulanics opened 1 year ago

simulanics commented 1 year ago

Say you have a file containing:

The quick brown

fox

jumped

over the lazy

dog

When looping thru a readlines generated list, as the code current stands, will render:

The quick brown foxjumpedover the lazydog

when...

The quick brown fox jumped over the lazy dog

is expected.

The changes to the function as below, rectify the issue on all instances containing multiple EndofLines.

Thank you!

`Protected Sub ReadLines(vm As ObjoScript.VM) /// Returns a list where each item is a line in the file. /// /// FSItem.readLines() -> List

Var file As ObjoScript.Instance = vm.GetSlotValue(0)

// Error checks. If file.ForeignData = Nil Or FolderItem(file.ForeignData).Exists = False Then vm.Error("The file does not exist.") ElseIf FolderItem(file.ForeignData).IsFolder Then vm.Error("Cannot read lines from a folder.") ElseIf Not FolderItem(file.ForeignData).IsReadable Then vm.Error("Cannot read file.") End If

Var tin As TextInputStream tin = TextInputStream.Open(file.ForeignData)

Var lines() As String

try var tContent as String = tin.ReadAll().ReplaceLineEndings(EndOfLine).ReplaceAll(EndOfLine + EndOfLine, EndOfLine)

While tContent.IndexOf(EndOfLine + EndOfLine) <> - 1
  //recursion
  tContent = tContent.ReplaceAll(EndOfLine + EndOfLine, EndOfLine)
wend

lines = tContent.Split(EndOfLine)

Catch e As RuntimeException vm.Error("An error occurred whilst reading the file: " + e.Message + ".") Finally tin.Close End Try

vm.SetReturn(vm.NewList(lines))

End Sub`

I wrapped the function with code tags, but GitHub fails to 'code' block the entire function

gkjpettet commented 1 year ago

Thank you for this. Great spot. I’ll get this folded into main.

gkjpettet commented 1 year ago

@simulanics

So I've finally had a chance to investigate and I'm not seeing what you're seeing.

Using your input in a file called readLines-test.txt on my desktop:

The
quick
brown

fox

jumped

over
the
lazy

dog

This script:

var f = FSItem("/Users/garry/Desktop/readLines-test.txt")

foreach line in f.readLines {
System.print(line)
}

Gives me this output:

The
quick
brown

fox

jumped

over
the
lazy

dog

Which is expected, no?

This is running on macOS.

simulanics commented 1 year ago

I'll have to retest with current release and let you know. On windows the EOL is joining lines with no empty lines between them if 2 or more EOLs are contiguous. There is a known similar issue in Xojo with the TextArea control for windows not respecting endoflines. Will play around with updated success tonight and report back. Have a great Monday in the meantime. Thanks for taking time to look also.