1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-09-28 14:30:51 +00:00

Xomw: Add message param utility classes

This commit is contained in:
gnosygnu 2017-03-31 09:36:47 -04:00
parent f57fd74e13
commit 145635b6c7
5 changed files with 401 additions and 299 deletions

View File

@ -54,8 +54,9 @@ public class Byte_ {
public static byte[] Ary_by_ints(int... ary) {
int ary_len = ary.length;
byte[] rv = new byte[ary_len];
for (int i = 0; i < ary_len; i++)
for (int i = 0; i < ary_len; i++) {
rv[i] = By_int(ary[i]);
}
return rv;
}
}

View File

@ -47,6 +47,22 @@ public class Hex_utl_ {
}
return rv;
}
public static byte[] Parse_hex_to_bry(String src) {return Parse_hex_to_bry(Bry_.new_u8(src));}
public static byte[] Parse_hex_to_bry(byte[] src) {
int src_len = src.length;
if ((src_len % 2) != 0) throw Err_.new_wo_type("hex_utl: hex_string must have an even length; src=~{0}", src);
int ary_len = src_len / 2;
byte[] rv = new byte[ary_len];
int rv_idx = 0;
for (int i = 0; i < src_len; i += 2) {
int val = Parse_or(src, i, i + 2, -1);
if (val == -1) throw Err_.new_wo_type("hex_utl: hex_string has invalid char; src=~{0}", src);
rv[rv_idx] = (byte)val;
rv_idx++;
}
return rv;
}
public static byte[] Encode_bry(byte[] src) {
int src_len = src.length;
byte[] trg = new byte[src_len * 2];

View File

@ -53,6 +53,9 @@ public class Hex_utl__tst {
fxt.Test__write_bfr(Bool_.Y, 256, "100");
fxt.Test__write_bfr(Bool_.Y, Int_.Max_value, "7fffffff");
}
@Test public void Encode() {
fxt.Test__parse_hex_to_bry("E2A7BC", 226, 167, 188);
}
}
class Hex_utl__fxt {
public void Test__write(String s, int bgn, int end, int val, String expd) {
@ -75,8 +78,12 @@ class Hex_utl__fxt {
Hex_utl_.Write_bfr(bfr, lcase, val);
Gftest.Eq__str(expd, bfr.To_str_and_clear());
}
// public void Test__encode_bry(int val, int pad, String expd) {
// String actl = Hex_utl_.To_str(val, pad);
// Tfds.Eq(expd, actl);
// }
public void Test__encode_bry(String val, int... expd) {
byte[] actl = Hex_utl_.Encode_bry(Bry_.new_u8(val));
Gftest.Eq__ary(Byte_.Ary_by_ints(expd), actl, "encode");
}
public void Test__parse_hex_to_bry(String val, int... expd) {
byte[] actl = Hex_utl_.Parse_hex_to_bry(Bry_.new_u8(val));
Gftest.Eq__ary(Byte_.Ary_by_ints(expd), actl, "encode");
}
}

View File

@ -20,6 +20,7 @@ public class Gftest {
public static void Eq__ary(boolean[] expd, boolean[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bool, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary(int[] expd, int[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__int, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary(long[] expd, long[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__long, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary(byte[] expd, byte[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__byte, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary__lines(String expd, byte[] actl) {Eq__ary__lines(expd, String_.new_u8(actl), "no_msg");}
public static void Eq__ary__lines(String expd, byte[] actl, String msg_fmt, Object... msg_args) {Eq__ary__lines(expd, String_.new_u8(actl), msg_fmt, msg_args);}
public static void Eq__ary__lines(String expd, String actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bry, Bry_split_.Split_lines(Bry_.new_u8_safe(expd)), Bry_split_.Split_lines(Bry_.new_u8_safe(actl)), msg_fmt, msg_args);}
@ -154,6 +155,7 @@ public class Gftest {
case Type_adp_.Tid__bry: bfr.Add_safe((byte[])Array_.Get_at(ary, idx)); break;
case Type_adp_.Tid__long: bfr.Add_long_variable(Long_.cast(Array_.Get_at(ary, idx))); break;
case Type_adp_.Tid__int: bfr.Add_int_variable(Int_.cast(Array_.Get_at(ary, idx))); break;
case Type_adp_.Tid__byte: bfr.Add_int_variable((int)(Byte_.cast(Array_.Get_at(ary, idx)))); break;
default: throw Err_.new_unhandled_default(type_id);
}
}
@ -178,6 +180,7 @@ public class Gftest {
case Type_adp_.Tid__bry: eq = Bry_.Eq((byte[])expd_obj, (byte[])actl_obj); break;
case Type_adp_.Tid__long: eq = Long_.cast(expd_obj) == Long_.cast(actl_obj); break;
case Type_adp_.Tid__int: eq = Int_.cast(expd_obj) == Int_.cast(actl_obj); break;
case Type_adp_.Tid__byte: eq = Byte_.cast(expd_obj) == Byte_.cast(actl_obj); break;
}
}
if (!eq) {