webmcp
view framework/env/parse/currency.lua @ 153:c8c91216255f
Correct treatment of top-level null values in JSON parser
author | jbe |
---|---|
date | Thu Jul 31 01:21:33 2014 +0200 (2014-07-31) |
parents | 9fdfb27f8e67 |
children |
line source
1 local function extract_numbers(str)
2 local result = {}
3 for char in string.gmatch(str, "[0-9]") do
4 result[#result+1] = char
5 end
6 return table.concat(result)
7 end
9 function parse.currency(str, dest_type, options)
10 local str = parse._pre_fold(str)
11 local dest_type = dest_type or atom.number
12 local options = options or {}
13 local currency_decimal_point = locale.get("currency_decimal_point")
14 local decimal_point = locale.get("decimal_point") or "."
15 local pos1, pos2
16 if currency_decimal_point then
17 pos1, pos2 = string.find(str, currency_decimal_point, 1, true)
18 end
19 if not pos1 then
20 pos1, pos2 = string.find(str, decimal_point, 1, true)
21 end
22 if pos1 then
23 local p1 = extract_numbers(string.sub(str, 1, pos1 - 1))
24 local p2 = extract_numbers(string.sub(str, pos2 + 1, #str))
25 return parse.decimal(p1 .. decimal_point .. p2, dest_type, options)
26 else
27 return parse.decimal(extract_numbers(str), dest_type, options)
28 end
29 end