| Line 10: |
Line 10: |
| | | | |
| | Global options | | Global options |
| − | ignore_errors: If set to 'true' or 1, any error condition will result in
| + | ignore_errors: If set to 'true' or 1, any error condition will result in |
| − | an empty string being returned rather than an error message.
| + | an empty string being returned rather than an error message. |
| | | | |
| − | error_category: If an error occurs, specifies the name of a category to
| + | error_category: If an error occurs, specifies the name of a category to |
| − | include with the error message. The default category is
| + | include with the error message. The default category is |
| − | [Category:Errors reported by Module String].
| + | [Category:Errors reported by Module String]. |
| | | | |
| − | no_category: If set to 'true' or 1, no category will be added if an error
| + | no_category: If set to 'true' or 1, no category will be added if an error |
| − | is generated.
| + | is generated. |
| | | | |
| | Unit tests for this module are available at Module:String/tests. | | Unit tests for this module are available at Module:String/tests. |
| Line 36: |
Line 36: |
| | | | |
| | Parameters | | Parameters |
| − | s: The string whose length to report
| + | s: The string whose length to report |
| | | | |
| | If invoked using named parameters, Mediawiki will automatically remove any leading or | | If invoked using named parameters, Mediawiki will automatically remove any leading or |
| Line 58: |
Line 58: |
| | | | |
| | Parameters | | Parameters |
| − | s: The string to return a subset of
| + | s: The string to return a subset of |
| − | i: The fist index of the substring to return, defaults to 1.
| + | i: The fist index of the substring to return, defaults to 1. |
| − | j: The last index of the string to return, defaults to the last character.
| + | j: The last index of the string to return, defaults to the last character. |
| | | | |
| | The first character of the string is assigned an index of 1. If either i or j | | The first character of the string is assigned an index of 1. If either i or j |
| Line 107: |
Line 107: |
| | | | |
| | --[[ | | --[[ |
| − | match
| + | _match |
| | | | |
| | This function returns a substring from the source string that matches a | | This function returns a substring from the source string that matches a |
| − | specified pattern. | + | specified pattern. It is exported for use in other modules |
| | | | |
| | Usage: | | Usage: |
| − | {{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
| + | strmatch = require("Module:String")._match |
| − | OR
| + | sresult = strmatch( s, pattern, start, match, plain, nomatch ) |
| − | {{#invoke:String|match|s=source_string|pattern=pattern_string|start=start_index
| |
| − | |match=match_number|plain=plain_flag|nomatch=nomatch_output}}
| |
| | | | |
| | Parameters | | Parameters |
| − | s: The string to search
| + | s: The string to search |
| − | pattern: The pattern or string to find within the string
| + | pattern: The pattern or string to find within the string |
| − | start: The index within the source string to start the search. The first
| + | start: The index within the source string to start the search. The first |
| − | character of the string has index 1. Defaults to 1.
| + | character of the string has index 1. Defaults to 1. |
| − | match: In some cases it may be possible to make multiple matches on a single
| + | match: In some cases it may be possible to make multiple matches on a single |
| − | string. This specifies which match to return, where the first match is
| + | string. This specifies which match to return, where the first match is |
| − | match= 1. If a negative number is specified then a match is returned
| + | match= 1. If a negative number is specified then a match is returned |
| − | counting from the last match. Hence match = -1 is the same as requesting
| + | counting from the last match. Hence match = -1 is the same as requesting |
| − | the last match. Defaults to 1.
| + | the last match. Defaults to 1. |
| − | plain: A flag indicating that the pattern should be understood as plain
| + | plain: A flag indicating that the pattern should be understood as plain |
| − | text. Defaults to false.
| + | text. Defaults to false. |
| − | nomatch: If no match is found, output the "nomatch" value rather than an error.
| + | nomatch: If no match is found, output the "nomatch" value rather than an error. |
| − | | |
| − | If invoked using named parameters, Mediawiki will automatically remove any leading or
| |
| − | trailing whitespace from each string. In some circumstances this is desirable, in
| |
| − | other cases one may want to preserve the whitespace.
| |
| − | | |
| − | If the match_number or start_index are out of range for the string being queried, then
| |
| − | this function generates an error. An error is also generated if no match is found.
| |
| − | If one adds the parameter ignore_errors=true, then the error will be suppressed and
| |
| − | an empty string will be returned on any failure.
| |
| | | | |
| | For information on constructing Lua patterns, a form of [regular expression], see: | | For information on constructing Lua patterns, a form of [regular expression], see: |
| Line 209: |
Line 198: |
| | end | | end |
| | end | | end |
| | + | |
| | + | --[[ |
| | + | match |
| | + | |
| | + | This function returns a substring from the source string that matches a |
| | + | specified pattern. |
| | + | |
| | + | Usage: |
| | + | {{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}} |
| | + | OR |
| | + | {{#invoke:String|match|s=source_string|pattern=pattern_string|start=start_index |
| | + | |match=match_number|plain=plain_flag|nomatch=nomatch_output}} |
| | + | |
| | + | Parameters |
| | + | s: The string to search |
| | + | pattern: The pattern or string to find within the string |
| | + | start: The index within the source string to start the search. The first |
| | + | character of the string has index 1. Defaults to 1. |
| | + | match: In some cases it may be possible to make multiple matches on a single |
| | + | string. This specifies which match to return, where the first match is |
| | + | match= 1. If a negative number is specified then a match is returned |
| | + | counting from the last match. Hence match = -1 is the same as requesting |
| | + | the last match. Defaults to 1. |
| | + | plain: A flag indicating that the pattern should be understood as plain |
| | + | text. Defaults to false. |
| | + | nomatch: If no match is found, output the "nomatch" value rather than an error. |
| | + | |
| | + | If invoked using named parameters, Mediawiki will automatically remove any leading or |
| | + | trailing whitespace from each string. In some circumstances this is desirable, in |
| | + | other cases one may want to preserve the whitespace. |
| | + | |
| | + | If the match_number or start_index are out of range for the string being queried, then |
| | + | this function generates an error. An error is also generated if no match is found. |
| | + | If one adds the parameter ignore_errors=true, then the error will be suppressed and |
| | + | an empty string will be returned on any failure. |
| | + | |
| | + | For information on constructing Lua patterns, a form of [regular expression], see: |
| | + | |
| | + | * http://www.lua.org/manual/5.1/manual.html#5.4.1 |
| | + | * http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns |
| | + | * http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns |
| | + | |
| | + | ]] |
| | -- This is the entry point for #invoke:String|match | | -- This is the entry point for #invoke:String|match |
| | function str.match( frame ) | | function str.match( frame ) |
| Line 233: |
Line 265: |
| | | | |
| | Parameters | | Parameters |
| − | target: The string to search
| + | target: The string to search |
| − | pos: The index for the character to return
| + | pos: The index for the character to return |
| | | | |
| | If invoked using named parameters, Mediawiki will automatically remove any leading or | | If invoked using named parameters, Mediawiki will automatically remove any leading or |
| Line 302: |
Line 334: |
| | | | |
| | Parameters | | Parameters |
| − | source: The string to search
| + | source: The string to search |
| − | target: The string or pattern to find within source
| + | target: The string or pattern to find within source |
| − | start: The index within the source string to start the search, defaults to 1
| + | start: The index within the source string to start the search, defaults to 1 |
| − | plain: Boolean flag indicating that target should be understood as plain
| + | plain: Boolean flag indicating that target should be understood as plain |
| − | text and not as a Lua style regular expression, defaults to true
| + | text and not as a Lua style regular expression, defaults to true |
| | | | |
| | If invoked using named parameters, Mediawiki will automatically remove any leading or | | If invoked using named parameters, Mediawiki will automatically remove any leading or |
| Line 353: |
Line 385: |
| | | | |
| | Parameters | | Parameters |
| − | source: The string to search
| + | source: The string to search |
| − | pattern: The string or pattern to find within source
| + | pattern: The string or pattern to find within source |
| − | replace: The replacement text
| + | replace: The replacement text |
| − | count: The number of occurences to replace, defaults to all.
| + | count: The number of occurences to replace, defaults to all. |
| − | plain: Boolean flag indicating that pattern should be understood as plain
| + | plain: Boolean flag indicating that pattern should be understood as plain |
| − | text and not as a Lua style regular expression, defaults to true
| + | text and not as a Lua style regular expression, defaults to true |
| | ]] | | ]] |
| | function str.replace( frame ) | | function str.replace( frame ) |
| Line 390: |
Line 422: |
| | | | |
| | --[[ | | --[[ |
| − | simple function to pipe string.rep to templates.
| + | simple function to pipe string.rep to templates. |
| | ]] | | ]] |
| | function str.rep( frame ) | | function str.rep( frame ) |
| Line 412: |
Line 444: |
| | | | |
| | Parameters | | Parameters |
| − | pattern_string: The pattern string to escape.
| + | pattern_string: The pattern string to escape. |
| | ]] | | ]] |
| | function str.escapePattern( frame ) | | function str.escapePattern( frame ) |
| Line 552: |
Line 584: |
| | function str._escapePattern( pattern_str ) | | function str._escapePattern( pattern_str ) |
| | return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ) | | return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ) |
| − | end
| |
| − |
| |
| − | --[[
| |
| − | check if string is a number
| |
| − | ]]
| |
| − | function str.isNumber( frame )
| |
| − | if not tonumber( frame.args[1] ) then
| |
| − | return 0
| |
| − | else
| |
| − | return 1
| |
| − | end
| |
| | end | | end |
| | | | |
| | return str | | return str |