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

[1.46][SSP/SMP] string.format() behaves incorrectly with "%[width][.precision]s"

Started by Firedroide, 12 December 2012 - 07:36 AM
Firedroide #1
Posted 12 December 2012 - 08:36 AM
ComputerCraft Version Information: 1.46 Client (& 1.46 Server)

Description:
string.format() doesn't behave correctly in some situations when used with %s and a width or a 'precision', which would add whitespaces or limit the string to a certain length.

Examples:
print(string.format("%20s", "Test"))
ComputerCraft: "Test"
Lua console:   "				Test"

print(string.format("%-10s", "Test"))
ComputerCraft: "Test"
Lua console:   "Test	  "

print(string.format("%.10s", "A quite long string expression."))
ComputerCraft: "A quite long string expression."
Lua console:   "A quite lo"

I don't know if that's all bugs there are, but this is clearly some unexpected behavior.
The CraftOS just seems to return the string itself, without taking the parameters into account.

I also tested it with
print(string.format("%10i", 42))
ComputerCraft: "		42"
Lua console:   "		42"
but everything seems to work as expected there.

I'm sorry if this bug is already known, but I wasn't able to find it on the wiki or on the forums.

EDIT: Put stuff into code brackets so the spaces don't disappear
Cloudy #2
Posted 12 December 2012 - 09:58 AM
This will be a bug in LuaJ - however I doubt we will be fixing it. We are wanting to move over to a native Lua library so we can use Pluto persistence to save computer states.
Firedroide #3
Posted 12 December 2012 - 10:16 AM
This will be a bug in LuaJ - however I doubt we will be fixing it. We are wanting to move over to a native Lua library so we can use Pluto persistence to save computer states.

Cool, good to know.
I'll use a for loop to add some spaces until this gets fixed :D/>
Cranium #4
Posted 12 December 2012 - 10:57 AM
Might I just say that I appreciate people who make proper bug reports like this. Too many just say "Help! I did something wrong! Please fix it!"
Probably not necessary for me to post, but I felt I had to comment on the organization here.
Cloudy #5
Posted 12 December 2012 - 11:12 AM
Agreed, thanks!
dissy #6
Posted 12 December 2012 - 03:31 PM
I found this bug a while back too, and ended up making a padString function that takes a string and a length, and returns a string that is either padded with spaces at end, or a trimmed string removing the end so it fits in the available space.
I was using it to display tabulated data aligned in columns.

In case it might be helpful to anyone else:


function padString (sText, iLen)
  local iTextLen = string.len(sText)
  -- Too short, pad
  if iTextLen < iLen then
	local iDiff = iLen - iTextLen
	return(sText..string.rep(" ",iDiff))
  end
  -- Too long, trim
  if iTextLen > iLen then
	return(string.sub(sText,1,iLen))
  end
  -- Exact length
  return(sText)
end

Test code:

local tt = "This is an example string"
print("Raw text: "..tt)
print(" Len 30 : >"..padString(tt,30).."<")
print(" Len 25 : >"..padString(tt,25).."<")
print(" Len 20 : >"..padString(tt,20).."<")

Results:
PixelToast #7
Posted 12 December 2012 - 04:22 PM
just concat an empty string to the output of it .-. then it be fixed
dissy #8
Posted 12 December 2012 - 04:36 PM
just concat an empty string to the output of it .-. then it be fixed

I don't see how that would fix it. The colums would be just as not-aligned, just wider.

If column A has a width of 5, col B is 15, col C is 10, and D is the rest of the line… you want the data in all your rows to line up to the right column, and thus be under the right header, as well as not wrap the line around the screen spilling into column A of the next row.

You need to determine the length of the data to display, and instead of putting in a fixed and always the same number of spaces from an empty string, you need the exact number of spaces so the next character lines up with the beginning of the next column.
You also want to assure the string doesn't intrude into the next column, misaligning all the columns after it.

Sure, for the header itself, and the next line if it's a seperator, you can just put spaces manually. Every line afterwards however you can't.


local emptyString = "	 "
print("Time	 Depth   Cords		    Note")
print("========= ======= ================== ==============")

How do you suggest I use your method to display the rows that come after using your empty string?
Lyqyd #9
Posted 12 December 2012 - 04:37 PM
just concat an empty string to the output of it .-. then it be fixed

What? No. That is an entirely different bug.
Firedroide #10
Posted 13 December 2012 - 08:42 AM
Might I just say that I appreciate people who make proper bug reports like this. Too many just say "Help! I did something wrong! Please fix it!"
Probably not necessary for me to post, but I felt I had to comment on the organization here.

Yes, I actually noticed that myself, too.
ComputerCraft is a mod that allows people to program stuff, so you'd expect a lot of programmers to use the mod, who know what a good and useful bug report is.
A lot of topics I see in the bug section just say "Help! My turtle won't move!" or "BUG! Reply please!", though :/