1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-09-20 23:43:51 -04:00
parent 5fe27b5b3b
commit fa70c05354
1056 changed files with 8375 additions and 7095 deletions

View File

@@ -0,0 +1,50 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.langs.regxs; import gplx.*; import gplx.langs.*;
public class Gfo_pattern {
private final Gfo_pattern_itm[] itms; private final int itms_len;
private final Gfo_pattern_ctx ctx = new Gfo_pattern_ctx();
public Gfo_pattern(byte[] raw) {
this.raw = raw;
itms = Gfo_pattern_itm_.Compile(raw);
itms_len = itms.length;
}
public byte[] Raw() {return raw;} private byte[] raw;
public boolean Match(byte[] val) {
int val_len = val.length;
int val_pos = 0;
ctx.Init(itms_len);
for (int i = 0; i < itms_len; ++i) {
Gfo_pattern_itm itm = itms[i];
ctx.Itm_idx_(i);
val_pos = itm.Match(ctx, val, val_len, val_pos);
if (!ctx.Rslt_pass()) return false;
}
return ctx.Rslt_pass() && val_pos == val_len;
}
public static Gfo_pattern[] Parse_to_ary(byte[] raw) {
byte[][] patterns = Bry_split_.Split(raw, Byte_ascii.Semic, true);
int patterns_len = patterns.length;
Gfo_pattern[] rv = new Gfo_pattern[patterns_len];
for (int i = 0; i < patterns_len; ++i) {
byte[] pattern = patterns[i];
rv[i] = new Gfo_pattern(pattern);
}
return rv;
}
}

View File

@@ -0,0 +1,31 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.langs.regxs; import gplx.*; import gplx.langs.*;
public class Gfo_pattern_ctx {
public boolean Rslt_pass() {return rslt;} private boolean rslt;
public void Rslt_fail_() {rslt = false;}
public boolean Prv_was_wild() {return prv_was_wild;} public void Prv_was_wild_(boolean v) {prv_was_wild = v;} private boolean prv_was_wild;
private int itm_len;
public int Itm_idx() {return itm_idx;} public void Itm_idx_(int v) {itm_idx = v;} private int itm_idx;
public boolean Itm_idx_is_last() {return itm_idx == itm_len - 1;}
public void Init(int itm_len) {
this.rslt = true;
this.itm_len = itm_len;
this.prv_was_wild = false;
}
}

View File

@@ -0,0 +1,64 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.langs.regxs; import gplx.*; import gplx.langs.*;
import gplx.core.strings.*;
public interface Gfo_pattern_itm {
byte Tid();
void Compile(byte[] src, int bgn, int end);
int Match(Gfo_pattern_ctx ctx, byte[] src, int src_len, int pos);
void Xto_str(String_bldr sb);
}
class Gfo_pattern_itm_text implements Gfo_pattern_itm {
public Gfo_pattern_itm_text() {}
public byte Tid() {return Gfo_pattern_itm_.Tid_text;}
public byte[] Text() {return text;} private byte[] text; private int text_len;
public void Xto_str(String_bldr sb) {sb.Add(this.Tid()).Add("|" + String_.new_u8(text));}
public void Compile(byte[] src, int bgn, int end) {
this.text = Bry_.Mid(src, bgn, end);
this.text_len = end - bgn;
}
public int Match(Gfo_pattern_ctx ctx, byte[] src, int src_len, int pos) {
boolean pass = false;
int text_end = pos + text_len;
if (text_end > src_len) text_end = src_len;
if (ctx.Prv_was_wild()) {
int text_bgn = Bry_find_.Find_fwd(src, text, pos);
pass = text_bgn != Bry_find_.Not_found;
if (pass)
pos = text_bgn + text_len;
}
else {
pass = Bry_.Match(src, pos, text_end, text);
if (pass)
pos = text_end;
}
if (!pass) ctx.Rslt_fail_();
ctx.Prv_was_wild_(false);
return pos;
}
}
class Gfo_pattern_itm_wild implements Gfo_pattern_itm {
public byte Tid() {return Gfo_pattern_itm_.Tid_wild;}
public void Compile(byte[] src, int bgn, int end) {}
public int Match(Gfo_pattern_ctx ctx, byte[] src, int src_len, int pos) {
ctx.Prv_was_wild_(true);
return ctx.Itm_idx_is_last() ? src_len : pos;
}
public void Xto_str(String_bldr sb) {sb.Add(this.Tid()).Add("|*");}
public static final Gfo_pattern_itm_wild _ = new Gfo_pattern_itm_wild(); Gfo_pattern_itm_wild() {}
}

View File

