mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Refactor: Clean up Int_ classes
This commit is contained in:
@@ -14,19 +14,26 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx;
|
||||
import gplx.langs.gfs.*;
|
||||
public class Bool_ {
|
||||
public class Bool_ {//RF:2017-10-08
|
||||
public static final String Cls_val_name = "bool";
|
||||
public static final Class<?> Cls_ref_type = Boolean.class;
|
||||
|
||||
public static final boolean N = false , Y = true;
|
||||
public static final byte N_byte = 0 , Y_byte = 1 , __byte = 127;
|
||||
public static final int N_int = 0 , Y_int = 1 , __int = -1;
|
||||
public static final byte[] N_bry = new byte[] {Byte_ascii.Ltr_n}, Y_bry = new byte[] {Byte_ascii.Ltr_y};
|
||||
|
||||
public static final String True_str = "true", False_str = "false";
|
||||
public static final byte[] True_bry = Bry_.new_a7(True_str), False_bry = Bry_.new_a7(False_str);
|
||||
|
||||
public static boolean By_int(int v) {return v == Y_int;}
|
||||
public static int To_int(boolean v) {return v ? Y_int : N_int;}
|
||||
public static byte To_byte(boolean v) {return v ? Y_byte : N_byte;}
|
||||
public static String To_str_lower(boolean v) {return v ? True_str : False_str;}
|
||||
|
||||
public static boolean Cast(Object obj) {try {return (Boolean)obj;} catch (Exception e) {throw Err_.new_type_mismatch_w_exc(e, boolean.class, obj);}}
|
||||
public static boolean Cast_or(Object obj, boolean or) {try {return (Boolean)obj;} catch (Exception e) {Err_.Noop(e); return or;}}
|
||||
|
||||
public static boolean Parse(String raw) {
|
||||
if ( String_.Eq(raw, True_str)
|
||||
|| String_.Eq(raw, "True") // needed for Store_Wtr(){boolVal.toString();}
|
||||
@@ -49,13 +56,10 @@ public class Bool_ {
|
||||
return false;
|
||||
return or;
|
||||
}
|
||||
|
||||
public static int Compare(boolean lhs, boolean rhs) {
|
||||
if ( lhs == rhs) return CompareAble_.Same;
|
||||
else if (!lhs && rhs) return CompareAble_.Less;
|
||||
else /*lhs && !rhs*/ return CompareAble_.More;
|
||||
}
|
||||
public static boolean By_int(int v) {return v == Y_int;}
|
||||
public static int To_int(boolean v) {return v ? Y_int : N_int;}
|
||||
public static byte To_byte(boolean v) {return v ? Y_byte : N_byte;}
|
||||
public static String To_str_lower(boolean v) {return v ? True_str : False_str;}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class Bry_ {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Object itm = ary[i];
|
||||
Class<?> type = Type_.Type_by_obj(itm);
|
||||
if (Type_.Eq(type, int.class)) bfr.Add_byte((byte)Int_.cast(itm));
|
||||
if (Type_.Eq(type, int.class)) bfr.Add_byte((byte)Int_.Cast(itm));
|
||||
else if (Type_.Eq(type, String.class)) bfr.Add_str_u8((String)itm);
|
||||
else if (Type_.Eq(type, byte[].class)) bfr.Add((byte[])itm);
|
||||
else throw Err_.new_unhandled(Type_.Canonical_name(type));
|
||||
|
||||
@@ -162,7 +162,7 @@ public class Bry__tst {
|
||||
} void tst_ReadCsvDte(String raw) {Tfds.Eq_date(DateAdp_.parse_fmt(raw, Bry_.Fmt_csvDte), Bry_.ReadCsvDte(Bry_.new_u8(raw + "|"), Int_obj_ref.New_zero(), (byte)'|'));}
|
||||
@Test public void ReadCsvInt() {
|
||||
tst_ReadCsvInt("1234567890");
|
||||
} void tst_ReadCsvInt(String raw) {Tfds.Eq(Int_.parse(raw), Bry_.ReadCsvInt(Bry_.new_u8(raw + "|"), Int_obj_ref.New_zero(), (byte)'|'));}
|
||||
} void tst_ReadCsvInt(String raw) {Tfds.Eq(Int_.Parse(raw), Bry_.ReadCsvInt(Bry_.new_u8(raw + "|"), Int_obj_ref.New_zero(), (byte)'|'));}
|
||||
@Test public void Trim() {
|
||||
Trim_tst("a b c", 1, 4, "b");
|
||||
Trim_tst("a c", 1, 3, "");
|
||||
|
||||
@@ -453,8 +453,8 @@ public class Bry_bfr {
|
||||
if (o == null) return this; // treat null as empty String;
|
||||
Class<?> o_type = o.getClass();
|
||||
if (o_type == byte[].class) Add((byte[])o);
|
||||
else if (o_type == Integer.class) Add_int_variable(Int_.cast(o));
|
||||
else if (o_type == Byte.class) Add_byte(Byte_.cast(o));
|
||||
else if (o_type == Integer.class) Add_int_variable(Int_.Cast(o));
|
||||
else if (o_type == Byte.class) Add_byte(Byte_.Cast(o));
|
||||
else if (o_type == Long.class) Add_long_variable(Long_.cast(o));
|
||||
else if (o_type == String.class) Add_str_u8((String)o);
|
||||
else if (o_type == Bry_bfr.class) Add_bfr_and_preserve((Bry_bfr)o);
|
||||
@@ -470,8 +470,8 @@ public class Bry_bfr {
|
||||
if (o == null) return this; // treat null as empty String;
|
||||
Class<?> o_type = o.getClass();
|
||||
if (o_type == byte[].class) Add((byte[])o);
|
||||
else if (o_type == Integer.class) Add_int_variable(Int_.cast(o));
|
||||
else if (o_type == Byte.class) Add_byte(Byte_.cast(o));
|
||||
else if (o_type == Integer.class) Add_int_variable(Int_.Cast(o));
|
||||
else if (o_type == Byte.class) Add_byte(Byte_.Cast(o));
|
||||
else if (o_type == Long.class) Add_long_variable(Long_.cast(o));
|
||||
else if (o_type == String.class) Add_str_u8((String)o);
|
||||
else if (o_type == Bry_bfr.class) Add_bfr_and_preserve((Bry_bfr)o);
|
||||
|
||||
@@ -67,7 +67,7 @@ public class Bry_bfr_tst {
|
||||
void Add_int_variable(int val) {
|
||||
bb.Clear();
|
||||
bb.Add_int_variable(val);
|
||||
Tfds.Eq(val, Int_.parse(String_.new_u8(bb.To_bry())));
|
||||
Tfds.Eq(val, Int_.Parse(String_.new_u8(bb.To_bry())));
|
||||
}
|
||||
@Test public void Add_int_fixed_len3() {tst_Add_int_fixed(123, 3, "123");}
|
||||
@Test public void Add_int_fixed_pad_1() {tst_Add_int_fixed(2, 1, "2");}
|
||||
|
||||
@@ -14,42 +14,48 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx;
|
||||
public class Byte_ {
|
||||
public class Byte_ {//RF:2017-10-08
|
||||
public static final String Cls_val_name = "byte";
|
||||
public static final Class<?> Cls_ref_type = Byte.class;
|
||||
|
||||
public static final byte
|
||||
Zero = 0
|
||||
, Min_value = Byte.MIN_VALUE
|
||||
, Max_value_127 = 127
|
||||
, Val_128 = -128
|
||||
, Val_255 = -1
|
||||
;
|
||||
public static byte cast(Object o) {try {return (Byte)o;} catch (Exception e) {throw Err_.new_type_mismatch_w_exc(e, byte.class, o);}}
|
||||
public static byte parse(String raw) {return Byte.parseByte(raw);}
|
||||
public static byte parse_or(String raw, byte or) {
|
||||
if (raw == null) return or;
|
||||
try {return parse(raw);}
|
||||
catch (Exception e) {Err_.Noop(e); return or;}
|
||||
}
|
||||
;
|
||||
|
||||
public static byte By_int(int v) {return v > 127 ? (byte)(v - 256) : (byte)v;} // PERF?: (byte)(v & 0xff)
|
||||
public static int To_int(byte v) {return v < 0 ? (int)v + 256 : v;}
|
||||
public static String To_str(byte v) {return new Byte(v).toString();}
|
||||
public static byte[] To_bry(byte v) {return new byte[] {v};}
|
||||
public static boolean In(byte v, byte... ary) {
|
||||
|
||||
public static byte Cast(Object o) {try {return (Byte)o;} catch (Exception e) {throw Err_.new_type_mismatch_w_exc(e, byte.class, o);}}
|
||||
public static byte Parse(String raw) {return Byte.parseByte(raw);}
|
||||
public static byte Parse_or(String raw, byte or) {
|
||||
if (raw == null) return or;
|
||||
try {return Parse(raw);}
|
||||
catch (Exception e) {Err_.Noop(e); return or;}
|
||||
}
|
||||
|
||||
public static boolean Match_any(byte v, byte... ary) {
|
||||
for (byte itm : ary)
|
||||
if (v == itm) return true;
|
||||
return false;
|
||||
}
|
||||
public static boolean Eq_many(byte v, byte... ary) {
|
||||
public static boolean Match_all(byte v, byte... ary) {
|
||||
for (byte itm : ary)
|
||||
if (v != itm) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static int Compare(byte lhs, byte rhs) {
|
||||
if (lhs == rhs) return CompareAble_.Same;
|
||||
else if (lhs < rhs) return CompareAble_.Less;
|
||||
else return CompareAble_.More;
|
||||
}
|
||||
|
||||
public static byte[] Ary(byte... ary) {return ary;}
|
||||
public static byte[] Ary_by_ints(int... ary) {
|
||||
int ary_len = ary.length;
|
||||
|
||||
@@ -43,6 +43,8 @@ public class Byte_ascii {
|
||||
public static final byte
|
||||
Angle_bgn = Lt, Angle_end = Gt
|
||||
;
|
||||
|
||||
public static final int Len_1 = 1;
|
||||
public static final byte Max_7_bit = (byte)127, Ascii_min = 0, Ascii_max = 127;
|
||||
public static boolean Is_sym(byte b) {
|
||||
switch (b) {
|
||||
|
||||
@@ -131,8 +131,8 @@ class GfoMsg_base implements GfoMsg {
|
||||
return this;
|
||||
}
|
||||
public boolean ReadBool(String k) {Object rv = ReadOr(k,false); if (rv == Nil) ThrowNotFound(k); return parse ? Yn.parse_or((String)rv, false) : Bool_.Cast(rv);}
|
||||
public int ReadInt(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Int_.parse((String)rv) : Int_.cast(rv);}
|
||||
public byte ReadByte(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Byte_.parse((String)rv) : Byte_.cast(rv);}
|
||||
public int ReadInt(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Int_.Parse((String)rv) : Int_.Cast(rv);}
|
||||
public byte ReadByte(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Byte_.Parse((String)rv) : Byte_.Cast(rv);}
|
||||
public long ReadLong(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Long_.parse((String)rv) : Long_.cast(rv);}
|
||||
public float ReadFloat(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Float_.parse((String)rv) : Float_.cast(rv);}
|
||||
public double ReadDouble(String k) {Object rv = ReadOr(k, 0) ; if (rv == Nil) ThrowNotFound(k); return parse ? Double_.parse((String)rv) : Double_.cast(rv);}
|
||||
@@ -142,7 +142,7 @@ class GfoMsg_base implements GfoMsg {
|
||||
public Io_url ReadIoUrl(String k) {Object rv = ReadOr(k, null); if (rv == Nil) ThrowNotFound(k); return parse ? Io_url_.new_any_((String)rv) : Io_url_.cast(rv);}
|
||||
public Object CastObj(String k) {Object rv = ReadOr(k, null); if (rv == Nil) ThrowNotFound(k); return rv;}
|
||||
public boolean ReadBoolOr(String k, boolean or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Yn.parse_or((String)rv, or) : Bool_.Cast(rv);}
|
||||
public int ReadIntOr(String k, int or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Int_.parse((String)rv) : Int_.cast(rv);}
|
||||
public int ReadIntOr(String k, int or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Int_.Parse((String)rv) : Int_.Cast(rv);}
|
||||
public long ReadLongOr(String k, long or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Long_.parse((String)rv) : Long_.cast(rv);}
|
||||
public float ReadFloatOr(String k, float or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Float_.parse((String)rv) : Float_.cast(rv);}
|
||||
public double ReadDoubleOr(String k,double or) {Object rv = ReadOr(k, or) ; if (rv == Nil) return or ; return parse ? Double_.parse((String)rv) : Double_.cast(rv);}
|
||||
@@ -258,11 +258,11 @@ class XtoStrWkr_gplx implements XtoStrWkr {
|
||||
if (o == null) return "<<NULL>>";
|
||||
Class<?> type = Type_.Type_by_obj(o);
|
||||
String rv = null;
|
||||
if (type == String.class) rv = String_.cast(o);
|
||||
else if (Int_.TypeMatch(type)) return Int_.To_str(Int_.cast(o));
|
||||
else if (Type_.Eq(type, Bool_.Cls_ref_type)) return Yn.To_str(Bool_.Cast(o));
|
||||
else if (type == DateAdp.class) return DateAdp_.cast(o).XtoStr_gplx();
|
||||
else rv = Object_.Xto_str_strict_or_empty(o);
|
||||
if (Type_.Eq(type, String_.Cls_ref_type)) rv = String_.cast(o);
|
||||
else if (Type_.Eq(type, Int_.Cls_ref_type)) return Int_.To_str(Int_.Cast(o));
|
||||
else if (Type_.Eq(type, Bool_.Cls_ref_type)) return Yn.To_str(Bool_.Cast(o));
|
||||
else if (Type_.Eq(type, DateAdp_.Cls_ref_type)) return DateAdp_.cast(o).XtoStr_gplx();
|
||||
else rv = Object_.Xto_str_strict_or_empty(o);
|
||||
return String_.Replace(rv, "'", "''");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,47 +18,44 @@ import gplx.core.strings.*; import gplx.langs.gfs.*;
|
||||
public class Int_ {
|
||||
public static final String Cls_val_name = "int";
|
||||
public static final Class<?> Cls_ref_type = Integer.class;
|
||||
public static final int Base1 = 1;
|
||||
public static final int Const_dlm_len = 1;
|
||||
public static final int Const_position_after_char = 1;
|
||||
public static final int Null = Int_.Min_value;
|
||||
public static int coerce_(Object v) {
|
||||
try {String s = String_.as_(v); return s == null ? Int_.cast(v) : Int_.parse(s);}
|
||||
|
||||
public static final int
|
||||
Min_value = Integer.MIN_VALUE
|
||||
, Max_value = Integer.MAX_VALUE
|
||||
, Max_value__31 = 2147483647
|
||||
, Neg1 = -1
|
||||
, Null = Int_.Min_value
|
||||
, Base1 = 1 // for super 1 lists / arrays; EX: PHP; [a, b, c]; [1] => a
|
||||
, Offset_1 = 1 // common symbol for + 1 after current pos; EX: String_.Mid(lhs + Offset_1, rhs)
|
||||
;
|
||||
|
||||
public static int Cast(Object obj) {
|
||||
try {
|
||||
return (Integer)obj;
|
||||
}
|
||||
catch(Exception exc) {
|
||||
throw Err_.new_type_mismatch_w_exc(exc, int.class, obj);
|
||||
}
|
||||
}
|
||||
public static int Cast_or(Object obj, int or) {
|
||||
try {
|
||||
return (Integer)obj;
|
||||
}
|
||||
catch(Exception e) {
|
||||
Err_.Noop(e);
|
||||
return or;
|
||||
}
|
||||
}
|
||||
public static int Coerce(Object v) {
|
||||
try {
|
||||
String s = String_.as_(v);
|
||||
return s == null ? Int_.Cast(v) : Int_.Parse(s);
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_cast(e, int.class, v);}
|
||||
}
|
||||
public static int[] Ary_empty = new int[0];
|
||||
public static int[] Ary(int... v) {return v;}
|
||||
public static int[] Ary_copy(int[] ary) {return Ary_copy(ary, ary.length);}
|
||||
public static int[] Ary_copy(int[] ary, int new_len) {
|
||||
int old_len = ary.length;
|
||||
int[] rv = new int[new_len];
|
||||
for (int i = 0; i < old_len; i++)
|
||||
rv[i] = ary[i];
|
||||
return rv;
|
||||
}
|
||||
public static void Ary_copy_to(int[] src, int src_len, int[] trg) {
|
||||
for (int i = 0; i < src_len; ++i)
|
||||
trg[i] = src[i];
|
||||
}
|
||||
public static String Ary_concat(String spr, int... ary) {
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (i != 0) bfr.Add_str_u8(spr);
|
||||
int itm = ary[i];
|
||||
bfr.Add_int_variable(itm);
|
||||
}
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
public static int[] AryRng(int bgn, int end) {
|
||||
int len = end - bgn + 1;
|
||||
int[] rv = new int[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
rv[i] = bgn + i;
|
||||
return rv;
|
||||
}
|
||||
public static boolean Bounds_chk(int bgn, int end, int len) {return bgn > -1 && end < len;}
|
||||
public static int parse_or(String raw, int or) {
|
||||
|
||||
public static int Parse(String raw) {try {return Integer.parseInt(raw);} catch(Exception e) {throw Err_.new_parse_exc(e, int.class, raw);}}
|
||||
public static int Parse_or(String raw, int or) {
|
||||
if (raw == null) return or;
|
||||
int rawLen = String_.Len(raw); if (rawLen == 0) return or;
|
||||
int rv = 0, tmp = 0, factor = 1;
|
||||
@@ -75,15 +72,95 @@ public class Int_ {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static int EnsureLessThan(int v, int max) {return v >= max ? max : v;}
|
||||
|
||||
public static int By_double(double v) {return (int)v;}
|
||||
public static int By_hex_bry(byte[] src) {return By_hex_bry(src, 0, src.length);}
|
||||
public static int By_hex_bry(byte[] src, int bgn, int end) {
|
||||
int rv = 0; int factor = 1;
|
||||
for (int i = end - 1; i >= bgn; i--) {
|
||||
int val = By_hex_byte(src[i]);
|
||||
rv += (val * factor);
|
||||
factor *= 16;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static int By_hex_byte(byte b) {
|
||||
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:
|
||||
return b - Byte_ascii.Num_0;
|
||||
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:
|
||||
return b - Byte_ascii.Ltr_A + 10;
|
||||
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:
|
||||
return b - Byte_ascii.Ltr_a + 10;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] To_bry(int v) {return Bry_.new_a7(To_str(v));}
|
||||
public static String To_str(int v) {return new Integer(v).toString();}
|
||||
public static String To_str_fmt(int v, String fmt) {return new java.text.DecimalFormat(fmt).format(v);}
|
||||
public static String To_str_pad_bgn_space(int val, int reqd_len) {return To_str_pad(val, reqd_len, Bool_.Y, Byte_ascii.Space);} // EX: 1, 3 returns " 1"
|
||||
public static String To_str_pad_bgn_zero (int val, int reqd_len) {return To_str_pad(val, reqd_len, Bool_.Y, Byte_ascii.Num_0);} // EX: 1, 3 returns "001"
|
||||
private static String To_str_pad(int val, int reqd_len, boolean bgn, byte pad_chr) {
|
||||
// get val_len and pad_len; exit early, if no padding needed
|
||||
int val_len = DigitCount(val);
|
||||
int pad_len = reqd_len - val_len;
|
||||
if (pad_len < 0)
|
||||
return Int_.To_str(val);
|
||||
|
||||
// padding needed
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
|
||||
// handle negative numbers; EX: -1 -> "-001", not "00-1"
|
||||
if (val < 0) {
|
||||
bfr.Add_byte(Byte_ascii.Dash);
|
||||
val *= -1;
|
||||
--val_len;
|
||||
}
|
||||
|
||||
// build outpt
|
||||
if (!bgn)
|
||||
bfr.Add_int_fixed(val, val_len);
|
||||
bfr.Add_byte_repeat(pad_chr, pad_len);
|
||||
if (bgn)
|
||||
bfr.Add_int_fixed(val, val_len);
|
||||
|
||||
return bfr.To_str();
|
||||
}
|
||||
public static String To_str_hex(int v) {return To_str_hex(Bool_.Y, Bool_.Y, v);}
|
||||
public static String To_str_hex(boolean zero_pad, boolean upper, int v) {
|
||||
String rv = Integer.toHexString(v);
|
||||
int rv_len = String_.Len(rv);
|
||||
if (zero_pad && rv_len < 8) rv = String_.Repeat("0", 8 - rv_len) + rv;
|
||||
return upper ? String_.Upper(rv) : rv;
|
||||
}
|
||||
|
||||
public static int Compare(int lhs, int rhs) {
|
||||
if (lhs == rhs) return CompareAble_.Same;
|
||||
else if (lhs < rhs) return CompareAble_.Less;
|
||||
else return CompareAble_.More;
|
||||
}
|
||||
public static boolean In(int v, int comp0, int comp1) {return v == comp0 || v == comp1;}
|
||||
public static boolean In(int v, int... ary) {
|
||||
for (int itm : ary)
|
||||
if (v == itm) return true;
|
||||
return false;
|
||||
}
|
||||
public static boolean Between(int v, int lhs, int rhs) {
|
||||
int lhsCompare = v == lhs ? 0 : (v < lhs ? -1 : 1);
|
||||
int rhsCompare = v == rhs ? 0 : (v < rhs ? -1 : 1);
|
||||
return (lhsCompare * rhsCompare) != 1; // 1 when v is (a) greater than both or (b) less than both
|
||||
}
|
||||
public static boolean RangeCheck(int v, int max) {return v >= 0 && v < max;}
|
||||
public static void RangeCheckOrFail_list(int v, int max, String s) {if (v < 0 || v >= max) throw Err_.new_wo_type("bounds check failed", "msg", s, "v", v, "min", 0, "max", max - 1);}
|
||||
public static boolean Bounds_chk(int bgn, int end, int len) {return bgn > -1 && end < len;}
|
||||
public static int BoundEnd(int v, int end) {return v >= end ? end - 1 : v;}
|
||||
public static int EnsureLessThan(int v, int max) {return v >= max ? max : v;}
|
||||
|
||||
public static int Min(int lhs, int rhs) {return lhs < rhs ? lhs : rhs;}
|
||||
public static int Max(int lhs, int rhs) {return lhs > rhs ? lhs : rhs;}
|
||||
public static int Min_many(int... ary) {
|
||||
int len = ary.length; if (len == 0) throw Err_.new_wo_type("Min_many requires at least 1 value");
|
||||
boolean init = true;
|
||||
@@ -101,16 +178,8 @@ public class Int_ {
|
||||
}
|
||||
return min;
|
||||
}
|
||||
public static int Max(int lhs, int rhs) {return lhs > rhs ? lhs : rhs;}
|
||||
public static int ModIfNeg1(int v, int or) {return v == -1 ? or : v;}
|
||||
public static boolean RangeCheck(int v, int max) {return v >= 0 && v < max;}
|
||||
public static void RangeCheckOrFail_list(int v, int max, String s) {if (v < 0 || v >= max) throw Err_.new_wo_type("bounds check failed", "msg", s, "v", v, "min", 0, "max", max - 1);}
|
||||
public static void RangeCheckOrFail(int v, int min, int max, String s) {if (v < min || v >= max) throw Err_.new_wo_type("bounds check failed", "msg", s, "v", v, "min", min, "max", max);}
|
||||
public static boolean Between(int v, int lhs, int rhs) {
|
||||
int lhsCompare = v == lhs ? 0 : (v < lhs ? -1 : 1);
|
||||
int rhsCompare = v == rhs ? 0 : (v < rhs ? -1 : 1);
|
||||
return (lhsCompare * rhsCompare) != 1; // 1 when v is (a) greater than both or (b) less than both
|
||||
}
|
||||
|
||||
public static int Subtract_long(long lhs, long rhs) {return (int)(lhs - rhs);}
|
||||
public static int Div(int v, float divisor) {return (int)((float)v / divisor);}
|
||||
public static int DivAndRoundUp(int v, int divisor) {
|
||||
int whole = v / divisor;
|
||||
@@ -121,15 +190,9 @@ public class Int_ {
|
||||
float product = ((float)v * multiplier); // WORKAROUND (DotNet): (int)((float)v * multiplier) returns 0 for 100 and .01f
|
||||
return (int)product;
|
||||
}
|
||||
public static int Compare(int lhs, int rhs) {
|
||||
if (lhs == rhs) return CompareAble_.Same;
|
||||
else if (lhs < rhs) return CompareAble_.Less;
|
||||
else return CompareAble_.More;
|
||||
}
|
||||
public static int DigitCount(int v) {
|
||||
int log10 = Log10(v);
|
||||
return v > -1 ? log10 + 1 : log10 * -1 + 2;
|
||||
}
|
||||
|
||||
public static int[] Log10Ary = new int[] {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, Int_.Max_value};
|
||||
public static int Log10AryLen = 11;
|
||||
public static int Log10(int v) {
|
||||
if (v == 0) return 0;
|
||||
int sign = 1;
|
||||
@@ -148,124 +211,10 @@ public class Int_ {
|
||||
if (v < Log10Ary[i]) {rv = i - 1; break;}
|
||||
}
|
||||
return rv * sign;
|
||||
} public static int[] Log10Ary = new int[] {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, Int_.Max_value}; public static int Log10AryLen = 11;
|
||||
public static String To_str_pad_bgn_space(int v, int reqdPlaces) {return To_str_pad_bgn_zero(v, reqdPlaces, Byte_ascii.Space, true);} // EX: 1, 3 returns " 1"
|
||||
public static String To_str_pad_bgn_zero(int v, int reqdPlaces) {return To_str_pad_bgn_zero(v, reqdPlaces, Byte_ascii.Num_0, true);} // EX: 1, 3 returns "001"
|
||||
static String To_str_pad_bgn_zero(int val, int places, byte pad_chr, boolean bgn) {
|
||||
int len = DigitCount(val);
|
||||
int pad_len = places - len; if (pad_len < 0) return Int_.To_str(val);
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
boolean neg = val < 0;
|
||||
if (bgn) { // special logic to handle negative numbers; EX: -1 -> "-001", not "00-1"
|
||||
if (neg) {
|
||||
bfr.Add_byte(Byte_ascii.Dash);
|
||||
val *= -1;
|
||||
--len;
|
||||
}
|
||||
}
|
||||
else
|
||||
bfr.Add_int_fixed(val, len);
|
||||
bfr.Add_byte_repeat(pad_chr, pad_len);
|
||||
if (bgn) bfr.Add_int_fixed(val, len); // NOTE: neg handled above
|
||||
return bfr.To_str();
|
||||
}
|
||||
public static int read_(Object o) {String s = String_.as_(o); return s != null ? Int_.parse(s) : Int_.cast(o);}
|
||||
public static int parse(String raw) {try {return Integer.parseInt(raw);} catch(Exception e) {throw Err_.new_parse_exc(e, int.class, raw);}}
|
||||
public static int cast(Object obj) {try {return (Integer)obj;} catch(Exception exc) {throw Err_.new_type_mismatch_w_exc(exc, int.class, obj);}}
|
||||
public static int cast_or(Object obj, int or) {try {return (Integer)obj;} catch(Exception e) {Err_.Noop(e); return or;}}
|
||||
public static int Xby_double_(double v) {return (int)v;}
|
||||
public static String To_str(int v) {return new Integer(v).toString();}
|
||||
public static String To_str_fmt(int v, String fmt) {return new java.text.DecimalFormat(fmt).format(v);}
|
||||
public static boolean TypeMatch(Class<?> type) {return type == int.class || type == Integer.class;}
|
||||
public static int To_int_hex(byte[] src) {return To_int_hex(src, 0, src.length);}
|
||||
public static int To_int_hex(byte[] src, int bgn, int end) {
|
||||
int rv = 0; int factor = 1;
|
||||
for (int i = end - 1; i >= bgn; i--) {
|
||||
int val = To_int_hex(src[i]);
|
||||
rv += (val * factor);
|
||||
factor *= 16;
|
||||
}
|
||||
return rv;
|
||||
|
||||
public static int DigitCount(int v) {
|
||||
int log10 = Log10(v);
|
||||
return v > -1 ? log10 + 1 : log10 * -1 + 2;
|
||||
}
|
||||
public static int Subtract_long(long lhs, long rhs) {return (int)(lhs - rhs);}
|
||||
public static int To_int_hex(byte b) {
|
||||
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:
|
||||
return b - Byte_ascii.Num_0;
|
||||
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:
|
||||
return b - Byte_ascii.Ltr_A + 10;
|
||||
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:
|
||||
return b - Byte_ascii.Ltr_a + 10;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
public static String To_str_hex(int v) {return To_str_hex(Bool_.Y, Bool_.Y, v);}
|
||||
public static String To_str_hex(boolean zero_pad, boolean upper, int v) {
|
||||
String rv = Integer.toHexString(v);
|
||||
int rv_len = String_.Len(rv);
|
||||
if (zero_pad && rv_len < 8) rv = String_.Repeat("0", 8 - rv_len) + rv;
|
||||
return upper ? String_.Upper(rv) : rv;
|
||||
}
|
||||
public static String To_str(int[] ary) {return To_str(ary, " ");}
|
||||
public static String To_str(int[] ary, String dlm) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < ary.length; i++)
|
||||
sb.Add_spr_unless_first(Int_.To_str(ary[i]), dlm, i);
|
||||
return sb.To_str();
|
||||
}
|
||||
public static int[] Ary_parse(String raw_str, int reqd_len, int[] or) {
|
||||
byte[] raw_bry = Bry_.new_a7(raw_str);
|
||||
int raw_bry_len = raw_bry.length;
|
||||
int[] rv = new int[reqd_len];
|
||||
int cur_val = 0, cur_mult = 1, cur_idx = reqd_len - 1; boolean signed = false;
|
||||
for (int i = raw_bry_len - 1; i > -2; i--) {
|
||||
byte b = i == -1 ? Byte_ascii.Comma : raw_bry[i];
|
||||
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:
|
||||
if (signed) return or;
|
||||
cur_val += (b - Byte_ascii.Num_0) * cur_mult;
|
||||
cur_mult *= 10;
|
||||
break;
|
||||
case Byte_ascii.Space: case Byte_ascii.Nl: case Byte_ascii.Cr: case Byte_ascii.Tab:
|
||||
break;
|
||||
case Byte_ascii.Comma:
|
||||
if (cur_idx < 0) return or;
|
||||
rv[cur_idx--] = cur_val;
|
||||
cur_val = 0; cur_mult = 1;
|
||||
signed = false;
|
||||
break;
|
||||
case Byte_ascii.Dash:
|
||||
if (signed) return or;
|
||||
cur_val *= -1;
|
||||
signed = true;
|
||||
break;
|
||||
case Byte_ascii.Plus: // noop; all values positive by default
|
||||
if (signed) return or;
|
||||
signed = true;
|
||||
break;
|
||||
default:
|
||||
return or;
|
||||
}
|
||||
}
|
||||
return cur_idx == -1 ? rv : or; // cur_idx == -1 checks for unfilled; EX: Ary_parse("1,2", 3, null) is unfilled
|
||||
}
|
||||
public static int[] Ary_parse(String raw_str, String spr) {
|
||||
String[] ary = String_.Split(raw_str, spr);
|
||||
int len = ary.length;
|
||||
int[] rv = new int[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
rv[i] = Int_.parse(ary[i]);
|
||||
return rv;
|
||||
}
|
||||
public static byte[] To_bry(int v) {return Bry_.new_a7(To_str(v));}
|
||||
public static final int
|
||||
Min_value = Integer.MIN_VALUE
|
||||
, Max_value = Integer.MAX_VALUE
|
||||
, Max_value__31 = 2147483647
|
||||
, Neg1 = -1
|
||||
, Neg1_count = -1
|
||||
;
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ package gplx;
|
||||
import org.junit.*;
|
||||
public class Int__tst {
|
||||
@Test public void XtoStr_PadBgn() {
|
||||
// tst_XtoStr_PadLeft_Zeroes(1 , 3, "001"); // pad
|
||||
// tst_XtoStr_PadLeft_Zeroes(123 , 3, "123"); // no pad
|
||||
// tst_XtoStr_PadLeft_Zeroes(1234 , 3, "1234"); // val exceeds pad; confirm noop
|
||||
tst_XtoStr_PadLeft_Zeroes(1 , 3, "001"); // pad
|
||||
tst_XtoStr_PadLeft_Zeroes(123 , 3, "123"); // no pad
|
||||
tst_XtoStr_PadLeft_Zeroes(1234 , 3, "1234"); // val exceeds pad; confirm noop
|
||||
tst_XtoStr_PadLeft_Zeroes(-1 , 3, "-01"); // negative
|
||||
tst_XtoStr_PadLeft_Zeroes(-12 , 3, "-12"); // negative
|
||||
tst_XtoStr_PadLeft_Zeroes(-123 , 3, "-123"); // negative
|
||||
@@ -29,7 +29,7 @@ public class Int__tst {
|
||||
tst_ParseOr("", -1); // empty
|
||||
tst_ParseOr("123", 123); // single
|
||||
tst_ParseOr("1a", -1); // fail
|
||||
} void tst_ParseOr(String raw, int expd) {Tfds.Eq(expd, Int_.parse_or(raw, -1));}
|
||||
} void tst_ParseOr(String raw, int expd) {Tfds.Eq(expd, Int_.Parse_or(raw, -1));}
|
||||
@Test public void Between() {
|
||||
tst_Between(1, 0, 2, true); // simple true
|
||||
tst_Between(3, 0, 2, false); // simple false
|
||||
@@ -40,9 +40,6 @@ public class Int__tst {
|
||||
tst_XtoStr_fmt(1, "1");
|
||||
tst_XtoStr_fmt(1000, "1,000");
|
||||
} void tst_XtoStr_fmt(int v, String expd) {Tfds.Eq(expd, Int_.To_str_fmt(v, "#,###"));}
|
||||
@Test public void AryRng() {
|
||||
tst_AryRng(1, 3, Int_.Ary(1, 2, 3));
|
||||
} void tst_AryRng(int bgn, int end, int[] expd) {Tfds.Eq_ary(expd, Int_.AryRng(bgn, end));}
|
||||
@Test public void Log10_pos() {
|
||||
tst_Log10(0, 0);
|
||||
tst_Log10(1, 0);
|
||||
@@ -96,18 +93,5 @@ public class Int__tst {
|
||||
}
|
||||
@Test public void Xto_int_hex_tst() {
|
||||
Xto_int_hex("007C", 124);
|
||||
} void Xto_int_hex(String raw, int expd) {Tfds.Eq(expd, Int_.To_int_hex(Bry_.new_a7(raw)));}
|
||||
@Test public void Ary_parse() {
|
||||
Ary_parse__tst("1,2,3" , 3, Int_.Ary_empty, 1, 2, 3);
|
||||
Ary_parse__tst("123,321,213" , 3, Int_.Ary_empty, 123, 321, 213);
|
||||
Ary_parse__tst(" 1, 2,3" , 3, Int_.Ary_empty, 1, 2, 3);
|
||||
Ary_parse__tst("-1,+2,-3" , 3, Int_.Ary_empty, -1, 2, -3);
|
||||
Ary_parse__tst(Int_.To_str(Int_.Min_value) , 1, Int_.Ary_empty, Int_.Min_value);
|
||||
Ary_parse__tst(Int_.To_str(Int_.Max_value) , 1, Int_.Ary_empty, Int_.Max_value);
|
||||
Ary_parse__tst("1,2" , 1, Int_.Ary_empty);
|
||||
Ary_parse__tst("1" , 2, Int_.Ary_empty);
|
||||
Ary_parse__tst("a" , 1, Int_.Ary_empty);
|
||||
Ary_parse__tst("1-2," , 1, Int_.Ary_empty);
|
||||
}
|
||||
void Ary_parse__tst(String raw, int reqd_len, int[] or, int... expd) {Tfds.Eq_ary(expd, Int_.Ary_parse(raw, reqd_len, or));}
|
||||
} void Xto_int_hex(String raw, int expd) {Tfds.Eq(expd, Int_.By_hex_bry(Bry_.new_a7(raw)));}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,79 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.core.primitives; import gplx.*; import gplx.core.*;
|
||||
public class Int_ary_ {
|
||||
public static int[] Parse_list_or(byte[] src, int[] or) {
|
||||
package gplx;
|
||||
import gplx.core.strings.*;
|
||||
public class Int_ary_ {//RF:DATE:2017-10-09
|
||||
public static int[] Empty = new int[0];
|
||||
|
||||
public static int[] New(int... v) {return v;}
|
||||
|
||||
public static void Copy_to(int[] src, int src_len, int[] trg) {
|
||||
for (int i = 0; i < src_len; ++i)
|
||||
trg[i] = src[i];
|
||||
}
|
||||
|
||||
public static String To_str(String spr, int... ary) {
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (i != 0) bfr.Add_str_u8(spr);
|
||||
int itm = ary[i];
|
||||
bfr.Add_int_variable(itm);
|
||||
}
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
|
||||
public static int[] Parse(String raw, String spr) {
|
||||
String[] ary = String_.Split(raw, spr);
|
||||
int len = ary.length;
|
||||
int[] rv = new int[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
rv[i] = Int_.Parse(ary[i]);
|
||||
return rv;
|
||||
}
|
||||
|
||||
// parses to a reqd len; EX: "1" -> "[1, 0]"
|
||||
public static int[] Parse(String raw_str, int reqd_len, int[] or) {
|
||||
byte[] raw_bry = Bry_.new_a7(raw_str);
|
||||
int raw_bry_len = raw_bry.length;
|
||||
int[] rv = new int[reqd_len];
|
||||
int cur_val = 0, cur_mult = 1, cur_idx = reqd_len - 1; boolean signed = false;
|
||||
for (int i = raw_bry_len - 1; i > -2; i--) {
|
||||
byte b = i == -1 ? Byte_ascii.Comma : raw_bry[i];
|
||||
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:
|
||||
if (signed) return or;
|
||||
cur_val += (b - Byte_ascii.Num_0) * cur_mult;
|
||||
cur_mult *= 10;
|
||||
break;
|
||||
case Byte_ascii.Space: case Byte_ascii.Nl: case Byte_ascii.Cr: case Byte_ascii.Tab:
|
||||
break;
|
||||
case Byte_ascii.Comma:
|
||||
if (cur_idx < 0) return or;
|
||||
rv[cur_idx--] = cur_val;
|
||||
cur_val = 0; cur_mult = 1;
|
||||
signed = false;
|
||||
break;
|
||||
case Byte_ascii.Dash:
|
||||
if (signed) return or;
|
||||
cur_val *= -1;
|
||||
signed = true;
|
||||
break;
|
||||
case Byte_ascii.Plus: // noop; all values positive by default
|
||||
if (signed) return or;
|
||||
signed = true;
|
||||
break;
|
||||
default:
|
||||
return or;
|
||||
}
|
||||
}
|
||||
return cur_idx == -1 ? rv : or; // cur_idx == -1 checks for unfilled; EX: Ary_parse("1,2", 3, null) is unfilled
|
||||
}
|
||||
|
||||
// optimizes parse
|
||||
public static int[] Parse_or(byte[] src, int[] or) {
|
||||
try {
|
||||
if (Bry_.Len_eq_0(src)) return or; // null, "" should return [0]
|
||||
int raw_len = src.length;
|
||||
58
100_core/src/gplx/Int_ary__tst.java
Normal file
58
100_core/src/gplx/Int_ary__tst.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Int_ary__tst {
|
||||
private Int_ary__fxt fxt = new Int_ary__fxt();
|
||||
|
||||
@Test public void Parse() {
|
||||
fxt.Test__Parse("1,2,3" , 3, Int_ary_.Empty, 1, 2, 3);
|
||||
fxt.Test__Parse("123,321,213" , 3, Int_ary_.Empty, 123, 321, 213);
|
||||
fxt.Test__Parse(" 1, 2,3" , 3, Int_ary_.Empty, 1, 2, 3);
|
||||
fxt.Test__Parse("-1,+2,-3" , 3, Int_ary_.Empty, -1, 2, -3);
|
||||
fxt.Test__Parse(Int_.To_str(Int_.Min_value) , 1, Int_ary_.Empty, Int_.Min_value);
|
||||
fxt.Test__Parse(Int_.To_str(Int_.Max_value) , 1, Int_ary_.Empty, Int_.Max_value);
|
||||
fxt.Test__Parse("1,2" , 1, Int_ary_.Empty);
|
||||
fxt.Test__Parse("1" , 2, Int_ary_.Empty);
|
||||
fxt.Test__Parse("a" , 1, Int_ary_.Empty);
|
||||
fxt.Test__Parse("1-2," , 1, Int_ary_.Empty);
|
||||
}
|
||||
|
||||
@Test public void Parse_list_or_() {
|
||||
fxt.Test__Parse_or("1", 1);
|
||||
fxt.Test__Parse_or("123", 123);
|
||||
fxt.Test__Parse_or("1,2,123", 1, 2, 123);
|
||||
fxt.Test__Parse_or("1,2,12,123", 1, 2, 12, 123);
|
||||
fxt.Test__Parse_or("1-5", 1, 2, 3, 4, 5);
|
||||
fxt.Test__Parse_or("1-1", 1);
|
||||
fxt.Test__Parse_or("1-3,7,11-13,21", 1, 2, 3, 7, 11, 12, 13, 21);
|
||||
|
||||
fxt.Test__Parse_or__empty("1 2"); // NOTE: MW would gen 12; treat as invalid
|
||||
fxt.Test__Parse_or__empty("1,"); // eos
|
||||
fxt.Test__Parse_or__empty("1,,2"); // empty comma
|
||||
fxt.Test__Parse_or__empty("1-"); // eos
|
||||
fxt.Test__Parse_or__empty("3-1"); // bgn > end
|
||||
fxt.Test__Parse_or__empty("1,a,2");
|
||||
fxt.Test__Parse_or__empty("a-1,2");
|
||||
fxt.Test__Parse_or__empty("-1"); // no rng bgn
|
||||
}
|
||||
}
|
||||
class Int_ary__fxt {
|
||||
public void Test__Parse_or__empty(String raw) {Tfds.Eq_ary(Int_ary_.Empty, Int_ary_.Parse_or(Bry_.new_a7(raw), Int_ary_.Empty));}
|
||||
public void Test__Parse_or(String raw, int... expd) {Tfds.Eq_ary(expd, Int_ary_.Parse_or(Bry_.new_a7(raw), Int_ary_.Empty));}
|
||||
|
||||
public void Test__Parse(String raw, int reqd_len, int[] or, int... expd) {Gftest.Eq__ary(expd, Int_ary_.Parse(raw, reqd_len, or), "failed to parse: {0}", raw);}
|
||||
}
|
||||
@@ -159,7 +159,7 @@ public class List_adp_tst {
|
||||
list.Shuffle();
|
||||
int hasMovedCount = 0;
|
||||
for (int i = 0; i < list.Count(); i++) {
|
||||
int val = Int_.cast(list.Get_at(i));
|
||||
int val = Int_.Cast(list.Get_at(i));
|
||||
if (val != i) hasMovedCount++;
|
||||
}
|
||||
Tfds.Eq_true(hasMovedCount > 0, "all documents have the same index"); // NOTE: may still fail occasionally (1%)
|
||||
|
||||
@@ -17,6 +17,7 @@ package gplx;
|
||||
public class Short_ {
|
||||
public static final String Cls_val_name = "short";
|
||||
public static final Class<?> Cls_ref_type = Short.class;
|
||||
|
||||
public static short cast(Object obj) {try {return (Short)obj;} catch(Exception exc) {throw Err_.new_type_mismatch_w_exc(exc, short.class, obj);}}
|
||||
public static short By_int(int v) {return (short)v;}
|
||||
}
|
||||
|
||||
@@ -396,7 +396,7 @@ public class String_ {
|
||||
numberStr = "";
|
||||
}
|
||||
else if (c == bracketEnd) {
|
||||
int aryIdx = Int_.parse_or(numberStr, Int_.Min_value);
|
||||
int aryIdx = Int_.Parse_or(numberStr, Int_.Min_value);
|
||||
if (aryIdx != Int_.Min_value && Int_.Between(aryIdx, 0, aryLength - 1)) // check (a) aryIdx is num; (b) aryIdx is in bounds
|
||||
aryVal = Object_.Xto_str_strict_or_empty(ary[aryIdx]);
|
||||
else
|
||||
|
||||
@@ -14,6 +14,6 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx;
|
||||
public interface To_str_able {
|
||||
public interface To_str_able {//RF:2017-10-08
|
||||
String To_str();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,6 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx;
|
||||
class To_str_able_ {
|
||||
class To_str_able_ {//RF:2017-10-08
|
||||
public static To_str_able as_(Object obj) {return obj instanceof To_str_able ? (To_str_able)obj : null;}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx;
|
||||
public class Type_ {
|
||||
public class Type_ {//RF:2017-10-08
|
||||
public static Class<?> Type_by_obj(Object o) {return o.getClass();}
|
||||
public static Class<?> Type_by_primitive(Object o) {
|
||||
Class<?> rv = o.getClass();
|
||||
|
||||
@@ -14,7 +14,7 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx;
|
||||
public class Type_ids_ {
|
||||
public class Type_ids_ {//RF:2017-10-08
|
||||
public static final int // SERIALIZABLE.N
|
||||
Id__obj = 0
|
||||
, Id__null = 1
|
||||
|
||||
@@ -22,7 +22,7 @@ public class Bry_bfr_mkr_mgr {
|
||||
public Bry_bfr_mkr_mgr(byte mgr_id, int reset) {// NOTE: random IndexOutOfBounds errors in Get around free[--free_len] with free_len being -1; put member variable initialization within thread_lock to try to avoid; DATE:2014-09-21
|
||||
this.mgr_id = mgr_id;
|
||||
this.reset = reset;
|
||||
this.free = Int_.Ary_empty;
|
||||
this.free = Int_ary_.Empty;
|
||||
this.free_len = 0;
|
||||
}
|
||||
public Bry_bfr Get() {
|
||||
@@ -66,7 +66,7 @@ public class Bry_bfr_mkr_mgr {
|
||||
used[i] = null;
|
||||
}
|
||||
used = Bry_bfr_.Ary_empty;
|
||||
free = Int_.Ary_empty;
|
||||
free = Int_ary_.Empty;
|
||||
free_len = used_len = used_max = 0;
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ public class Bry_bfr_mkr_mgr {
|
||||
used[i] = null;
|
||||
}
|
||||
used = Bry_bfr_.Ary_empty;
|
||||
free = Int_.Ary_empty;
|
||||
free = Int_ary_.Empty;
|
||||
free_len = 0;
|
||||
used_len = used_max = 0;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ public class Bfr_arg__int implements Bfr_arg {
|
||||
private int val, val_digits;
|
||||
public Bfr_arg__int(int v) {Set(v);}
|
||||
public Bfr_arg__int Set(int v) {
|
||||
this.val = Int_.cast(v);
|
||||
this.val = Int_.Cast(v);
|
||||
this.val_digits = Int_.DigitCount(val);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -214,12 +214,12 @@ public class Bry_fmtr {
|
||||
}
|
||||
}
|
||||
int Compile_eval_cmd(byte[] fmt, int fmt_len, int eval_lhs_bgn, List_adp list) {
|
||||
int eval_lhs_end = Bry_find_.Find_fwd(fmt, char_eval_end, eval_lhs_bgn + Int_.Const_dlm_len, fmt_len); if (eval_lhs_end == Bry_find_.Not_found) throw Err_.new_wo_type("eval_lhs_end_invalid: could not find eval_lhs_end", "snip", String_.new_u8(fmt, eval_lhs_bgn, fmt_len));
|
||||
byte[] eval_dlm = Bry_.Mid(fmt, eval_lhs_bgn , eval_lhs_end + Int_.Const_dlm_len);
|
||||
int eval_rhs_bgn = Bry_find_.Find_fwd(fmt, eval_dlm , eval_lhs_end + Int_.Const_dlm_len, fmt_len); if (eval_rhs_bgn == Bry_find_.Not_found) throw Err_.new_wo_type("eval_rhs_bgn_invalid: could not find eval_rhs_bgn", "snip", String_.new_u8(fmt, eval_lhs_end, fmt_len));
|
||||
byte[] eval_cmd = Bry_.Mid(fmt, eval_lhs_end + Int_.Const_dlm_len, eval_rhs_bgn);
|
||||
int eval_lhs_end = Bry_find_.Find_fwd(fmt, char_eval_end, eval_lhs_bgn + Byte_ascii.Len_1, fmt_len); if (eval_lhs_end == Bry_find_.Not_found) throw Err_.new_wo_type("eval_lhs_end_invalid: could not find eval_lhs_end", "snip", String_.new_u8(fmt, eval_lhs_bgn, fmt_len));
|
||||
byte[] eval_dlm = Bry_.Mid(fmt, eval_lhs_bgn , eval_lhs_end + Byte_ascii.Len_1);
|
||||
int eval_rhs_bgn = Bry_find_.Find_fwd(fmt, eval_dlm , eval_lhs_end + Byte_ascii.Len_1, fmt_len); if (eval_rhs_bgn == Bry_find_.Not_found) throw Err_.new_wo_type("eval_rhs_bgn_invalid: could not find eval_rhs_bgn", "snip", String_.new_u8(fmt, eval_lhs_end, fmt_len));
|
||||
byte[] eval_cmd = Bry_.Mid(fmt, eval_lhs_end + Byte_ascii.Len_1, eval_rhs_bgn);
|
||||
byte[] eval_rslt = eval_mgr.Eval(eval_cmd);
|
||||
int eval_rhs_end = eval_rhs_bgn + Int_.Const_dlm_len + eval_dlm.length;
|
||||
int eval_rhs_end = eval_rhs_bgn + Byte_ascii.Len_1 + eval_dlm.length;
|
||||
if (eval_rslt == null) eval_rslt = Bry_.Mid(fmt, eval_lhs_bgn - 2, eval_rhs_end); // not found; return original argument
|
||||
list.Add(Bry_fmtr_itm.dat_bry_(eval_rslt));
|
||||
return eval_rhs_end;
|
||||
|
||||
@@ -30,7 +30,7 @@ class GfoFldList_base implements GfoFldList {
|
||||
public boolean Has(String key) {return hash.Has(key);}
|
||||
public int Idx_of(String key) {
|
||||
Object rv = idxs.Get_by(key);
|
||||
return rv == null ? List_adp_.Not_found : Int_.cast(rv);
|
||||
return rv == null ? List_adp_.Not_found : Int_.Cast(rv);
|
||||
}
|
||||
public GfoFld Get_at(int i) {return (GfoFld)hash.Get_at(i);}
|
||||
public GfoFld FetchOrNull(String key) {return (GfoFld)hash.Get_by(key);}
|
||||
|
||||
@@ -16,7 +16,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
package gplx.core.ios; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.type_xtns.*;
|
||||
public class IoItmClassXtn extends ClassXtn_base implements ClassXtn {
|
||||
public String Key() {return Key_const;} public static final String Key_const = "ioItemType";
|
||||
public String Key() {return Key_const;} public static final String Key_const = "ioItemType";
|
||||
@Override public Class<?> UnderClass() {return int.class;}
|
||||
public Object DefaultValue() {return IoItmDir.Type_Dir;}
|
||||
public boolean Eq(Object lhs, Object rhs) {return ((IoItm_base)lhs).compareTo(rhs) == CompareAble_.Same;}
|
||||
@@ -26,6 +26,6 @@ public class IoItmClassXtn extends ClassXtn_base implements ClassXtn {
|
||||
else if (String_.Eq(rawLower, "fil")) return IoItmFil.Type_Fil;
|
||||
else throw Err_.new_unhandled(raw);
|
||||
}
|
||||
@Override public Object XtoDb(Object obj) {return Int_.cast(obj);}
|
||||
public static final IoItmClassXtn Instance = new IoItmClassXtn(); IoItmClassXtn() {}
|
||||
@Override public Object XtoDb(Object obj) {return Int_.Cast(obj);}
|
||||
public static final IoItmClassXtn Instance = new IoItmClassXtn(); IoItmClassXtn() {}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,9 @@ public class EnmMgr {
|
||||
if (prefix != null) term = String_.Replace(term, prefix, "");
|
||||
int cur = -1;
|
||||
if (String_.Has_at_bgn(term, "#"))
|
||||
cur = Int_.parse(String_.Mid(term, 1));
|
||||
cur = Int_.Parse(String_.Mid(term, 1));
|
||||
else
|
||||
cur = Int_.cast(rawRegy.Get_by(term));
|
||||
cur = Int_.Cast(rawRegy.Get_by(term));
|
||||
rv |= cur;
|
||||
}
|
||||
return rv;
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.core.primitives; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*;
|
||||
public class Int_ary__tst {
|
||||
private Int_ary__fxt fxt = new Int_ary__fxt();
|
||||
@Test public void Parse_list_or_() {
|
||||
fxt.Test_Parse_list_or("1", 1);
|
||||
fxt.Test_Parse_list_or("123", 123);
|
||||
fxt.Test_Parse_list_or("1,2,123", 1, 2, 123);
|
||||
fxt.Test_Parse_list_or("1,2,12,123", 1, 2, 12, 123);
|
||||
fxt.Test_Parse_list_or("1-5", 1, 2, 3, 4, 5);
|
||||
fxt.Test_Parse_list_or("1-1", 1);
|
||||
fxt.Test_Parse_list_or("1-3,7,11-13,21", 1, 2, 3, 7, 11, 12, 13, 21);
|
||||
|
||||
fxt.Test_Parse_list_empty("1 2"); // NOTE: MW would gen 12; treat as invalid
|
||||
fxt.Test_Parse_list_empty("1,"); // eos
|
||||
fxt.Test_Parse_list_empty("1,,2"); // empty comma
|
||||
fxt.Test_Parse_list_empty("1-"); // eos
|
||||
fxt.Test_Parse_list_empty("3-1"); // bgn > end
|
||||
fxt.Test_Parse_list_empty("1,a,2");
|
||||
fxt.Test_Parse_list_empty("a-1,2");
|
||||
fxt.Test_Parse_list_empty("-1"); // no rng bgn
|
||||
}
|
||||
}
|
||||
class Int_ary__fxt {
|
||||
public void Test_Parse_list_empty(String raw) {Tfds.Eq_ary(Int_.Ary_empty, Int_ary_.Parse_list_or(Bry_.new_a7(raw), Int_.Ary_empty));}
|
||||
public void Test_Parse_list_or(String raw, int... expd) {Tfds.Eq_ary(expd, Int_ary_.Parse_list_or(Bry_.new_a7(raw), Int_.Ary_empty));}
|
||||
}
|
||||
@@ -16,15 +16,15 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
package gplx.core.primitives; import gplx.*; import gplx.core.*;
|
||||
public class Int_list {
|
||||
private int capacity = 0;
|
||||
private int[] ary = Int_.Ary_empty; private int ary_len, ary_max;
|
||||
public Int_list() {this.capacity = 0; this.ary = Int_.Ary_empty;}
|
||||
private int[] ary = Int_ary_.Empty; private int ary_len, ary_max;
|
||||
public Int_list() {this.capacity = 0; this.ary = Int_ary_.Empty;}
|
||||
public Int_list(int capacity) {this.capacity = capacity; this.ary = new int[capacity];}
|
||||
public void Add(int uid) {
|
||||
int new_len = ary_len + 1;
|
||||
if (new_len > ary_max) {
|
||||
ary_max += 16;
|
||||
int[] new_ary = new int[ary_max];
|
||||
Int_.Ary_copy_to(ary, ary_len, new_ary);
|
||||
Int_ary_.Copy_to(ary, ary_len, new_ary);
|
||||
ary = new_ary;
|
||||
}
|
||||
ary[ary_len] = uid;
|
||||
@@ -34,7 +34,7 @@ public class Int_list {
|
||||
public int Get_at(int i) {return ary[i];}
|
||||
public void Clear() {
|
||||
if (ary_len > capacity) {
|
||||
ary = (capacity == 0) ? Int_.Ary_empty : new int[capacity];
|
||||
ary = (capacity == 0) ? Int_ary_.Empty : new int[capacity];
|
||||
}
|
||||
ary_len = ary_max = 0;
|
||||
}
|
||||
|
||||
@@ -62,12 +62,12 @@ public abstract class DataRdr_base implements SrlMgr {
|
||||
|
||||
public int ReadInt(String key) {
|
||||
Object val = Read(key);
|
||||
try {return (parse) ? Int_.parse(String_.as_(val)) : Int_.cast(val);}
|
||||
try {return (parse) ? Int_.Parse(String_.as_(val)) : Int_.Cast(val);}
|
||||
catch (Exception exc) {throw Err_dataRdr_ReadFailed_err(int.class, key, val, exc);}
|
||||
}
|
||||
public int ReadIntOr(String key, int or) {
|
||||
Object val = Read(key); if (val == null) return or;
|
||||
try {return (parse) ? Int_.parse(String_.as_(val)) : Int_.cast(val);}
|
||||
try {return (parse) ? Int_.Parse(String_.as_(val)) : Int_.Cast(val);}
|
||||
catch (Exception exc) {Err_dataRdr_ReadFailed_useOr(exc, int.class, key, val, or); return or;}
|
||||
}
|
||||
public long ReadLongOr(String key, long or) {
|
||||
@@ -112,12 +112,12 @@ public abstract class DataRdr_base implements SrlMgr {
|
||||
}
|
||||
@gplx.Virtual public byte ReadByte(String key) {
|
||||
Object val = Read(key);
|
||||
try {return (parse) ? Byte_.parse(String_.as_(val)) : Byte_.cast(val);}
|
||||
try {return (parse) ? Byte_.Parse(String_.as_(val)) : Byte_.Cast(val);}
|
||||
catch (Exception exc) {throw Err_dataRdr_ReadFailed_err(byte.class, key, val, exc);}
|
||||
}
|
||||
@gplx.Virtual public byte ReadByteOr(String key, byte or) {
|
||||
Object val = Read(key); if (val == null) return or;
|
||||
try {return (parse) ? Byte_.parse(String_.as_(val)) : Byte_.cast(val);}
|
||||
try {return (parse) ? Byte_.Parse(String_.as_(val)) : Byte_.Cast(val);}
|
||||
catch (Exception exc) {Err_dataRdr_ReadFailed_useOr(exc, byte.class, key, val, or); return or;}
|
||||
}
|
||||
@gplx.Virtual public DateAdp ReadDate(String key) {
|
||||
|
||||
@@ -154,8 +154,8 @@ public class Gftest {
|
||||
case Type_ids_.Id__bool: bfr.Add_yn(Bool_.Cast(Array_.Get_at(ary, idx))); break;
|
||||
case Type_ids_.Id__bry: bfr.Add_safe((byte[])Array_.Get_at(ary, idx)); break;
|
||||
case Type_ids_.Id__long: bfr.Add_long_variable(Long_.cast(Array_.Get_at(ary, idx))); break;
|
||||
case Type_ids_.Id__int: bfr.Add_int_variable(Int_.cast(Array_.Get_at(ary, idx))); break;
|
||||
case Type_ids_.Id__byte: bfr.Add_int_variable((int)(Byte_.cast(Array_.Get_at(ary, idx)))); break;
|
||||
case Type_ids_.Id__int: bfr.Add_int_variable(Int_.Cast(Array_.Get_at(ary, idx))); break;
|
||||
case Type_ids_.Id__byte: bfr.Add_int_variable((int)(Byte_.Cast(Array_.Get_at(ary, idx)))); break;
|
||||
default: throw Err_.new_unhandled_default(type_id);
|
||||
}
|
||||
}
|
||||
@@ -179,8 +179,8 @@ public class Gftest {
|
||||
case Type_ids_.Id__bool: eq = Bool_.Cast(expd_obj) == Bool_.Cast(actl_obj); break;
|
||||
case Type_ids_.Id__bry: eq = Bry_.Eq((byte[])expd_obj, (byte[])actl_obj); break;
|
||||
case Type_ids_.Id__long: eq = Long_.cast(expd_obj) == Long_.cast(actl_obj); break;
|
||||
case Type_ids_.Id__int: eq = Int_.cast(expd_obj) == Int_.cast(actl_obj); break;
|
||||
case Type_ids_.Id__byte: eq = Byte_.cast(expd_obj) == Byte_.cast(actl_obj); break;
|
||||
case Type_ids_.Id__int: eq = Int_.Cast(expd_obj) == Int_.Cast(actl_obj); break;
|
||||
case Type_ids_.Id__byte: eq = Byte_.Cast(expd_obj) == Byte_.Cast(actl_obj); break;
|
||||
}
|
||||
}
|
||||
if (!eq) {
|
||||
|
||||
@@ -15,12 +15,12 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.core.type_xtns; import gplx.*; import gplx.core.*;
|
||||
public class ByteClassXtn extends ClassXtn_base implements ClassXtn {
|
||||
public static final String Key_const = "byte";
|
||||
public static final String Key_const = "byte";
|
||||
public String Key() {return Key_const;}
|
||||
@Override public Class<?> UnderClass() {return byte.class;}
|
||||
public Object DefaultValue() {return 0;}
|
||||
public boolean Eq(Object lhs, Object rhs) {try {return Byte_.cast(lhs) == Byte_.cast(rhs);} catch (Exception e) {Err_.Noop(e); return false;}}
|
||||
@Override public Object ParseOrNull(String raw) {return raw == null ? (Object)null : Byte_.parse(raw);}
|
||||
@Override public Object XtoDb(Object obj) {return Byte_.cast(obj);}
|
||||
public static final ByteClassXtn Instance = new ByteClassXtn(); ByteClassXtn() {} // added to ClassXtnPool by default
|
||||
public boolean Eq(Object lhs, Object rhs) {try {return Byte_.Cast(lhs) == Byte_.Cast(rhs);} catch (Exception e) {Err_.Noop(e); return false;}}
|
||||
@Override public Object ParseOrNull(String raw) {return raw == null ? (Object)null : Byte_.Parse(raw);}
|
||||
@Override public Object XtoDb(Object obj) {return Byte_.Cast(obj);}
|
||||
public static final ByteClassXtn Instance = new ByteClassXtn(); ByteClassXtn() {} // added to ClassXtnPool by default
|
||||
}
|
||||
@@ -15,12 +15,12 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.core.type_xtns; import gplx.*; import gplx.core.*;
|
||||
public class IntClassXtn extends ClassXtn_base implements ClassXtn {
|
||||
public String Key() {return Key_const;} public static final String Key_const = "int";
|
||||
public String Key() {return Key_const;} public static final String Key_const = "int";
|
||||
@Override public Class<?> UnderClass() {return Integer.class;}
|
||||
public Object DefaultValue() {return 0;}
|
||||
@Override public Object ParseOrNull(String raw) {return raw == null ? (Object)null : Int_.parse(raw);}
|
||||
public boolean Eq(Object lhs, Object rhs) {try {return Int_.cast(lhs) == Int_.cast(rhs);} catch (Exception e) {Err_.Noop(e); return false;}}
|
||||
@Override public Object XtoDb(Object obj) {return Int_.cast(obj);} // necessary for enums
|
||||
@Override public Object ParseOrNull(String raw) {return raw == null ? (Object)null : Int_.Parse(raw);}
|
||||
public boolean Eq(Object lhs, Object rhs) {try {return Int_.Cast(lhs) == Int_.Cast(rhs);} catch (Exception e) {Err_.Noop(e); return false;}}
|
||||
@Override public Object XtoDb(Object obj) {return Int_.Cast(obj);} // necessary for enums
|
||||
|
||||
public static final IntClassXtn Instance = new IntClassXtn(); IntClassXtn() {} // added to ClassXtnPool by default
|
||||
public static final IntClassXtn Instance = new IntClassXtn(); IntClassXtn() {} // added to ClassXtnPool by default
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user