Jump to content

Module:Escape/testcases: Difference between revisions

From Humanipedia
hp>Codehydro
m remove nowiki
m 1 revision imported
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
====Original:====
p = {}
{{#invoke:Escape/testcases|test_string2}}


====Using internal method to remove {{code|{}}:====
function p.test_string()
{{demo
  return 'test, \\test, \\{,test\\\\ \\\\ \\\\\\\\'
  |br=0
end
|{{escape|mode=kill
  |char=!
  |{*{#invoke:Escape!main!mode{{=}}kill!{{#invoke:Escape/testcases|test_string2}}!{}*}
  |*
  |{{!}}
}}}}


====Using {{t|replace}} to remove {{code|{}}:====
function p.test_string2()
{{demo|br=0|{{escape|mode=kill|char=!|{*{#invoke:Escape!main!mode{{=}}undo!{*{replace!{*{#invoke:Escape!main!mode{{=}}text!{{#invoke:Escape/testcases|test_string2}}}*}!{!}*}*}*}|*|{{!}}}}}}
return 'test { test {\\{ test, \\test, \\{,test\\ \\ \\ {\\'
end


====No removal of {{code|{}} between escape/unescape (escape char not restored):====
function p.test_string_module(frame)
{{demo|br=0|{{escape|mode=kill|char=!|{*{#invoke:Escape!main!mode{{=}}undo
return (p['test_string' .. (frame.args[1] or '')]():gsub('\\', '\\\\'))
!{*{#invoke:Escape!main!mode{{=}}text!{{#invoke:Escape/testcases|test_string2}}}*}
end
*}*}|*|{{!}}}}}}


====Restore to original after escape====
function p.test_kill500(frame)
{{demo|br=0|{{escape|mode=kill|char=!|{*{#invoke:Escape!main!mode{{=}}undo
local esc = require('Module:Escape')
!{*{#invoke:Escape!main!mode{{=}}text!{{#invoke:Escape/testcases|test_string2}}}*}
for k = 1, 500 do
!\
  local v = esc:kill(p.test_string2(), 'test')
*}*}|*|{{!}}}}}}
end
return os.clock(esc)
end


====Remove the word {{code|test}} if not escaped and then place a different escape char in the place of the old escape char (for use by something else):====
return p
Note: The '%' char is a special in Lua, so use '%%' if that is the desired replacement. Otherwise, just a single char is fine (or a word).
{{demo|br=0|{{((}}#invoke:Escape{{!}}main{{!}}mode{{=}}kill
{{!}}{{#invoke:Escape/testcases|test_string2}}
{{!}}test
{{!}}%%
{{))}}}}

Latest revision as of 21:11, 3 February 2025

Documentation for this module may be created at Module:Escape/testcases/doc

p = {}

function p.test_string()
 return 'test, \\test, \\{,test\\\\ \\\\ \\\\\\\\'
end

function p.test_string2()
 return 'test { test {\\{ test, \\test, \\{,test\\ \\ \\ {\\'
end

function p.test_string_module(frame)
 return (p['test_string' .. (frame.args[1] or '')]():gsub('\\', '\\\\'))
end

function p.test_kill500(frame)
 local esc = require('Module:Escape')
 for k = 1, 500 do
  local v = esc:kill(p.test_string2(), 'test')
 end
 return os.clock(esc)
end

return p