Std.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 inline function is(v:Dynamic, t:Dynamic):Bool {
  26. return isOfType(v, t);
  27. }
  28. public static function isOfType(v:Dynamic, t:Dynamic):Bool {
  29. if (v == null || t == null) {
  30. return false;
  31. }
  32. var clt:java.lang.Class<Dynamic> = cast t;
  33. if (clt == null) {
  34. return false;
  35. }
  36. if (clt == cast java.lang.Object) {
  37. return true;
  38. }
  39. clt = Jvm.getWrapperClass(clt);
  40. if (clt == cast java.lang.Double.DoubleClass) {
  41. // Haxe semantics: any number is assignable to Float
  42. clt = cast java.lang.Number;
  43. } else if (clt == cast java.lang.Integer.IntegerClass) {
  44. if (!Jvm.instanceof(v, java.lang.Number)) {
  45. return false;
  46. }
  47. var n = (cast v : java.lang.Number);
  48. // Haxe semantics: 2.0 is an integer...
  49. return n.doubleValue() == n.intValue();
  50. }
  51. return clt.isAssignableFrom((v : java.lang.Object).getClass());
  52. }
  53. public static function string(s:Dynamic):String {
  54. return jvm.Jvm.toString(s);
  55. }
  56. public static function int(x:Float):Int {
  57. return cast x;
  58. }
  59. static var integerFormatter = java.text.NumberFormat.getIntegerInstance(java.util.Locale.US);
  60. static var doubleFormatter = {
  61. var fmt = new java.text.DecimalFormat();
  62. fmt.setParseBigDecimal(true);
  63. fmt.setDecimalFormatSymbols(new java.text.DecimalFormatSymbols(java.util.Locale.US));
  64. fmt;
  65. };
  66. public static function parseInt(x:String):Null<Int> {
  67. try {
  68. x = StringTools.trim(x);
  69. var signChars = switch (cast x : java.NativeString).codePointAt(0) {
  70. case '-'.code | '+'.code: 1;
  71. case _: 0;
  72. }
  73. if (x.length < 2 + signChars) {
  74. return integerFormatter.parse(x).intValue();
  75. }
  76. switch ((cast x : java.NativeString).codePointAt(1 + signChars)) {
  77. case 'x'.code | 'X'.code:
  78. return java.lang.Integer.decode(x).intValue();
  79. case _:
  80. return integerFormatter.parse(x).intValue();
  81. }
  82. } catch (_:Dynamic) {
  83. return null;
  84. }
  85. }
  86. public static function parseFloat(x:String):Float {
  87. try {
  88. x = StringTools.trim(x);
  89. x = x.split("+").join(""); // TODO: stupid
  90. return doubleFormatter.parse(x.toUpperCase()).doubleValue();
  91. } catch (_:Dynamic) {
  92. return Math.NaN;
  93. }
  94. }
  95. inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  96. return Std.isOfType(value, c) ? cast value : null;
  97. }
  98. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  99. inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  100. return downcast(value, c);
  101. }
  102. public static function random(x:Int):Int {
  103. if (x <= 0) {
  104. return 0;
  105. }
  106. return Std.int(Math.random() * x);
  107. }
  108. }