Module:Sandbox/User:Ssola

local p = {}

--[[
Split

This function Splits a string based on a pattern, returns nth substring based on count.

Usage:
{{#invoke:StringFunc|split|source_string|pattern|count|plain}}

Parameters:
	source:  The string to return a subset of
	pattern: The pattern or string to split on 
	count:   The nth substring based on the pattern to return
	plain:   A flag indicating if the pattern should be understood as plain text, defaults to true.
]]
function p.split( frame )
	local new_args = p._getParameters( frame.args, {'source', 'pattern', 'count', 'plain'} )
	local source_str = new_args['source'] or '';
	local pattern = new_args['pattern'] or '';
	if source_str == '' or pattern == '' then
		return source_str;
	end
	local l_plain = p._getBoolean( new_args['plain'] or true );
	if l_plain then
		pattern = p._escapePattern( pattern );
	end
	local plain = false;
	pattern = "[" .. pattern .. "]" 
	local ret_count = tonumber( new_args['count'] ) or 1;
	local start = 1;
	local iter = mw.ustring.find(source_str, pattern, start, plain);
	if iter == nil then
		return source_str;
	end
	if ret_count == 1 then
		return mw.ustring.sub( source_str, start, iter);
	end
    for i=2, ret_count do
    	start = iter+1;
    	iter = mw.ustring.find(source_str,  pattern, start, plain);
    	if iter == nil then
    		break 
    	end
    end
    return mw.ustring.sub( source_str,start,iter); 
end

-- Argument list helper function, as per Module:String
function p._getParameters( frame_args, arg_list)
	local new_args = {};
	local index = 1;
	local value;
	for i, arg in ipairs( arg_list ) do
		value = frame_args[arg]
		if value == nil then
			value = frame_args[index];
			index = index + 1;
		end
		new_args[arg] = value;
	end
	return new_args;
end

-- Escape Pattern helper function so that all charecters are treated as plain text, as per Module:String
function p._escapePattern( pattern_str)
	return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" );
end

-- Helper Function to interpret boolean strings, as per Module:String
function p._getBoolean( boolean_str ) 
	local boolean_value;
	
	if type( boolean_str ) == 'string' then
		boolean_str = boolean_str:lower();
		if boolean_str == 'false' or boolean_str == 'no' or boolean_str == 'O' or boolean_str == '' then
			boolean_value = false;
		else
			boolean_value = true;
		end
	elseif type(boolean_str) == 'boolean' then
			boolean_value = boolean_str;
	else
		error( 'No boolean value found' );
	end
	return boolean_value
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.