Module:String and Module:String/sandbox: Difference between pages
Appearance
(Difference between pages)
Content deleted Content added
No edit summary |
More precise comment |
||
Line 59: | Line 59: | ||
Parameters |
Parameters |
||
s: The string to return a subset of |
s: The string to return a subset of |
||
i: The |
i: The index of the substring to return, defaults to 1. |
||
j: The last index of the string to return, defaults to the last character. |
j: The last index of the string to return, defaults to the last character. |
||
Line 165: | Line 165: | ||
end |
end |
||
local iterator = mw.ustring.gmatch(s, pattern) |
local iterator = mw.ustring.gmatch(s, pattern) |
||
if match_index > 0 then |
if match_index > 0 then |
||
-- Forward search |
-- Forward search |
||
Line 177: | Line 177: | ||
else |
else |
||
-- Reverse search |
-- Reverse search |
||
local |
local = {} |
||
local |
local = 1 |
||
match_index = -match_index |
|||
for w in iterator do |
for w in iterator do |
||
[] = w |
|||
count = count + 1 |
|||
-- Circular buffer with length 'match_index' |
|||
-- Wraps around to overwrite matches too old to be the result, |
|||
-- so memory use is O(match_index) and not O(total match count)) |
|||
index = (index == match_index) and 1 or (index + 1) |
|||
end |
end |
||
-- 'index' is now at oldest element, which next iteration would have |
|||
result = result_table[ count + match_index ] |
|||
-- overwritten. If 'match_buffer' has fewer than 'index' elements, |
|||
-- there were too few matches; the resulting nil is handled below. |
|||
result = match_buffer[index] |
|||
end |
end |
||
end |
end |