Difference between revisions of "Module:String"

From hyperspacewiki
Jump to: navigation, search
(Experimenting with the Scribunto module by implementing the String module)
 
(s1 = "Hello, world")
Line 1: Line 1:
 
local str = {}
 
local str = {}
  
function str.len( frame )
+
function str:len( frame )
 
     local new_args = str._getParameters( frame.args, {'s'} );
 
     local new_args = str._getParameters( frame.args, {'s'} );
 
     local s = new_args['s'] or '';
 
     local s = new_args['s'] or '';

Revision as of 04:20, 19 December 2018

String Module

The original source code for this module was taken from the Wikipedia String Module but had to be re-written because the Wikipedia code used an overly general method for getting the function parameters that resulted in a null reference error. I simplified the argument referencing which solved the error.

Functions

Length

Returns the length of the passed-in string.


local str = {}

function str:len( frame )
    local new_args = str._getParameters( frame.args, {'s'} );
    local s = new_args['s'] or '';
    
    return mw.ustring.len( s )
end