Simplify `is_uuid` function. (#1781)
* Simplify `is_uuid` function. Also because it returns now value itself it possible write ```Lua my_uuid = is_uuid(value1) or is_uuid(value2) ``` * Change. Ensure `is_uuid` returns only boolean value
This commit is contained in:
parent
4f9c1d7f67
commit
e7c813a559
|
|
@ -1,13 +1,40 @@
|
||||||
|
local pattern = '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$'
|
||||||
|
|
||||||
function is_uuid(s)
|
function is_uuid(s)
|
||||||
if (string.len(s) == 36) then
|
return not not string.match(s, pattern)
|
||||||
local x = "%x";
|
|
||||||
local t = { x:rep(8), x:rep(4), x:rep(4), x:rep(4), x:rep(12) }
|
|
||||||
local pattern = table.concat(t, '%-');
|
|
||||||
result = s:match(pattern);
|
|
||||||
end
|
|
||||||
if (result == nil) then
|
|
||||||
return false;
|
|
||||||
else
|
|
||||||
return true;
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
local function is_uuid_self_test()
|
||||||
|
print('Is UUID self test ...')
|
||||||
|
local pass_tests = {
|
||||||
|
'34dd925b-f320-425f-ad87-0573c5b853c8',
|
||||||
|
'34DD925B-F320-425F-AD87-0573C5B853C8',
|
||||||
|
}
|
||||||
|
for _, value in ipairs(pass_tests) do
|
||||||
|
assert(true == is_uuid(value), value)
|
||||||
|
end
|
||||||
|
|
||||||
|
local fail_tests = {
|
||||||
|
-- no some digints
|
||||||
|
'4dd925b-f320-425f-ad87-0573c5b853c8',
|
||||||
|
'34DD925B-320-425F-AD87-0573C5B853C8',
|
||||||
|
'34dd925b-f320-425f-d87-0573c5b853c8',
|
||||||
|
'34DD925B-F320-425F-AD87-573C5B853C8',
|
||||||
|
'034dd925b-f320-425f-ad87-0573c5b853c8',
|
||||||
|
'34DD925B-0F320-425F-AD87-0573C5B853C8',
|
||||||
|
'34dd925b-f320-0425f-ad87-0573c5b853c8',
|
||||||
|
'34DD925B-F320-425F-0AD87-0573C5B853C8',
|
||||||
|
'34dd925b-f320-425f-ad87-00573c5b853c8',
|
||||||
|
' 34DD925B-F320-425F-AD87-0573C5B853C8',
|
||||||
|
'34DD925B-F320-425F-AD87-0573C5B853C8 ',
|
||||||
|
'G4DD925B-F320-425F-AD87-573C5B853C8',
|
||||||
|
}
|
||||||
|
for _, value in ipairs(fail_tests) do
|
||||||
|
assert(false == is_uuid(value), value)
|
||||||
|
end
|
||||||
|
print('Is UUID self test - pass')
|
||||||
|
end
|
||||||
|
|
||||||
|
is_uuid_self_test()
|
||||||
|
--]]
|
||||||
Loading…
Reference in New Issue