summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/mw.ustring.lua
blob: 0825a738dccfa901838df4ff0456e3ec44961467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
-- Get a fresh copy of the basic ustring
local old_ustring = package.loaded.ustring
package.loaded.ustring = nil
local ustring = require( 'ustring' )
package.loaded.ustring = old_ustring
old_ustring = nil

local util = require 'libraryUtil'
local checkType = util.checkType

local gmatch_init = nil
local gmatch_callback = nil
local function php_gmatch( s, pattern )
	checkType( 'gmatch', 1, s, 'string' )
	checkType( 'gmatch', 2, pattern, 'string' )

	local re, capt = gmatch_init( s, pattern )
	local pos = 0
	return function()
		local ret
		pos, ret = gmatch_callback( s, re, capt, pos )
		return unpack( ret )
	end, nil, nil
end

local gcodepoint_init = nil
local function php_gcodepoint( s, i, j )
	checkType( 'gcodepoint', 1, s, 'string' )
	checkType( 'gcodepoint', 2, i, 'number', true )
	checkType( 'gcodepoint', 3, j, 'number', true )
	local cp = gcodepoint_init( s, i, j or -1 )
	local pos, len = 1, #cp
	return function ()
		if pos <= len then
			local tmp = cp[pos]
			pos = pos + 1
			return tmp
		end
	end
end

function ustring.setupInterface( opt )
	-- Boilerplate
	ustring.setupInterface = nil

	-- Set string limits
	ustring.maxStringLength = opt.stringLengthLimit
	ustring.maxPatternLength = opt.patternLengthLimit

	-- Gmatch
	if mw_interface.gmatch_callback and mw_interface.gmatch_init then
		gmatch_init = mw_interface.gmatch_init
		gmatch_callback = mw_interface.gmatch_callback
		ustring.gmatch = php_gmatch
	end
	mw_interface.gmatch_init = nil
	mw_interface.gmatch_callback = nil

	-- codepoint and gcodepoint
	if mw_interface.gcodepoint_init then
		gcodepoint_init = mw_interface.gcodepoint_init
		ustring.gcodepoint = php_gcodepoint
	end
	mw_interface.gcodepoint_init = nil

	-- Replace pure-lua implementation with php callbacks
	local nargs = {
		char = 0,
		find = 2,
		match = 2,
		gsub = 3,
	}
	for k, v in pairs( mw_interface ) do
		local n = nargs[k] or 1
		if n == 0 then
			ustring[k] = v
		else
			-- Avoid PHP warnings for missing arguments by checking before
			-- calling PHP.
			ustring[k] = function ( ... )
				if select( '#', ... ) < n then
					error( "too few arguments to mw.ustring." .. k, 2 )
				end
				return v( ... )
			end
		end
	end
	mw_interface = nil

	-- Replace upper/lower with mw.language versions if available
	if mw and mw.language then
		local lang = mw.language.getContentLanguage()
		ustring.upper = function ( s )
			return lang:uc( s )
		end
		ustring.lower = function ( s )
			return lang:lc( s )
		end
	end

	-- Register this library in the "mw" global
	mw = mw or {}
	mw.ustring = ustring

	package.loaded['mw.ustring'] = ustring
end

return ustring