모듈:String 편집하기

편집하면 당신의 IP 주소가 공개적으로 기록됩니다. 계정을 만들고 로그인하면 편집 시 사용자 이름만 보이며, 위키 이용에 여러 가지 편의가 주어집니다.

편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.

최신판 당신의 편집
2번째 줄: 2번째 줄:
local ustring = mw.ustring
local ustring = mw.ustring


local p = {}
local str = {}


-- string.find
-- string.find
function p.len( frame )
function str.len( frame )
local new_args = p._getParameters( frame.args, {'s'} )
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 p.sub( frame )
function str.sub( frame )
local args = getArgs(frame)
local args = getArgs(frame)
local new_args = p._getParameters( args, { 's', 'i', 'j' } )
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 p._error( 'String subset index out of range' )
return str._error( 'String subset index out of range' )
end
end
if j < i then
if j < i then
return p._error( 'String subset indices out of order' )
return str._error( 'String subset indices out of order' )
end
end


39번째 줄: 39번째 줄:


-- 다른 모듈에서 require 되는 함수
-- 다른 모듈에서 require 되는 함수
function p._match( s, pattern, start, match_index, plain_flag, nomatch )
function str._match( s, pattern, start, match_index, plain_flag, nomatch )
if s == '' then
if s == '' then
return p._error( 'Target string is empty' )
return str._error( 'Target string is empty' )
end
end
if pattern == '' then
if pattern == '' then
return p._error( 'Pattern string is empty' )
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 p._error( 'Requested start is out of range' )
return str._error( 'Requested start is out of range' )
end
end
if match_index == 0 then
if match_index == 0 then
return p._error( 'Match index is out of range' )
return str._error( 'Match index is out of range' )
end
end
if plain_flag then
if plain_flag then
pattern = p._escapePattern( 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 p._error( 'Match not found' )
return str._error( 'Match not found' )
else
else
return nomatch
return nomatch
101번째 줄: 101번째 줄:


-- string.match
-- string.match
function p.match( frame )
function str.match( frame )
local args = getArgs(frame)
local args = getArgs(frame)
local new_args = p._getParameters( args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} )
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 = p._getBoolean( new_args['plain'] or false )
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 p._match( s, pattern, start, match_index, plain_flag, nomatch )
return str._match( s, pattern, start, match_index, plain_flag, nomatch )
end
end


--  
--  
function p.pos( frame )
function str.pos( frame )
local args = getArgs(frame)
local args = getArgs(frame)
local new_args = p._getParameters( args, {'target', 'pos'} )
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 p._error( 'String index out of range' )
return str._error( 'String index out of range' )
end
end


129번째 줄: 129번째 줄:


-- string.find
-- string.find
function p.find( frame )
function str.find( frame )
local args = getArgs(frame)
local args = getArgs(frame)
local new_args = p._getParameters( args, {'source', 'target', 'start', 'plain' } )
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 = p._getBoolean( 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 p.find_( str1, str2 , str3, str4)
function str.ifexist(  )
local source_str = str1 or ''
if str.find( frame ) == 0 then
local pattern_str = str2 or ''
local start_ = str3 or 1
local plain_ = str4 or false
 
if source_str == '' or pattern_str == '' then
return 0
return 0
end
plain_ = p._getBoolean( plain_ )
local start = ustring.find( source_str, pattern_str, start_, plain_ )
if start == nil then
start = 0
end
return start
end
function p.ifexist( frame )
local args = getArgs(frame)
local new_args = p._getParameters( args, {'source', 'target', 'return1', 'return2' } )
local source_str = new_args['source'] or ''
local pattern = new_args['target'] or ''
local return1 = new_args['return1'] or ''
local return2 = new_args['return2'] or ''
if p.find_( source_str,  pattern) == 0 then
return return2
else
return return1
end
end
function p.ifexist_( source_str, pattern, return1, return2 )
if p.find_( source_str,  pattern) == 0 then
return return2
else
else
return return1
return 1
end
end
end
end


-- string.gsub
-- string.gsub
function p.gsub( frame )
function str.gsub( frame )
local new_args = p._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } )
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번째 줄: 172번째 줄:
return source_str
return source_str
end
end
plain = p._getBoolean( plain )
plain = str._getBoolean( plain )


