| 第87行: |
第87行: |
| | | | |
| | if i > len or j > len or i < 1 or j < 1 then | | if i > len or j > len or i < 1 or j < 1 then |
| − | return str._error( 'String subset index out of range' ); | + | return str._error( ' 截取字符串索引脱离区间' ); |
| | end | | end |
| | if j < i then | | if j < i then |
| − | return str._error( 'String subset indices out of order' ); | + | return str._error( ' 截取字符串指示脱离顺序' ); |
| | end | | end |
| | | | |
| 第146行: |
第146行: |
| | * http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns | | * http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns |
| | * http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns | | * http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns |
| | + | |
| | ]] | | ]] |
| | function str.match( frame ) | | function str.match( frame ) |
| 第157行: |
第158行: |
| | | | |
| | if s == '' then | | if s == '' then |
| − | return str._error( 'Target string is empty' ); | + | return str._error( ' 目标字符串是空的' ); |
| | end | | end |
| | if pattern == '' then | | if pattern == '' then |
| − | return str._error( 'Pattern string is empty' ); | + | return str._error( ' 模式字符串是空的' ); |
| | end | | end |
| | if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then | | if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then |
| − | return str._error( 'Requested start is out of range' ); | + | return str._error( ' 要求的起始点脱离区间' ); |
| | end | | end |
| | if match_index == 0 then | | if match_index == 0 then |
| − | return str._error( 'Match index is out of range' ); | + | return str._error( ' 匹配索引脱离区间' ); |
| | end | | end |
| | if plain_flag then | | if plain_flag then |
| 第206行: |
第207行: |
| | if result == nil then | | if result == nil then |
| | if nomatch == nil then | | if nomatch == nil then |
| − | return str._error( 'Match not found' ); | + | return str._error( ' 找不到匹配' ); |
| | else | | else |
| | return nomatch; | | return nomatch; |
| 第246行: |
第247行: |
| | | | |
| | if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then | | if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then |
| − | return str._error( 'String index out of range' ); | + | return str._error( ' 字符串索引脱离区间' ); |
| | end | | end |
| | | | |
| 第380行: |
第381行: |
| | | | |
| | return result; | | return result; |
| | + | end |
| | + | |
| | + | --[[ |
| | + | simple function to pipe string.rep to templates. |
| | + | ]] |
| | + | |
| | + | function str.rep( frame ) |
| | + | local repetitions = tonumber( frame.args[2] ) |
| | + | if not repetitions then |
| | + | return str._error( 'function rep expects a number as second parameter, received "' .. ( frame.args[2] or '' ) .. '"' ) |
| | + | end |
| | + | 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 |
| | + | args = getArgs(inputstr, {parentFirst=true}) |
| | + | for arg_name, arg_value in pairs( args ) do |
| | + | if arg_name == 1 or arg_name == '1' or arg_name == "str" or arg_name == "inputstr" or arg_name == "input" then |
| | + | input_str = arg_value |
| | + | elseif arg_name == 2 or arg_name == '2' or arg_name == "sep" or arg_name == "separator" then |
| | + | separ = arg_value |
| | + | elseif arg_name == 3 or arg_name == '3' or arg_name == "no_pattern" or arg_name == "no pattern" then |
| | + | no_pattern_flag = arg_value |
| | + | elseif arg_name == 4 or arg_name == '4' or arg_name == "ignore_null" or arg_name == "ignore null" then |
| | + | ignore_null_flag = arg_value |
| | + | elseif arg_name == 5 or arg_name == '5' or arg_name == "format" then |
| | + | format = arg_value or "*{{{1}}}\n"; |
| | + | end |
| | + | end |
| | + | if not yesno then yesno = require('Module:Yesno') end |
| | + | no_pattern_flag = yesno( no_pattern_flag or 'yes' ) |
| | + | ignore_null_flag = yesno( ignore_null_flag or 'no' ) |
| | + | is_invoke = true |
| | + | format = mw.ustring.gsub(format or "*{{{1}}}\n", "%{%{%{.-%}%}%}", "%%s" ); |
| | + | it = mw.ustring.find(format, "%%s", 1) |
| | + | if it == nil then format = format .. "%s" end |
| | + | format = mw.ustring.gsub(format, "\\n", "\n") |
| | + | else |
| | + | input_str = inputstr |
| | + | separ = sep |
| | + | no_pattern_flag = no_pattern |
| | + | ignore_null_flag = ignore_null |
| | + | is_invoke = false |
| | + | end |
| | + | input_str = input_str or '' |
| | + | separ = separ or "%s" |
| | + | if no_pattern_flag == nil then no_pattern_flag = true end |
| | + | if ignore_null_flag == nil then ignore_null_flag = false 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, |
| | + | "%[", "%["), "%]", "%]"), "%{", "%{"), "%}", "%}"), "%%", "%%%%"), "%)", "%)"), "%-", "%-"), |
| | + | "%^", "%^"), "%$", "%$"), "%(", "%("), "%.", "%."), "%*", "%*"), "%+", "%+"), "%|", "%|"); |
| | + | end |
| | + | iterator = 1 ; i = 1 ; flag = true |
| | + | result = {} |
| | + | separ_str_begin, separ_str_end = mw.ustring.find(input_str, separ, iterator) |
| | + | -- |
| | + | debug1 = 1 |
| | + | -- |
| | + | while flag do |
| | + | debug1 = debug1 + 1 |
| | + | if separ_str_begin == nil or iterator > length or debug1 >= 100 then |
| | + | separ_str_begin = 0 |
| | + | separ_str_end = -2 |
| | + | flag = false |
| | + | end |
| | + | if separ_str_end < separ_str_begin then separ_str_end = separ_str_begin end |
| | + | finded_str = mw.ustring.sub(input_str, iterator, separ_str_begin - 1) |
| | + | if not(mw.text.trim(finded_str) == '' and ignore_null_flag) then |
| | + | result[i] = finded_str |
| | + | i = i + 1 |
| | + | end |
| | + | |
| | + | iterator = separ_str_end + 1 |
| | + | separ_str_begin, separ_str_end = mw.ustring.find(input_str, separ, iterator) |
| | + | end |
| | + | if is_invoke then |
| | + | body = '' |
| | + | for i, result_str in pairs( result ) do |
| | + | body = body .. mw.ustring.gsub(format, "%%s", result_str) |
| | + | end |
| | + | return body |
| | + | end |
| | + | return result; |
| | + | end |
| | + | |
| | + | --[[ |
| | + | join |
| | + | |
| | + | Join all non empty arguments together; the first argument is the separator. |
| | + | Usage: |
| | + | {{#invoke:String|join|sep|one|two|three}} |
| | + | ]] |
| | + | function str.join(frame) |
| | + | local args = {} |
| | + | local sep |
| | + | for _, v in ipairs( frame.args ) do |
| | + | if sep then |
| | + | if v ~= '' then |
| | + | table.insert(args, v) |
| | + | end |
| | + | else |
| | + | sep = v |
| | + | end |
| | + | end |
| | + | return table.concat( args, sep or '' ) |
| | end | | end |
| | | | |
| 第410行: |
第524行: |
| | function str._error( error_str ) | | function str._error( error_str ) |
| | local frame = mw.getCurrentFrame(); | | local frame = mw.getCurrentFrame(); |
| − | local error_category = frame.args.error_category or 'Errors reported by Module String'; | + | local error_category = frame.args.error_category or ' 字符串模块报告的错误'; |
| | local ignore_errors = frame.args.ignore_errors or false; | | local ignore_errors = frame.args.ignore_errors or false; |
| | local no_category = frame.args.no_category or false; | | local no_category = frame.args.no_category or false; |
| 第418行: |
第532行: |
| | end | | end |
| | | | |
| − | local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>'; | + | local error_str = '<strong class="error"> 字符串模块出错:' .. error_str .. '</strong>'; |
| | if error_category ~= '' and not str._getBoolean( no_category ) then | | if error_category ~= '' and not str._getBoolean( no_category ) then |
| | error_str = '[[Category:' .. error_category .. ']]' .. error_str; | | error_str = '[[Category:' .. error_category .. ']]' .. error_str; |
| 第443行: |
第557行: |
| | boolean_value = boolean_str; | | boolean_value = boolean_str; |
| | else | | else |
| − | error( 'No boolean value found' ); | + | error( ' 布尔值找不到' ); |
| | end | | end |
| | return boolean_value | | return boolean_value |