This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CometWolf's profile picture

Custom line split troubles, CC bug?

Started by CometWolf, 11 January 2014 - 06:39 AM
CometWolf #1
Posted 11 January 2014 - 07:39 AM
Running this piece of code on the online lua demo http://www.lua.org/cgi-bin/demo

local tLines = {}
local lineLength = (51)
local message = "This is the first line|second line|This line is longer than the 51 character limit, and will be split!"
while #message > 0 do
  local line = message:sub(1,lineLength)
  local newLine = line:find"|" --check for new line character
  if newLine then
	line = line:sub(1,newLine-1)
	message = message:sub(#line+2,#message)
  elseif #line == lineLength then
	local endSpace = line:find"%s%S-$" or lineLength
	line = line:sub(1,endSpace)
	message = message:sub(#line+1,#message)
  else
	message = ""
  end
  table.insert(tLines,line)
end
for i=1,#tLines do
  print(tLines[i])
end

As expected splits the string into lines at all "|" and at the space closest to the length limit, and prints the following:
This is the first line
second line
This line is longer than the 51 character limit,
and will be split!

However, running it on a computercraft computer prints the following
This is the first line
second line| This line is longer th
n the 51 character limit, and will be split!
And im just left thinking… wtf is going on here?
Edited on 11 January 2014 - 06:54 AM
MKlegoman357 #2
Posted 11 January 2014 - 09:21 AM
There is a bug when using string.find on a string that was created using string.sub.
CometWolf #3
Posted 11 January 2014 - 09:37 AM
Ah i see, thanks for the info.
Anavrins #4
Posted 11 January 2014 - 10:39 AM
https://twitter.com/DanTwoHundred/status/416888477389574144
This bug should be fixed in the next update.