--------------------------------------------------------------------------------------------------------------------------- -- SpecialChars --------------------------------------------------------------------------------------------------------------------------- -- Description: -- A table of the scpecial character, the replacement codes, and the patterns for finding them in strings using gsub. -- --------------------------------------------------------------------------------------------------------------------------- SpecialCharCodes = { [1]={char='`', pat='%`', code='_btick_'}, [2]={char='~', pat='%~', code='_tilde_'}, [3]={char='!', pat='%!', code='_bang_'}, [4]={char='@', pat='%@', code='_at_'}, [5]={char='#', pat='%#', code='_hash_'}, [6]={char='$', pat='%$', code='_dol_'}, [7]={char='%', pat='%%', code='_pct_'}, [8]={char='^', pat='%^', code='_hat_'}, [9]={char='&', pat='%&', code='_amp_'}, [10]={char='*', pat='%*', code='_splat_'}, [11]={char='(', pat='%(', code='_op_'}, [12]={char=')', pat='%)', code='_cp_'}, [13]={char='+', pat='%+', code='_plus_'}, [14]={char='=', pat='%=', code='_eq_'}, [15]={char='{', pat='%{', code='_ocb_'}, [16]={char='}', pat='%}', code='_ccb_'}, [17]={char='|', pat='%|', code='_pipe_'}, [18]={char='[', pat='%[', code='_osb_'}, [19]={char=']', pat='%]', code='_csb_'}, [20]={char='\\', pat='%\\', code='_bslash_'}, [21]={char=':', pat='%:', code='_colon_'}, [22]={char='"', pat='%"', code='_quote_'}, [23]={char=';', pat='%;', code='_scolon_'}, [24]={char='\'', pat='%\'', code='_tick_'}, [25]={char='<', pat='%<', code='_lt_'}, [26]={char='>', pat='%>', code='_gt_'}, [27]={char='?', pat='%?', code='_ques_'}, [28]={char=',', pat='%,', code='_comma_'}, [29]={char='.', pat='%.', code='_stop_'}, [30]={char='/', pat='%/', code='_slash_'}, [31]={char=' ', pat='%s', code='_sp_'}, } --------------------------------------------------------------------------------------------------------------------------- -- RemoveSpecialChars --------------------------------------------------------------------------------------------------------------------------- -- Description: -- Removes the special characters in tehs tring and replaces each with a special code. -- This is to prepare the string for use as the kay(name) for a KeyValue. -- -- NOTE: because KeyValues are stored as XML elements, the names MUST follow the XML element standards. -- This measn no special character and not spaces. -- -- Parameters: -- intputStr string the string to convert (removing the special characters) -- -- Return: the string with special cahracters replaced by allowed codes. -- --------------------------------------------------------------------------------------------------------------------------- RemoveSpecialChars = function(inputStr) local retval = inputStr local i = 0 while i < #LeMay.SpecialCharCodes do i = i + 1 local cData = LeMay.SpecialCharCodes[i] retval = string.gsub(retval, cData.pat, cData.code) end return retval end --------------------------------------------------------------------------------------------------------------------------- -- RepalceSpecialChars --------------------------------------------------------------------------------------------------------------------------- -- Description: -- Replaces the special characters in the string where they have previously been removed. -- -- Parameters: -- intputStr string the string to convert (replacing the special characters) -- -- Return: the string with special cahracters restored. -- --------------------------------------------------------------------------------------------------------------------------- ReplaceSpecialChars = function(inputStr) local retval = inputStr local i = #LeMay.SpecialCharCodes -1 while i > 0 do local cData = LeMay.SpecialCharCodes[i] local rpl = cData.char if (rpl == '%') then rpl = '%%' end if (rpl ~= ' ') then retval= string.gsub(retval, cData.code, rpl) end i = i - 1 end --handle spaces! local temp = '' local strdata = LeMay.splitString(retval, '_sp_') local j = 0 while j < #strdata do j=j+1 if (temp ~= '') then temp = temp..' ' end temp = temp..strdata[j] end retval = temp return retval end --Testing RemoveSpecialChars and ReplaceSpecialChars (inverse pair): all special characters in string do local testName = 'RemoveSpecialChars and ReplaceSpecialChars (inverse pair): all special characters in string' local testStr = ']\\!@#${}|[%^&*()+=:";\'<>?,./`~' local result = LeMay.RemoveSpecialChars(testStr) local expected = '_csb__bslash__bang__at__hash__dol__ocb__ccb__pipe__osb__pct__hat__amp__splat__op__cp__plus_'.. '_eq__colon__quote__scolon__tick__lt__gt__ques__comma__stop__slash__btick__tilde_' local success1 = (result == expected) local finalStr = LeMay.ReplaceSpecialChars(result) local success2 = (finalStr == testStr) if (success1 and success2) then print('PASS: Test: '..testName) else print('FAIL: Test: '..testName) end end --Testing RemoveSpecialChars and ReplaceSpecialChars (inverse pair): all special characters in string scrambled do local testName = 'RemoveSpecialChars and ReplaceSpecialChars (inverse pair): all special characters in string scrambled' local testStr = '`~!@#$^&*()+={}|[]\\:";\'<>?,./' local result = LeMay.RemoveSpecialChars(testStr) local expected = '_btick__tilde__bang__at__hash__dol__hat__amp__splat__op__cp__plus__eq__ocb__ccb__pipe__osb_'.. '_csb__bslash__colon__quote__scolon__tick__lt__gt__ques__comma__stop__slash_' local success1 = (result == expected) local finalStr = LeMay.ReplaceSpecialChars(result) local success2 = (finalStr == testStr) if (success1 and success2) then print('PASS: Test: '..testName) else print('FAIL: Test: '..testName) end end --Testing RemoveSpecialChars and ReplaceSpecialChars (inverse pair): all special characters in string with normal do local testName = 'RemoveSpecialChars and ReplaceSpecialChars (inverse pair): all special characters in string with normal' local testStr = 'man`man~man!man@man#man$man^man&man*man(man)man+man=man{man}man|man[man]man\\man:man"man;man\'manman?man,man.man/man' local result = LeMay.RemoveSpecialChars(testStr) local expected = 'man_btick_man_tilde_man_bang_man_at_man_hash_man_dol_man_hat_man_amp_man_splat_man_op_man_cp_'.. 'man_plus_man_eq_man_ocb_man_ccb_man_pipe_man_osb_man_csb_man_bslash_man_colon_man_quote_man_scolon_'.. 'man_tick_man_lt_man_gt_man_ques_man_comma_man_stop_man_slash_man' local success1 = (result == expected) local finalStr = LeMay.ReplaceSpecialChars(result) local success2 = (finalStr == testStr) if (success1 and success2) then print('PASS: Test: '..testName) else print('FAIL: Test: '..testName) end end --Testing RemoveSpecialChars and ReplaceSpecialChars (inverse pair): just some words do local testName = 'RemoveSpecialChars and ReplaceSpecialChars (inverse pair): just some words' local testStr = 'just a few words with no special characters' local result = LeMay.RemoveSpecialChars(testStr) local expected = 'just_sp_a_sp_few_sp_words_sp_with_sp_no_sp_special_sp_characters' local success1 = (result == expected) local finalStr = LeMay.ReplaceSpecialChars(result) local success2 = (finalStr == testStr) if (success1 and success2) then print('PASS: Test: '..testName) else print('FAIL: Test: '..testName) end end --Testing RemoveSpecialChars and ReplaceSpecialChars (inverse pair): special characters with spaces do local testName = 'RemoveSpecialChars and ReplaceSpecialChars (inverse pair): special characters with spaces' local testStr = '`~!@#$ ^&*()+ ={}|[] \\:";\'< >%?,./' local result = LeMay.RemoveSpecialChars(testStr) local expected = '_btick__tilde__bang__at__hash__dol__sp__hat__amp__splat__op__cp__plus__sp__eq__ocb__ccb__pipe_'.. '_osb__csb__sp__bslash__colon__quote__scolon__tick__lt__sp__gt__pct__ques__comma__stop__slash_' local success1 = (result == expected) local finalStr = LeMay.ReplaceSpecialChars(result) local success2 = (finalStr == testStr) if (success1 and success2) then print('PASS: Test: '..testName) else print('FAIL: Test: '..testName) end end