Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/lualib/mw.ustring.lua

raw symbols SCIP
1 -- Get a fresh copy of the basic ustring
2 local old_ustring = package.loaded.ustring
3 package.loaded.ustring = nil
4 local ustring = require( 'ustring' )
5 package.loaded.ustring = old_ustring
6 old_ustring = nil
7
8 local util = require 'libraryUtil'
9 local checkType = util.checkType
10
11 local gmatch_init = nil
12 local gmatch_callback = nil
13 local function php_gmatch( s, pattern )
14 checkType( 'gmatch', 1, s, 'string' )
15 checkType( 'gmatch', 2, pattern, 'string' )
16
17 local re, capt = gmatch_init( s, pattern )
18 local pos = 0
19 return function()
20 local ret
21 pos, ret = gmatch_callback( s, re, capt, pos )
22 return unpack( ret )
23 end, nil, nil
24 end
25
26 local gcodepoint_init = nil
27 local function php_gcodepoint( s, i, j )
28 checkType( 'gcodepoint', 1, s, 'string' )
29 checkType( 'gcodepoint', 2, i, 'number', true )
30 checkType( 'gcodepoint', 3, j, 'number', true )
31 local cp = gcodepoint_init( s, i, j or -1 )
32 local pos, len = 1, #cp
33 return function ()
34 if pos <= len then
35 local tmp = cp[pos]
36 pos = pos + 1
37 return tmp
38 end
39 end
40 end
41
42 function ustring.setupInterface( opt )
43 -- Boilerplate
44 ustring.setupInterface = nil
45
46 -- Set string limits
47 ustring.maxStringLength = opt.stringLengthLimit
48 ustring.maxPatternLength = opt.patternLengthLimit
49
50 -- Gmatch
51 if mw_interface.gmatch_callback and mw_interface.gmatch_init then
52 gmatch_init = mw_interface.gmatch_init
53 gmatch_callback = mw_interface.gmatch_callback
54 ustring.gmatch = php_gmatch
55 end
56 mw_interface.gmatch_init = nil
57 mw_interface.gmatch_callback = nil
58
59 -- codepoint and gcodepoint
60 if mw_interface.gcodepoint_init then
61 gcodepoint_init = mw_interface.gcodepoint_init
62 ustring.gcodepoint = php_gcodepoint
63 end
64 mw_interface.gcodepoint_init = nil
65
66 -- Replace pure-lua implementation with php callbacks
67 local nargs = {
68 char = 0,
69 find = 2,
70 match = 2,
71 gsub = 3,
72 }
73 for k, v in pairs( mw_interface ) do
74 local n = nargs[k] or 1
75 if n == 0 then
76 ustring[k] = v
77 else
78 -- Avoid PHP warnings for missing arguments by checking before
79 -- calling PHP.
80 ustring[k] = function ( ... )
81 if select( '#', ... ) < n then
82 error( "too few arguments to mw.ustring." .. k, 2 )
83 end
84 return v( ... )
85 end
86 end
87 end
88 mw_interface = nil
89
90 -- Replace upper/lower with mw.language versions if available
91 if mw and mw.language then
92 local lang = mw.language.getContentLanguage()
93 ustring.upper = function ( s )
94 return lang:uc( s )
95 end
96 ustring.lower = function ( s )
97 return lang:lc( s )
98 end
99 end
100
101 -- Register this library in the "mw" global
102 mw = mw or {}
103 mw.ustring = ustring
104
105 package.loaded['mw.ustring'] = ustring
106 end
107
108 return ustring