模块:String:修订间差异

删除2,339字节 、​ 2022年10月6日 (星期四)
更新“模块:String”并翻译部分内容 // 维基盐编辑器
(创建页面,内容为“--[[ 引自维基百科(enwp:Module:String,oldid=552254999) This module is intended to provide access to basic string functions. Most of the functions provided here can be invoked with named parameters, unnamed parameters, or a mixture. If named parameters are used, Mediawiki will automatically remove any leading or trailing whitespace from the parameter. Depending on the intended use, it may be advantageous to either preserve or remove such whi…”)
 
(更新“模块:String”并翻译部分内容 // 维基盐编辑器)
 
-- 此模块引用自维基百科同名页面,引用行为依据 CC BY-SA 3.0 条款
-- 部分内容因为本地化或其他修改,可能与原模块有出入
--[[
 
该模块提供了基本的字符串函数。
引自维基百科(enwp:Module:String,oldid=552254999)
 
这里的大多数函数都能以命名参数、匿名参数或混合前两种的形式调用。
This module is intended to provide access to basic string functions.
若使用命名参数,Mediawiki 将自动删除参数前后的空格符号。
是否应该删除或保留前后空格,取决于具体用途。
 
全局配置
Most of the functions provided here can be invoked with named parameters,
ignore_errors: 如果设为 'true' 或 1,任何出错都将返回空字符串而非报错信息。
unnamed parameters, or a mixture. If named parameters are used, Mediawiki will
automatically remove any leading or trailing whitespace from the parameter.
Depending on the intended use, it may be advantageous to either preserve or
remove such whitespace.
 
Global options
ignore_errors: If set to 'true' or 1, any error condition will result in
an empty string being returned rather than an error message.
error_category: 如果发生异常,报错信息会自动添加一个分类到页面上,默认分类是
error_category: If an error occurs, specifies the name of a category to
[Category:字符串模块报告的错误]。
include with the error message. The default category is
[Category:Errors reported by Module String].
no_category: If set to如果设为 'true' or 1, no category will be added if an error1,报错时不会添加分类。
is generated.
Unit tests for this module are available at你可以在 Module:String/tests. 找到单元测试。
----
该模块旨在提供对基本字符串函数的访问。
 
这里提供的大多数函数都可以用命名参数调用,
未命名的参数或混合着用。 如果使用命名参数,媒体维基将会
从参数中自动删除任何前部或者尾部的空格符号。
取决于预期的用途,保存或可能是有利的
删除这样的空白。
 
全局选项
re_errors:如果设置为'true'或1,则会导致任何错误情况符串而不是错误消息。
r_category:如果发生错误,请指定要分类的名称消息。 默认分类是 [Category:Errors reported by Module String]。(类别:模块字符串报告的错误)
no_category:如果设置为'true'或1,如果发生错误,则不会添加任何分类 生成。
这个模块的测试单元可以在Module:String / tests下找到。
]]
 
len
 
返回目标字符串的长度。
This function returns the length of the target string.
 
用法:
Usage:
{{#invoke:String|len|target_string目标字符串|}}
OR
{{#invoke:String|len|s=target_string目标字符串}}
 
Parameters
s: The string whose length to report
 
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the target string.
----
LEN
 
该函数返回目标字符串的长度。
 
用法:
{{#invoke:String|len|target_string|}}
要么
{{#invoke:String|len|s=target_string}}
 
参数:
s: s:报告计算长度的字符串长度
 
如果使用命名参数(第二种调用,媒体维基方法),Mediawiki 将自动删除任何目标字符串部或后的空格符号。
后部目标字符串的空格。
]]
function str.len( frame )
sub
 
根据指定索引,从目标字符串中获取子字符串
This function returns a substring of the target string at specified indices.
 
Usage:
{{#invoke:String|sub|target_string|start_index|end_index}}
OR
{{#invoke:String|sub|s=target_string|i=start_index|j=end_index}}
 
Parameters
s: The string to return a subset of
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.
The first character of the string is assigned an index of 1. If either i or j
is a negative value, it is interpreted the same as selecting a character by
counting from the end of the string. Hence, a value of -1 is the same as
selecting the last character of the string.
 
If the requested indices are out of range for the given string, an error is
reported.
----
 
該函數返回指定索引處目標字符串的子字符串。
 
用法:
{{#invoke:String|sub|目标字符串|开始位置|结束位置}}
{{#invoke:字符串|分| target_string| START_INDEX| END_INDEX}}
要么
{{#invoke:String|sub|s=目标字符串|i=开始位置|j=结束位置}}
{{#invoke:字符串|子| S= target_string| I= START_INDEX| J = END_INDEX}}
 
参数:
參數
s: s:返回一個目标字符串,集的字符串来自于它。
i: i:要返回的子字符串的第一個索引开始位置,默認為认为 1。
j: j:要返回的字符串的最後一個索引结束位置,默認為认为個字符
字符串第一字符被分配索引1.如果i或j
如果 i 或 j 传入了负数,将视为从字符串末尾开始计数的索引。
是一個負值,它被解釋為與通過選擇一個字符相同
比如 -1 会视为字符串的最后一位。
從字符串的末尾開始計數。 因此,-1的值與1相同
選擇字符串的最後一個字符。
 
如果請求的索引位置超出給定字符串的範圍长度則會出現錯誤将会报错。
報導。
]]
function str.sub( frame )
local len = mw.ustring.len( s );
 
-- 转换负数并检查区间
-- Convert negatives for range checking
if i < 0 then
i = len + i + 1;
if i > len or j > len or i < 1 or j < 1 then
return str._error( 'String subset index out of range截取字符串索引脱离区间' );
end
if j < i then
return str._error( 'String subset indices out of order截取字符串指示脱离顺序' );
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|pos|s=source_string目标字符串|pattern=pattern_string|start=start_index
|match=match_number|plain=plain_flag|nomatch=nomatch_output无匹配输出}}
 
参数:
Parameters
s: 目标字符串,以它查找匹配
s: The string to search
pattern: 在目标字符串中查找用的,指定模式或字符串
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: 指示 pattern 参数是否视为"字符串"的标记,默认为否(即默认视为"模式")。
plain: A flag indicating that the pattern should be understood as plain
nomatch: 如果找不到匹配项,输出 "nomatch" 的值而非报错。
text. Defaults to false.
nomatch: If no match is found, output the "nomatch" value rather than an error.
 
若使用命名参数(第二种调用方法),Mediawiki 将自动删除参数前后的空格符号。
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.
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
an empty string will be returned on any failure.
 
关于构造 Lua 匹配模式的更多信息,一种[正则表达式],参见:
For information on constructing Lua patterns, a form of [regular expression], see:
 
* http://www.lua.org/manual/5.1/manual.html#5.4.1
if s == '' then
return str._error( 'Target string is empty目标字符串是空的' );
end
if pattern == '' then
return str._error( 'Pattern string is empty模式字符串是空的' );
end
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
return str._error( 'Requested start is out of range要求的起始点脱离区间' );
end
if match_index == 0 then
return str._error( 'Match index is out of range匹配索引脱离区间' );
end
if plain_flag then
if result == nil then
if nomatch == nil then
return str._error( 'Match not found找不到匹配' );
else
return nomatch;
pos
 
返回目标字符串在索引位置的那个字符。
This function returns a single character from the target string at position pos.
 
用法:
Usage:
{{#invoke:String|pos|target_string目标字符串|index_value索引值}}
OR
{{#invoke:String|pos|target=target_string目标字符串|pos=index_value索引值}}
 
参数:
Parameters
target: The string to search目标字符串,从它获取字符
pos: 需要返回的字符之索引位置
pos: The index for the character to return
 
If invoked using named parameters, Mediawiki will automatically remove any leading or
other cases one may want to preserve the whitespace.
 
字符串第一个字符的索引为 1。
The first character has an index value of 1.
 
If one requests a negative value, this function will select a character by counting backwards
 
if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then
return str._error( 'String index out of range字符串索引脱离区间' );
end
string.
 
用法:
Usage:
{{#invoke:String|find|source_str|target_string|start_index|plain_flag}}
OR
{{#invoke:String|find|source=source_str|target=target_str|start=start_index|plain=plain_flag}}
 
参数:
Parameters
source: The string to search
target: The string or pattern to find within source
start: The index within the source string to start the search, defaults to默认为 1
plain: 指示 pattern 参数是否视为"字符串"而非 Lua 风格的正则表达式的 Boolean 标记, 默认为 true。
plain: Boolean flag indicating that target should be understood as plain
text and not as a Lua style regular expression, defaults to true
 
If invoked using named parameters, Mediawiki will automatically remove any leading or
string.
 
用法:
Usage:
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
OR
count=replacement_count|plain=plain_flag}}
 
参数:
Parameters
source: The string to search
pattern: The string or pattern to find within source
replace: The replacement text
count: The number of occurences to replace, defaults to默认为 all.
plain: 指示 pattern 参数是否视为"字符串"而非 Lua 风格的正则表达式的 Boolean 标记, 默认为 true。
plain: Boolean flag indicating that pattern should be understood as plain
text and not as a Lua style regular expression, defaults to true
]]
function str.replace( frame )
return string.rep( frame.args[1] or '', repetitions )
end
 
 
function str.split(inputstr, sep, no_pattern, ignore_null)
--#invoke 支
if type(inputstr) == type({table}) then
if not getArgs then getArgs = require('Module:Arguments').getArgs end
 
length = mw.ustring.len(input_str)
--split函
if no_pattern_flag then
separ = mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(separ,
join
 
合并所有非空参数;第一个参数是分隔符。
Join all non empty arguments together; the first argument is the separator.
 
Usage:
用法:
{{#invoke:String|join|sep|one|two|three}}
{{#invoke:String|join|分隔符|文字1|文字2|文字3}}
]]
function str.join(frame)
 
--[[
处理报错信息的辅助函数。
Helper function to handle error messages.
]]
function str._error( error_str )
local frame = mw.getCurrentFrame();
local error_category = frame.args.error_category or 'Errors reported by Module String字符串模块报告的错误';
local ignore_errors = frame.args.ignore_errors or false;
local no_category = frame.args.no_category or false;
end
local error_str = '<strong class="error">String Module Error: 字符串模块出错:' .. error_str .. '</strong>';
if error_category ~= '' and not str._getBoolean( no_category ) then
error_str = '[[Category:' .. error_category .. ']]' .. error_str;
 
--[[
Helper Function to interpret解析 boolean strings 的辅助函数。
]]
function str._getBoolean( boolean_str )
boolean_value = boolean_str;
else
error( 'No boolean value found找不到布尔值' );
end
return boolean_value
 
--[[
转换正则表达式字符,使之能作为普通字符串解析的辅助函数。
Helper function that escapes all pattern characters so that they will be treated
as plain text.
]]
function str._escapePattern( pattern_str )