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

[LUA] string.sub() does not work under unknown circumstances

Started by DerWisch, 01 October 2012 - 03:15 PM
DerWisch #1
Posted 01 October 2012 - 05:15 PM
Hi,

I'm currently working on some kind of keycard system.

It currently consists of two parts, a keycard writer to create keycards and a reader to read them.

Basically i have a key for example: "aaaaaBBBBBcccccDDDDDeeeee"

this key has a variable length depending on access level

Level A has the full 25 character long key.
Level E has only the first five characters.

To enter a Level E Area with a Level A card I use substring to get the first 5 chars:

sLevelKey = the key generated from the card to compare with the master key for this station
sKey = the full key from the card
iLevel = security level of the station
iLevelLength = the length of each security segment (in our case constantly 5)


sLevelKey = string.sub(sKey, iLevel*iLevelLength, iLevelLength)
this code in the card reader returns an empty string (not nil)

this code from the writer returns a valid string as it should:

sWriteKey = the key to be written on the keycard
sMKey = the masterkey to get the new key from
iLevel = security level of the keycard
iLevelLength = the length of each security segment (in our case constantly 5)


sWriteKey = string.sub(sMKey, 1, iLevel*iLevelLength)
I checked all values in both cases and they are what they have to be to make this method work but apparently it doesn't like me :|
MysticT #2
Posted 01 October 2012 - 06:16 PM
I think it has to do with a bug report some time ago about a bug in LuaJ's string.sub. There was a workaround, concatenate the result of string.sub to an empty string:

local s = string.sub(someString, n, m)..""
immibis #3
Posted 02 October 2012 - 03:23 AM

sLevelKey = string.sub(sKey, iLevel*iLevelLength, iLevelLength)

The second argument is the index of the first character. The third argument is the index of the last character - not the total number of characters.
DerWisch #4
Posted 02 October 2012 - 07:24 AM

sLevelKey = string.sub(sKey, iLevel*iLevelLength, iLevelLength)

The second argument is the index of the first character. The third argument is the index of the last character - not the total number of characters.

Thank you that was it

I knew it must be something simple :|
Lyqyd #5
Posted 02 October 2012 - 03:18 PM
I think it has to do with a bug report some time ago about a bug in LuaJ's string.sub. There was a workaround, concatenate the result of string.sub to an empty string:

local s = string.sub(someString, n, m)..""

I'm reasonably sure that that was actually string.find().
MysticT #6
Posted 02 October 2012 - 10:44 PM
I think it has to do with a bug report some time ago about a bug in LuaJ's string.sub. There was a workaround, concatenate the result of string.sub to an empty string:

local s = string.sub(someString, n, m)..""

I'm reasonably sure that that was actually string.find().
Yes, the bug is in string.find, but it only occurs after using string.sub, that's why I always think it's string.sub.
Anyway, that bug had nothing to do, I should read the code before posting :(/>/>