Int_.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012-2017 gnosygnu@gmail.com
  4. XOWA is licensed under the terms of the General Public License (GPL) Version 3,
  5. or alternatively under the terms of the Apache License Version 2.0.
  6. You may use XOWA according to either of these licenses as is most appropriate
  7. for your project on a case-by-case basis.
  8. The terms of each license can be found in the source code repository:
  9. GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
  10. Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
  11. */
  12. package gplx;
  13. import gplx.objects.lists.CompareAbleUtl;
  14. import gplx.objects.primitives.BoolUtl;
  15. import gplx.objects.strings.AsciiByte;
  16. public class Int_ {
  17. // -------- BASELIB_COPY --------
  18. public static final String Cls_val_name = "int";
  19. public static final Class<?> Cls_ref_type = Integer.class;
  20. public static final int
  21. Min_value = Integer.MIN_VALUE
  22. , Max_value = Integer.MAX_VALUE
  23. , Max_value__31 = 2147483647
  24. , Neg1 = -1
  25. , Null = Int_.Min_value
  26. , Base1 = 1 // for super 1 lists / arrays; EX: PHP; [a, b, c]; [1] => a
  27. , Offset_1 = 1 // common symbol for + 1 after current pos; EX: String_.Mid(lhs + Offset_1, rhs)
  28. , Zero = 0
  29. ;
  30. public static int Cast(Object obj) {
  31. try {
  32. return (Integer)obj;
  33. }
  34. catch(Exception exc) {
  35. throw Err_.new_type_mismatch_w_exc(exc, int.class, obj);
  36. }
  37. }
  38. public static String To_str(int v) {return new Integer(v).toString();}
  39. public static int Parse_or(String raw, int or) {
  40. // process args
  41. if (raw == null) return or;
  42. int raw_len = String_.Len(raw);
  43. if (raw_len == 0) return or;
  44. // loop backwards from nth to 0th char
  45. int rv = 0, power_of_10 = 1;
  46. for (int idx = raw_len - 1; idx >= 0; idx--) {
  47. char cur = String_.CharAt(raw, idx);
  48. int digit = -1;
  49. switch (cur) {
  50. // numbers -> assign digit
  51. case '0': digit = 0; break; case '1': digit = 1; break; case '2': digit = 2; break; case '3': digit = 3; break; case '4': digit = 4; break;
  52. case '5': digit = 5; break; case '6': digit = 6; break; case '7': digit = 7; break; case '8': digit = 8; break; case '9': digit = 9; break;
  53. // negative sign
  54. case '-':
  55. if (idx != 0) { // invalid if not 1st
  56. return or;
  57. }
  58. else { // is first; multiply by -1
  59. rv *= -1;
  60. continue;
  61. }
  62. // anything else
  63. default:
  64. return or;
  65. }
  66. rv += (digit * power_of_10);
  67. power_of_10 *= 10;
  68. }
  69. return rv;
  70. }
  71. public static int[] Log10Ary = new int[] {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, Int_.Max_value};
  72. public static int Log10AryLen = 11;
  73. public static int Log10(int v) {
  74. if (v == 0) return 0;
  75. int sign = 1;
  76. if (v < 0) {
  77. if (v == Int_.Min_value) return -9; // NOTE: Int_.Min_value * -1 = Int_.Min_value
  78. v *= -1;
  79. sign = -1;
  80. }
  81. int rv = Log10AryLen - 2; // rv will only happen when v == Int_.Max_value
  82. int bgn = 0;
  83. if (v > 1000) { // optimization to reduce number of ops to < 5
  84. bgn = 3;
  85. if (v > 1000000) bgn = 6;
  86. }
  87. for (int i = bgn; i < Log10AryLen; i++) {
  88. if (v < Log10Ary[i]) {rv = i - 1; break;}
  89. }
  90. return rv * sign;
  91. }
  92. public static int DigitCount(int v) {
  93. int log10 = Log10(v);
  94. return v > -1 ? log10 + 1 : log10 * -1 + 2;
  95. }
  96. // -------- TO_MIGRATE --------
  97. public static int Cast_or(Object obj, int or) {
  98. try {
  99. return (Integer)obj;
  100. }
  101. catch(Exception e) {
  102. Err_.Noop(e);
  103. return or;
  104. }
  105. }
  106. public static int Coerce(Object v) {
  107. try {
  108. String s = String_.as_(v);
  109. return s == null ? Int_.Cast(v) : Int_.Parse(s);
  110. }
  111. catch (Exception e) {throw Err_.new_cast(e, int.class, v);}
  112. }
  113. public static int Parse(String raw) {try {return Integer.parseInt(raw);} catch(Exception e) {throw Err_.new_parse_exc(e, int.class, raw);}}
  114. public static int By_double(double v) {return (int)v;}
  115. public static int By_hex_bry(byte[] src) {return By_hex_bry(src, 0, src.length);}
  116. public static int By_hex_bry(byte[] src, int bgn, int end) {
  117. int rv = 0; int factor = 1;
  118. for (int i = end - 1; i >= bgn; i--) {
  119. int val = By_hex_byte(src[i]);
  120. rv += (val * factor);
  121. factor *= 16;
  122. }
  123. return rv;
  124. }
  125. public static int By_hex_byte(byte b) {
  126. switch (b) {
  127. case AsciiByte.Num0: case AsciiByte.Num1: case AsciiByte.Num2: case AsciiByte.Num3: case AsciiByte.Num4:
  128. case AsciiByte.Num5: case AsciiByte.Num6: case AsciiByte.Num7: case AsciiByte.Num8: case AsciiByte.Num9:
  129. return b - AsciiByte.Num0;
  130. case AsciiByte.Ltr_A: case AsciiByte.Ltr_B: case AsciiByte.Ltr_C: case AsciiByte.Ltr_D: case AsciiByte.Ltr_E: case AsciiByte.Ltr_F:
  131. return b - AsciiByte.Ltr_A + 10;
  132. case AsciiByte.Ltr_a: case AsciiByte.Ltr_b: case AsciiByte.Ltr_c: case AsciiByte.Ltr_d: case AsciiByte.Ltr_e: case AsciiByte.Ltr_f:
  133. return b - AsciiByte.Ltr_a + 10;
  134. default:
  135. return -1;
  136. }
  137. }
  138. public static byte[] To_bry(int v) {return Bry_.new_a7(To_str(v));}
  139. public static String To_str_fmt(int v, String fmt) {return new java.text.DecimalFormat(fmt).format(v);}
  140. public static String To_str_pad_bgn_space(int val, int reqd_len) {return To_str_pad(val, reqd_len, BoolUtl.Y, AsciiByte.Space);} // EX: 1, 3 returns " 1"
  141. public static String To_str_pad_bgn_zero (int val, int reqd_len) {return To_str_pad(val, reqd_len, BoolUtl.Y, AsciiByte.Num0);} // EX: 1, 3 returns "001"
  142. private static String To_str_pad(int val, int reqd_len, boolean bgn, byte pad_chr) {
  143. // get val_len and pad_len; exit early, if no padding needed
  144. int val_len = DigitCount(val);
  145. int pad_len = reqd_len - val_len;
  146. if (pad_len < 0)
  147. return Int_.To_str(val);
  148. // padding needed
  149. Bry_bfr bfr = Bry_bfr_.New();
  150. // handle negative numbers; EX: -1 -> "-001", not "00-1"
  151. if (val < 0) {
  152. bfr.Add_byte(AsciiByte.Dash);
  153. val *= -1;
  154. --val_len;
  155. }
  156. // build outpt
  157. if (!bgn)
  158. bfr.Add_int_fixed(val, val_len);
  159. bfr.Add_byte_repeat(pad_chr, pad_len);
  160. if (bgn)
  161. bfr.Add_int_fixed(val, val_len);
  162. return bfr.To_str();
  163. }
  164. public static String To_str_hex(int v) {return To_str_hex(BoolUtl.Y, BoolUtl.Y, v);}
  165. public static String To_str_hex(boolean zero_pad, boolean upper, int v) {
  166. String rv = Integer.toHexString(v);
  167. int rv_len = String_.Len(rv);
  168. if (zero_pad && rv_len < 8) rv = String_.Repeat("0", 8 - rv_len) + rv;
  169. return upper ? String_.Upper(rv) : rv;
  170. }
  171. public static int Compare(int lhs, int rhs) {
  172. if (lhs == rhs) return CompareAbleUtl.Same;
  173. else if (lhs < rhs) return CompareAbleUtl.Less;
  174. else return CompareAbleUtl.More;
  175. }
  176. public static boolean In(int v, int comp0, int comp1) {return v == comp0 || v == comp1;}
  177. public static boolean In(int v, int... ary) {
  178. for (int itm : ary)
  179. if (v == itm) return true;
  180. return false;
  181. }
  182. public static boolean Between(int v, int lhs, int rhs) {
  183. int lhsCompare = v == lhs ? 0 : (v < lhs ? -1 : 1);
  184. int rhsCompare = v == rhs ? 0 : (v < rhs ? -1 : 1);
  185. return (lhsCompare * rhsCompare) != 1; // 1 when v is (a) greater than both or (b) less than both
  186. }
  187. public static boolean RangeCheck(int v, int max) {return v >= 0 && v < max;}
  188. 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);}
  189. public static boolean Bounds_chk(int bgn, int end, int len) {return bgn > -1 && end < len;}
  190. public static int BoundEnd(int v, int end) {return v >= end ? end - 1 : v;}
  191. public static int EnsureLessThan(int v, int max) {return v >= max ? max : v;}
  192. public static int Min(int lhs, int rhs) {return lhs < rhs ? lhs : rhs;}
  193. public static int Max(int lhs, int rhs) {return lhs > rhs ? lhs : rhs;}
  194. public static int Min_many(int... ary) {
  195. int len = ary.length; if (len == 0) throw Err_.new_wo_type("Min_many requires at least 1 value");
  196. boolean init = true;
  197. int min = Int_.Min_value;
  198. for (int i = 0; i < len; ++i) {
  199. int val = ary[i];
  200. if (init) {
  201. min = val;
  202. init = false;
  203. }
  204. else {
  205. if (val < min)
  206. min = val;
  207. }
  208. }
  209. return min;
  210. }
  211. public static int Subtract_long(long lhs, long rhs) {return (int)(lhs - rhs);}
  212. public static int Div(int v, float divisor) {return (int)((float)v / divisor);}
  213. public static int DivAndRoundUp(int v, int divisor) {
  214. int whole = v / divisor;
  215. int partial = v % divisor == 0 ? 0 : 1;
  216. return whole + partial;
  217. }
  218. public static int Mult(int v, float multiplier) {
  219. float product = ((float)v * multiplier); // WORKAROUND (DotNet): (int)((float)v * multiplier) returns 0 for 100 and .01f
  220. return (int)product;
  221. }
  222. }