Std.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import jvm.Jvm;
  23. @:coreApi
  24. class Std {
  25. public static function is(v:Dynamic, t:Dynamic):Bool {
  26. if (v == null || t == null) {
  27. return false;
  28. }
  29. var clt:java.lang.Class<Dynamic> = cast t;
  30. if (clt == null) {
  31. return false;
  32. }
  33. if (clt == cast java.lang.Object) {
  34. return true;
  35. }
  36. clt = Jvm.getWrapperClass(clt);
  37. if (clt == cast java.lang.Double.DoubleClass) {
  38. // Haxe semantics: any number is assignable to Float
  39. clt = cast java.lang.Number;
  40. } else if (clt == cast java.lang.Integer.IntegerClass) {
  41. if (!Jvm.instanceof(v, java.lang.Number)) {
  42. return false;
  43. }
  44. var n = (cast v : java.lang.Number);
  45. // Haxe semantics: 2.0 is an integer...
  46. return n.doubleValue() == n.intValue();
  47. }
  48. return clt.isAssignableFrom((v : java.lang.Object).getClass());
  49. }
  50. public static function string(s:Dynamic):String {
  51. return jvm.Jvm.toString(s);
  52. }
  53. public static function int(x:Float):Int {
  54. return cast x;
  55. }
  56. static var integerFormatter = java.text.NumberFormat.getIntegerInstance(java.util.Locale.US);
  57. static var doubleFormatter = {
  58. var fmt = new java.text.DecimalFormat();
  59. fmt.setParseBigDecimal(true);
  60. fmt.setDecimalFormatSymbols(new java.text.DecimalFormatSymbols(java.util.Locale.US));
  61. fmt;
  62. };
  63. public static function parseInt(x:String):Null<Int> {
  64. try {
  65. x = StringTools.trim(x);
  66. var signChars = switch (cast x : java.NativeString).codePointAt(0) {
  67. case '-'.code | '+'.code: 1;
  68. case _: 0;
  69. }
  70. if (x.length < 2 + signChars) {
  71. return integerFormatter.parse(x).intValue();
  72. }
  73. switch ((cast x : java.NativeString).codePointAt(1 + signChars)) {
  74. case 'x'.code | 'X'.code:
  75. return java.lang.Integer.decode(x).intValue();
  76. case _:
  77. return integerFormatter.parse(x).intValue();
  78. }
  79. } catch (_:Dynamic) {
  80. return null;
  81. }
  82. }
  83. public static function parseFloat(x:String):Float {
  84. try {
  85. x = StringTools.trim(x);
  86. x = x.split("+").join(""); // TODO: stupid
  87. return doubleFormatter.parse(x.toUpperCase()).doubleValue();
  88. } catch (_:Dynamic) {
  89. return Math.NaN;
  90. }
  91. }
  92. inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  93. return Std.is(value, c) ? cast value : null;
  94. }
  95. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  96. inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  97. return downcast(value, c);
  98. }
  99. public static function random(x:Int):Int {
  100. if (x <= 0) {
  101. return 0;
  102. }
  103. return Std.int(Math.random() * x);
  104. }
  105. }