Module:Escape/testcases
This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
Usage
[සංස්කරණය]This module is designed as an way to escape strings in a customized and efficient manner. It works by replacing characters that are preceded by your escape char (or phrase) There are two ways to call this module:
From another module:
සැකිල්ල:Codett esc:char(escape char (or sequence)) සැකිල්ල:Codett(string) code that replaces or removes unescaped chars සැකිල්ල:Codett
From a template:
සැකිල්ල:MlxIn a template, the most useful function is
kill
This module is primarily intended to be used by other modules. However all functions can be called in template space using |mode=the function you want to call
followed by arguments.
main()
esc:char('%')
esc:kill{'{{example|\\}}}', '}'} == '{{example|}'
සැකිල්ල:TOC tab | This function takes only one argument: A string. All characters in this string which are preceded by the sequence set by XML Example escape:char()
|
---|---|
සැකිල්ල:TOC tab | Takes two arguments:
|
සැකිල්ල:TOC tab | This is basically equivalent to calling XML Example string.gsub()
XML Example escape:text()
XML Example escape:undo()
|
සැකිල්ල:TOC tab | This function's primary use is to initialize the patterns to scan a string for an escape/escaped sequence. It takes two arguments, the first being the escape character and the second being a table of arguments (optional). By default, this module will escape the XML Example \
XML Example {
XML Example require('Module:Escape'):char('{')
XML Example esc:char('{')
XML Example esc
XML Example escape:char('*'):kill('1*23', '%d')
For the most part, there is very little reason to set Shortcut[සංස්කරණය]{key = value} pair, such that the key is XML Example text
XML Example undo
XML Example kill
XML Example escape:char('\\', {text = 'string'})
XML Example escape:char('\\'):text('string')
XML Example kill
XML Example text
XML Example undo
XML Example undo
XML Example text
|
Caveats
[සංස්කරණය]- When using a multi-character escape sequence, this module only marks it using the byte value of the first character. Thus, will unescape, for example, all characters escaped withXML Example
escape:undo()
andXML Example'e'
if both were used. In practice however this shouldn't be a problem as multiple escape sequences are pretty rare unless you're transitioning between multiple code languages. (Multiple multi-char escape sequences beginning with the same character are simply bad practice anyhow.)XML Example'esc'
- Since byte values are stored as numbers, it is not recommended for you to use a number as an escape sequence (though it may work just fine).
- Placeholder byte values separated with return () characters--chosen because they are seldom used at all, and virtually never used unpaired withXML Example
'\r'
; moreover, it is distinct from the markers generated byXML Example'\n'
<nowiki>...</nowiki>
or(which use the delete char). To set a different separator char, include the key-value pairXML Examplemw.text.nowiki()
{safeChr = alternate character}
in the table that you pass to escape:char().
Speed
[සංස්කරණය]The following are benchmarks...
when executing the following module function:
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
0.01942
when repeating the following line 500 times in a template:
{{#invoke:Escape|main|mode=kill|{{#invoke:Escape/testcases|test_string2}}|test}}
0.767
All times in seconds. The module time x500 was calculated when you loaded this doc page (normally between 0.02 and 0.07). The template time x500 was recorded on Jan 15, 2015.
Examples
[සංස්කරණය]Template
[සංස්කරණය]ආයාත කිරීමට පෙර ඉතිහාසය
[සංස්කරණය]oldid | date/time | username | edit summary |
---|---|---|---|
642652924 | 2015-01-15T20:45:13Z | Codehydro | |
642648556 | 2015-01-15T20:11:45Z | Codehydro | speed benchmark |
642495498 | 2015-01-14T19:30:40Z | Codehydro | |
642491193 | 2015-01-14T18:54:04Z | Codehydro | |
642490918 | 2015-01-14T18:51:43Z | Codehydro | |
642490836 | 2015-01-14T18:51:04Z | Codehydro | |
642490541 | 2015-01-14T18:48:41Z | Codehydro | [[WP:AES|←]]Created page with '-- Example Unit tests for [[Module:Bananas]]. Click talk page to run tests. local p = require('Module:UnitTests') function p:test_string() return self:equal...' |
Module
[සංස්කරණය]Here's some sample output from the debug console below the module editor:
local escape = require('Module:Escape')
test = 'test, \\test, \\{,test\\\\ \\\\ \\\\\\\\'
test2 = escape:char('{'):text(test)
=test2
test, \test, \7b 044 7btest\\ \\ \\\\ test3 = escape:char('\\'):text(test2)
=test3
test, 5c 0116 5cest, 5c 055 5cb 044 7btest5c 092 5c 5c 092 5c 5c 092 5c5c 092 5c test4 = escape:char('{', {undo = test3})
=test4
test, 5c 0116 5cest, 5c 055 5cb 044 7btest5c 092 5c 5c 092 5c 5c 092 5c5c 092 5c test4 = escape:char('\\', {undo = test3})
=test4
test, test, 7b 044 7btest\ \ \\ test5 = escape:char('{', {undo = test4})
=test5 == test
true =escape:undo(test3)--doesn't work because char is still set to '{' in current session
test, 5c 0116 5cest, 5c 055 5cb 044 7btest5c 092 5c 5c 092 5c 5c 092 5c5c 092 5c =escape:undo(test4)
test, \test, \,test\\ \\ \\\\ =escape:char('\\'):undo(test3)
test, test, 7b 044 7btest\ \ \\ =escape:char('{', {undo = escape:char('\\'):undo(test3)})
test, test, {,test\ \ \\ =test == escape:char('{', {undo = escape:char('\\'):undo(test3)})
false =test == escape:char('{', {undo = escape:char('\\'):undo(test3, '\\')})
true local t = 'test { test {\\{ test, \\test, \\{,test\\ \\ \\ {\\'
=t
test { test {\{ test, \test, \{,test\ \ \ {\ local e = require('Module:Escape')
local t2 = escape:text(t)
local t3 = string.gsub(t2, '{', )
local t4 = escape:undo(t3)
=t4
test test { test, test, {,test \ local tk0 = escape:kill(t, '{')
=tk0 == t4
true
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