mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.11.2.1
This commit is contained in:
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.brys; import gplx.*; import gplx.core.*;
|
||||
public class Bit_ {
|
||||
public static String XtoBitStr(int val) {
|
||||
public static String ToBitStr(int val) {
|
||||
boolean[] bits = new boolean[8];
|
||||
int idx = 7;
|
||||
while (val > 0) {
|
||||
@@ -30,56 +30,7 @@ public class Bit_ {
|
||||
rv[i] = bits[i] ? Byte_ascii.Num_1 : Byte_ascii.Num_0;
|
||||
return String_.new_a7(rv);
|
||||
}
|
||||
public static int Get_flag(int i) {return Base2_ary[i];}
|
||||
public static int[] Bld_pow_ary(int... seg_ary) {
|
||||
int seg_ary_len = seg_ary.length;
|
||||
int pow = 0;
|
||||
int[] rv = new int[seg_ary_len];
|
||||
for (int i = seg_ary_len - 1; i > -1; i--) {
|
||||
rv[i] = Base2_ary[pow];
|
||||
pow += seg_ary[i];
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static int Xto_int(int[] pow_ary, int[] val_ary) {
|
||||
int pow_ary_last = pow_ary.length - 1;
|
||||
int val = 0;
|
||||
for (int i = pow_ary_last; i > -1; i--)
|
||||
val += pow_ary[i] * val_ary[i];
|
||||
return val;
|
||||
}
|
||||
public static int[] Xto_intAry(int[] pow_ary, int v) {
|
||||
int[] rv = new int[pow_ary.length];
|
||||
Xto_intAry(rv, pow_ary, v);
|
||||
return rv;
|
||||
}
|
||||
public static void Xto_intAry(int[] rv, int[] pow_ary, int v) {
|
||||
int pow_ary_len = pow_ary.length;
|
||||
int rv_len = rv.length;
|
||||
for (int i = 0; i < pow_ary_len; i++) {
|
||||
if (i >= rv_len) break;
|
||||
rv[i] = v / pow_ary[i];
|
||||
int factor = pow_ary[i] * rv[i];
|
||||
v = factor == 0 ? v : (v % factor); // NOTE: if 0, do not do modulus or else div by zero
|
||||
}
|
||||
}
|
||||
public static byte Xto_byte(byte[] pow_ary, byte... val_ary) {
|
||||
int pow_ary_last = pow_ary.length - 1;
|
||||
int val = 0;
|
||||
for (int i = pow_ary_last; i > -1; --i)
|
||||
val += pow_ary[i] * val_ary[i];
|
||||
return (byte)val;
|
||||
}
|
||||
public static void Xto_bry(byte[] rv, byte[] pow_ary, byte val) {
|
||||
int pow_ary_len = pow_ary.length;
|
||||
int rv_len = rv.length;
|
||||
for (int i = 0; i < pow_ary_len; i++) {
|
||||
if (i >= rv_len) break;
|
||||
rv[i] = (byte)(val / pow_ary[i]);
|
||||
int factor = pow_ary[i] * rv[i];
|
||||
val = (byte)(factor == 0 ? val : (val % factor)); // NOTE: if 0, do not do modulus or else div by zero
|
||||
}
|
||||
}
|
||||
public static int Get_flag(int i) {return Int_flag_bldr_.Base2_ary[i];}
|
||||
public static int Shift_lhs(int val, int shift) {return val << shift;}
|
||||
public static int Shift_rhs(int val, int shift) {return val >> shift;}
|
||||
public static int Shift_lhs_to_int(int[] shift_ary, int... val_ary) {
|
||||
@@ -101,24 +52,4 @@ public class Bit_ {
|
||||
val -= (itm << shift);
|
||||
}
|
||||
}
|
||||
public static int Xto_int_date_short(int[] val_ary) {
|
||||
val_ary[0] -= 1900;
|
||||
return Xto_int(Pow_ary_date_short, val_ary);
|
||||
}
|
||||
public static void Xto_date_short_int_ary(int[] rv, int v) {
|
||||
Xto_intAry(rv, Pow_ary_date_short, v);
|
||||
rv[0] += 1900;
|
||||
}
|
||||
public static DateAdp Xto_date_short(int v) {
|
||||
int[] rv = new int[Pow_ary_date_short.length];
|
||||
Xto_date_short_int_ary(rv, v);
|
||||
return DateAdp_.seg_(rv);
|
||||
}
|
||||
private static final int[] Pow_ary_date_short = new int[] {1048576, 65536, 2048, 64, 1}; // yndhm -> 12,4,5,5,6
|
||||
private static final int[] Base2_ary = new int[]
|
||||
{ 1, 2, 4, 8, 16, 32, 64, 128
|
||||
, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
|
||||
, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608
|
||||
, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 0
|
||||
};
|
||||
}
|
||||
|
||||
@@ -25,48 +25,7 @@ public class Bit__tst {
|
||||
tst_XtoBitStr( 2, "00000010");
|
||||
tst_XtoBitStr( 3, "00000011");
|
||||
tst_XtoBitStr(255, "11111111");
|
||||
} void tst_XtoBitStr(int val, String expd) {Tfds.Eq(expd, Bit_.XtoBitStr(val));}
|
||||
@Test public void Bld_pow_ary() {
|
||||
tst_Bld_pow_ary(ary_(1, 1, 1, 1), ary_(8, 4, 2, 1));
|
||||
tst_Bld_pow_ary(ary_(3, 2) , ary_(4, 1));
|
||||
} void tst_Bld_pow_ary(int[] seg_ary, int[] expd) {Tfds.Eq_ary_str(expd, Bit_.Bld_pow_ary(seg_ary));}
|
||||
@Test public void Xto_int() {
|
||||
tst_Xto_int(ary_(1, 1, 1, 1) , ary_(1, 1, 1, 1), 15);
|
||||
tst_Xto_int(ary_(1, 1, 1, 1) , ary_(0, 0, 0, 0), 0);
|
||||
tst_Xto_int(ary_(1, 1, 1, 1) , ary_(1, 0, 0, 1), 9);
|
||||
tst_Xto_int(ary_(1, 1, 1, 1) , ary_(0, 1, 1, 0), 6);
|
||||
tst_Xto_int(ary_(3, 2) , ary_(7, 3) , 31);
|
||||
tst_Xto_int(ary_(3, 2, 1) , ary_(7, 3, 1) , 63);
|
||||
tst_Xto_int(ary_(11, 4, 5, 5, 6), ary_(2012, 6, 3, 23, 17), 2110135761);
|
||||
tst_Xto_int(ary_(11, 4, 5, 5, 6), ary_(2012, 6, 3, 23, 18), 2110135762);
|
||||
}
|
||||
private void tst_Xto_int(int[] seg_ary, int[] val_ary, int expd) {
|
||||
int[] pow_ary = Bit_.Bld_pow_ary(seg_ary);
|
||||
Tfds.Eq(expd, Bit_.Xto_int(pow_ary, val_ary));
|
||||
}
|
||||
@Test public void Xto_intAry() {
|
||||
tst_Xto_intAry(ary_(1, 1, 1, 1) , 15, ary_(1, 1, 1, 1));
|
||||
tst_Xto_intAry(ary_(3, 2) , 31, ary_(7, 3));
|
||||
tst_Xto_intAry(ary_(3, 2, 1) , 63, ary_(7, 3, 1));
|
||||
tst_Xto_intAry(ary_(12, 4, 5, 5, 6), 2110135761, ary_(2012, 6, 3, 23, 17));
|
||||
tst_Xto_intAry(ary_(12, 4, 5, 5, 6), 2110135762, ary_(2012, 6, 3, 23, 18));
|
||||
}
|
||||
private void tst_Xto_intAry(int[] seg_ary, int val, int[] expd) {
|
||||
int[] pow_ary = Bit_.Bld_pow_ary(seg_ary);
|
||||
Tfds.Eq_ary_str(expd, Bit_.Xto_intAry(pow_ary, val));
|
||||
}
|
||||
int[] ary_(int... v) {return v;}
|
||||
@Test public void Xto_int_date_short() {
|
||||
tst_Xto_int_date_short("20120604 2359", 117843451);
|
||||
tst_Xto_int_date_short("20120604 2358", 117843450);
|
||||
tst_Xto_int_date_short("20120605 0000", 117843968);
|
||||
}
|
||||
private void tst_Xto_int_date_short(String date_str, int expd) {
|
||||
DateAdp date = DateAdp_.parse_fmt(date_str, "yyyyMMdd HHmm");
|
||||
int date_int = Bit_.Xto_int_date_short(date.XtoSegAry());
|
||||
Tfds.Eq(expd, date_int);
|
||||
Tfds.Eq(date_str, Bit_.Xto_date_short(date_int).XtoStr_fmt("yyyyMMdd HHmm"));
|
||||
}
|
||||
} void tst_XtoBitStr(int val, String expd) {Tfds.Eq(expd, Bit_.ToBitStr(val));}
|
||||
@Test public void Shift_lhs() {// simple: shift 1 bit
|
||||
fxt.Test_shift_lhs(1, 1, 2);
|
||||
fxt.Test_shift_lhs(2, 1, 4);
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
/*
|
||||
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.core.brys; import gplx.*; import gplx.core.*;
|
||||
public class Bry_parser {
|
||||
private final gplx.core.primitives.Int_obj_ref pos_ref = gplx.core.primitives.Int_obj_ref.neg1_();
|
||||
private byte[] page; private String wkr_name; private int hook_bgn;
|
||||
public byte[] Src() {return src;} private byte[] src;
|
||||
public int Src_len() {return src_len;} private int src_len;
|
||||
public int Pos() {return pos;} private int pos;
|
||||
public void Init_src(byte[] page, byte[] src, int src_len, int pos) {
|
||||
this.page = page; this.src = src; this.src_len = src_len; this.pos = pos;
|
||||
}
|
||||
public void Init_hook(String wkr_name, int hook_bgn, int hook_end) {
|
||||
this.wkr_name = wkr_name; this.hook_bgn = hook_bgn; this.pos = hook_end;
|
||||
}
|
||||
public int Pos_(int v) {this.pos = v; return pos;}
|
||||
public int Pos_add_one() {return Pos_add(1);}
|
||||
public int Pos_add(int adj) {
|
||||
this.pos += adj;
|
||||
return pos;
|
||||
}
|
||||
public byte Read_byte() {
|
||||
byte rv = src[pos];
|
||||
++pos;
|
||||
return rv;
|
||||
}
|
||||
public int Read_int_to(byte to_char) {
|
||||
int bgn = pos;
|
||||
int rv = 0;
|
||||
int negative = 1;
|
||||
while (pos < src_len) {
|
||||
byte b = src[pos++];
|
||||
switch (b) {
|
||||
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:
|
||||
rv = (rv * 10) + (b - Byte_ascii.Num_0);
|
||||
break;
|
||||
case Byte_ascii.Dash:
|
||||
if (negative == -1) // 2nd negative
|
||||
throw Fail("invalid int", String_.new_u8(src, bgn, pos));
|
||||
else // 1st negative
|
||||
negative = -1; // flag negative
|
||||
break;
|
||||
default: {
|
||||
boolean match = b == to_char;
|
||||
if (to_char == Byte_ascii.Null) {// hack for Read_int_to_non_num
|
||||
--pos;
|
||||
match = true;
|
||||
}
|
||||
if (!match) throw Fail("invalid int", String_.new_u8(src, bgn, pos));
|
||||
return rv * negative;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bgn == pos) throw Fail("int is empty", String_.Empty);
|
||||
return rv * negative;
|
||||
}
|
||||
public byte Read_byte_as_a7_int() {
|
||||
byte rv = Byte_ascii.To_a7_int(src[pos]);
|
||||
++pos;
|
||||
return rv;
|
||||
}
|
||||
public int Read_int_by_base85(int reqd) {
|
||||
int rv = gplx.xowa.htmls.core.hzips.Xoh_hzip_int_.Decode(reqd, src, src_len, pos, pos_ref);
|
||||
pos = pos_ref.Val();
|
||||
return rv;
|
||||
}
|
||||
public boolean Is(byte find) {
|
||||
boolean rv = src[pos] == find;
|
||||
if (rv) ++pos; // only advance if match;
|
||||
return rv;
|
||||
}
|
||||
public int Chk(byte[] find) {
|
||||
int find_end = pos + find.length;
|
||||
if (!Bry_.Match(src, pos, find_end, find)) throw Fail("failed check", String_.new_u8(find));
|
||||
pos = find_end;
|
||||
return pos;
|
||||
}
|
||||
public int Chk(byte find) {
|
||||
if (src[pos] != find) throw Fail("failed check", Byte_ascii.To_str(find));
|
||||
++pos;
|
||||
return pos;
|
||||
}
|
||||
public int Fwd_bgn(byte[] find) {
|
||||
int find_pos = Bry_find_.Find_fwd(src, find, pos, src_len);
|
||||
if (find_pos == Bry_find_.Not_found) throw Fail("missing", String_.new_u8(find));
|
||||
pos = find_pos + find.length;
|
||||
return find_pos;
|
||||
}
|
||||
public int Fwd_bgn(byte find) {
|
||||
int find_pos = Bry_find_.Find_fwd(src, find, pos, src_len);
|
||||
if (find_pos == Bry_find_.Not_found) throw Fail("missing", Byte_ascii.To_str(find));
|
||||
pos = find_pos + 1;
|
||||
return find_pos;
|
||||
}
|
||||
public int Fwd_end(byte[] find) {
|
||||
int find_pos = Bry_find_.Find_fwd(src, find, pos, src_len);
|
||||
if (find_pos == Bry_find_.Not_found) throw Fail("missing", String_.new_u8(find));
|
||||
pos = find_pos + find.length;
|
||||
return pos;
|
||||
}
|
||||
public int Fwd_end(byte find) {
|
||||
int find_pos = Bry_find_.Find_fwd(src, find, pos, src_len);
|
||||
if (find_pos == Bry_find_.Not_found) throw Fail("missing", Byte_ascii.To_str(find));
|
||||
pos = find_pos + 1;
|
||||
return pos;
|
||||
}
|
||||
public int Fwd_while(byte find) {
|
||||
this.pos = Bry_find_.Find_fwd_while(src, pos, src_len, find);
|
||||
return pos;
|
||||
}
|
||||
public Err Fail(String msg, String arg) {
|
||||
return Err_.new_("Bry_parser", msg, "arg", arg, "page", page, "wkr", wkr_name, "excerpt", Bry_.Mid_by_len_safe(src, hook_bgn, 255));
|
||||
}
|
||||
}
|
||||
171
400_xowa/src/gplx/core/brys/Bry_rdr.java
Normal file
171
400_xowa/src/gplx/core/brys/Bry_rdr.java
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
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.core.brys; import gplx.*; import gplx.core.*;
|
||||
public class Bry_rdr {
|
||||
private final gplx.core.primitives.Int_obj_ref pos_ref = gplx.core.primitives.Int_obj_ref.neg1_();
|
||||
private String ctx; private String wkr; private int err_bgn;
|
||||
public byte[] Src() {return src;} private byte[] src;
|
||||
public int Pos() {return pos;} private int pos;
|
||||
public int Src_end() {return src_end;} private int src_end;
|
||||
public Bry_rdr Dflt_dlm_(byte b) {this.dflt_dlm = b; return this;} private byte dflt_dlm;
|
||||
public Bry_rdr Fail_throws_err_(boolean v) {this.fail_throws_err = v; return this;} private boolean fail_throws_err = true;
|
||||
public Bry_rdr Ctor_by_page(byte[] ctx, byte[] src, int src_len) {this.ctx = Quote(String_.new_u8(ctx)); this.src = src; this.src_end = src_len; this.pos = 0; return this;}
|
||||
public Bry_rdr Init_by_hook(String wkr, int err_bgn, int pos) {this.wkr = Quote(wkr); this.err_bgn = err_bgn; this.pos = pos; return this;}
|
||||
public Bry_rdr Init_by_sub(Bry_rdr rdr, String wkr, int pos, int src_end) {
|
||||
this.src = rdr.src; this.ctx = rdr.ctx; this.wkr = Quote(wkr); this.err_bgn = pos; this.pos = pos; this.src_end = src_end;
|
||||
this.dflt_dlm = Byte_ascii.Null;
|
||||
return this;
|
||||
}
|
||||
public int Move_to(int v) {this.pos = v; return pos;}
|
||||
public int Move_by_one() {return Move_by(1);}
|
||||
public int Move_by(int v) {this.pos += v; return pos;}
|
||||
public int Find_fwd_lr() {return Find_fwd(dflt_dlm , Bool_.Y, Bool_.N);}
|
||||
public int Find_fwd_lr(byte find) {return Find_fwd(find , Bool_.Y, Bool_.N);}
|
||||
public int Find_fwd_lr(byte[] find) {return Find_fwd(find , Bool_.Y, Bool_.N);}
|
||||
public int Find_fwd_rr() {return Find_fwd(dflt_dlm , Bool_.N, Bool_.N);}
|
||||
public int Find_fwd_rr(byte find) {return Find_fwd(find , Bool_.N, Bool_.N);}
|
||||
public int Find_fwd_rr(byte[] find) {return Find_fwd(find , Bool_.N, Bool_.N);}
|
||||
private int Find_fwd(byte find, boolean ret_lhs, boolean pos_lhs) {
|
||||
int find_pos = Bry_find_.Find_fwd(src, find, pos, src_end); if (find_pos == Bry_find_.Not_found) {Fail("find failed", "find", Byte_ascii.To_str(find)); return Bry_find_.Not_found;}
|
||||
pos = find_pos + (pos_lhs ? 0 : 1);
|
||||
return ret_lhs ? find_pos : pos;
|
||||
}
|
||||
private int Find_fwd(byte[] find, boolean ret_lhs, boolean pos_lhs) {
|
||||
int find_pos = Bry_find_.Find_fwd(src, find, pos, src_end); if (find_pos == Bry_find_.Not_found) {Fail("find failed", "find", String_.new_u8(find)); return Bry_find_.Not_found;}
|
||||
pos = find_pos + (pos_lhs ? 0 : find.length);
|
||||
return ret_lhs ? find_pos : pos;
|
||||
}
|
||||
public byte Read_byte() {
|
||||
byte rv = src[pos];
|
||||
++pos;
|
||||
return rv;
|
||||
}
|
||||
public byte Read_byte_to() {return Read_byte_to(dflt_dlm);}
|
||||
public byte Read_byte_to(byte to_char) {
|
||||
byte rv = src[pos];
|
||||
++pos;
|
||||
if (pos < src_end) {
|
||||
if (src[pos] != to_char) {Fail("read byte to failed", "to", Byte_ascii.To_str(to_char)); return Byte_.Max_value_127;}
|
||||
++pos;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public double Read_double_to() {return Read_double_to(dflt_dlm);}
|
||||
public double Read_double_to(byte to_char) {
|
||||
byte[] bry = Read_bry_to(to_char);
|
||||
return Double_.parse(String_.new_a7(bry));
|
||||
}
|
||||
public int Read_int_to() {return Read_int_to(dflt_dlm);}
|
||||
public int Read_int_to(byte to_char) {
|
||||
int bgn = pos;
|
||||
int rv = 0;
|
||||
int negative = 1;
|
||||
while (pos < src_end) {
|
||||
byte b = src[pos++];
|
||||
switch (b) {
|
||||
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:
|
||||
rv = (rv * 10) + (b - Byte_ascii.Num_0);
|
||||
break;
|
||||
case Byte_ascii.Dash:
|
||||
if (negative == -1) { // 2nd negative
|
||||
Fail("invalid int", "mid", String_.new_u8(src, bgn, pos));
|
||||
return Int_.Min_value;
|
||||
}
|
||||
else // 1st negative
|
||||
negative = -1; // flag negative
|
||||
break;
|
||||
default: {
|
||||
boolean match = b == to_char;
|
||||
if (to_char == Byte_ascii.Null) {// hack for Read_int_to_non_num
|
||||
--pos;
|
||||
match = true;
|
||||
}
|
||||
if (!match) {
|
||||
Fail("invalid int", "mid", String_.new_u8(src, bgn, pos));
|
||||
return Int_.Min_value;
|
||||
}
|
||||
return rv * negative;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bgn == pos) {Fail("int is empty", String_.Empty, String_.Empty); return Int_.Min_value;}
|
||||
return rv * negative;
|
||||
}
|
||||
public byte Read_byte_as_a7_int() {
|
||||
byte rv = Byte_ascii.To_a7_int(src[pos]);
|
||||
++pos;
|
||||
return rv;
|
||||
}
|
||||
public int Read_int_by_base85(int reqd) {
|
||||
int rv = gplx.xowa.htmls.core.hzips.Xoh_hzip_int_.Decode(reqd, src, src_end, pos, pos_ref);
|
||||
pos = pos_ref.Val();
|
||||
return rv;
|
||||
}
|
||||
public byte[] Read_bry_to() {return Read_bry_to(dflt_dlm);}
|
||||
public byte[] Read_bry_to(byte b) {
|
||||
int bgn = pos;
|
||||
return Bry_.Mid(src, bgn, Find_fwd_lr(b));
|
||||
}
|
||||
public boolean Is(byte find) {
|
||||
boolean rv = src[pos] == find;
|
||||
if (rv) ++pos; // only advance if match;
|
||||
return rv;
|
||||
}
|
||||
public boolean Is(byte[] find) {
|
||||
int find_len = find.length;
|
||||
int find_end = pos + find_len;
|
||||
boolean rv = Bry_.Match(src, pos, find_end, find, 0, find_len);
|
||||
if (rv) pos = find_end; // only advance if match;
|
||||
return rv;
|
||||
}
|
||||
public int Chk(byte find) {
|
||||
if (src[pos] != find) {Fail("failed check", "chk", Byte_.To_str(find)); return Bry_find_.Not_found;}
|
||||
++pos;
|
||||
return pos;
|
||||
}
|
||||
public int Chk(byte[] find) {
|
||||
int find_end = pos + find.length;
|
||||
if (!Bry_.Match(src, pos, find_end, find)) {Fail("failed check", "chk", String_.new_u8(find)); return Bry_find_.Not_found;}
|
||||
pos = find_end;
|
||||
return pos;
|
||||
}
|
||||
public byte Chk(gplx.core.btries.Btrie_slim_mgr trie) {return Chk(trie, pos, src_end);}
|
||||
public byte Chk(gplx.core.btries.Btrie_slim_mgr trie, int itm_bgn, int itm_end) {
|
||||
Object rv_obj = trie.Match_bgn(src, itm_bgn, itm_end);
|
||||
if (rv_obj == null) {Fail("failed trie check", "mid", String_.new_u8(Bry_.Mid_by_len_safe(src, pos, 16))); return Byte_.Max_value_127;}
|
||||
pos = trie.Match_pos();
|
||||
return ((gplx.core.primitives.Byte_obj_val)rv_obj).Val();
|
||||
}
|
||||
public int Fail(String msg, String arg_key, Object arg_val) {return Fail(msg, arg_key, arg_val, err_bgn, err_bgn + 255);}
|
||||
public int Fail(String msg, String arg_key, Object arg_val, int excerpt_bgn, int excerpt_end) {
|
||||
arg_val = Quote(Object_.Xto_str_strict_or_null_mark(arg_val));
|
||||
String err_msg = Msg_make(msg, arg_key, arg_val, excerpt_bgn, excerpt_end);
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", err_msg);
|
||||
if (fail_throws_err) throw Err_.new_("Bry_rdr", err_msg);
|
||||
return Bry_find_.Not_found;
|
||||
}
|
||||
public Err Err_make(String msg, String arg_key, Object arg_val, int excerpt_bgn, int excerpt_end) {return Err_.new_("Bry_rdr", Msg_make(msg, arg_key, arg_val, excerpt_bgn, excerpt_end));}
|
||||
private String Msg_make(String msg, String arg_key, Object arg_val, int excerpt_bgn, int excerpt_end) {
|
||||
if (String_.EqEmpty(arg_key))
|
||||
return Err_msg.To_str(msg, "ctx", ctx, "wkr", wkr, "excerpt", Bry_.Mid_safe(src, excerpt_bgn, excerpt_end));
|
||||
else
|
||||
return Err_msg.To_str(msg, arg_key, arg_val, "ctx", ctx, "wkr", wkr, "excerpt", Quote(String_.new_u8(Bry_.Mid_by_len_safe(src, excerpt_bgn, excerpt_end))));
|
||||
}
|
||||
private static String Quote(String v) {return "'" + v + "'";}
|
||||
}
|
||||
@@ -41,8 +41,8 @@ public class Bry_rdr_tst {
|
||||
}
|
||||
}
|
||||
class Bry_rdr_fxt {
|
||||
private Bry_rdr rdr;
|
||||
public void Clear() {rdr = new Bry_rdr();}
|
||||
private Bry_rdr_old rdr;
|
||||
public void Clear() {rdr = new Bry_rdr_old();}
|
||||
public Bry_rdr_fxt Init_src(String v) {rdr.Init(Bry_.new_u8(v)); return this;}
|
||||
public Bry_rdr_fxt Init_pos(int v) {rdr.Pos_(v); return this;}
|
||||
public void Test_read_int(int expd_val) {
|
||||
|
||||
29
400_xowa/src/gplx/core/brys/Int_flag_bldr.java
Normal file
29
400_xowa/src/gplx/core/brys/Int_flag_bldr.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
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.core.brys; import gplx.*; import gplx.core.*;
|
||||
public class Int_flag_bldr {
|
||||
private int[] pow_ary;
|
||||
public int[] Val_ary() {return val_ary;} private int[] val_ary;
|
||||
public Int_flag_bldr Pow_ary_bld_(int... ary) {
|
||||
this.pow_ary = Int_flag_bldr_.Bld_pow_ary(ary);
|
||||
this.val_ary = new int[pow_ary.length];
|
||||
return this;
|
||||
}
|
||||
public int Encode() {return Int_flag_bldr_.To_int(pow_ary, val_ary);}
|
||||
public void Decode(int v) {Int_flag_bldr_.To_int_ary(val_ary, pow_ary, v);}
|
||||
}
|
||||
89
400_xowa/src/gplx/core/brys/Int_flag_bldr_.java
Normal file
89
400_xowa/src/gplx/core/brys/Int_flag_bldr_.java
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
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.core.brys; import gplx.*; import gplx.core.*;
|
||||
public class Int_flag_bldr_ {
|
||||
public static int[] Bld_pow_ary(int... ary) {
|
||||
int len = ary.length;
|
||||
int[] rv = new int[len];
|
||||
int pow = 0;
|
||||
for (int i = len - 1; i > -1; --i) {
|
||||
rv[i] = Int_flag_bldr_.Base2_ary[pow];
|
||||
pow += ary[i];
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static int To_int(int[] pow_ary, int[] val_ary) {
|
||||
int pow_ary_last = pow_ary.length - 1;
|
||||
int val = 0;
|
||||
for (int i = pow_ary_last; i > -1; i--)
|
||||
val += pow_ary[i] * val_ary[i];
|
||||
return val;
|
||||
}
|
||||
public static int[] To_int_ary(int[] pow_ary, int v) {
|
||||
int[] rv = new int[pow_ary.length];
|
||||
To_int_ary(rv, pow_ary, v);
|
||||
return rv;
|
||||
}
|
||||
public static void To_int_ary(int[] rv, int[] pow_ary, int v) {
|
||||
int pow_ary_len = pow_ary.length;
|
||||
int rv_len = rv.length;
|
||||
for (int i = 0; i < pow_ary_len; i++) {
|
||||
if (i >= rv_len) break;
|
||||
rv[i] = v / pow_ary[i];
|
||||
int factor = pow_ary[i] * rv[i];
|
||||
v = factor == 0 ? v : (v % factor); // NOTE: if 0, do not do modulus or else div by zero
|
||||
}
|
||||
}
|
||||
public static byte To_byte(byte[] pow_ary, byte... val_ary) {
|
||||
int pow_ary_last = pow_ary.length - 1;
|
||||
int val = 0;
|
||||
for (int i = pow_ary_last; i > -1; --i)
|
||||
val += pow_ary[i] * val_ary[i];
|
||||
return (byte)val;
|
||||
}
|
||||
public static void To_bry(byte[] rv, byte[] pow_ary, byte val) {
|
||||
int pow_ary_len = pow_ary.length;
|
||||
int rv_len = rv.length;
|
||||
for (int i = 0; i < pow_ary_len; i++) {
|
||||
if (i >= rv_len) break;
|
||||
rv[i] = (byte)(val / pow_ary[i]);
|
||||
int factor = pow_ary[i] * rv[i];
|
||||
val = (byte)(factor == 0 ? val : (val % factor)); // NOTE: if 0, do not do modulus or else div by zero
|
||||
}
|
||||
}
|
||||
public static int To_int_date_short(int[] val_ary) {
|
||||
val_ary[0] -= 1900;
|
||||
return Int_flag_bldr_.To_int(Pow_ary_date_short, val_ary);
|
||||
}
|
||||
public static void To_date_short_int_ary(int[] rv, int v) {
|
||||
Int_flag_bldr_.To_int_ary(rv, Pow_ary_date_short, v);
|
||||
rv[0] += 1900;
|
||||
}
|
||||
public static DateAdp To_date_short(int v) {
|
||||
int[] rv = new int[Pow_ary_date_short.length];
|
||||
To_date_short_int_ary(rv, v);
|
||||
return DateAdp_.seg_(rv);
|
||||
}
|
||||
private static final int[] Pow_ary_date_short = new int[] {1048576, 65536, 2048, 64, 1}; // yndhm -> 12,4,5,5,6
|
||||
public static final int[] Base2_ary = new int[]
|
||||
{ 1, 2, 4, 8, 16, 32, 64, 128
|
||||
, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
|
||||
, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608
|
||||
, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 0
|
||||
};
|
||||
}
|
||||
72
400_xowa/src/gplx/core/brys/Int_flag_bldr__tst.java
Normal file
72
400_xowa/src/gplx/core/brys/Int_flag_bldr__tst.java
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
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.core.brys; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*;
|
||||
public class Int_flag_bldr__tst {
|
||||
private final Int_flag_bldr__fxt fxt = new Int_flag_bldr__fxt();
|
||||
@Test public void Bld_pow_ary() {
|
||||
fxt.Test__bld_pow_ary(fxt.Make__ary(1, 1, 1, 1), fxt.Make__ary(8, 4, 2, 1));
|
||||
fxt.Test__bld_pow_ary(fxt.Make__ary(3, 2) , fxt.Make__ary(4, 1));
|
||||
}
|
||||
@Test public void To_int__1_1_1() {
|
||||
int[] seg_ary = fxt.Make__ary(1, 1, 1);
|
||||
fxt.Test__to_int(seg_ary , fxt.Make__ary(0, 0, 0), 0);
|
||||
fxt.Test__to_int(seg_ary , fxt.Make__ary(1, 1, 1), 7);
|
||||
fxt.Test__to_int(seg_ary , fxt.Make__ary(1, 0, 0), 4);
|
||||
fxt.Test__to_int(seg_ary , fxt.Make__ary(1, 1, 0), 6);
|
||||
fxt.Test__to_int(seg_ary , fxt.Make__ary(0, 1, 1), 3);
|
||||
}
|
||||
@Test public void To_int__2_3() {
|
||||
fxt.Test__to_int(fxt.Make__ary(2, 3) , fxt.Make__ary(3, 7) , 31);
|
||||
fxt.Test__to_int(fxt.Make__ary(1, 2, 3) , fxt.Make__ary(1, 3, 7) , 63);
|
||||
}
|
||||
@Test public void To_int__11_4_5_5_6() {
|
||||
fxt.Test__to_int(fxt.Make__ary(11, 4, 5, 5, 6), fxt.Make__ary(2012, 6, 3, 23, 17), 2110135761);
|
||||
fxt.Test__to_int(fxt.Make__ary(11, 4, 5, 5, 6), fxt.Make__ary(2012, 6, 3, 23, 18), 2110135762);
|
||||
}
|
||||
@Test public void To_int_ary() {
|
||||
fxt.Test__to_int_ary(fxt.Make__ary(1, 1, 1, 1) , 15, fxt.Make__ary(1, 1, 1, 1));
|
||||
fxt.Test__to_int_ary(fxt.Make__ary(3, 2) , 31, fxt.Make__ary(7, 3));
|
||||
fxt.Test__to_int_ary(fxt.Make__ary(3, 2, 1) , 63, fxt.Make__ary(7, 3, 1));
|
||||
fxt.Test__to_int_ary(fxt.Make__ary(12, 4, 5, 5, 6), 2110135761, fxt.Make__ary(2012, 6, 3, 23, 17));
|
||||
fxt.Test__to_int_ary(fxt.Make__ary(12, 4, 5, 5, 6), 2110135762, fxt.Make__ary(2012, 6, 3, 23, 18));
|
||||
}
|
||||
@Test public void To_int_date_short() {
|
||||
fxt.Test__to_int_date_short("20120604 2359", 117843451);
|
||||
fxt.Test__to_int_date_short("20120604 2358", 117843450);
|
||||
fxt.Test__to_int_date_short("20120605 0000", 117843968);
|
||||
}
|
||||
}
|
||||
class Int_flag_bldr__fxt {
|
||||
public int[] Make__ary(int... v) {return v;}
|
||||
public void Test__bld_pow_ary(int[] seg_ary, int[] expd) {Tfds.Eq_ary_str(expd, Int_flag_bldr_.Bld_pow_ary(seg_ary));}
|
||||
public void Test__to_int(int[] seg_ary, int[] val_ary, int expd) {
|
||||
int[] pow_ary = Int_flag_bldr_.Bld_pow_ary(seg_ary);
|
||||
Tfds.Eq(expd, Int_flag_bldr_.To_int(pow_ary, val_ary));
|
||||
}
|
||||
public void Test__to_int_ary(int[] seg_ary, int val, int[] expd) {
|
||||
int[] pow_ary = Int_flag_bldr_.Bld_pow_ary(seg_ary);
|
||||
Tfds.Eq_ary_str(expd, Int_flag_bldr_.To_int_ary(pow_ary, val));
|
||||
}
|
||||
public void Test__to_int_date_short(String date_str, int expd) {
|
||||
DateAdp date = DateAdp_.parse_fmt(date_str, "yyyyMMdd HHmm");
|
||||
int date_int = Int_flag_bldr_.To_int_date_short(date.XtoSegAry());
|
||||
Tfds.Eq(expd, date_int);
|
||||
Tfds.Eq(date_str, Int_flag_bldr_.To_date_short(date_int).XtoStr_fmt("yyyyMMdd HHmm"));
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,7 @@ class Gfo_cmd_arg_mgr_fxt {
|
||||
return this;
|
||||
}
|
||||
public Gfo_cmd_arg_mgr_fxt Test_write(String... expd) {
|
||||
Tfds.Eq_ary_str(expd, ((Gfo_usr_dlg__gui_test)usr_dlg.Gui_wkr()).Xto_str_ary_and_clear());
|
||||
Tfds.Eq_ary_str(expd, ((Gfo_usr_dlg__gui_test)usr_dlg.Gui_wkr()).Msgs().To_str_ary_and_clear());
|
||||
return this;
|
||||
}
|
||||
public void Test_val_as_url_rel_dir_or(String root_dir, String dir_spr, String val, String expd) {
|
||||
|
||||
Reference in New Issue
Block a user