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

[1.63] string.format("a%-2sc", "b") doesn't pad spaces

Started by electrodude512, 09 July 2014 - 05:52 PM
electrodude512 #1
Posted 09 July 2014 - 07:52 PM
Version:
ComputerCraft 1.63 on Minecraft 1.6.4

Description:
string.format("a%-2sc", "b") returns "abc"

This is probably a LuaJ bug. Is it fixable, or should it be added to the list of broken string functions at http://computercraft.info/wiki/String_%28API%29?

Expected Result:
string.format("a%-2sc", "b") returns "ab c"

Reproduction Steps:
ComputerCraft:

lua> string.format("a%-2sc", "b")
abc

Lua 5.1

bash $ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> =string.format("a%-2sc", "b")
ab c
>
Edited on 09 July 2014 - 07:44 PM
Lyqyd #2
Posted 09 July 2014 - 09:58 PM
String.format is broken in the LuaJ implementation in a number of ways, so feel free to add the space-padding part of the %s type to the list. It would be misleading to state that string.format is broken in general, since it does mostly work. Can anyone verify that the syntax of the command is correct and duplicate the results before this gets added to the wiki? I'm not around a Lua interpreter at the moment.
Cranium #3
Posted 09 July 2014 - 10:27 PM
Using lua.org's online emulator, I came up with the same result.
electrodude512 #4
Posted 09 July 2014 - 11:02 PM
I requested wiki edit priveleges on the wiki edit privileges page. In case a moderator sees this post before my post on that page, my wiki username is Electrodude512. I'll add how padding is broken and I'll put my above example and probably also a fully functional (or closer to fully functional) string.format (but in lua so slower) because I'm about to write one anyway.
Edited on 09 July 2014 - 09:12 PM
Lyqyd #5
Posted 10 July 2014 - 12:56 AM
Adding a differently broken string.format wouldn't help anything.
electrodude512 #6
Posted 10 July 2014 - 01:16 AM
Adding a differently broken string.format wouldn't help anything.
OK, then I'll add a fully standards-compliant implementation instead of one that only adds padding. I might not be able to add it any time soon though, because I'll be busy with real life and I'll want to debug it before posting it. However, I can write that padding doesn't work without posting a fixed version as soon as I get wiki edit privileges.
Edited on 09 July 2014 - 11:17 PM
YoYoYonnY #7
Posted 21 July 2014 - 08:24 PM
Want proof?

There you go!