From 91ce171472560953cd4d62a916fa321c6efd59a5 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Fri, 29 Jul 2016 01:49:36 +0300 Subject: [PATCH] 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 --- .../scripts/resources/functions/is_uuid.lua | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/resources/install/scripts/resources/functions/is_uuid.lua b/resources/install/scripts/resources/functions/is_uuid.lua index dc4647c8a3..eba6818939 100644 --- a/resources/install/scripts/resources/functions/is_uuid.lua +++ b/resources/install/scripts/resources/functions/is_uuid.lua @@ -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) - if (string.len(s) == 36) then - 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 + return not not string.match(s, pattern) 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() +--]] \ No newline at end of file