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
2014-07-13 23:23:30 -04:00
parent ecbe2918d8
commit bc10cd76b6
316 changed files with 3251 additions and 1652 deletions

View File

@@ -174,6 +174,25 @@ public class Bry_finder {
}
}
}
public static int Trim_fwd_space_tab(byte[] src, int cur, int end) {
while (true) {
if (cur == end) return cur;
switch (src[cur]) {
case Byte_ascii.Space: case Byte_ascii.Tab: ++cur; break;
default: return cur;
}
}
}
public static int Trim_bwd_space_tab(byte[] src, int cur, int bgn) {
while (true) {
int prv_cur = cur - 1; // check byte before cur; EX: "a b " will have len of 4, and pass cur=4;
if (prv_cur < bgn) return cur; // checking byte before prv; exit;
switch (src[prv_cur]) {
case Byte_ascii.Space: case Byte_ascii.Tab: --cur; break;
default: return cur;
}
}
}
public static int Find_fwd_while_ws(byte[] src, int cur, int end) {
while (true) {
if (cur == end) return cur;

View File

@@ -19,29 +19,60 @@ package gplx;
import org.junit.*;
import gplx.texts.*;
public class Bry_finder_tst {
private Bry_finder_fxt fxt = new Bry_finder_fxt();
@Test public void Find_fwd() {
tst_Find_fwd("abcba", "b", 0, 1);
tst_Find_fwd("abcba", "z", 0, -1);
tst_Find_fwd("abcba", "b", 1, 1);
tst_Find_fwd("abcba", "b", 2, 3);
tst_Find_fwd("abcba", "b", 4, -1);
tst_Find_fwd("abcba", "zb", 4, -1);
tst_Find_fwd("abcba", "a", 6, -1);
} void tst_Find_fwd(String src, String lkp, int bgn, int expd) {Tfds.Eq(expd, Bry_finder.Find_fwd(Bry_.new_utf8_(src), Bry_.new_utf8_(lkp), bgn));}
@Test public void Find_bwd() {
tst_Find_bwd("abcba", "b", 4, 3);
tst_Find_bwd("abcba", "z", 4, -1);
tst_Find_bwd("abcba", "b", 3, 1);
tst_Find_bwd("abcba", "b", 2, 1);
tst_Find_bwd("abcba", "b", 0, -1);
tst_Find_bwd("abcba", "zb", 4, -1);
tst_Find_fwd("abcba", "a", -1, -1);
tst_Find_bwd("abcba", "ab", 4, 0);
} void tst_Find_bwd(String src, String lkp, int bgn, int expd) {Tfds.Eq(expd, Bry_finder.Find_bwd(Bry_.new_utf8_(src), Bry_.new_utf8_(lkp), bgn));}
@Test public void Find_bwd_last_ws() {
Find_bwd_1st_ws_tst("a b" , 2, 1); // basic
Find_bwd_1st_ws_tst("a b" , 3, 1); // multiple
Find_bwd_1st_ws_tst("ab" , 1, Bry_.NotFound); // none
fxt.Test_Find_fwd("abcba", "b", 0, 1);
fxt.Test_Find_fwd("abcba", "z", 0, -1);
fxt.Test_Find_fwd("abcba", "b", 1, 1);
fxt.Test_Find_fwd("abcba", "b", 2, 3);
fxt.Test_Find_fwd("abcba", "b", 4, -1);
fxt.Test_Find_fwd("abcba", "zb", 4, -1);
fxt.Test_Find_fwd("abcba", "a", 6, -1);
}
@Test public void Find_bwd() {
fxt.Test_Find_bwd("abcba", "b", 4, 3);
fxt.Test_Find_bwd("abcba", "z", 4, -1);
fxt.Test_Find_bwd("abcba", "b", 3, 1);
fxt.Test_Find_bwd("abcba", "b", 2, 1);
fxt.Test_Find_bwd("abcba", "b", 0, -1);
fxt.Test_Find_bwd("abcba", "zb", 4, -1);
fxt.Test_Find_fwd("abcba", "a", -1, -1);
fxt.Test_Find_bwd("abcba", "ab", 4, 0);
}
@Test public void Find_bwd_last_ws() {
fxt.Test_Find_bwd_1st_ws_tst("a b" , 2, 1); // basic
fxt.Test_Find_bwd_1st_ws_tst("a b" , 3, 1); // multiple
fxt.Test_Find_bwd_1st_ws_tst("ab" , 1, Bry_.NotFound); // none
}
@Test public void Trim_fwd_space_tab() {
fxt.Test_Trim_fwd_space_tab(" a b" , 1);
fxt.Test_Trim_fwd_space_tab("\ta b" , 1);
fxt.Test_Trim_fwd_space_tab(" \ta b" , 2);
fxt.Test_Trim_fwd_space_tab("a bc" , 0);
fxt.Test_Trim_fwd_space_tab("" , 0);
fxt.Test_Trim_fwd_space_tab(" \t" , 2);
}
@Test public void Trim_bwd_space_tab() {
fxt.Test_Trim_bwd_space_tab("a b " , 3);
fxt.Test_Trim_bwd_space_tab("a b\t" , 3);
fxt.Test_Trim_bwd_space_tab("a b\t " , 3);
fxt.Test_Trim_bwd_space_tab("a bc" , 4);
fxt.Test_Trim_bwd_space_tab("" , 0);
fxt.Test_Trim_bwd_space_tab(" \t" , 0);
}
}
class Bry_finder_fxt {
public void Test_Find_fwd(String src, String lkp, int bgn, int expd) {Tfds.Eq(expd, Bry_finder.Find_fwd(Bry_.new_utf8_(src), Bry_.new_utf8_(lkp), bgn));}
public void Test_Find_bwd(String src, String lkp, int bgn, int expd) {Tfds.Eq(expd, Bry_finder.Find_bwd(Bry_.new_utf8_(src), Bry_.new_utf8_(lkp), bgn));}
public void Test_Find_bwd_1st_ws_tst(String src, int pos, int expd) {Tfds.Eq(expd, Bry_finder.Find_bwd_last_ws(Bry_.new_ascii_(src), pos));}
public void Test_Trim_bwd_space_tab(String raw_str, int expd) {
byte[] raw_bry = Bry_.new_utf8_(raw_str);
int actl = Bry_finder.Trim_bwd_space_tab(raw_bry, raw_bry.length, 0);
Tfds.Eq(expd, actl, raw_str);
}
public void Test_Trim_fwd_space_tab(String raw_str, int expd) {
byte[] raw_bry = Bry_.new_utf8_(raw_str);
int actl = Bry_finder.Trim_fwd_space_tab(raw_bry, 0, raw_bry.length);
Tfds.Eq(expd, actl, raw_str);
}
void Find_bwd_1st_ws_tst(String src, int pos, int expd) {Tfds.Eq(expd, Bry_finder.Find_bwd_last_ws(Bry_.new_ascii_(src), pos));}
}

View File

@@ -22,6 +22,11 @@ public class Double_ {
public static final double NaN = Double.NaN;;
public static final byte[] NaN_bry = Bry_.new_ascii_("NaN");
public static boolean IsNaN(double v) {return Double.isNaN(v);}
public static int Compare(double lhs, double rhs) {
if (lhs == rhs) return CompareAble_.Same;
else if (lhs < rhs) return CompareAble_.Less;
else return CompareAble_.More;
}
public static double coerce_(Object v) {
try {String s = String_.as_(v); return s == null ? Double_.cast_(v) : Double_.parse_(s);}
catch (Exception e) {throw Err_.cast_(e, double.class, v);}

View File

@@ -0,0 +1,32 @@
/*
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;
public class Double_obj_val implements CompareAble {
public double Val() {return val;} double val;
@Override public String toString() {return Double_.XtoStr(val);}
@Override public int hashCode() {return (int)val;}
@Override public boolean equals(Object obj) {return obj == null ? false : val == ((Double_obj_val)obj).Val();}
public int compareTo(Object obj) {Double_obj_val comp = (Double_obj_val)obj; return Double_.Compare(val, comp.val);}
public static Double_obj_val neg1_() {return new_(-1);}
public static Double_obj_val zero_() {return new_(0);}
public static Double_obj_val new_(double val) {
Double_obj_val rv = new Double_obj_val();
rv.val = val;
return rv;
} Double_obj_val() {}
}

View File

@@ -62,6 +62,38 @@ public class KeyVal_ {
}
return null;
}
public static String Ary_xto_str_nested(KeyVal... ary) {
Bry_bfr bfr = Bry_bfr.new_();
Ary_xto_str_nested(bfr, 0, ary);
return bfr.XtoStrAndClear();
}
private static void Ary_xto_str_nested(Bry_bfr bfr, int indent, KeyVal[] ary) {
int len = ary.length;
for (int i = 0; i < len; ++i) {
KeyVal itm = ary[i];
if (indent > 0)
bfr.Add_byte_repeat(Byte_ascii.Space, indent * 2); // add indent : " "
bfr.Add_str(Object_.XtoStr_OrEmpty(itm.Key())).Add_byte_eq(); // add key + eq : "key="
Object val = itm.Val();
if (val == null)
bfr.Add_str(String_.Null_mark);
else {
Class<?> val_type = ClassAdp_.ClassOf_obj(val);
if (ClassAdp_.Eq(val_type, KeyVal[].class)) { // val is KeyVal[]; recurse
bfr.Add_byte_nl(); // add nl : "\n"
Ary_xto_str_nested(bfr, indent + 1, (KeyVal[])val);
continue; // don't add \n below
}
else if (ClassAdp_.Eq(val_type, Bool_.ClassOf)) { // val is boolean
boolean val_as_bool = Bool_.cast_(val);
bfr.Add(val_as_bool ? Bool_.True_bry : Bool_.False_bry); // add "true" or "false"; don't call toString
}
else
bfr.Add_str(Object_.XtoStr_OrNullStr(val)); // call toString()
}
bfr.Add_byte_nl();
}
}
public static KeyVal as_(Object obj) {return obj instanceof KeyVal ? (KeyVal)obj : null;}
public static KeyVal new_(String key) {return new KeyVal(KeyVal_.Key_tid_str, key, key);}
public static KeyVal new_(String key, Object val) {return new KeyVal(KeyVal_.Key_tid_str, key, val);}

View File

@@ -52,7 +52,6 @@ public class Hash_adp_bry extends gplx.lists.HashAdp_base implements HashAdp {
public static Hash_adp_bry ci_ascii_() {return new Hash_adp_bry(Hash_adp_bry_itm_ci_ascii._);}
public static Hash_adp_bry ci_utf8_(Gfo_case_mgr case_mgr) {return new Hash_adp_bry(Hash_adp_bry_itm_ci_utf8.get_or_new(case_mgr));}
public static Hash_adp_bry c__utf8_(boolean case_match, Gfo_case_mgr case_mgr) {return case_match ? cs_() : ci_utf8_(case_mgr);}
public static Hash_adp_bry ci_() {return new Hash_adp_bry(Hash_adp_bry_itm_ci_ascii._);}
}
abstract class Hash_adp_bry_itm_base {
public abstract Hash_adp_bry_itm_base New();
@@ -129,7 +128,7 @@ class Hash_adp_bry_itm_ci_utf8 extends Hash_adp_bry_itm_base {
int rv = 0;
for (int i = src_bgn; i < src_end; i++) {
byte b = src[i];
int b_int = b & 0xFF; // JAVA: patch
int b_int = b & 0xFF; // JAVA: patch
Gfo_case_itm itm = case_mgr.Get_or_null(b, src, i, src_end);
if (itm == null) { // unknown itm; byte is a number, symbol, or unknown; just use the existing byte
}
@@ -145,26 +144,30 @@ class Hash_adp_bry_itm_ci_utf8 extends Hash_adp_bry_itm_base {
if (obj == null) return false;
Hash_adp_bry_itm_ci_utf8 trg_itm = (Hash_adp_bry_itm_ci_utf8)obj;
byte[] trg = trg_itm.src; int trg_bgn = trg_itm.src_bgn, trg_end = trg_itm.src_end;
int trg_len = trg_end - trg_bgn, src_len = src_end - src_bgn;
if (trg_len != src_len) return false;
for (int i = 0; i < trg_len; i++) { // ASSUME: upper/lower have same width; i.e.: upper'ing a character doesn't go from a 2-width byte to a 3-width byte
int src_pos = src_bgn + i;
if (src_pos >= src_end) return false; // ran out of src; exit; EX: src=ab; find=abc
byte src_byte = src[src_pos];
byte trg_byte = trg[i + trg_bgn];
Gfo_case_itm src_case_itm = case_mgr.Get_or_null(src_byte, src, i, src_len);
Gfo_case_itm trg_case_itm = case_mgr.Get_or_null(trg_byte, trg, i, trg_len);
if (src_case_itm != null && trg_case_itm == null) return false;
else if (src_case_itm == null && trg_case_itm != null) return false;
else if (src_case_itm == null && trg_case_itm == null) {
if (src_byte != trg_byte) return false;
int src_c_bgn = src_bgn;
int trg_c_bgn = trg_bgn;
while ( src_c_bgn < src_end
&& trg_c_bgn < trg_end) { // exit once one goes out of bounds
byte src_c = src[src_c_bgn];
byte trg_c = trg[trg_c_bgn];
int src_c_len = Utf8_.Len_of_char_by_1st_byte(src_c);
int trg_c_len = Utf8_.Len_of_char_by_1st_byte(trg_c);
int src_c_end = src_c_bgn + src_c_len;
int trg_c_end = trg_c_bgn + trg_c_len;
Gfo_case_itm src_c_itm = case_mgr.Get_or_null(src_c, src, src_c_bgn, src_c_end);
Gfo_case_itm trg_c_itm = case_mgr.Get_or_null(trg_c, trg, trg_c_bgn, trg_c_end);
if (src_c_itm != null && trg_c_itm == null) return false; // src == ltr; trg != ltr; EX: a, 1
else if (src_c_itm == null && trg_c_itm != null) return false; // src != ltr; trg == ltr; EX: 1, a
else if (src_c_itm == null && trg_c_itm == null) { // src != ltr; trg != ltr; EX: 1, 2; _, Ⓐ
if (!Bry_.Match(src, src_c_bgn, src_c_end, trg, trg_c_bgn, trg_c_end)) return false;// syms do not match; return false;
}
else {
if (!src_case_itm.Eq_lo(trg_case_itm)) return false;
i += src_case_itm.Len_lo() - 1;
if (!src_c_itm.Eq_lo(trg_c_itm)) return false; // lower-case hash-codes don't match; return false;
}
src_c_bgn = src_c_end;
trg_c_bgn = trg_c_end;
}
return true;
return src_c_bgn == src_end && trg_c_bgn == trg_end; // only return true if both src and trg read to end of their brys, otherwise "a","ab" will match
}
public static Hash_adp_bry_itm_ci_utf8 get_or_new(Gfo_case_mgr case_mgr) {
switch (case_mgr.Tid()) {

View File

@@ -17,9 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
import org.junit.*;
public class Hash_adp_bry_tst {
Hash_adp_bry_fxt fxt = new Hash_adp_bry_fxt();
@Before public void setup() {fxt.Clear();}
public class Hash_adp_bry_tst {
@Before public void setup() {fxt.Clear();} private Hash_adp_bry_fxt fxt = new Hash_adp_bry_fxt();
@Test public void Add_bry() {
fxt .New_cs()
.Add("a0").Add("b0").Add("c0")
@@ -50,7 +49,7 @@ class Hash_adp_bry_fxt {
Hash_adp_bry hash;
public void Clear() {}
public Hash_adp_bry_fxt New_cs() {hash = Hash_adp_bry.cs_(); return this;}
public Hash_adp_bry_fxt New_ci() {hash = Hash_adp_bry.ci_(); return this;}
public Hash_adp_bry_fxt New_ci() {hash = Hash_adp_bry.ci_ascii_(); return this;}
public Hash_adp_bry_fxt Add(String key) {byte[] key_bry = Bry_.new_utf8_(key); hash.Add(key_bry, key_bry); return this;}
public Hash_adp_bry_fxt Count_tst(int expd) {Tfds.Eq(expd, hash.Count()); return this;}
public Hash_adp_bry_fxt Get_bry_tst(String key) {return Get_bry_tst(key, key);}

View File

@@ -20,4 +20,5 @@ public interface Gfo_case_itm {
boolean Eq_lo(Gfo_case_itm itm);
int Hashcode_lo();
int Len_lo();
byte[] Asymmetric_bry();
}

View File

@@ -53,6 +53,9 @@ public class Env_ {
public static int TickCount_elapsed_in_frac(long time_bgn) {return (int)(Env_.TickCount() - time_bgn);}
public static long TickCount_Test = -1; // in milliseconds
public static void TickCount_normal() {TickCount_Test = -1;}
public static long System_cpu_count() {return Runtime.getRuntime().availableProcessors();}
public static long System_memory_max() {return Runtime.getRuntime().maxMemory();}
public static long System_memory_total() {return Runtime.getRuntime().totalMemory();}
public static long System_memory_free() {return Runtime.getRuntime().freeMemory();}
public static final String LocalHost = "127.0.0.1";
public static String NewLine_lang() {return mode_testing ? "\n" : "\n";}