rzel / kahlua

Automatically exported from code.google.com/p/kahlua
0 stars 0 forks source link

Can not retrieve the correct character when using string.byte method #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Run the following code:

bit = nil
num = "123"
for i=1,string.len(num) do
    bit = string.byte(num, i) - 48
    print("current bit:"..bit)
end

2.
3.

What is the expected output? What do you see instead?
expected output:
current bit:1
current bit:2
current bit:3

actual result:
current bit:1
current bit:2
current bit:nil

What version of the product are you using? On what operating system?
revision 181.
runs on XP os, WTK 2.5.2 simulator.

Please provide any additional information below.
My temporary fixes is:

File (StringLib.java)

private int stringByte(LuaCallFrame callFrame, int nArguments) {
    BaseLib.luaAssert(nArguments >= 1, "not enough arguments");
    String s = (String) callFrame.get(0);

    Double di = null;
    Double dj = null;
    if (nArguments >= 2) {
        di = BaseLib.rawTonumber(callFrame.get(1));
        if (nArguments >= 3) {
            dj = BaseLib.rawTonumber(callFrame.get(2));
        }
    }
    double di2 = 1, dj2 = 1;
    if (di != null) {
        di2 = LuaState.fromDouble(di);
    }
    if (dj != null) {
        dj2 = LuaState.fromDouble(dj);
    } else {
        dj2 = LuaState.fromDouble(di);
        }

    int ii = (int) di2, ij = (int) dj2;

    int len = s.length();
    ii = Math.min(ii, len);
    ij = Math.min(ij, len);
    int nReturns = 1 +ij - ii;

    callFrame.setTop(nReturns);
    int offset = ii - 1;
    for (int i = 0; i < nReturns; i++) {
        char c = s.charAt(offset + i);
        callFrame.set(i, new Double((double) c));                   
    }
    return nReturns;
}

Original issue reported on code.google.com by cpxster@gmail.com on 1 Feb 2009 at 6:06

GoogleCodeExporter commented 8 years ago
Hi, thanks for the report. I confirm the bug and I am now working on making a 
patch
that adds correct behaviour for all valid inputs.

Original comment by kristofer.karlsson@gmail.com on 1 Feb 2009 at 7:39

GoogleCodeExporter commented 8 years ago
The bug has been fixed and commited along with additional test cases to ensure 
it
doesn't regress in the future.

Original comment by kristofer.karlsson@gmail.com on 1 Feb 2009 at 7:56