@@ -0,0 +1,51 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.langs.regxs; import gplx.*; import gplx.langs.*;
public class Gfo_pattern_itm_ {
public static final byte Tid_text = 0, Tid_wild = 1;
public static Gfo_pattern_itm[] Compile(byte[] raw) {
List_adp rv = List_adp_.new_();
int raw_len = raw.length;
int itm_bgn = -1;
Gfo_pattern_itm itm = null;
int pos = 0;
while (true) {
boolean last = pos == raw_len;
byte b = last ? Byte_ascii.Null : raw[pos];
switch (b) {
case Byte_ascii.Null:
if (itm != null) {itm.Compile(raw, itm_bgn, pos); itm = null; itm_bgn = -1;}
break;
case Byte_ascii.Star:
if (itm != null) {itm.Compile(raw, itm_bgn, pos); itm = null; itm_bgn = -1;}
rv.Add(Gfo_pattern_itm_wild._);
break;
default:
if (itm_bgn == -1) {
itm_bgn = pos;
itm = new Gfo_pattern_itm_text();
rv.Add(itm);
}
break;
}
++pos;
if (last) break;
}
return (Gfo_pattern_itm[])rv.To_ary_and_clear(Gfo_pattern_itm.class);
}
}

View File

@@ -0,0 +1,93 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.langs.regxs; import gplx.*; import gplx.langs.*;
import org.junit.*; import gplx.core.strings.*;
public class Gfo_pattern_tst {
@Before public void init() {fxt.Clear();} private Gfo_pattern_itm_fxt fxt = new Gfo_pattern_itm_fxt();
@Test public void Compile() {
fxt.Test_Compile("a" , fxt.itm_text_("a"));
fxt.Test_Compile("*" , fxt.itm_wild_());
fxt.Test_Compile("a*" , fxt.itm_text_("a"), fxt.itm_wild_());
fxt.Test_Compile("*a" , fxt.itm_wild_(), fxt.itm_text_("a"));
fxt.Test_Compile("*ab*" , fxt.itm_wild_(), fxt.itm_text_("ab"), fxt.itm_wild_());
fxt.Test_Compile("" );
}
@Test public void Match() {
Gfo_pattern pattern = fxt.pattern_("abc");
fxt.Test_Match_y(pattern, "abc");
fxt.Test_Match_n(pattern, "ab", "a", "bc", "Abc", "");
}
@Test public void Match_all() {
Gfo_pattern pattern = fxt.pattern_("*");
fxt.Test_Match_y(pattern, "a", "abc", "");
}
@Test public void Match_bgn() {
Gfo_pattern pattern = fxt.pattern_("abc*");
fxt.Test_Match_y(pattern, "abc", "abcdef");
fxt.Test_Match_n(pattern, "abd", "aabc", "");
}
@Test public void Match_end() {
Gfo_pattern pattern = fxt.pattern_("*abc");
fxt.Test_Match_y(pattern, "abc", "xyzabc");
fxt.Test_Match_n(pattern, "abcd", "");
}
@Test public void Match_mid() {
Gfo_pattern pattern = fxt.pattern_("a*c*e");
fxt.Test_Match_y(pattern, "ace", "abcde");
fxt.Test_Match_n(pattern, "abc", "");
}
@Test public void Bug_ctx() { // PURPOSE.fix: cb was true b/c ctx was not reset correctly
Gfo_pattern pattern = fxt.pattern_("b*");
fxt.Test_Match_y(pattern, "bc");
fxt.Test_Match_n(pattern, "cb");
}
}
class Gfo_pattern_itm_fxt {
public void Clear() {}
public Gfo_pattern pattern_(String raw) {return new Gfo_pattern(Bry_.new_u8(raw));}
public void Test_Match_y(Gfo_pattern pattern, String... itms) {Test_Match(pattern, itms, Bool_.Y);}
public void Test_Match_n(Gfo_pattern pattern, String... itms) {Test_Match(pattern, itms, Bool_.N);}
private void Test_Match(Gfo_pattern pattern, String[] itms, boolean expd) {
int len = itms.length;
for (int i = 0; i < len; i++) {
String itm = itms[i];
Tfds.Eq(expd, pattern.Match(Bry_.new_u8(itm)), "pattern={0} itm={1} expd={2}", String_.new_u8(pattern.Raw()), itm, expd);
}
}
public Gfo_pattern_itm_wild itm_wild_() {return Gfo_pattern_itm_wild._;}
public Gfo_pattern_itm_text itm_text_(String raw) {
Gfo_pattern_itm_text rv = new Gfo_pattern_itm_text();
byte[] bry = Bry_.new_u8(raw);
rv.Compile(bry, 0, bry.length);
return rv;
}
public void Test_Compile(String raw, Gfo_pattern_itm... expd) {
Gfo_pattern_itm[] actl = Gfo_pattern_itm_.Compile(Bry_.new_u8(raw));
Tfds.Eq(Ary_xto_str(expd), Ary_xto_str(actl));
}
private static String Ary_xto_str(Gfo_pattern_itm[] ary) {
int len = ary.length;
String_bldr sb = String_bldr_.new_();
for (int i = 0; i < len; i++) {
if (i != 0) sb.Add_char_nl();
Gfo_pattern_itm itm = ary[i];
itm.Xto_str(sb);
}
return sb.Xto_str_and_clear();
}
}