Log In  

?"\*0abc" 
-- expected: bc 
-- actual: abc

Perhaps this isn't a bug, but it could make a padding function a lot simpler.

function pad(str, char, len)
 return str.."\*"..(len-#str)..char
end
?pad("hello","h",6) -- helloh
?pad("hello","h",5) -- helloh
-- second one should just be hello
P#118291 2022-10-01 17:52

Hi @Einstein2:

I'm wondering if this might break some existing carts already making using of the \* repeat command.

Your function could be though:

function pad(str, char, len)
 return sub(str.."\*"..(len-#str)..char,1,len)
end
cls()
print(pad("apple","*",8))
?""
?""
?""

APPLE***

Which will truncate not just the added characters but the string itself, giving you no string for zero length.

P#118296 2022-10-01 18:31 ( Edited 2022-10-01 18:32)

@dw817
Ah, that is true. Thanks for the tip!

P#118298 2022-10-01 18:37

You're very welcome, @Einstein2 ! Try entering a negative value above for length and you get a funny result though. :)

Easily fixed.

function pad(str, char, len)
 return sub(str.."\*"..(len-#str)..char,1,max(0,len))
end
P#118300 2022-10-01 18:49
1

Thanks @Einstein2 -- This is fixed for 0.2.5d

It might cause a little breakage, but I agree that ?"*0abc" should print "bc" and think it's worth changing.

P#121029 2022-11-20 00:34 ( Edited 2022-11-20 00:34)

[Please log in to post a comment]