lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


With my PowerMac Lua 4.0 I have discovered something that seems like a bug in "strfind". (I don't have access to compiled Lua 4.0 for other platforms yet.)

I'm using strfind to search for a pattern (no magic) that occurs a few times in my source string. When the search starts after the last occurrence, strfind (sometimes) returns position numbers that are greater than the length of my source string! However, to be quite sure that this strange thing happens, it seems that I have to build up my source string step by step, as happens when I use my bytesToHex function (below). That's why I convert my source string from hex to binary and back to hex in the example below. Before you run it, make sure you iron out any newlines that the email transfer may have put into the three long lines (sorry) where the s string is given its initial value.

/Jon


-- A function that turns a string of bytes into a string of hex codes
function bytesToHex(bytes, byteGap)
  if not byteGap then
    byteGap = ""
  end
  local h = "" -- the string to return
  local nBytes = strlen(bytes)
  local i = 1
  while i <= nBytes do
    h = h..format("%02X"..byteGap, strbyte(bytes, i))
    i = i+1
  end
  return h
end -- bytesToHex

-- The string of hex codes to be searched
s = "4E AC 00 2A 02 78 77 D4 00 00 00 01 4E AC 00 4E 00 03 00 01 4E AC 00 06 4E AC 00 5A 00 02 00 0A FF " s = s.."7E 4E AC 00 06 48 6E FF 72 2F 2E FF 76 2F 2E FF 7A 4E AC 01 0E 02 76 41 10 4E AC 00 06 2F 2E FF 72 " s = s.."3F 3C 01 00 4E AC 00 D8 02 76 6C 18 0C 40 00 01 66 06 4E AC 00 12 07 D1 4E AC 00 06 4E EC 00 36 "

-- First we turn the string of hex codes into a string of bytes ...
bb = ""
for pos = 1, strlen(s), 3 do
  bb = bb..strchar(tonumber(strsub(s, pos, pos+1), 16))
end

-- Then we convert it back to the original string of hex codes ...
s = bytesToHex(bb, " ")

-- Doing a search for a pattern that occurs (a few times) in the source,
-- but starting the search way beyond the last occurrence ...
print(strlen(s), strfind(s, "4E AC", 276, 1))

-- gives this strange result: 294 337 341 (instead of 294 nil)
-- i.e. the length of the source is 294, but the reported position is 337!

---------------------------------------------------------------------------
Jon Kleiser / ADB-seksjonen / USIT / University of Oslo / Norway
Mail: Jon.Kleiser@usit.uio.no / Tel: +47-22 85 28 04 / Fax: +47-22 85 29 70
---------------------------------------------------------------------------