Skip to content

Commit

Permalink
Optimized sleep.lua interval parsing (#2436)
Browse files Browse the repository at this point in the history
* Optimized sleep.lua interval parsing

Modified regex to use captures, consolidated invalid interval checks.
Note: tonumber(nil) safely returns nil

* Update sleep.lua

Updated pattern matching
  • Loading branch information
SDPhantom authored and payonel committed Jun 23, 2017
1 parent cdfdb27 commit f9f3789
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/main/resources/assets/opencomputers/loot/openos/bin/sleep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,15 @@ end
local total_time = 0

for _,v in ipairs(args) do
local interval = v:match('^%d+%.?%d*[smhd]?$')
if not interval then
help(v)
return 1
end

local time_type = interval:match('[smhd]') or ''
interval = interval:sub(1, -#time_type-1)
local interval, time_type = v:match('^([%d%.]+)([smhd]?)$')
interval = tonumber(interval)

if interval < 0 then
if not interval or interval < 0 then
help(v)
return 1
end

interval = time_type_multiplier(time_type) * interval
total_time = total_time + interval
total_time = total_time + time_type_multiplier(time_type) * interval
end

os.sleep(total_time)

0 comments on commit f9f3789

Please sign in to comment.