--[[
Деяк? додатков? функц?? для рядк?в
]]
local
M
=
{
bs
=
function
(
f
)
-- Перший параметр до початку другого параметра (або до к?нця, якщо в?н не зустр?вся).
-- Необов’язковий 3-й параметр ? з якого за номером (з 1) символу починати пошук.
return
mw
.
ustring
.
sub
(
f
.
args
[
1
],
1
,
(
mw
.
ustring
.
find
(
f
.
args
[
1
],
f
.
args
[
2
],
tonumber
(
f
.
args
[
3
]
or
1
),
true
)
or
0
)
-
1
)
end
;
as
=
function
(
f
)
-- Перший параметр п?сля початку другого параметра.
return
mw
.
ustring
.
sub
(
f
.
args
[
1
],
(
mw
.
ustring
.
find
(
f
.
args
[
1
],
f
.
args
[
2
],
1
,
true
)
or
0
)
+
1
)
end
;
Tr
=
function
(
s
,
f
,
t
,
cf
,
df
,
sf
)
-- Трансл?терац?я першого параметра шляхом зам?ни символ?в з другого параметра символами з третього.
-- Окремими параметрами можна передавати прапори c, d ? s, як у Perl; д?апазони в зам?н? не працюють, т?льки в л?в?й частин?
-- (тобто деф?с треба передавати першим або останн?м). Другий результат ? число зам?нених символ?в.
local
r
,
l
,
l2
=
{},
mw
.
ustring
.
len
(
f
),
mw
.
ustring
.
len
(
t
);
for
i
=
1
,
l
do
r
[
mw
.
ustring
.
sub
(
f
,
i
,
i
)]
=
i
<=
l2
and
mw
.
ustring
.
sub
(
t
,
i
,
i
)
or
df
and
''
or
mw
.
ustring
.
sub
(
t
,
l2
,
l2
)
end
local
n2
=
0
;
local
res
,
n
=
mw
.
ustring
.
gsub
(
s
,
(
'[%s%s]%s'
):
format
(
cf
and
'^'
or
''
,
f
:
gsub
(
'%'
,
'%%'
):
gsub
(
']'
,
'%]'
):
gsub
(
'^%^'
,
'%^'
),
sf
and
'+'
or
''
),
sf
and
function
(
cc
)
n2
=
n2
+
mw
.
ustring
.
len
(
cc
)
-
1
;
return
mw
.
ustring
.
gsub
(
cc
,
'.'
,
r
)
end
or
r
)
return
res
,
n
+
n2
end
;
-- tr = function(f) return (M.Tr(f.args[1],f.args[2],f.args[3],f.args['c'],f.args['d'],f.args['s'])) end;-- трансл?терувати
-- trс = function(f) return ({M.Tr(f.args[1],f.args[2],f.args[3],f.args['c'],f.args['d'],f.args['s'])})[2] end;-- порахувати символи
Trg
=
function
(
s
,
t
,
f
,
fi
)
-- Викону? зам?ну рядк?в дов?льно? довжини (якщо з fi, не врахову? рег?стр).
-- Пр?оритет ? порядок у таблицях.
for
n
,
p
in
ipairs
(
t
)
do
t
[
n
]
=
{
fi
and
mw
.
ustring
.
upper
(
p
)
or
p
,
mw
.
ustring
.
len
(
p
)}
end
local
r
,
i
,
l
,
N
=
{},
1
,
mw
.
ustring
.
len
(
s
),
0
while
i
<=
l
do
(
function
()
for
n
,
p
in
ipairs
(
t
)
do
if
(
fi
and
mw
.
ustring
.
upper
(
mw
.
ustring
.
sub
(
s
,
i
,
i
+
p
[
2
]
-
1
))
or
mw
.
ustring
.
sub
(
s
,
i
,
i
+
p
[
2
]
-
1
)
)
==
p
[
1
]
then
table.insert
(
r
,
f
[
n
]);
i
=
i
+
p
[
2
];
N
=
N
+
1
;
return
end
end
table.insert
(
r
,
mw
.
ustring
.
sub
(
s
,
i
,
i
));
i
=
i
+
1
;
return
end
)()
end
return
table.concat
(
r
),
N
end
;
trg
=
function
(
frame
)
-- Працю? з номерними аргументами шаблону, якщо заданий параметр u, ?накше з? сво?ми.
-- Зам?ню? в першому аргумент? аргументи 2, 4, 6… на аргументи 3, 5, 7…
local
tf
,
t
,
f
,
i
=
frame
.
args
[
'u'
]
and
frame
.
getParent
()
or
f
,
{},
{},
1
;
while
tf
.
args
[
2
*
i
]
do
t
[
tf
.
args
[
2
*
i
]]
=
tf
.
args
[
2
*
i
+
1
]
or
''
end
return
(
M
.
Trg
(
tf
.
args
[
1
],
t
,
f
,(
frame
.
args
[
'i'
]
or
''
)
~=
''
)
)
end
;
join
=
function
(
f
)
-- Об’?дну? нумерован? аргументи вих?дного шаблону
-- в?д from або 1 до to або першого в?дсутнього через перший параметр invoke.
-- Останн?й елемент можна при?днувати ?накше, задавши другий параметр:
-- за замовчанням ',' & 'и'.
-- Якщо 3-й параметр invoke ? "s", рядки з проб?л?в ?гноруються. Якщо "_", ?гноруються пуст? рядки.
local
t
,
tf
,
i
=
{},
f
:
getParent
(),
tonumber
(
f
.
args
.
from
)
or
1
local
k
,
j
,
m
=
tonumber
(
f
.
args
.
to
),
i
,
f
.
args
[
3
]
while
k
and
i
<=
k
or
tf
.
args
[
i
]
do
if
(
({
[
'_'
]
=
function
(
s
)
return
s
~=
''
end
;
[
's'
]
=
function
(
s
)
return
not
tostring
(
s
):
match
(
"^%s*$"
)
end
})[
m
]
or
function
()
return
true
end
)(
tf
.
args
[
i
])
then
t
[
j
]
=
tf
.
args
[
i
];
j
=
j
+
1
end
;
i
=
i
+
1
end
return
mw
.
text
.
listToText
(
t
,
f
.
args
[
1
],
f
.
args
[
2
]
or
f
.
args
[
1
])
end
}
function
M
.
formatRound
(
frame
)
-- Формату? число, щоби воно мало order знак?в п?сля коми.
return
string.format
(
"%0."
..
frame
.
args
[
2
]
..
"f"
,
tonumber
(
frame
.
args
[
1
]));
end
-- split splits text at boundaries specified by separator
-- and returns the chunk for the index idx (starting at 1)
-- #invoke:String2 |split |text |separator |index |true/false
-- #invoke:String2 |split |txt=text |sep=separator |idx=index |plain=true/false
-- if plain is false/no/0 then separator is treated as a Lua pattern - defaults to plain=true
M
.
split
=
function
(
frame
)
local
args
=
frame
.
args
if
not
(
args
[
1
]
or
args
.
txt
)
then
args
=
frame
:
getParent
().
args
end
local
txt
=
args
[
1
]
or
args
.
txt
or
""
if
txt
==
""
then
return
nil
end
local
sep
=
(
args
[
2
]
or
args
.
sep
or
""
):
gsub
(
'"'
,
''
)
local
idx
=
tonumber
(
args
[
3
]
or
args
.
idx
)
or
1
local
plain
=
(
args
[
4
]
or
args
.
plain
or
"true"
):
sub
(
1
,
1
)
plain
=
(
plain
~=
"f"
and
plain
~=
"n"
and
plain
~=
"0"
)
local
splittbl
=
mw
.
text
.
split
(
txt
,
sep
,
plain
)
if
idx
<
0
then
idx
=
#
splittbl
+
idx
+
1
end
return
splittbl
[
idx
]
end
return
M