Comparison

util/jid.lua @ 366:5691edc7dd63

Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
author Matthew Wild <mwild1@gmail.com>
date Fri, 21 Nov 2008 05:02:53 +0000
parent 365:a59300fc22ec
child 367:cc26368294a3
comparison
equal deleted inserted replaced
365:a59300fc22ec 366:5691edc7dd63
1 1
2 local match = string.match; 2 local match = string.match;
3 3 local tostring = tostring;
4 local print = print
4 module "jid" 5 module "jid"
5 6
6 function split(jid) 7 function split(jid)
7 if not jid then return; end 8 if not jid then return; end
8 -- TODO verify JID, and return; if invalid 9 -- TODO verify JID, and return; if invalid
9 local node = match(jid, "^([^@]+)@"); 10 local node, nodelen = match(jid, "^([^@]+)@()");
10 local server = (node and match(jid, ".-@([^@/]+)")) or match(jid, "^([^@/]+)"); 11 local host, hostlen = match(jid, "^([^@/]+)()", nodelen)
11 local resource = match(jid, "/(.+)$"); 12 if node and not host then return nil, nil, nil; end
12 return node, server, resource; 13 local resource = match(jid, "^/(.+)$", hostlen);
14 if (not host) or ((not resource) and #jid >= hostlen) then return nil, nil, nil; end
15 return node, host, resource;
13 end 16 end
14 17
15 function bare(jid) 18 function bare(jid)
16 local node, host = split(jid); 19 local node, host = split(jid);
17 return node.."@"..host; 20 if node and host then
21 return node.."@"..host;
22 elseif host then
23 return host;
24 end
25 return nil;
18 end 26 end
19 27
20 return _M; 28 return _M;