편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.
최신판 | 당신의 편집 | ||
2번째 줄: | 2번째 줄: | ||
local ustring = mw.ustring | local ustring = mw.ustring | ||
local | local str = {} | ||
-- string.find | -- string.find | ||
function | function str.len( frame ) | ||
local new_args = | local new_args = str._getParameters( frame.args, {'s'} ) | ||
local s = new_args['s'] or '' | local s = new_args['s'] or '' | ||
return ustring.len( s ) | return ustring.len( s ) | ||
end | end | ||
-- string.sub | -- string.sub | ||
function | function str.sub( frame ) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local new_args = | local new_args = str._getParameters( args, { 's', 'i', 'j' } ) | ||
local s = new_args['s'] or '' | local s = new_args['s'] or '' | ||
local i = tonumber( new_args['i'] ) or 1 | local i = tonumber( new_args['i'] ) or 1 | ||
29번째 줄: | 29번째 줄: | ||
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 | return str._error( 'String subset index out of range' ) | ||
end | end | ||
if j < i then | if j < i then | ||
return | return str._error( 'String subset indices out of order' ) | ||
end | end | ||
39번째 줄: | 39번째 줄: | ||
-- 다른 모듈에서 require 되는 함수 | -- 다른 모듈에서 require 되는 함수 | ||
function | function str._match( s, pattern, start, match_index, plain_flag, nomatch ) | ||
if s == '' then | if s == '' then | ||
return | return str._error( 'Target string is empty' ) | ||
end | end | ||
if pattern == '' then | if pattern == '' then | ||
return | return str._error( 'Pattern string is empty' ) | ||
end | end | ||
start = tonumber(start) or 1 | start = tonumber(start) or 1 | ||
if math.abs(start) < 1 or math.abs(start) > ustring.len( s ) then | if math.abs(start) < 1 or math.abs(start) > ustring.len( s ) then | ||
return | return str._error( 'Requested start is out of range' ) | ||
end | end | ||
if match_index == 0 then | if match_index == 0 then | ||
return | return str._error( 'Match index is out of range' ) | ||
end | end | ||
if plain_flag then | if plain_flag then | ||
pattern = | pattern = str._escapePattern( pattern ) | ||
end | end | ||
91번째 줄: | 91번째 줄: | ||
if result == nil then | if result == nil then | ||
if nomatch == nil then | if nomatch == nil then | ||
return | return str._error( 'Match not found' ) | ||
else | else | ||
return nomatch | return nomatch | ||
101번째 줄: | 101번째 줄: | ||
-- string.match | -- string.match | ||
function | function str.match( frame ) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local new_args = | local new_args = str._getParameters( args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} ) | ||
local s = new_args['s'] or '' | local s = new_args['s'] or '' | ||
local start = tonumber( new_args['start'] ) or 1 | local start = tonumber( new_args['start'] ) or 1 | ||
local plain_flag = | local plain_flag = str._getBoolean( new_args['plain'] or false ) | ||
local pattern = new_args['pattern'] or '' | local pattern = new_args['pattern'] or '' | ||
local match_index = math.floor( tonumber(new_args['match']) or 1 ) | local match_index = math.floor( tonumber(new_args['match']) or 1 ) | ||
local nomatch = new_args['nomatch'] | local nomatch = new_args['nomatch'] | ||
return | return str._match( s, pattern, start, match_index, plain_flag, nomatch ) | ||
end | end | ||
-- | -- | ||
function | function str.pos( frame ) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local new_args = | local new_args = str._getParameters( args, {'target', 'pos'} ) | ||
local target_str = new_args['target'] or '' | local target_str = new_args['target'] or '' | ||
local pos = tonumber( new_args['pos'] ) or 0 | local pos = tonumber( new_args['pos'] ) or 0 | ||
if pos == 0 or math.abs(pos) > ustring.len( target_str ) then | if pos == 0 or math.abs(pos) > ustring.len( target_str ) then | ||
return | return str._error( 'String index out of range' ) | ||
end | end | ||
129번째 줄: | 129번째 줄: | ||
-- string.find | -- string.find | ||
function | function str.find( frame ) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local new_args = | local new_args = str._getParameters( args, {'source', 'target', 'start', 'plain' } ) | ||
local source_str = new_args['source'] or '' | local source_str = new_args['source'] or '' | ||
local pattern = new_args['target'] or '' | local pattern = new_args['target'] or '' | ||
141번째 줄: | 141번째 줄: | ||
end | end | ||
plain = | plain = str._getBoolean( plain ) | ||
local start = ustring.find( source_str, pattern, start_pos, plain ) | local start = ustring.find( source_str, pattern, start_pos, plain ) | ||
152번째 줄: | 152번째 줄: | ||
end | end | ||
function | function str.find_( str1, str2 , str3, str4) | ||
local source_str = str1 or '' | local source_str = str1 or '' | ||
local pattern_str = str2 or '' | local pattern_str = str2 or '' | ||
162번째 줄: | 162번째 줄: | ||
end | end | ||
plain_ = | plain_ = str._getBoolean( plain_ ) | ||
local start = ustring.find( source_str, pattern_str, start_, plain_ ) | local start = ustring.find( source_str, pattern_str, start_, plain_ ) | ||
173번째 줄: | 173번째 줄: | ||
end | end | ||
function | function str.ifexist( frame ) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local new_args = | local new_args = str._getParameters( args, {'source', 'target', 'return1', 'return2' } ) | ||
local source_str = new_args['source'] or '' | local source_str = new_args['source'] or '' | ||
local pattern = new_args['target'] or '' | local pattern = new_args['target'] or '' | ||
181번째 줄: | 181번째 줄: | ||
local return2 = new_args['return2'] or '' | local return2 = new_args['return2'] or '' | ||
if | if str.find_( source_str, pattern) == 0 then | ||
return return2 | return return2 | ||
else | else | ||
188번째 줄: | 188번째 줄: | ||
end | end | ||
function | function str.ifexist_( source_str, pattern, return1, return2 ) | ||
if | if str.find_( source_str, pattern) == 0 then | ||
return return2 | return return2 | ||
else | else | ||
197번째 줄: | 197번째 줄: | ||
-- string.gsub | -- string.gsub | ||
function | function str.gsub( frame ) | ||
local new_args = | local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } ) | ||
local source_str = new_args['source'] or '' | local source_str = new_args['source'] or '' | ||
local pattern = new_args['pattern'] or '' | local pattern = new_args['pattern'] or '' | ||
208번째 줄: | 208번째 줄: | ||
return source_str | return source_str | ||
end | end | ||
plain = | plain = str._getBoolean( plain ) | ||
if plain then | if plain then | ||
pattern = | pattern = str._escapePattern( pattern ) | ||
replace = ustring.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences. | replace = ustring.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences. | ||
end | end | ||
234번째 줄: | 234번째 줄: | ||
-- string.rep | -- string.rep | ||
function | function str.rep( frame ) | ||
local repetitions = tonumber( frame.args[2] ) | local repetitions = tonumber( frame.args[2] ) | ||
if not repetitions then | if not repetitions then | ||
return | return str._error( 'function rep expects a number as second parameter, received "' .. ( frame.args[2] or '' ) .. '"' ) | ||
end | end | ||
return ustring.rep( frame.args[1] or '', repetitions ) | return ustring.rep( frame.args[1] or '', repetitions ) | ||
243번째 줄: | 243번째 줄: | ||
-- string.upper | -- string.upper | ||
function | function str.upper( frame ) | ||
local new_args = | local new_args = str._getParameters( frame.args, { 's' } ) | ||
local s = new_args['s'] or '' | local s = new_args['s'] or '' | ||
return ustring.upper( s ) | return ustring.upper( s ) | ||
250번째 줄: | 250번째 줄: | ||
-- string.lower | -- string.lower | ||
function | function str.lower( frame ) | ||
local new_args = | local new_args = str._getParameters( frame.args, { 's' } ) | ||
local s = new_args['s'] or '' | local s = new_args['s'] or '' | ||
return ustring.lower( s ) | return ustring.lower( s ) | ||
end | end | ||
function | function str.reverse( frame ) | ||
local new_args = | local new_args = str._getParameters( frame.args, { 's' } ) | ||
local s = new_args['s'] or '' | local s = new_args['s'] or '' | ||
local str = "" | local str = "" | ||
267번째 줄: | 267번째 줄: | ||
-- string.byte의 유니코드 호환 버전 | -- string.byte의 유니코드 호환 버전 | ||
function | function str.tounicode( frame ) | ||
local args = getArgs( frame ) | local args = getArgs( frame ) | ||
local s = args[1] or -1 | local s = args[1] or -1 | ||
277번째 줄: | 277번째 줄: | ||
end | end | ||
function | function str.tounicode_( char ) | ||
local s = char or -1 | local s = char or -1 | ||
if s == -1 then | if s == -1 then | ||
287번째 줄: | 287번째 줄: | ||
-- string.char | -- string.char | ||
function | function str.char( frame ) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local new_args = | local new_args = str._getParameters( args, { 'c' } ) | ||
local c = tonumber(new_args['c']) | local c = tonumber(new_args['c']) | ||
return ustring.char( c ) | return ustring.char( c ) | ||
end | end | ||
function | function str.escapePattern( frame ) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local pattern_str = args[1] | local pattern_str = args[1] | ||
if not pattern_str then | if not pattern_str then | ||
return | return str._error( 'No pattern string specified' ) | ||
end | end | ||
local result = | local result = str._escapePattern( pattern_str ) | ||
return result | return result | ||
end | end | ||
function | function str.count(frame) | ||
local args_ = getArgs(frame) | local args_ = getArgs(frame) | ||
local args = | local args = str._getParameters(args_, {'source', 'pattern', 'plain'}) | ||
local source = args.source or '' | local source = args.source or '' | ||
local pattern = args.pattern or '' | local pattern = args.pattern or '' | ||
local plain = | local plain = str._getBoolean(args.plain or true) | ||
if plain then | if plain then | ||
pattern = | pattern = str._escapePattern(pattern) | ||
end | end | ||
local _, count = ustring.gsub(source, pattern, '') | local _, count = ustring.gsub(source, pattern, '') | ||
317번째 줄: | 317번째 줄: | ||
end | end | ||
function | function str.count_(str, pattern) | ||
local _, count = ustring.gsub(str, pattern, '') | local _, count = ustring.gsub(str, pattern, '') | ||
return count | return count | ||
326번째 줄: | 326번째 줄: | ||
This function determines whether a string ends with another string. | This function determines whether a string ends with another string. | ||
]] | ]] | ||
function | function str.endswith(frame) | ||
local args_ = getArgs(frame) | local args_ = getArgs(frame) | ||
local args = | local args = str._getParameters(args_, {'source', 'pattern'}) | ||
local source = args.source or '' | local source = args.source or '' | ||
local pattern = args.pattern or '' | local pattern = args.pattern or '' | ||
342번째 줄: | 342번째 줄: | ||
end | end | ||
function | function str.join(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local table_ = {} | local table_ = {} | ||
367번째 줄: | 367번째 줄: | ||
we sometimes want to either preserve or remove that whitespace depending on the application. | we sometimes want to either preserve or remove that whitespace depending on the application. | ||
]] | ]] | ||
function | function str._getParameters( frame_args, arg_list ) | ||
local new_args = {} | local new_args = {} | ||
local index = 1 | local index = 1 | ||
387번째 줄: | 387번째 줄: | ||
Helper function to handle error messages. | Helper function to handle error messages. | ||
]] | ]] | ||
function | function str._error( error_str ) | ||
local frame = mw.getCurrentFrame() | local frame = mw.getCurrentFrame() | ||
local error_category = frame.args.error_category or 'String 모듈에 의해 보고된 오류' | local error_category = frame.args.error_category or 'String 모듈에 의해 보고된 오류' | ||
393번째 줄: | 393번째 줄: | ||
local no_category = frame.args.no_category or false | local no_category = frame.args.no_category or false | ||
if | if str._getBoolean(ignore_errors) then | ||
return '' | return '' | ||
end | end | ||
local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>' | local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>' | ||
if error_category ~= '' and not | if error_category ~= '' and not str._getBoolean( no_category ) then | ||
error_str = '[[Category:' .. error_category .. ']]' .. error_str | error_str = '[[Category:' .. error_category .. ']]' .. error_str | ||
end | end | ||
408번째 줄: | 408번째 줄: | ||
Helper Function to interpret boolean strings | Helper Function to interpret boolean strings | ||
]] | ]] | ||
function | function str._getBoolean( boolean_str ) | ||
local boolean_value | local boolean_value | ||
431번째 줄: | 431번째 줄: | ||
as plain text. | as plain text. | ||
]] | ]] | ||
function | function str._escapePattern( pattern_str ) | ||
return ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ) | return ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ) | ||
end | end | ||
return | return str |