From 06e7149573cc310254b691b3944eeb5d99619ff8 Mon Sep 17 00:00:00 2001 From: payonel Date: Wed, 26 Oct 2016 22:16:25 -0700 Subject: [PATCH] greatly simplify readnum by leveraging tonumber --- .../openos/lib/tools/advanced-buffering.lua | 140 +++++------------- 1 file changed, 35 insertions(+), 105 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/tools/advanced-buffering.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/tools/advanced-buffering.lua index 405a5158a..b2753784c 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/tools/advanced-buffering.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/tools/advanced-buffering.lua @@ -12,120 +12,50 @@ function adv_buf.readNumber(self, readChunk) len = unicode.len sub = unicode.sub 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(checksign) - local _buffer = "" - local sign = "" - 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 white_done + + local function peek() + 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 tonumber(buffer) or nil - end + return result, reason 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 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(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(true) -- this eats the next sign if any - if temp2 then - buffer = buffer .. temp .. temp2 - end - end + return sub(self.bufferRead, 1, 1) + end + + local function pop() + local n = sub(self.bufferRead, 1, 1) + self.bufferRead = sub(self.bufferRead, 2) + return n + end + + local function take() + buffer = buffer .. pop() + end + + while true do + local peeked = peek() + if not peeked then break - else - local x,y = string.find(self.bufferRead, pat) - if not x then - minbuf = 1 - decimal = true - else - buffer = buffer .. sub(self.bufferRead, 1, y) - self.bufferRead = sub(self.bufferRead, y + 1) + end + + if peeked:match("[%s]") then + if white_done then + break end + pop() + else + white_done = true + if not tonumber(buffer .. peeked .. "0") then + break + end + take() -- add pop to buffer end end + return tonumber(buffer) end