if plain then
if plain then
pattern = p._escapePattern( 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번째 줄: 198번째 줄:


-- string.rep
-- string.rep
function p.rep( frame )
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 p._error( 'function rep expects a number as second parameter, received "' .. ( frame.args[2] or '' ) .. '"' )
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번째 줄: 207번째 줄:


-- string.upper
-- string.upper
function p.upper( frame )
function str.upper( frame )
local new_args = p._getParameters( frame.args, { 's' } )
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번째 줄: 214번째 줄:


-- string.lower
-- string.lower
function p.lower( frame )
function str.lower( frame )
local new_args = p._getParameters( frame.args, { 's' } )
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 p.reverse( frame )
function str.reverse( frame )
local new_args = p._getParameters( frame.args, { 's' } )
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번째 줄: 231번째 줄:


-- string.byte의 유니코드 호환 버전
-- string.byte의 유니코드 호환 버전
function p.tounicode( frame )
function str.tounicode( frame )
local args = getArgs( frame )
local args = getArgs(frame)
local s = args[1] or -1
local new_args = str._getParameters( args, { 's', 'i', 'j' } )
if s == -1 then
local s = new_args['s'] or ''
return '입력값이 없습니다.'
local i = tonumber( new_args['i'] ) or 1
else
local j = tonumber( new_args['j'] ) or -1
return 'U+'..ustring.upper(string.format("%x", ustring.codepoint(s)))
return ustring.codepoint( frame.args[1], i, j )
end
end
 
function p.tounicode_( char )
local s = char or -1
if s == -1 then
return 0
else
return 'U+0'..ustring.upper(string.format("%x", ustring.codepoint(s)))
end
end
end


-- string.char
-- string.char
function p.char( frame )
function str.char( frame )
local args = getArgs(frame)
local args = getArgs(frame)
local new_args = p._getParameters( args, { 'c' } )
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 p.escapePattern( frame )
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 p._error( 'No pattern string specified' )
return str._error( 'No pattern string specified' )
end
end
local result = p._escapePattern( pattern_str )
local result = str._escapePattern( pattern_str )
return result
return result
end
end


function p.count(frame)
function str.count(frame)
local args_ = getArgs(frame)
local args_ = getArgs(frame)
local args = p._getParameters(args_, {'source', 'pattern', 'plain'})
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 = p._getBoolean(args.plain or true)
local plain = str._getBoolean(args.plain or true)
if plain then
if plain then
pattern = p._escapePattern(pattern)
pattern = str._escapePattern(pattern)
end
end
local _, count = ustring.gsub(source, pattern, '')
local _, count = ustring.gsub(source, pattern, '')
return count
end
function p.count_(str, pattern)
local _, count = ustring.gsub(str, pattern, '')
return count
return count
end
end
326번째 줄: 275번째 줄:
This function determines whether a string ends with another string.
This function determines whether a string ends with another string.
]]
]]
function p.endswith(frame)
function str.endswith(frame)
local args_ = getArgs(frame)
local args_ = getArgs(frame)
local args = p._getParameters(args_, {'source', 'pattern'})
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번째 줄: 291번째 줄:
end
end


function p.join(frame)
function str.join(frame)
local args = getArgs(frame)
local args = getArgs(frame)
local table_ = {}
local table_ = {}
367번째 줄: 316번째 줄:
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 p._getParameters( frame_args, arg_list )
function str._getParameters( frame_args, arg_list )
local new_args = {}
local new_args = {}
local index = 1
local index = 1
387번째 줄: 336번째 줄:
Helper function to handle error messages.
Helper function to handle error messages.
]]
]]
function p._error( error_str )
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번째 줄: 342번째 줄:
local no_category = frame.args.no_category or false
local no_category = frame.args.no_category or false


if p._getBoolean(ignore_errors) then
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 p._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
end
end
408번째 줄: 357번째 줄:
Helper Function to interpret boolean strings
Helper Function to interpret boolean strings
]]
]]
function p._getBoolean( boolean_str )
function str._getBoolean( boolean_str )
local boolean_value
local boolean_value


431번째 줄: 380번째 줄:
as plain text.
as plain text.
]]
]]
function p._escapePattern( pattern_str )
function str._escapePattern( pattern_str )
return ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
return ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
end
end


return p
return str
리브레 위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0 라이선스로 배포됩니다(자세한 내용에 대해서는 리브레 위키:저작권 문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요.
글이 직접 작성되었거나 호환되는 라이선스인지 확인해주세요. 리그베다 위키, 나무위키, 오리위키, 구스위키, 디시위키 및 CCL 미적용 사이트 등에서 글을 가져오실 때는 본인이 문서의 유일한 기여자여야 하고, 만약 본인이 문서의 유일한 기여자라는 증거가 없다면 그 문서는 불시에 삭제될 수 있습니다.
취소 편집 도움말 (새 창에서 열림)

| () [] [[]] {{}} {{{}}} · <!-- --> · [[분류:]] · [[파일:]] · [[미디어:]] · #넘겨주기 [[]] · {{ㅊ|}} · <onlyinclude></onlyinclude> · <includeonly></includeonly> · <noinclude></noinclude> · <br /> · <ref></ref> · {{각주}} · {|class="wikitable" · |- · rowspan=""| · colspan=""| · |}

이 문서는 다음의 숨은 분류 1개에 속해 있습니다: