From ff2d0342e13e640b8374e642f365516bd1cb14b5 Mon Sep 17 00:00:00 2001 From: "Soni L." Date: Tue, 23 Dec 2014 11:41:41 -0200 Subject: [PATCH 1/2] Add readNumber()/file:read("*n") --- .../opencomputers/loot/OpenOS/lib/buffer.lua | 118 +++++++++++++++++- 1 file changed, 115 insertions(+), 3 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/OpenOS/lib/buffer.lua b/src/main/resources/assets/opencomputers/loot/OpenOS/lib/buffer.lua index d844d64cf..471f77dec 100644 --- a/src/main/resources/assets/opencomputers/loot/OpenOS/lib/buffer.lua +++ b/src/main/resources/assets/opencomputers/loot/OpenOS/lib/buffer.lua @@ -103,6 +103,119 @@ function buffer:read(...) return buffer end + local function readNumber() + local len, sub + if self.mode.b then + len = rawlen + sub = string.sub + else + len = unicode.len + sub = unicode.sub + end + local buffer = "" + local first = true + local last = false + local hex = false + local pat = "^[0-9]+" + local minbuf = 3 -- "+0x" (sign + hexadecimal tag) + -- this function is used to read trailing numbers (1e2, 0x1p2, etc) + local function readnum() + local _buffer = "" + local sign = "" + local checksign = true + while true do + if len(self.bufferRead) == 0 then + local result, reason = readChunk() + if not result then + if reason then + return nil, reason + else -- eof + return #_buffer > 0 and (sign .. _buffer) or nil + end + end + end + if checksign then + local _sign = sub(self.bufferRead, 1, 1) + if _sign == "+" or _sign == "-" then + -- "eat" the sign (Rio Lua behaviour) + sign = sub(self.bufferRead, 1, 1) + self.bufferRead = sub(self.bufferRead, 2) + end + checksign = false + else + local x,y = string.find(self.bufferRead, pat) + if not x then + break + else + _buffer = _buffer .. sub(self.bufferRead, 1, y) + self.bufferRead = sub(self.bufferRead, y + 1) + end + end + end + return #_buffer > 0 and (sign .. _buffer) or nil + end + while true do + if len(self.bufferRead) == 0 or len(self.bufferRead) < minbuf then + local result, reason = readChunk() + if not result then + if reason then + return nil, reason + else -- eof + return #buffer > 0 and tonumber(buffer) or nil + end + end + end + -- these ifs are here so we run the buffer check above + if first then + local sign = sub(self.bufferRead, 1, 1) + if sign == "+" or sign == "-" then + -- "eat" the sign (Rio Lua behaviour) + buffer = buffer .. sub(self.bufferRead, 1, 1) + self.bufferRead = sub(self.bufferRead, 2) + end + local hextag = sub(self.bufferRead, 1, 2) + if hextag == "0x" or hextag == "0X" then + pat = "^[0-9A-Fa-f]+" + -- "eat" the 0x, see https://gist.github.com/SoniEx2/570a363d81b743353151 + buffer = buffer .. sub(self.bufferRead, 1, 2) + self.bufferRead = sub(self.bufferRead, 3) + hex = true + end + minbuf = 0 + first = false + elseif last then + local tag = sub(self.bufferRead, 1, 1) + if hex and (tag == "p" or tag == "P") then + local temp = sub(self.bufferRead, 1, 1) + self.bufferRead = sub(self.bufferRead, 2) + local temp2 = readnum() -- this eats the next sign if any + if temp2 then + buffer = buffer .. temp .. temp2 + end + elseif tag == "e" or tag == "E" then + local temp = sub(self.bufferRead, 1, 1) + self.bufferRead = sub(self.bufferRead, 2) + local temp2 = readnum() -- this eats the next sign if any + if temp2 then + buffer = buffer .. temp .. temp2 + end + end + break + else + local x,y = string.find(self.bufferRead, pat) + if not x then + if not tonumber(buffer) then break end + minbuf = 3 + last = true + else + buffer = buffer .. sub(self.bufferRead, 1, y) + self.bufferRead = sub(self.bufferRead, y + 1) + end + end + end + return tonumber(buffer) + end + local function readLine(chop) local start = 1 while true do @@ -148,8 +261,7 @@ function buffer:read(...) end format = unicode.sub(format, 2, 2) if format == "n" then - --[[ TODO ]] - error("not implemented") + return readNumber() elseif format == "l" then return readLine(true) elseif format == "L" then @@ -297,4 +409,4 @@ function buffer:write(...) return self end -return buffer \ No newline at end of file +return buffer From d6d8d0b08a5d5f2d3052fe7d2a819116bfb752db Mon Sep 17 00:00:00 2001 From: "Soni L." Date: Tue, 23 Dec 2014 14:03:49 -0200 Subject: [PATCH 2/2] Add decimals to file:read("*n") --- .../opencomputers/loot/OpenOS/lib/buffer.lua | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/OpenOS/lib/buffer.lua b/src/main/resources/assets/opencomputers/loot/OpenOS/lib/buffer.lua index 471f77dec..b1deaf408 100644 --- a/src/main/resources/assets/opencomputers/loot/OpenOS/lib/buffer.lua +++ b/src/main/resources/assets/opencomputers/loot/OpenOS/lib/buffer.lua @@ -114,15 +114,15 @@ function buffer:read(...) end local buffer = "" local first = true + local decimal = false local last = false local hex = false local pat = "^[0-9]+" local minbuf = 3 -- "+0x" (sign + hexadecimal tag) -- this function is used to read trailing numbers (1e2, 0x1p2, etc) - local function readnum() + local function readnum(checksign) local _buffer = "" local sign = "" - local checksign = true while true do if len(self.bufferRead) == 0 then local result, reason = readChunk() @@ -183,19 +183,33 @@ function buffer:read(...) end minbuf = 0 first = false + elseif decimal then + local sep = sub(self.bufferRead, 1, 1) + if sep == "." then + buffer = buffer .. sep + self.bufferRead = sub(self.bufferRead, 2) + local temp = readnum(false) -- no sign + if temp then + buffer = buffer .. temp + end + end + if not tonumber(buffer) then break end + decimal = false + last = true + minbuf = 1 elseif last then local tag = sub(self.bufferRead, 1, 1) if hex and (tag == "p" or tag == "P") then local temp = sub(self.bufferRead, 1, 1) self.bufferRead = sub(self.bufferRead, 2) - local temp2 = readnum() -- this eats the next sign if any + local temp2 = readnum(true) -- this eats the next sign if any if temp2 then buffer = buffer .. temp .. temp2 end elseif tag == "e" or tag == "E" then local temp = sub(self.bufferRead, 1, 1) self.bufferRead = sub(self.bufferRead, 2) - local temp2 = readnum() -- this eats the next sign if any + local temp2 = readnum(true) -- this eats the next sign if any if temp2 then buffer = buffer .. temp .. temp2 end @@ -204,9 +218,8 @@ function buffer:read(...) else local x,y = string.find(self.bufferRead, pat) if not x then - if not tonumber(buffer) then break end - minbuf = 3 - last = true + minbuf = 1 + decimal = true else buffer = buffer .. sub(self.bufferRead, 1, y) self.bufferRead = sub(self.bufferRead, y + 1)