mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
Scribunto: Pull 0th argument if %1 is specified and 0 groups [#393]
This commit is contained in:
parent
71232e4402
commit
9b5d585750
@ -91,6 +91,11 @@ public class Scrib_lib_ustring__gsub__tst {
|
||||
Exec_gsub("aa" , "(a)%1" , 1, "%1z", "az;1"); // capture; handle %1+; PAGE:en.w:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
Exec_gsub("a\"b'c\"d" , "([\"'])(.-)%1" , 1, "%1z", "a\"zd;1"); // capture; http://www.lua.org/pil/20.3.html; {{#invoke:test|gsub_string|a"b'c"d|(["'])(.-)%1|%1z}}
|
||||
}
|
||||
@Test public void Regx__capture_wo_group() {
|
||||
Exec_gsub("Ab", "%u", 1, "<%0>", "<A>b;1");
|
||||
Exec_gsub("Ab", "%u", 1, "<%1>", "<A>b;1"); // NOTE: %1 should be same as %0 if no matches; ISSUE#:393; DATE:2019-03-20
|
||||
Exec_gsub_fail("Ab", "%u", 1, "<%2>", "invalid capture index %2 in replacement String");
|
||||
}
|
||||
@Test public void Regx__frontier_pattern() { // PURPOSE: handle frontier pattern; EX:"%f[%a]"; DATE:2015-07-21
|
||||
Exec_gsub("a b c", "%f[%W]", 5, "()", "a() b() c();3");
|
||||
Exec_gsub("abC DEF gHI JKm NOP", "%f[%a]%u+%f[%A]", Int_.Max_value, "()", "abC () gHI JKm ();2"); // based on http://lua-users.org/wiki/FrontierPattern
|
||||
@ -120,6 +125,15 @@ public class Scrib_lib_ustring__gsub__tst {
|
||||
private void Exec_gsub(String text, Object regx, int limit, Object repl, String expd) {
|
||||
fxt.Test__proc__kvps__flat(lib, Scrib_lib_ustring.Invk_gsub, Scrib_kv_utl_.base1_many_(text, regx, repl, limit), expd);
|
||||
}
|
||||
private void Exec_gsub_fail(String text, Object regx, int limit, Object repl, String expd) {
|
||||
try {
|
||||
fxt.Test__proc__kvps__flat(lib, Scrib_lib_ustring.Invk_gsub, Scrib_kv_utl_.base1_many_(text, regx, repl, limit), expd);
|
||||
} catch (Exception e) {
|
||||
Gftest.Eq__bool(true, String_.Has(Err_.Message_gplx_log(e), expd));
|
||||
return;
|
||||
}
|
||||
throw Err_.new_wo_type("expected failure");
|
||||
}
|
||||
}
|
||||
class Mock_proc__recursive extends Mock_proc_fxt { private final Mock_scrib_fxt fxt; private final Scrib_lib lib; private final Mock_proc__recursive inner;
|
||||
private final Bry_bfr bfr;
|
||||
|
@ -134,18 +134,21 @@ class Scrib_lib_ustring_gsub_mgr {
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
int idx = b - Byte_ascii.Num_0;
|
||||
if (idx == 0) // NOTE: 0 means take result; REF.MW:if ($x === '0'); return $m[0]; PAGE:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
// REF.MW: https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/master/includes/engines/LuaCommon/UstringLibrary.php#L785-L796
|
||||
// NOTE: 0 means take result; REF.MW:if ($x === '0'); return $m[0]; PAGE:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
if (idx == 0)
|
||||
tmp_bfr.Add_str_u8(String_.Mid(text, match.Find_bgn(), match.Find_end()));
|
||||
else { // NOTE: > 0 means get from groups if it exists; REF.MW:elseif (isset($m["m$x"])) return $m["m$x"]; PAGE:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
idx -= List_adp_.Base1;
|
||||
if (idx < match.Groups().length) { // retrieve numbered capture; TODO_OLD: support more than 9 captures
|
||||
Regx_group grp = match.Groups()[idx];
|
||||
// NOTE: > 0 means get from groups if it exists; REF.MW:elseif (isset($m["m$x"])) return $m["m$x"]; PAGE:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
else if (idx - 1 < match.Groups().length) { // retrieve numbered capture; TODO_OLD: support more than 9 captures
|
||||
Regx_group grp = match.Groups()[idx - 1];
|
||||
tmp_bfr.Add_str_u8(String_.Mid(text, grp.Bgn(), grp.End())); // NOTE: grp.Bgn() / .End() is for String pos (bry pos will fail for utf8 strings)
|
||||
}
|
||||
else {
|
||||
tmp_bfr.Add_byte(Byte_ascii.Percent);
|
||||
tmp_bfr.Add_byte(b);
|
||||
// NOTE: 1 per MW "Match undocumented Lua String.gsub behavior"; PAGE:en.d:Wiktionary:Scripts ISSUE#:393; DATE:2019-03-20
|
||||
else if (idx == 1) {
|
||||
tmp_bfr.Add_str_u8(String_.Mid(text, match.Find_bgn(), match.Find_end()));
|
||||
}
|
||||
else {
|
||||
throw Err_.new_wo_type("invalid capture index %" + Char_.To_str(b) + " in replacement String");
|
||||
}
|
||||
break;
|
||||
case Byte_ascii.Percent:
|
||||
|
Loading…
Reference in New Issue
Block a user