mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v3.1.3.1
This commit is contained in:
@@ -23,6 +23,7 @@ public class Bry_ {
|
||||
public static final byte[] Empty = new byte[0];
|
||||
public static final byte[][] Ary_empty = new byte[0][];
|
||||
public static final Class<?> Cls_ref_type = byte[].class;
|
||||
public static byte[] cast(Object val) {return (byte[])val;}
|
||||
public static byte[] New_by_byte(byte b) {return new byte[] {b};}
|
||||
public static byte[] New_by_ints(int... ary) {
|
||||
int len = ary.length;
|
||||
@@ -833,13 +834,16 @@ public class Bry_ {
|
||||
if (src[i] == find) src[i] = replace;
|
||||
}
|
||||
}
|
||||
public static byte[] Replace(byte[] src, byte find, byte replace) {
|
||||
public static byte[] Replace(byte[] src, byte find, byte replace) {return Replace(src, 0, src.length, find, replace);}
|
||||
public static byte[] Replace(byte[] src, int bgn, int end, byte find, byte replace) {
|
||||
int src_len = src.length;
|
||||
byte[] rv = new byte[src_len];
|
||||
for (int i = 0; i < src_len; i++) {
|
||||
for (int i = bgn; i < end; ++i) {
|
||||
byte b = src[i];
|
||||
rv[i] = b == find ? replace : b;
|
||||
}
|
||||
for (int i = end; i < src_len; ++i)
|
||||
rv[i] = src[i];
|
||||
return rv;
|
||||
}
|
||||
public static byte[] Replace_safe(Bry_bfr bfr, byte[] src, byte[] find, byte[] repl) {
|
||||
|
||||
@@ -21,16 +21,9 @@ public class Io_url implements CompareAble, ParseAble, GfoInvkAble { //_20101005
|
||||
public IoUrlInfo Info() {return info;} IoUrlInfo info;
|
||||
public String Raw() {return raw;} final String raw;
|
||||
public byte[] RawBry() {return Bry_.new_u8(raw);}
|
||||
// public byte[] Http_file_bry() {
|
||||
// try {return Bry_.new_u8(String_.Concat(http_file_str, java.net.URLEncoder.encode(raw, "UTF-8")));}
|
||||
// catch (Exception e) {throw Err_.err_(e, "Http_file_bry");}
|
||||
// }
|
||||
public String To_http_file_str() {return Http_file_str + Http_file_str_encoder.Encode_str(raw);}
|
||||
public byte[] To_http_file_bry() {
|
||||
return Bry_.Add(Http_file_bry, Http_file_str_encoder.Encode_bry(raw));
|
||||
}
|
||||
public String To_http_file_str() {return String_.Len_eq_0(raw) ? String_.Empty : String_.Concat (Http_file_str, Http_file_str_encoder.Encode_str(raw));}
|
||||
public byte[] To_http_file_bry() {return String_.Len_eq_0(raw) ? Bry_.Empty : Bry_.Add (Http_file_bry, Http_file_str_encoder.Encode_bry(raw));}
|
||||
public static Url_encoder_interface Http_file_str_encoder = Url_encoder_interface_same.Instance;
|
||||
|
||||
public static final String Http_file_str = "file:///";
|
||||
public static final int Http_file_len = String_.Len(Http_file_str);
|
||||
public static final byte[] Http_file_bry = Bry_.new_a7(Http_file_str);
|
||||
|
||||
@@ -37,6 +37,7 @@ public class Object_ {
|
||||
else if (lhs == null || rhs == null) return false;
|
||||
else return lhs.equals(rhs);
|
||||
}
|
||||
public static String Xto_str_or(Object v, String or) {return v == null ? or : ToString_lang(v);}
|
||||
public static String Xto_str_strict_or_null(Object v) {return v == null ? null : ToString_lang(v);}
|
||||
public static String Xto_str_strict_or_null_mark(Object v) {return v == null ? String_.Null_mark : ToString_lang(v);}
|
||||
public static String Xto_str_strict_or_empty(Object v) {return v == null ? String_.Empty : ToString_lang(v);}
|
||||
|
||||
@@ -534,6 +534,13 @@ public class String_ implements GfoInvkAble {
|
||||
}
|
||||
return trg_ary;
|
||||
}
|
||||
public static boolean Ary_eq(String[] lhs, String[] rhs) {
|
||||
int lhs_len = lhs.length, rhs_len = rhs.length;
|
||||
if (lhs_len != rhs_len) return false;
|
||||
for (int i = 0; i < lhs_len; ++i)
|
||||
if (!String_.Eq(lhs[i], rhs[i])) return false;
|
||||
return true;
|
||||
}
|
||||
public static String To_str__as_kv_ary(String... ary) {
|
||||
int len = ary.length;
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
|
||||
53
100_core/src/gplx/core/brys/Bry_err_wkr.java
Normal file
53
100_core/src/gplx/core/brys/Bry_err_wkr.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
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 gplx.core.errs.*;
|
||||
public class Bry_err_wkr {
|
||||
private String sect; private int sect_bgn;
|
||||
public byte[] Src() {return src;} private byte[] src;
|
||||
public String Page() {return page;} private String page;
|
||||
public void Fail_throws_err_(boolean v) {this.fail_throws_err = v;} private boolean fail_throws_err = true;
|
||||
public void Init_by_page(String page, byte[] src) {this.page = page; this.src = src;}
|
||||
public void Init_by_sect(String sect, int sect_bgn) {this.sect = sect; this.sect_bgn = sect_bgn;}
|
||||
public void Warn(String msg, Object... args) {
|
||||
boolean old = fail_throws_err;
|
||||
fail_throws_err = false;
|
||||
this.Fail(msg, args);
|
||||
fail_throws_err = old;
|
||||
}
|
||||
public int Fail(String msg, Object... args) {return Fail(msg, sect_bgn, sect_bgn + 255, args);}
|
||||
private int Fail(String msg, int excerpt_bgn, int excerpt_end, Object[] args) {
|
||||
String err_msg = Make_msg(msg, excerpt_bgn, excerpt_end, args);
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", err_msg);
|
||||
if (fail_throws_err) throw Err_.new_("Bry_err_wkr", err_msg).Logged_y_();
|
||||
return Bry_find_.Not_found;
|
||||
}
|
||||
private String Make_msg(String msg, int excerpt_bgn, int excerpt_end, Object[] args) {
|
||||
int args_len = args.length;
|
||||
args_len += 6;
|
||||
args = (Object[])Array_.Resize(args, args_len);
|
||||
args[args_len - 6] = "page"; args[args_len - 5] = Quote(page);
|
||||
args[args_len - 4] = "sect"; args[args_len - 3] = Quote(sect);
|
||||
args[args_len - 2] = "text"; args[args_len - 1] = Bry_.Escape_ws(Bry_.Mid_safe(src, excerpt_bgn, excerpt_end));
|
||||
for (int i = 0; i < args_len - 6; i += 2) {
|
||||
args[i + 1] = Quote(Object_.Xto_str_strict_or_null_mark(args[i + 1]));
|
||||
}
|
||||
return Err_msg.To_str(msg, args);
|
||||
}
|
||||
private static String Quote(String v) {return "'" + v + "'";}
|
||||
}
|
||||
204
100_core/src/gplx/core/brys/Bry_rdr.java
Normal file
204
100_core/src/gplx/core/brys/Bry_rdr.java
Normal file
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
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 gplx.core.errs.*; import gplx.core.btries.*;
|
||||
public class Bry_rdr {
|
||||
private final gplx.core.primitives.Int_obj_ref pos_ref = gplx.core.primitives.Int_obj_ref.neg1_();
|
||||
public byte[] Src() {return src;} protected byte[] src;
|
||||
public int Src_end() {return src_end;} protected int src_end;
|
||||
public int Pos() {return pos;} protected int pos;
|
||||
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) {err_wkr.Fail_throws_err_(v); return this;}
|
||||
public Bry_rdr Init_by_src(byte[] src) {err_wkr.Init_by_page("", src); this.pos = 0; this.src = src; this.src_end = src.length; return this;}
|
||||
public Bry_rdr Init_by_page(byte[] page, byte[] src, int src_len) {err_wkr.Init_by_page(String_.new_u8(page), src); this.pos = 0; this.src = src; this.src_end = src_len; return this;}
|
||||
public Bry_rdr Init_by_sect(String sect, int sect_bgn, int pos) {err_wkr.Init_by_sect(sect, sect_bgn); this.pos = pos; return this;}
|
||||
public Bry_rdr Init_by_wkr (Bry_err_wkr wkr, String sect, int pos, int src_end) {
|
||||
this.pos = pos; this.src = wkr.Src(); this.src_end = src_end;
|
||||
err_wkr.Init_by_page(wkr.Page(), src);
|
||||
err_wkr.Init_by_sect(sect, pos);
|
||||
return this;
|
||||
}
|
||||
public Bry_err_wkr Err_wkr() {return err_wkr;} private Bry_err_wkr err_wkr = new Bry_err_wkr();
|
||||
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) {err_wkr.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) {err_wkr.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) {err_wkr.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_non_num() {return Read_int_to(Byte_ascii.Null);}
|
||||
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
|
||||
err_wkr.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) {
|
||||
err_wkr.Fail("invalid int", "mid", String_.new_u8(src, bgn, pos));
|
||||
return Int_.Min_value;
|
||||
}
|
||||
return rv * negative;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bgn == pos) {err_wkr.Fail("int is empty"); return Int_.Min_value;}
|
||||
return rv * negative;
|
||||
}
|
||||
public int Read_hzip_int(int reqd) {
|
||||
int rv = gplx.core.encoders.Gfo_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) {err_wkr.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)) {err_wkr.Fail("failed check", "chk", String_.new_u8(find)); return Bry_find_.Not_found;}
|
||||
pos = find_end;
|
||||
return pos;
|
||||
}
|
||||
public byte Chk(Btrie_slim_mgr trie) {return Chk(trie, pos, src_end);}
|
||||
public void Chk_trie_val(Btrie_slim_mgr trie, byte val) {
|
||||
byte rv = Chk_or(trie, Byte_.Max_value_127);
|
||||
if (rv == Byte_.Max_value_127) err_wkr.Fail("failed trie check", "mid", String_.new_u8(Bry_.Mid_by_len_safe(src, pos, 16)));
|
||||
}
|
||||
public Object Chk_trie_as_obj(Btrie_slim_mgr trie) {
|
||||
Object rv = trie.Match_bgn(src, pos, src_end); if (rv == null) err_wkr.Fail("failed trie check", "mid", String_.new_u8(Bry_.Mid_by_len_safe(src, pos, 16)));
|
||||
return rv;
|
||||
}
|
||||
public byte Chk_or(Btrie_slim_mgr trie, byte or) {return Chk_or(trie, pos, src_end, or);}
|
||||
public byte Chk(Btrie_slim_mgr trie, int itm_bgn, int itm_end) {
|
||||
byte rv = Chk_or(trie, itm_bgn, itm_end, Byte_.Max_value_127);
|
||||
if (rv == Byte_.Max_value_127) {err_wkr.Fail("failed trie check", "mid", String_.new_u8(Bry_.Mid_by_len_safe(src, pos, 16))); return Byte_.Max_value_127;}
|
||||
return rv;
|
||||
}
|
||||
public byte Chk_or(Btrie_slim_mgr trie, int itm_bgn, int itm_end, byte or) {
|
||||
Object rv_obj = trie.Match_bgn(src, itm_bgn, itm_end);
|
||||
if (rv_obj == null) return or;
|
||||
pos = trie.Match_pos();
|
||||
return ((gplx.core.primitives.Byte_obj_val)rv_obj).Val();
|
||||
}
|
||||
@gplx.Virtual public Bry_rdr Skip_ws() {
|
||||
while (pos < src_end) {
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Tab: case Byte_ascii.Nl: case Byte_ascii.Cr: case Byte_ascii.Space:
|
||||
++pos;
|
||||
break;
|
||||
default:
|
||||
return this;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public Bry_rdr Skip_alpha_num_under() {
|
||||
while (pos < src_end) {
|
||||
switch (src[pos]) {
|
||||
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:
|
||||
case Byte_ascii.Ltr_A: case Byte_ascii.Ltr_B: case Byte_ascii.Ltr_C: case Byte_ascii.Ltr_D: case Byte_ascii.Ltr_E:
|
||||
case Byte_ascii.Ltr_F: case Byte_ascii.Ltr_G: case Byte_ascii.Ltr_H: case Byte_ascii.Ltr_I: case Byte_ascii.Ltr_J:
|
||||
case Byte_ascii.Ltr_K: case Byte_ascii.Ltr_L: case Byte_ascii.Ltr_M: case Byte_ascii.Ltr_N: case Byte_ascii.Ltr_O:
|
||||
case Byte_ascii.Ltr_P: case Byte_ascii.Ltr_Q: case Byte_ascii.Ltr_R: case Byte_ascii.Ltr_S: case Byte_ascii.Ltr_T:
|
||||
case Byte_ascii.Ltr_U: case Byte_ascii.Ltr_V: case Byte_ascii.Ltr_W: case Byte_ascii.Ltr_X: case Byte_ascii.Ltr_Y: case Byte_ascii.Ltr_Z:
|
||||
case Byte_ascii.Ltr_a: case Byte_ascii.Ltr_b: case Byte_ascii.Ltr_c: case Byte_ascii.Ltr_d: case Byte_ascii.Ltr_e:
|
||||
case Byte_ascii.Ltr_f: case Byte_ascii.Ltr_g: case Byte_ascii.Ltr_h: case Byte_ascii.Ltr_i: case Byte_ascii.Ltr_j:
|
||||
case Byte_ascii.Ltr_k: case Byte_ascii.Ltr_l: case Byte_ascii.Ltr_m: case Byte_ascii.Ltr_n: case Byte_ascii.Ltr_o:
|
||||
case Byte_ascii.Ltr_p: case Byte_ascii.Ltr_q: case Byte_ascii.Ltr_r: case Byte_ascii.Ltr_s: case Byte_ascii.Ltr_t:
|
||||
case Byte_ascii.Ltr_u: case Byte_ascii.Ltr_v: case Byte_ascii.Ltr_w: case Byte_ascii.Ltr_x: case Byte_ascii.Ltr_y: case Byte_ascii.Ltr_z:
|
||||
case Byte_ascii.Underline:
|
||||
++pos;
|
||||
break;
|
||||
default:
|
||||
return this;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
56
100_core/src/gplx/core/brys/Bry_rdr_tst.java
Normal file
56
100_core/src/gplx/core/brys/Bry_rdr_tst.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
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 Bry_rdr_tst {
|
||||
@Before public void init() {fxt.Clear();} private Bry_rdr_fxt fxt = new Bry_rdr_fxt();
|
||||
@Test public void Int() {
|
||||
fxt.Init_src("12|3456|789");
|
||||
fxt.Test_read_int(12);
|
||||
fxt.Test_read_int(3456);
|
||||
fxt.Test_read_int(789);
|
||||
fxt.Test_read_int(Int_.Min_value);
|
||||
}
|
||||
@Test public void Int_negative() {
|
||||
fxt.Init_src("-1|-2");
|
||||
fxt.Test_read_int(-1);
|
||||
fxt.Test_read_int(-2);
|
||||
}
|
||||
@Test public void Bry() {
|
||||
fxt.Init_src("abc|d||ef");
|
||||
fxt.Test_read_bry("abc");
|
||||
fxt.Test_read_bry("d");
|
||||
fxt.Test_read_bry("");
|
||||
fxt.Test_read_bry("ef");
|
||||
fxt.Test_read_bry(null);
|
||||
}
|
||||
}
|
||||
class Bry_rdr_fxt {
|
||||
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) {
|
||||
Tfds.Eq(expd_val, rdr.Read_int_to_pipe());
|
||||
}
|
||||
public void Test_read_bry(String expd_str) {
|
||||
byte[] actl_bry = rdr.Read_bry_to_pipe();
|
||||
String actl_str = actl_bry == null ? null : String_.new_u8(actl_bry);
|
||||
Tfds.Eq(expd_str, actl_str);
|
||||
}
|
||||
}
|
||||
63
100_core/src/gplx/core/encoders/Gfo_hzip_int_.java
Normal file
63
100_core/src/gplx/core/encoders/Gfo_hzip_int_.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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.encoders; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Gfo_hzip_int_ {
|
||||
public static final int Neg_1_adj = 1;
|
||||
public static void Encode(int reqd, Bry_bfr bfr, int val) {
|
||||
int bfr_len = bfr.Len();
|
||||
int len_in_base85 = Base85_.Bry_len(val);
|
||||
boolean abrv = val < Base85_.Pow85[reqd];
|
||||
int adj = abrv ? 0 : 1;
|
||||
int actl_len = len_in_base85 + adj;
|
||||
if (actl_len < reqd) actl_len = reqd;
|
||||
bfr.Add_byte_repeat(Byte_ascii.Bang, actl_len); // fill with 0s; this asserts that there underlying array will be large enough for following write
|
||||
byte[] bfr_bry = bfr.Bfr(); // NOTE: set bry reference here b/c Add_byte_repeat may create a new one
|
||||
Base85_.Set_bry(val, bfr_bry, bfr_len + adj, reqd); // calc base85 val for val; EX: 7224 -> "uu"
|
||||
if (!abrv) {
|
||||
byte base85_byte = Base85_len__2;
|
||||
switch (len_in_base85) {
|
||||
case 3: base85_byte = Base85_len__3; break;
|
||||
case 4: base85_byte = Base85_len__4; break;
|
||||
case 5: base85_byte = Base85_len__5; break;
|
||||
}
|
||||
bfr_bry[bfr_len] = base85_byte;
|
||||
}
|
||||
}
|
||||
public static int Decode(int reqd, byte[] src, int src_len, int src_bgn, Int_obj_ref pos_ref) {
|
||||
byte b0 = src[src_bgn];
|
||||
int base85_bgn = src_bgn + 1;
|
||||
int len_in_base85 = 1; // default to 1
|
||||
switch (b0) {
|
||||
case Base85_len__2: len_in_base85 = 2; break;
|
||||
case Base85_len__3: len_in_base85 = 3; break;
|
||||
case Base85_len__4: len_in_base85 = 4; break;
|
||||
case Base85_len__5: len_in_base85 = 5; break;
|
||||
default: --base85_bgn; break;
|
||||
}
|
||||
if (len_in_base85 < reqd) len_in_base85 = reqd;
|
||||
int base85_end = base85_bgn + len_in_base85;
|
||||
pos_ref.Val_(base85_end);
|
||||
return Base85_.To_int_by_bry(src, base85_bgn, base85_end - 1);
|
||||
}
|
||||
private static final byte
|
||||
Base85_len__2 = Byte_ascii.Curly_bgn
|
||||
, Base85_len__3 = Byte_ascii.Pipe
|
||||
, Base85_len__4 = Byte_ascii.Curly_end
|
||||
, Base85_len__5 = Byte_ascii.Tilde;
|
||||
}
|
||||
Reference in New Issue
Block a user