Software /
code /
prosody
Comparison
util/hex.lua @ 6802:442019e955dc
util.hex: Normalize hex to lowercase and discard non-hex input
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 26 Aug 2015 14:32:20 +0200 |
parent | 6545:ec566d7cd518 |
child | 12355:a0ff5c438e9d |
comparison
equal
deleted
inserted
replaced
6798:8b4c8e957211 | 6802:442019e955dc |
---|---|
1 local s_char = string.char; | 1 local s_char = string.char; |
2 local s_format = string.format; | 2 local s_format = string.format; |
3 local s_gsub = string.gsub; | 3 local s_gsub = string.gsub; |
4 local s_lower = string.lower; | |
4 | 5 |
5 local char_to_hex = {}; | 6 local char_to_hex = {}; |
6 local hex_to_char = {}; | 7 local hex_to_char = {}; |
7 | 8 |
8 do | 9 do |
17 local function to(s) | 18 local function to(s) |
18 return (s_gsub(s, ".", char_to_hex)); | 19 return (s_gsub(s, ".", char_to_hex)); |
19 end | 20 end |
20 | 21 |
21 local function from(s) | 22 local function from(s) |
22 return (s_gsub(s, "..", hex_to_char)); | 23 return (s_gsub(s_lower(s), "%X*(%x%x)%X*", hex_to_char)); |
23 end | 24 end |
24 | 25 |
25 return { to = to, from = from } | 26 return { to = to, from = from } |