Std.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (x.length < 2) {
  67. return integerFormatter.parse(x).intValue();
  68. }
  69. switch ((cast x : java.NativeString).codePointAt(1)) {
  70. case 'x'.code | 'X'.code:
  71. return java.lang.Integer.decode(x).intValue();
  72. case _:
  73. return integerFormatter.parse(x).intValue();
  74. }
  75. } catch (_:Dynamic) {
  76. return null;
  77. }
  78. }
  79. public static function parseFloat(x:String):Float {
  80. try {
  81. x = StringTools.trim(x);
  82. x = x.split("+").join(""); // TODO: stupid
  83. return doubleFormatter.parse(x.toUpperCase()).doubleValue();
  84. } catch (_:Dynamic) {
  85. return Math.NaN;
  86. }
  87. }
  88. inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  89. return Std.is(value, c) ? cast value : null;
  90. }
  91. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  92. inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  93. return downcast(value, c);
  94. }
  95. public static function random(x:Int):Int {
  96. if (x <= 0) {
  97. return 0;
  98. }
  99. return Std.int(Math.random() * x);
  100. }
  101. }