[QUESTION/HELP] printf error: slice out of range

Started by EK.IceFlake, Mar 05, 2015, 11:13 AM

Previous topic - Next topic

EK.IceFlake

Hi guys!
I implemented my printf function, but it works strangely:
this works
printf("Server information:\n  -Name: %s\n  -Max players: %i\n  -Gravity: %i\n  -Speed: %i\n  -Gamemode name: %s", servername, maxplayers, gravity, speed, gamemodename);this doesnt (there is space added to the end
printf("Server information:\n  -Name: %s\n  -Max players: %i\n  -Gravity: %i\n  -Speed: %i\n  -Gamemode name: %s ", servername, maxplayers, gravity, speed, gamemodename);printf:
function printf(string, ...)
{
if (vargv.len() == -1) return print(string);
local s = "", d = 0, tu = 0;
for (local c = 0; c < string.len(); c++)
{
        if (d == 0)
        {
        if (c < string.len() && string.slice(c, c+2) == "%s") // Error goes here, slice out of range
        {
            if (tu <= vargv.len() && typeof vargv[tu] == "string")  s = s + vargv[tu];
            else s = s + "null[0x0000000]";
            tu++;
        d = 1;
        }
        else if (c < string.len() && string.slice(c, c+2) == "%i")
        {
            if (tu <= vargv.len() && typeof vargv[tu] == "integer")  s = s + vargv[tu];
            else s = s + "null[0x0000000]";
            tu++;
        d = 1;
        }
        else if (c < string.len() && string.slice(c, c+2) == "%f")
        {
            if (tu <= vargv.len() && typeof vargv[tu] == "float")  s = s + vargv[tu];
            else s = s + "null[0x0000000]";
            tu++;
        d = 1;
        }
        else s = s + string.slice(c, c+1);
        }
        else d = 0;
}
print(s);
}
Thanks

.

Not really sure what you're trying to do :-\
function printf(...)
{
    // Insert the 'this' environment as the first argument
    vargv.insert(0, this);
    // Forward all the arguments to the format function
    local str = ::format.acall(vargv);
    // Do something with our generated message
    print(str);
}

printf("This is %d and %s and %f", 322345, "haha",  6.324234);
.

EK.IceFlake

Quote from: S.L.C on Mar 05, 2015, 04:41 PMNot really sure what you're trying to do :-\
function printf(...)
{
    // Insert the 'this' environment as the first argument
    vargv.insert(0, this);
    // Forward all the arguments to the format function
    local str = ::format.acall(vargv);
    // Do something with our generated message
    print(str);
}

printf("This is %d and %s and %f", 322345, "haha",  6.324234);
Thanks this works. But I wonder why do you have to insert the "this". And for some reason you defined str to be used only once!
    print(::format.acall(vargv));

.

Quote from: NE.CrystalBlue on Mar 06, 2015, 03:49 AMBut I wonder why do you have to insert the "this".

Try to remove it and see what happens :P Then go read the documentation to see why that happens and why I insert it there at that exact position.

Quote from: NE.CrystalBlue on Mar 06, 2015, 03:49 AMAnd for some reason you defined str to be used only once!
    print(::format.acall(vargv));

I'm quite aware of that. I just wanted to let know know what's returned by the format function and that you can do anything you like after calling it.

Seriously people you need to stop assuming that I do these simple things by mistake. I am making those choices in my code to let you know what's happening and to avoid follow up posts like "Why did you do that?".
.