File: util.lua

package info (click to toggle)
lua-lpeg-patterns 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220 kB
  • sloc: makefile: 6
file content (35 lines) | stat: -rw-r--r-- 672 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
local function read_hex(hex_num)
	return tonumber(hex_num, 16)
end

local safe_tonumber do -- locale independent tonumber function
	local tolocale
	local function updatelocale()
		local decpoint = string.format("%f", 0.5):match "[^05]+"
		if decpoint == "." then
			tolocale = function(str)
				return str
			end
		else
			tolocale = function(str)
				str = str:gsub("%.", decpoint, 1)
				return str
			end
		end
	end
	updatelocale()
	safe_tonumber = function(str)
		local num = tonumber(tolocale(str))
		if num then
			return num
		else
			updatelocale()
			return tonumber(tolocale(str))
		end
	end
end

return {
	read_hex = read_hex;
	safe_tonumber = safe_tonumber;
}